Simple script to update Philips Hue status

From Domoticz
Jump to: navigation, search


You may have noticed that after you switch your Hue lights outside Domoticz the status of your Hue lights can be incorrect. This may cause scripts or Blockly events to malfunction. Or more accurately said: you scripts work just fine, but they are not getting the correct inputs from Domoticz. The purpose of this script is to eliminate these problems.
The concept of the script is that it will loop through your Hue lights (1, 2, 3, etc) until no more lights are found. It gets both the name and status of each light and corrects Domoticz when needed. All the script does is correct the on/off status. It does nothing with (for instance) dim-level or colors. If you need more advanced functions look here[1].


To use the script you'll need to install socket library [2] and dkjson [3]. Both need to end up in /usr/local/lib/lua/5.2 (path is for Synology NAS, it may be different on RPI).
You need to be able to access your bridge using the HUE api. More information on Philips HUE api and how to get/change your username can be found here [4].

Domoticz setup

Another thing is that the name of each light must be the same in your bridge and Domoticz. The script is very simple: it gets the name of each light from your bridge and then sends the status to Domoticz using the exact same name. So if the names differ between the two then the script will not work. You can check the names of your lights using the Hue app.
I saved it in my scripts folder as "script_time_checkLights.lua". Time based scripts are executed every minute. More information on the event system can be found here [5].
To configure the script all you have to do is enter the IP of your HUE bridge and your HUE username.

The script

-- script to check Philips Hue light status (on/off)
-- m.hagenaars
-- feb. 2016
-- you'll need to install socket library & dkjson, more info can be found here:
-- both need to end up in /usr/local/lib/lua/5.2
-- this time based script wil use the HUE api to check the current status of your lights as reported by the HUE bridge
-- the script starts with light #1 and checks if the status reported by your bridge corresponds with the status in Domoticz
-- if HUE state is not equal to Domoticz state then the script will correct the status in Domoticz
-- the script will keep looping until there are no more lights to check and will then stop
-- in order for the script to work it is important that the names of each light are exactly equal in both your bridge & domoticz
-- I suppose you know the names of the HUE switches in Domoticz, you can check the names in your bridge with the HUE app
-- more info on Philips HUE api and how to get/change your username can be found here: 
-- configure Hue Bridge
local hueBridgeIP = 'enter_HUE_ip_here'
local hueBridgeAPI = 'enter_HUE_username_here'
-- do not change beyond this line
-- this part will get the Hue status
function getHueLight(id)
   local http = require('socket.http')
   local ltn12 = require('ltn12')
   local json = require('dkjson')
   t = {}
   local url = string.format("http://%s/api/%s/lights/%s", hueBridgeIP, hueBridgeAPI, id)
   b, c, h = http.request{url=url, sink = ltn12.sink.table(t), method='GET'}
   huestring = tostring(table.concat(t))
   local hue, pos, err = json.decode(huestring, 1, nil)
   if ( then 
      hue_name = (
      hue_state = (hue.state.on)
      stop = true
   return hue_name, hue_state, stop
-- now check Hue state & correct Domoticz if needed
commandArray = {}
i = 1
   local hue = getHueLight(i)
   if hue_state == true and otherdevices[hue_name] == 'Off' then
      commandArray[hue_name] = 'On'
      print(string.format("Status %s corrigeren", hue_name))
    elseif hue_state == false and otherdevices[hue_name] ~= 'Off' then
      commandArray[hue_name] = 'Off'
      print(string.format("Status %s corrigeren", hue_name))
    i = i + 1
return commandArray

Update Script Fix if you remove a broken bulb this script will stop there... not updating the lamps after the broken bulb. Change until(stop) into until(i > 25) where 25 is the highest number of lamps found in your Philips HUE app, this will quick fix it...

Expanded Script

En example of an expanded script can be found Here It add's virtual power and energy meters for HUE and expand/fix the update script.