Philips Hue Control Script

Alles m.b.t. de Homeseer Philips Hue HS3 Plugin van Willem Eradus (Dutch Forum)

Moderator: Willem4ever

Philips Hue Control Script

Postby bartbakels » Mon Jan 21, 2013 8:56 pm

Dear All,

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 :D " 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



Example how to configure the events;
event config.png
How to configure the events
event config.png (50.25 KiB) Viewed 21168 times


Configuration of the Hue Mode Device;
Mode device config.png
How to Configure the Hue Mode Device
Mode device config.png (77.45 KiB) Viewed 21168 times



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
Software: HS3, HStouch, Plugwise , BLBackup, BLLatestImage, Zwave, JowiHUE, PHlocation, Netcam, Harmony Plugin, ThinkingCleaner, HSPhone, Eneco Toon Script, Pushover, Visonic Powermax LV Interface

(ON ESXI )on NUC
bartbakels
Advanced Member
Advanced Member
 
Posts: 511
Joined: May 2011

Re: Philips Hue Control Script

Postby Willem4ever » Fri Jan 25, 2013 11:55 am

Hallo Bart, script werkt hier ook. Ik draai HS2 Pro onder W2K8R2.
User avatar
Willem4ever
Global Moderator
Global Moderator
 
Posts: 804
Joined: October 2006
Location: Uithoorn / Netherlands

Re: Philips Hue Control Script

Postby bartbakels » Fri Jan 25, 2013 7:53 pm

Willem,

Goed te horen,

Hierbij een nieuwe versie van het script, met de volgende aanpassingen;

- Registeren van een Hash Code mbv het script
- Status readback van de Lampen is verbeterd
- support voor livingcolor lights toegevoegd (er bestaat nl geen CT mode)
- WakeupLight functie
- ColorFade functie


mbt de register functie; Men kan het script gebruiken as is. men moet enkel het IP adres opgeven. Echter ik raad aan de hash code te veranderen. Dit is een unieke code die eerst geregistreerd dient te zijn. Het mag duidelijk zijn dat de hash code die nu in het script staat random is en niet de mijne ;)

Hoe de overige functies te gebruiken is omschreven in de header van het script.

Succes

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") I also use a status update on device value change
' 6 - trigger Alarm by ("Main","Alarm;X;X;1,2,3") or ("Main","CustomAlarm;X;X;1,2,3") this causes the hues to flash red.
' 7 - Colorloop mode; enable by ("Main","ColorfadeStart;X;X;1,2,3") disable by ("Main","ColorfadeStop;X;X;1,2,3")
' 8 - WakeupLight; enable by ("Main","Wakeup;X;X;4") causes fade in from off to full brightness in 4 min, time can be changed in script.


'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 = "YOUBRIDGEIP"
HASH="3d417b315c4d4372782f251988188b"


'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; Register " Bridge   ===
'==================================================

Case "Register"


'Declare params
'=============================

Dim Hashdata 'string to post
Dim Hashresponse 'response bridge
Dim httpres 'http response
Dim objHTTP

'=============================

Hashdata =  "{""username"": """ & HASH & """, ""devicetype"": ""Homeseer""}"



' Start the HTTP communication
   objHTTP = CreateObject ("WinHttp.WinHttpRequest.5.1" )
   objHTTP.Open("POST", "http://" & IP & "/api/" , False)
   objHTTP.Send(Hashdata)
   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", Hashdata)
hs.WriteLog("response", Hashresponse)
 
'==================================================
'==   Execution Mode; Control HUE Lamp   ===
'==================================================


Case "Control"




'Case selection depending on mode

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"":")
   
   'if living color light no ct available
   if ctindex <> 0 Then ctindex = ctindex+5
   if ctindex <> 0 Then statusct = Mid(stateresponse,ctindex , 3 )
   if ctindex <> 0 Then statusct =Replace(statusct," ","")
   if ctindex <> 0 Then statusct =Replace(statusct,",","")
   if ctindex <> 0 Then statusct =Replace(statusct,"""","")
   if ctindex <> 0 Then statusct = Int((statusct-154)/3.46)
   if ctindex = 0 Then statusct = 0   



'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

'==================================================
'==   Execution Mode; Wakeup         ===
'==================================================


case "Wakeup"

   For Each BULBNO In ArrBULBS
      
      status = SetHueState(BULBNO, IP, HASH, "{""on"":true,""bri"":254,""transitiontime"":2400}", Debug )
      hs.writelog("HueControl wakeup" , status)
   
   Next




'==================================================
'==   Execution Mode; Colorfade Start      ===
'==================================================


case "ColorfadeStart"

   For Each BULBNO In ArrBULBS
      
      status = SetHueState(BULBNO, IP, HASH, "{""on"":true,""effect"":""colorloop""}", Debug )
      hs.writelog("HueControl Fade Stop" , status)
   
   Next


'==================================================
'==   Execution Mode; Colorfade Stop      ===
'==================================================


case "ColorfadeStop"

   For Each BULBNO In ArrBULBS
      
      status = SetHueState(BULBNO, IP, HASH, "{""on"":true,""effect"":""none""}", Debug )
      hs.writelog("HueControl Fade Start" , status)
   
   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
Software: HS3, HStouch, Plugwise , BLBackup, BLLatestImage, Zwave, JowiHUE, PHlocation, Netcam, Harmony Plugin, ThinkingCleaner, HSPhone, Eneco Toon Script, Pushover, Visonic Powermax LV Interface

(ON ESXI )on NUC
bartbakels
Advanced Member
Advanced Member
 
Posts: 511
Joined: May 2011

Re: Philips Hue Control Script

Postby jakob.sand » Tue Jan 29, 2013 11:13 pm

Great start - will you post updates on board.homeseer.com also or only here?
jakob.sand
Starting Member
Starting Member
 
Posts: 2
Joined: January 2013

Re: Philips Hue Control Script

Postby bartbakels » Wed Jan 30, 2013 6:30 pm

Hello,

Im working on an installation script, Ini file, fixed color settings and several code improvements. regarding the installation script I m looking for a manner to interpret Json code in a simple way. Does someone have a simple solution?

In addition if someone can make a table with predefined colors and the set sat and hue settings, would be great.

Thanks in advance,

Bart
Software: HS3, HStouch, Plugwise , BLBackup, BLLatestImage, Zwave, JowiHUE, PHlocation, Netcam, Harmony Plugin, ThinkingCleaner, HSPhone, Eneco Toon Script, Pushover, Visonic Powermax LV Interface

(ON ESXI )on NUC
bartbakels
Advanced Member
Advanced Member
 
Posts: 511
Joined: May 2011

Re: Philips Hue Control Script

Postby Willem4ever » Wed Jan 30, 2013 6:43 pm

Hi Bart,

There is NewtonSoft or built-in support since .NET 3.5 (i think). I'm doing some experimenting around an available library to control the hue lamps. Sofar I can dim/on/off the lamps using an application which remotely connects to homeseer. More too follow :-)
User avatar
Willem4ever
Global Moderator
Global Moderator
 
Posts: 804
Joined: October 2006
Location: Uithoorn / Netherlands

Re: Philips Hue Control Script

Postby EaLoo » Sun Feb 03, 2013 7:39 pm

Thanks for the work
EaLoo
Starting Member
Starting Member
 
Posts: 1
Joined: November 2012

Re: Philips Hue Control Script

Postby jakob.sand » Wed Feb 06, 2013 10:04 pm

Is there a way to read the status of each bulb for each mode - I would like to save the specific color, hue etc. for a bulb - before changing it - in order to set i back to original mode. This way I could make the bulb flash RED and back to original setting etc.
jakob.sand
Starting Member
Starting Member
 
Posts: 2
Joined: January 2013

Re: Philips Hue Control Script

Postby bartbakels » Thu Feb 07, 2013 10:52 am

Hi jakob, that was one of the features i would like to add, saving settings to ini including last state. However im at bit busy at the moment so development has a stalled a bit..

Regards

Bart
Software: HS3, HStouch, Plugwise , BLBackup, BLLatestImage, Zwave, JowiHUE, PHlocation, Netcam, Harmony Plugin, ThinkingCleaner, HSPhone, Eneco Toon Script, Pushover, Visonic Powermax LV Interface

(ON ESXI )on NUC
bartbakels
Advanced Member
Advanced Member
 
Posts: 511
Joined: May 2011

Re: Philips Hue Control Script

Postby cblzaccie » Fri Feb 22, 2013 9:51 pm

thanks,

im a little lost on the final setup, can you explain and maybe show actual example of how i set the colors and brightness, i must be doing something wrong as im getting errors.

i set up the 3 hue lights and called them L1 , L2 and L3 i created the devices as lamp modules

i then set up 1 virtual device V9 named it Hue Mode and added the optional device values

when sending the command say to turn lamp 1 on to blue , how exactly do i do it

hue light control.vb("Main","Control;v9;1,2,3") produces the following ( debug is on )

huecontrol Debug calculated color temperature :154
2/22/2013 3:50:15 PM huecontrol Debug calculated Hue :0
2/22/2013 3:50:15 PM huecontrol Debug calculated Saturation :0
2/22/2013 3:50:15 PM Huecontrol Debug calculated Brightness :0
2/22/2013 3:50:15 PM Huecontrol Debug Selected Mode :0
2/22/2013 3:50:15 PM Huecontrol Debug Execution mode :
2/22/2013 3:50:15 PM Huecontrol Debug Action :0
2/22/2013 3:50:15 PM Huecontrol Debug Hue Number :
2/22/2013 3:50:15 PM Huecontrol Debug Device for Mode :
2/22/2013 3:50:15 PM Huecontrol Debug Device to Control :
cblzaccie
Starting Member
Starting Member
 
Posts: 2
Joined: February 2013

Re: Philips Hue Control Script

Postby cblzaccie » Sat Feb 23, 2013 2:56 am

ok i figured the basics out , a few questions: is there a quick easy way to select a particular color for i can add color buttons to hs touch?

also can i co a color wheel type thing on tstouch to dial a color up?
cblzaccie
Starting Member
Starting Member
 
Posts: 2
Joined: February 2013

Re: Philips Hue Control Script

Postby bartbakels » Thu Feb 28, 2013 12:42 pm

hi,

You can use events to do this, by sending a http command to the bridge. Once i have some time I will create this also in the script.
Software: HS3, HStouch, Plugwise , BLBackup, BLLatestImage, Zwave, JowiHUE, PHlocation, Netcam, Harmony Plugin, ThinkingCleaner, HSPhone, Eneco Toon Script, Pushover, Visonic Powermax LV Interface

(ON ESXI )on NUC
bartbakels
Advanced Member
Advanced Member
 
Posts: 511
Joined: May 2011

Re: Philips Hue Control Script

Postby christiand » Sat May 04, 2013 8:25 pm

Bart thank you for the script. I am new to Homeseer and I cant figure out how to create the virtual control device with the 4 values pairs from the Homeseer web interface. I have created now just a virtual device called "Lamp Module w Status" with housecode b20 and for the lights B21, B22 etc. I can turn most lights on, off and dimm, but some wont react. Probably cause I use the default Lamp module with status I dont see the color picking options.

I have setup a scene for the status update, but this gives me an error in the log. I cant figure where it goes wrong. Any help on creating the right device types would be appreciated.

Code: Select all
Scripting runtime error: System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. --- System.InvalidCastException: Conversion from string "te" to type 'Double' is not valid. --- System.FormatException: Input string was not in a correct format. at Microsoft.VisualBasic.CompilerServices.Conversions.ParseDouble(String Value, NumberFormatInfo NumberFormat) at Microsoft.VisualBasic.CompilerServices.Conversions.ToDouble(String Value, NumberFormatInfo NumberFormat) --- End of inner exception stack trace --- at Microsoft.VisualBasic.CompilerServices.Conversions.ToDouble(String Value, NumberFormatInfo NumberFormat) at Microsoft.VisualBasic.CompilerServices.Operators.DivideObject(Object Left, Object Right) at scriptcode1.scriptcode1.Main(Object params) --- End of inner exception stack trace --- at System.RuntimeMethodHandle._InvokeMethodFast(Object target, Object[] arguments, SignatureStruct& sig, MethodAttributes methodAttributes, RuntimeTypeHandle typeOwner) at System.RuntimeMethodHandle.InvokeMethodFast(Object target, Object[] arguments, Signature sig, MethodAttributes methodAttributes, RuntimeTypeHandle typeOwner) at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, Boolean skipVisibilityChecks) at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture) at System.Reflection.MethodBase.Invoke(Object obj, Object[] parameters) at Scheduler.VsaScriptHost.Invoke(String ModuleName, String MethodName, Object[] Arguments)
christiand
Member
Member
 
Posts: 69
Joined: October 2010

Re: Philips Hue Control Script

Postby bartbakels » Thu May 16, 2013 8:23 pm

Cristiaan,

Sorry for my late response, i am quite busy. Did you figure out howto setup. Probably you used the wrong params in the control script for each device. In addition for mode selection you have to create a virtual device with the values dimm hue saturation etc. See post 1 for more info

Regards,

Bart
Software: HS3, HStouch, Plugwise , BLBackup, BLLatestImage, Zwave, JowiHUE, PHlocation, Netcam, Harmony Plugin, ThinkingCleaner, HSPhone, Eneco Toon Script, Pushover, Visonic Powermax LV Interface

(ON ESXI )on NUC
bartbakels
Advanced Member
Advanced Member
 
Posts: 511
Joined: May 2011

Re: Philips Hue Control Script

Postby bartbakels » Sat Jun 15, 2013 5:29 pm

Dear all,

I dont update this script anymore. Im currently working on a Labview application that interfaces with homeseer. This will improve performance and possibilities. However it will take sometime for me to complete this labview application/plugin.


I also will try to make a sort of huedisco functionality controllable by homeseer using the audio input on the server box


I will keep u posted over here

Regards

Bart bakels
Software: HS3, HStouch, Plugwise , BLBackup, BLLatestImage, Zwave, JowiHUE, PHlocation, Netcam, Harmony Plugin, ThinkingCleaner, HSPhone, Eneco Toon Script, Pushover, Visonic Powermax LV Interface

(ON ESXI )on NUC
bartbakels
Advanced Member
Advanced Member
 
Posts: 511
Joined: May 2011

Next

Return to Homeseer Philips Hue - Plugin HS3

Who is online

Users browsing this forum: No registered users and 0 guests