User Submitted CoyoteScript

Philips Hue

Author: Sander

Script Version: 1

Minimum CoyoteMIDI Build: 562

With this script you can control Philips Hue compatible lights directly from translations. This will require your lights to be connected to a Philips Hue bridge.

Once installed, the script will ask you to pair it with your Philips Hue bridge. Once that is done, your lights will immediately become available to be modified through translations.

If it should ever be necessary, you can unpair the script from your bridge by selecting the “Forget Pairing” option in the script configuration window, and closing it.

This script will add the following actions and parameters:

change_on:
This will change the on state of your light.
Parameters:
Light: Selects the light to modify. This will automatically show the names of your available lights.
State: The state to apply to the light. Options are: “On”, “Off”, and “Toggle”.

change_color:
This will change the colour of your light.
Parameters:
Light: Selects the light to modify.
Hue: The hue of the colour to apply to the light.
Saturation: The saturation of the colour to apply to the light.
Brightness: The brightness of the colour to apply to the light.
Transition Time: The duration of the transition when changing from the previous colour to the next, in seconds.

brightness_from_midi:
This will change the brightness of a light in relation to the MIDI value of the MIDI event that triggered the translation. For example, if the translation was triggered by a Control Change MIDI event, the brightness will be based on the Control Value of that MIDI event.
Parameters:
Light: Selects the light to modify.

This script also allows you to use it as an API for your own scripts. Once installed and paired, you can use the following runscript commands to modify your lights:

Get a map describing the light states of all lights on your bridge:
$lights = (runscript (hubscript 4026 filename) get_lights)
The returned data will be a complex map describing all properties of each light.

Modify the state of a light:
runscript (hubscript 4026 filename) change_light_state id:1,hue:5000,sat:255,bri:255,time:10,on:true
All properties are optional, except for the id of the light.
				
					script default hidden
    return


// --- Change the on state of the light. ---
script Change_On
    $id = (runscript default get_light_from_name name:($parameters change_on_light))
    
    $on = ($parameters change_on_state)
    if $on == toggle
        $lights = (runscript default get_lights)
        $on = (lowercase ($lights $id state on) != true)
    else
        $on = (lowercase ($on == On))

    runscript default change_light_state id:$id,on:$on


// --- Change the colour of the light. ---
script Change_Color
    $id = (runscript default get_light_from_name name:($parameters change_color_light))
    
    $hue = ($parameters change_color_hue)
    $sat = ($parameters change_color_sat)
    $bri = ($parameters change_color_bri)
    $time = ($parameters change_color_time)
    $time = (round ($time * 10))
    
    $rgb = (runscript default get_rgb hue:$hue,sat:$sat,bri:$bri)
    indicator true $rgb 1 5000
    
    runscript default change_light_state id:$id,hue:$hue,sat:$sat,bri:$bri,time:$time


// --- Change the brightness of the light, taking the brightness directly from the MIDI data. ---
script Brightness_From_MIDI
    if $trigger !contains rawvalue
        messagebox "This subscript should only be ran by a translation triggered by MIDI!" "Invalid usage!"
        return
        
    $id = (runscript default get_light_from_name name:($parameters brightness_from_midi_light))
    $bri = ($trigger rawvalue) * 2
    runscript default change_light_state id:$id,hue:$hue,sat:$sat,bri:$bri,time:$time

event uirefreshed
    configurationparameter reset_bridge (checkbox "Forget Pairing" "When selected, the script will delete any existing information of its pairing with a Philips Hue Bridge" false)
    // only register the translation parameters when the script is paired because we need the light information.
    if !(runscript default is_paired)
        return
    runscript default apply_parameters

event configchanged
    // Allow for the bridge data to be reset, effectively removing the pairing to the bridge.
    if ($trigger parameters reset_bridge) == true && (messagebox "Are you sure you want to forget your bridge pairing? All information of your Philips Hue bridge will be removed and the script will need to be paired again in order to work." "Reset Pairing?" yes,cancel) == yes
        %phillips_hue_bridge_ip = ""
        %phillips_hue_bridge_api_key = ""

event scriptsloaded
    // Check if the script is paired with the bridge or not, and offer to pair if necessary.
    if !(runscript default is_paired)
        if (messagebox "CoyoteMIDI is not paired with your Philips Hue bridge. Pair now?" "Pair Philips Hue bridge?" yes,no) == yes
            runscript default pair_bridge
    // Update the light data.
    if (runscript default is_paired)
        runscript default get_lights


// --- Provides the parameters shown in translation settings. ---
script apply_parameters private
    $light_selector = (combobox "Light" "Selects the light to change." (mapkeys %philips_hue_light_name_map) default 0.8)

    translationparameter change_on_light $light_selector Change_On
    translationparameter change_on_state (combobox "State" "The state to apply to the light" "On,Off,Toggle" "Toggle" 0.5) Change_On

    translationparameter change_color_light $light_selector Change_Color
    translationparameter change_color_hue (numberbox Hue "The hue of the color to apply to the light" 1 default false 0 65535 250) Change_Color
    translationparameter change_color_sat (numberbox Saturation "The saturation of the color to apply to the light" 255 default false 0 255 1) Change_Color
    translationparameter change_color_bri (numberbox Brightness "The brightness of the color to apply to the light" 255 default false 0 255 1) Change_Color
    translationparameter change_color_time (numberbox "Transition Time" "The duration of the transition from the previous colour to the next, in seconds." 1 default true 0 default 1) Change_Color
    
    translationparameter brightness_from_midi_light $light_selector Brightness_From_MIDI


// --- starts the bridge pairing procedure. ---
script pair_bridge private
    // Get the bridge IP address.
    $discover = (parsejson (web get "https://discovery.meethue.com"))
    if $discover !contains 1 internalipaddress
        messagebox "Failed to find a Philips Hue bridge." "Bridge not found!" ok
        return
    %phillips_hue_bridge_ip = ($discover 1 internalipaddress)
    
    // create an API access key.
    $bridge_api = "http://%{phillips_hue_bridge_ip}/api"
    $device_info = (tojson devicetype:coyotescript#coyotemidi)
    $register_json = (web post $bridge_api $device_info)
    messagebox "Please press the button on your Philips Hue bridge to confirm pairing with CoyoteMIDI. Click OK after pairing." "Pair with bridge" ok
    $register_json = (web post $bridge_api $device_info)
    $register = (parsejson $register_json)
    if $register !contains 1 success username
        messagebox "Failed to pair CoyoteMIDI with your Philips Hue bridge." ok
        %phillips_hue_bridge_ip = ""
        %phillips_hue_bridge_api_key = ""
        return
    %phillips_hue_bridge_api_key = (encrypt ($register 1 success username))
    messagebox "You've successfully paired CoyoteMIDI with your Philips Hue bridge." "Pairing success!" ok
    runscript default get_lights
    runscript default apply_parameters


// --- Returns whether the script is currently paired with the bridge. ---
script is_paired hidden
    return %phillips_hue_bridge_ip != "" && %phillips_hue_bridge_api_key != ""


// --- Returns the available lights and their data, also caches them in a global variable. ---
script get_lights hidden
    $apiKey = (decrypt %phillips_hue_bridge_api_key)
    %philips_hue_light_data = (parsejson (web get "http://%{phillips_hue_bridge_ip}/api/${apiKey}/lights"))
    %philips_hue_light_name_map new map
    foreach $light_id : $light_data in %philips_hue_light_data
        %philips_hue_light_name_map + ($light_data name):$light_id
    return %philips_hue_light_data


// --- Returns the ID of a given light by name. ---
script get_light_from_name hidden
    if !$name
        error "No light name specified!"
    if %philips_hue_light_name_map !contains $name
        return -1
    return (%philips_hue_light_name_map $name)


// --- converts Philips Hue's Hue/Sat/Bri into RGB. ---
script get_rgb hidden
    if !$hue || !$sat || !$bri
        error "Invalid arguments specified!"
    $h = $hue / 65535 * 360
    $s = $sat / 255
    $v = $bri / 255
    $c = $v * $s
    $modh = $h / 60 - (floor ($h / 60))
    $x = $c * (1 - (abs $modh - 1))
    $m = $v - $c
    $r1 = 0
    $g1 = 0
    $b1 = 0
    if $h < 60
        $r1 = $c
        $g1 = $x
    else if $h < 120
        $r1 = $x
        $g1 = $c
    else if $h < 180
        $g1 = $c
        $b1 = $x
    else if $h < 240
        $g1 = $x
        $b1 = $c
    else if $h < 300
        $r1 = $x
        $b1 = $c
    else
        $r1 = $c
        $b1 = $x
    $result new map
    $result + R: (($r1 + $m) * 255)
    $result + G: (($g1 + $m) * 255)
    $result + B: (($b1 + $m) * 255)
    return $result


// --- Changes the state of a given light. ---
script change_light_state hidden
    if !$id
        error "No light ID specified!"
    
    $body new map
    if $on != ""
        $body + on: $on
    if $hue
        $body + hue: $hue
    if $sat
        $body + sat: $sat
    if $bri
        $body + bri: $bri
    if $time
        $body + transitiontime:$time
    
    $apiKey = (decrypt %phillips_hue_bridge_api_key)
    web put "http://%{phillips_hue_bridge_ip}/api/${apiKey}/lights/${id}/state" (tojson $body)

				
			

7 downloads