Hereby a script I created to control and monitor Philips Hue Lights. Maybe also useable to control and monitor Brdidge connected devices such as living colors and living whites.
Im not a VB specialist so its not "moeders moooiste " But it works like a charm for me. Feel free to comment and ask for improvements and or wishes. Maybe someone can use it to create a real homeseer pugin...
Improvements I want to make;
- embed function to register new "HASH" on bridge
- Create cutom homeseer device with 4 DIM sliders used for dimming, sat, hue and ct.
- Predefined colors
- Installation script
- ..????
Installation instructions can be found in the main in script (Below post) header. Just copy the script in a *.vb file and place this in the homeseer scripts DIR. Dont forget to fill in the IP of your bridge and HASH code. more infor on http://www.everyhue.com
To get a HASH code, run the code below once, after u pressed the center button on the bridge. Before running the script, fill in the IP of your bridge.
Code: Select all
'HueControl get Hash.vb
'by B. Bakels
'www.promedes.nl
'21 Jan 2013 - version 1.0
'
'USAGE with HomeSeer
' 1 - Fill Bridge IP into this script
' 2 - Press Center button on Bridge
' 3 - Run Script Once
' 4 - Check Homeseer log for response including Hash
' If error is returned check if you pressed the button op the Bridge before executing the script
'
'===========================================
'== Main Program ==
'===========================================
sub Main(byVal params As Object)
'Declare params
'=============================
Dim IP As String
Dim ACTION
Dim setdata 'string to post
Dim Hashresponse 'response bridge
Dim httpres 'http response
Dim objHTTP
'=============================
' please edit below
IP = "YOURBRIDGEIP"
'=============================
'hashcode can me changed to unique oneif needed
setdata = "{""username"": ""22a828f1898a4257c3f181e753241334"", ""devicetype"": ""Homeseer""}"
' Start the HTTP communication
objHTTP = CreateObject ("WinHttp.WinHttpRequest.5.1" )
objHTTP.Open("POST", "http://" & IP & "/api/" , False)
objHTTP.Send(setdata)
Hashresponse = objHTTP.ResponseText
' Check if all communication went well
httpres = objHTTP.Status
If httpres <> 200 Then
Hs.writelog("hHueControl SET","HueControlHASHPOST error " & httpres)
End if
' And close down
objHTTP = Nothing
hs.WriteLog("send", setdata)
hs.WriteLog("response", Hashresponse)
'End of Main
End Sub
Main Script can be found below
With Regards,
Bart
Code: Select all
'HueControl.vb
'by B. Bakels
'www.promedes.nl
'19 Jan 2013 - version 2.1
'
'USAGE with HomeSeer
' 1 - Fill in HASH and Bridge IP into this script
' 2 - Create Homeseer Lamp Devices for every Hue in your system
''3 - Create one or more Hue Mode devices as virtual device with 4 Value Pairs;
' 0=Dimming
' 100=Hue
' 200-Saturation
' 300=colortemp
' 4 - Make an event for status change for every hue homeseer device and select this script to execute.
' For the parameter field use ("Main","Control;B80;B90;1")
' 5 - Make a recurring event to trigger status update of Hues in Homeseer with this parameter field, ("Main","Status;B80,B81,B82;B90;1,2,3")
' 6 - also possible to trigger Alarm by ("Main","Alarm;X;X;1,2,3") or ("Main","CustomAlarm;X;X;1,2,3") this causes the hues to flash.
'Script parameters: ("Main","<executionmode (control or status)>;<DeviceCode Homeseer Control Device>;<DeviceCode mode Device>;<Hue Number to control> ")
'Main with a capital M
' execution modes; COntrol: controls the Hue lamp
' Status: Updates the homeseer device status with the current hue status
' Alarm: activate hue alert state for selected Hue lampno``s
' CustomAlarm: Custom Flashing Hue Lights for selected Hue Lampno`s
'
' Devicecode HUe Homeseer; The devicecode of the homeseer device for the selected hue, Also possible to create more than one Hu ata a time by filling in B1,B2,B3
' Devicecode HueHomeseer Mode; the devicecode f the device that contains the controlmode of the Hue
' Hue Number; Number of the Hue to be controlled, also possible to control more then one hue at a time by filling in 1,2,3
'===========================================
'== Main Program ==
'===========================================
sub Main(byVal params As Object)
'Declare params
'=============================
Dim HASH As String
Dim IP As String
Dim ACTION
Dim MODE
Dim status
DIM BRI As Integer
DIM SAT As Integer
DIM HUE As Integer
DIM COLTEMP As Integer
DIM Debug as boolean
DIM StateResponse
DIM ArrDEVICES
DIM ArrBULBS
'setup specific settings
'=============================
Debug = false
IP = "IP"
HASH="HASH"
'read script execution additional parameters
'=============================
Dim strExecution As String = hs.StringItem(Params, 1, ";")
Dim strDeviceControl As String = hs.StringItem(Params, 2, ";")
Dim strDeviceMode As String = hs.StringItem(Params, 3, ";")
Dim BULBNO As string = hs.StringItem(Params, 4, ";")
'If used more that one devices declaration put Devices in array, for now only used for status readback
ArrDEVICES = Split(strDeviceControl, ",")
'If used more that one bulb declaration put bulbs in array
ArrBULBS = Split(BULBNO, ",")
'Calculate Values and get devicestatus and value
'=============================
MODE = hs.DeviceValue(strDeviceMode)
ACTION = hs.DeviceStatus(strDeviceControl)
BRI = hs.DeviceValue(strDeviceControl)*2.54
SAT = hs.DeviceValue(strDeviceControl)*2.54
HUE = hs.DeviceValue(strDeviceControl)*655.35
COLTEMP = (hs.DeviceValue(strDeviceControl)*3.46)+154
'debug mode
'=============================
If Debug = true Then
hs.writelog("Huecontrol Debug","Device to Control :" & strDeviceControl)
hs.writelog("Huecontrol Debug","Device for Mode :" & strDeviceMode )
hs.writelog("Huecontrol Debug","Hue Number :" & BULBNO )
hs.writelog("Huecontrol Debug","Action :" & ACTION )
hs.writelog("Huecontrol Debug","Execution mode :" & strExecution )
hs.writelog("Huecontrol Debug","Selected Mode :" & MODE )
hs.writelog("Huecontrol Debug","calculated Brightness :" & BRI )
hs.writelog("huecontrol Debug","calculated Saturation :" & SAT )
hs.writelog("huecontrol Debug","calculated Hue :" & HUE )
hs.writelog("huecontrol Debug","calculated color temperature :" & COLTEMP )
end if
Select case strExecution
'==================================================
'== Execution Mode; Control HUE Lamp ===
'==================================================
Case "Control"
Select case ACTION
'Hue changed to ON
'=============================
case 2
For Each BULBNO In ArrBULBS
status = SetHueState(BULBNO, IP, HASH, "{""on"":true}", Debug )
hs.writelog("HueControl Control" ,"Hue Bulb: " & BULBNO)
hs.writelog("HueControl On" , status )
Next
'Hue changed to Off
'=============================
case 3
For Each BULBNO In ArrBULBS
status = SetHueState(BULBNO, IP, HASH, "{""on"":false}", Debug )
hs.writelog("HueControl Control" ,"Hue Bulb: " & BULBNO)
hs.writelog("HueControl Off" , status)
Next
'Hue change Dim value
'=============================
case 4
Select case MODE
'DIMMER Mode
case 0
For Each BULBNO In ArrBULBS
status = SetHueState(BULBNO, IP, HASH, "{""on"":true,""bri"":" & BRI & "}", Debug )
hs.writelog("HueControl Control" ,"Hue Bulb: " & BULBNO)
hs.writelog("HueControl bri" , status)
Next
'Saturation Mode
case 100
For Each BULBNO In ArrBULBS
status = SetHueState(BULBNO, IP, HASH, "{""on"":true,""sat"":" & SAT & "}", Debug )
hs.writelog("HueControl Control" ,"Hue Bulb: " & BULBNO)
hs.writelog("HueControl sat" , status)
Next
'Hue Mode
case 200
For Each BULBNO In ArrBULBS
status = SetHueState(BULBNO, IP, HASH, "{""on"":true,""hue"":" & HUE & "}", Debug )
hs.writelog("HueControl Control" ,"Hue Bulb: " & BULBNO)
hs.writelog("HueControl hue" , status)
Next
'Color Temperature Mode
case 300
For Each BULBNO In ArrBULBS
status = SetHueState(BULBNO, IP, HASH, "{""on"":true,""ct"":" & COLTEMP & "}", Debug )
hs.writelog("HueControl Control" ,"Hue Bulb: " & BULBNO)
hs.writelog("HueControl ct" , status)
Next
End Select
'every other case
'=============================
case else
For Each BULBNO In ArrBULBS
status = SetHueState(BULBNO, IP, HASH, "{""on"":false}", Debug )
hs.writelog("HueControl Control" ,"Hue Bulb: " & BULBNO)
hs.writelog("HueControl" , status)
Next
End Select
'==================================================
'==Execution Mode; Status update homeseer device===
'==================================================
case "Status"
dim statusloop
dim activedevice
dim position
dim statuson
dim satindex
dim statussat
dim briindex
dim statusbri
dim hueindex
dim statushue
dim ctindex
dim statusct
dim ActDeviceVal
dim ActDeviceStat
statusloop=0
For Each BULBNO In ArrBULBS
activedevice = ArrDEVICES(statusloop)
StateResponse= GetHueStatus(BULBNO, IP, HASH, Debug )
'Detect on/off status
statuson = InStr(Stateresponse, """on"":true")
if statuson = 12 then statuson = 2 else statuson = 3
'detect bri status
briindex = InStr(Stateresponse, """bri"":")
briindex = briindex+6
statusbri = Mid(stateresponse,briindex , 3 )
statusbri =Replace(statusbri," ","")
statusbri =Replace(statusbri,",","")
statusbri =Replace(statusbri,"""","")
statusbri = Int(statusbri/2.54)
'detect sat status
satindex = InStr(Stateresponse, """sat"":")
satindex = satindex+6
statussat = Mid(stateresponse,satindex , 3 )
statussat =Replace(statussat," ","")
statussat =Replace(statussat,",","")
statussat =Replace(statussat,"""","")
statussat = Int(statussat/2.54)
'detect hue status
hueindex = InStr(Stateresponse, """hue"":")
hueindex = hueindex+6
statushue = Mid(stateresponse,hueindex , 5 )
statushue =Replace(statushue," ","")
statushue =Replace(statushue,",","")
statushue =Replace(statushue,"""","")
statushue =Replace(statushue,"s","")
statushue =Replace(statushue,"a","")
statushue =Replace(statushue,"t","")
statushue = Int(statushue/655.35)
'detect ct status
ctindex = InStr(Stateresponse, """ct"":")
ctindex = ctindex+5
statusct = Mid(stateresponse,ctindex , 3 )
statusct =Replace(statusct," ","")
statusct =Replace(statusct,",","")
statusct =Replace(statusct,"""","")
statusct = Int((statusct-154)/3.46)
'Set Homeseer Devices with new status and value
'===============================================
'Check Current homeseer device state and value
ActDeviceStat = hs.DeviceStatus(activedevice)
ActDeviceVal = hs.DeviceValue(activedevice)
'Set Devicestatus and value depending on Status On(2) or Off(3)
Select case statuson
case 2
'Set Devicestatus and value depending on MODE
Select case MODE
'DIMMER Mode
case 0
if statuson = 2 and statusbri < 98 then statuson = 4
if ActDeviceStat <> statuson Then hs.SetDeviceStatus(activedevice, statuson)
if ActDeviceVal <> statusbri Then hs.SetDeviceValue(activedevice, statusbri)
'Saturation Mode
case 100
if ActDeviceStat <> statuson Then hs.SetDeviceStatus(activedevice, statuson)
if ActDeviceVal <> statussat Then hs.SetDeviceValue(activedevice, statussat)
'Hue Mode
case 200
if ActDeviceStat <> statuson Then hs.SetDeviceStatus(activedevice, statuson)
if ActDeviceVal <> statushue Then hs.SetDeviceValue(activedevice, statushue)
'Color Temperature Mode
case 300
if ActDeviceStat <> statuson Then hs.SetDeviceStatus(activedevice, statuson)
if ActDeviceVal <> statusct Then hs.SetDeviceValue(activedevice, statusct)
End Select
'hue device is off
Case 3
if ActDeviceStat <> statuson Then hs.SetDeviceStatus(activedevice, statuson)
if ActDeviceVal <> 0 Then hs.SetDeviceValue(activedevice, 0)
End Select
If Debug = true Then hs.writelog("HueControl status", "status on: " & Statuson & " devicecode: " & activedevice & " Status Bri: " & statusbri & " Status ct: " & statusct & " Status Hue: " & statushue & " Status Sat: " & statussat)
statusloop = statusloop + 1
Next
'==================================================
'== Execution Mode; Alarm on Hue standard ===
'==================================================
case "Alarm"
For Each BULBNO In ArrBULBS
status = SetHueState(BULBNO, IP, HASH, "{""on"":true, ""sat"":254, ""hue"":0,""bri"":254,""alert"":""lselect""}", Debug )
hs.writelog("HueControl alarm" , status)
Next
'==================================================
'== Execution Mode; Alarm on Hue custom ===
'==================================================
case "AlarmCustom"
dim count
dim runs
runs=40 'Number of flashes
count=0 'flash counter
hs.writelog("HueControl alarm" ,runs & " Flashes Started")
For count=1 To runs Step 1
For Each BULBNO In ArrBULBS
status = SetHueState(BULBNO, IP, HASH, "{""on"":true, ""sat"":254, ""hue"":0,""bri"":254,""transitiontime"":5}", Debug )
If Debug = true Then hs.writelog("HueControl alarm" , status)
Next
hs.WaitSecs (0.7)
For Each BULBNO In ArrBULBS
status = SetHueState(BULBNO, IP, HASH, "{""on"":true, ""sat"":254, ""hue"":0,""bri"":0,""transitiontime"":5}", Debug )
If Debug = true Then hs.writelog("HueControl alarm" , status)
Next
hs.WaitSecs (0.7)
If count=runs Then Exit For
Next
End Select
'End of Main
End Sub
'===========================================
'== Functions ==
'===========================================
'GET HUE STATUS
'===========================================
Function GetHueStatus (BULBNO, IP, HASH, Debug)
Dim Stateresponse
Stateresponse = hs.URLAction("http://" & IP & "/api/" & HASH &"/lights/" & BULBNO & "/","GET", "", "")
If Debug = true Then hs.writelog("HueControl GET", Stateresponse)
return Stateresponse
End Function
'SET HUE STATE
'===========================================
Function SetHueState (BULBNO, IP, HASH, setdata, Debug)
Dim Setresponse
dim httpres 'http response
dim objHTTP
' Start the HTTP communication
objHTTP = CreateObject ("WinHttp.WinHttpRequest.5.1" )
objHTTP.Open("PUT", "http://" & IP & "/api/" & HASH & "/lights/" & BULBNO & "/state" , False)
objHTTP.Send(setdata)
Setresponse = objHTTP.ResponseText
' Check if all communication went well
httpres = objHTTP.Status
If httpres <> 200 Then
Hs.writelog("hHueControl SET" , "HueControl Debug set action returns error " & httpres)
End if
' And close down
objHTTP = Nothing
If Debug = true Then hs.WriteLog("send", setdata)
If Debug = true Then hs.WriteLog("response", Setresponse)
return Setresponse
End Function