[122] Philips Hue support

Use this forum to discuss possible implementation of a new feature before opening a ticket.
A developer shall edit the topic title with "[xxx]" where xxx is the id of the accompanying tracker id.
Duplicate posts about the same id. +1 posts are not allowed.

Moderators: leecollings, remb0

simonrg
Posts: 329
Joined: Tuesday 16 July 2013 22:54
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.8807
Location: North East England
Contact:

Re: [122] Philips Hue support

Post by simonrg »

Inspired by what Ewald has done with bash scripts and what paulclinger has done in porting the sockets library to Lua 5.2 on the Raspberry Pi, I have started to put together some simple Lua scripts to directly control the Philips Hue lights.

This is work in progress, so any suggestions / help most welcome. This post assumes you understand how to interact with Hue lights and have installed the socket library from Zerobrane, I will document fully when complete.

Currently, I just wanted to show what is possible, as I have managed to use Lua with the Lua sockets library to control the Philips Hue lights.

The script below demonstrates what is possible, if you have a number of Hue light, then using this script and just creating dummy switches in Domoticz called Hue1, Hue2, Hue3, ...., you will be able to turn the lights on and off.

Code: Select all

-- ~/domoticz/scripts/lua/script_device_hue.lua
-- Simple script to turn on and off Philips Hue lights
-- For each of your Philips Hue lights you need to create a dummy switch named: HueXanythingatall
-- where x is the number of your light, e.g. Hue3Lounge would control Hue light 3
-- Ensure that you have registered the user with the Philips Hue bridge
-- N.B. one wrongly named Hue dummy switch may stop the script, check log for any issues

commandArray =  {}

io = require('io')
http = require('socket.http')
ltn12 = require('ltn12')
base_url = 'http://192.168.1.70/api/newdeveloper/'

function hueit(device, operation)
   local url = base_url .. device
   print('About to try')
   print(url)
   --local req_body = '{"on":true}'
   local req_body = operation
   local headers = {
       ["Content-Type"] = "application/x-www-form-urlencoded";
       ["Content-Length"] = #req_body;
   }
   client, code, headers, status = http.request{url=url, headers=headers, source=ltn12.source.string(req_body), method='PUT'}
   print('Tried')
   print(status)
   return
end

tc=next(devicechanged)
v=tostring(tc)
if (v:sub(1,3) == 'Hue') then
   print(v..' spotted')
   device = 'lights/' .. v:sub(4,4) .. '/state'
   if(devicechanged[v] =='On') then
      hueit(device, '{"on":true}')
   else
      hueit(device, '{"on":false}')
   end
end

return commandArray
My next plans are to incorporate the ability to change the light properties using dummy dimmers in Domoticz to correspond to R, G, B values which are then translated into colours which are then sent to the Hue lights.

So gHue1 would set the green level of the Hue light 1, rHue1 and bHue1 would then give the full colours for Hue light 1. I just need to understand the colour model used by the Hue lights and translate this into Lua.

I hope that while this will be rather clunky for setting colours interactively, that combining these switches and dimmers in scenes with preset level, it will be possible to create a numbers of scenes which allow rapid switching of a set of Hue lights' properties.
Raspberry Pi 2 B - 2A@5V PSU - Raspbian + Domoticz + RFXtrx(89), LightwaveRF House(dimmers, sockets, wireless/mood switches), Owl CM113, 4 LaCross Temp / Humidity Sensors, 4 Siemens PIR, Smappee, Solaredge, ESP8266
simonrg
Posts: 329
Joined: Tuesday 16 July 2013 22:54
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.8807
Location: North East England
Contact:

Re: [122] Philips Hue support

Post by simonrg »

I have now got colour changing working via rgb dimmer switches, still a work in progress.

The short script will work with any number of Hue lights, is able to turn the light (dummy switch Hue1) on using colours set on dummy dimmers (rHue1, gHue1, bHue1) in Domoticz and turn the light off. Note colour changes only happen when the light is switched on by its own dummy switch not by the dimmer switches.

Firstly, you need to connect your Domoticz computer to your Philips Hue bridge, follow the instructions on the link from the wiki here - http://www.domoticz.com/wiki/Philips_Hue_Lights

Secondly, you need to install the socket and luacolor libraries on your machine, I have attached gzipped tar files for the Raspberry Pi, as there is no packaged versions available. Download the two attached files, placing usrlocalsharelua5p2.tar.gz in directory /usr/local/share/lua/5.2/ and extracting the file with sudo tax -xvf usrlocalsharelua5p2.tar.gz, then put usrlocalliblua5p2.tar.gz in /usr/local/lib/lua/5.2/ and extract all the files with sudo tar -xvf usrlocalliblua5p2.tar.gz.

Thirdly, create a dummy on/off switch Hue1, and three dummy dimmer switches rHue1, gHue2 and bHue3 the dimmer switch must not use X10 protocol as this does not support dimmer switches.

Foruthly, edit the script below, to put the ip address of your Philips Hue bridge and replace newdeveloper with the user you have registered on the Philips Hue bridge:

Code: Select all

-- ~/domoticz/scripts/lua/script_device_hue.lua
-- Simple script to turn on and off Philips Hue lights with colour control

-- For each of your Philips Hue lights you need to create a dummy switch named: HueXanythingatall
-- and 3 dummy dimmer switch (not X10) named rHueXanythingatall, gHueXanythingatall, bHueXanythingatall
-- where x is the number of your light, e.g. Hue3Lounge would control Hue light 3, 
--  with rHue3Lounge, gHue3Lounge and bHue3Lounge dummy dimmers controlling the colour.

-- Ensure that you have registered the user with the Philips Hue bridge
-- N.B. one wrongly named Hue dummy switch may stop the script, check log for any issues

commandArray =  {}
base_url = 'http://192.168.89.170/api/newdeveloper/'

tc=next(devicechanged)
v=tostring(tc)
-- Check to see if the device changed is a Hue light
if (v:sub(1,3) == 'Hue') then
   -- Only load libaries if a Hue light
   io = require('io')
   http = require('socket.http')
   ltn12 = require('ltn12')
   colors = require('luacolors')

   function hueit(device, operation)
   -- Sends commands to Philips Hue bridge
   --  device is the device specific url i.e. /lights/1/state
   --  operation is the message body i.e. {"on":true, "sat":255, "bri":255,"hue":10000}
   --    for details see http://www.developers.meethue.com/documentation/getting-started
      local url = base_url .. device
      print(url)
      local req_body = operation
      local headers = {
        ["Content-Type"] = "application/x-www-form-urlencoded";
        ["Content-Length"] = #req_body;
      }
      client, code, headers, status = http.request{url=url, headers=headers, source=ltn12.source.string(req_body), method='PUT'}
      print(status)
      return
   end

   function huecolors(switch)
   -- Reads dummy dimmers from Domoticz with rgb values i.e. rHue1, gHue1, bHue1
   -- Converts the rgb values to x,y for Philips Hue
   -- Return command string ready to be sent to Philips Hue bridge
--      print('r'..switch)
      red = otherdevices_svalues['r'..switch]
      green = otherdevices_svalues['g'..switch]
      blue = otherdevices_svalues['b'..switch]
--      print('rgb = ' .. red ..','.. green ..','.. blue)
      X, Y, Z = colors.sRGBtoXYZ(red, green, blue)
--      print('XYZ = ' .. X ..','.. Y ..','.. Z)
      return ',"xy":[' .. (X / (X + Y + Z)) .. ',' .. (Y / (X + Y + Z)) .. ']'
   end

   print(v..' spotted')
   device = 'lights/' .. v:sub(4,4) .. '/state'
   if(devicechanged[v] =='On') then
      params = '{"on":true' .. huecolors(v) ..'}'
--      print(params)
      hueit(device, params)
   else
      hueit(device, '{"on":false}')
   end
end

return commandArray
Attachments
usrlocalsharelua5p2.tar.gz
Unpack in /usr/local/share/lua/5.2/
(117.12 KiB) Downloaded 3722 times
usrlocalliblua5p2.tar.gz
Unpack in /usr/local/lib/lua/5.2/
(119.53 KiB) Downloaded 3025 times
Raspberry Pi 2 B - 2A@5V PSU - Raspbian + Domoticz + RFXtrx(89), LightwaveRF House(dimmers, sockets, wireless/mood switches), Owl CM113, 4 LaCross Temp / Humidity Sensors, 4 Siemens PIR, Smappee, Solaredge, ESP8266
joske522
Posts: 18
Joined: Friday 19 September 2014 20:46
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: [122] Philips Hue support

Post by joske522 »

Edgar wrote:This was the hard part.

You can use Textwrangler to edit your files directly on the rpi.

Open TextWrangler (download first), go to File menu, select Open from FTP/SFTP server.
Fill in server ip address, loginname (pi) and password (raspberry), and don't forget to select SFTP. (SFTP uses the SSH protocol (..) to transfer data.)

Now you can go to the bash directory and you'll find your previously created file. From here you can edit it (to switch off) and save it under a new name.

You'll still have to use Terminal and ssh connection to make the files executable, but editing is lot easier now.
Hi Edgar!

I followed your explanation and it works flawlessly! Thank you so much!

Just one more question: I have a kaku switch and added it as a sub/slave device to my newly created script for turning all my hue lights on at the same time and on a specific color. When I press the switch, the status in Domoticz changes, but the lights don't come on or go off... When I click the little lamp icon in Domoticz, the lights do come on or off. I added the kaku as an on/off device.

Thanks!
joske522
Posts: 18
Joined: Friday 19 September 2014 20:46
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: [122] Philips Hue support

Post by joske522 »

Ok I found it by using the scene function and learning the code of the switch to it! 8-)

Now everything works! This is awesome, thanks for the support you guys!
User avatar
bbqkees
Posts: 407
Joined: Sunday 17 August 2014 21:01
Target OS: Linux
Domoticz version: 4.1x
Location: The Netherlands
Contact:

Re: [122] Philips Hue support

Post by bbqkees »

Although Hue is not completely integrated yet, the aforementioned code and the Wiki item were very helpful in getting my Hue lights connected with Domoticz.

What I like in the Hue Zigbee lights is that the communication is encrypted and that every bulb also acts a a repeater so I can use LivingWhite dimmers as light switches further away in the garden.
Bosch / Nefit / Buderus / Junkers / Worcester / Sieger EMS bus Wi-Fi MQTT Gateway and interface boards: https://bbqkees-electronics.nl/
User avatar
sincze
Posts: 1299
Joined: Monday 02 June 2014 22:46
Target OS: Raspberry Pi / ODroid
Domoticz version: 2024.4
Location: Netherlands / Breda Area
Contact:

Re: [122] Philips Hue support

Post by sincze »

joske522 wrote:Ok I found it by using the scene function and learning the code of the switch to it! 8-)

Now everything works! This is awesome, thanks for the support you guys!
Should I wait a little longer before buying my first starter kit ?? :D
Or is everybody convinced about the current possibilities?
Pass2php
LAN: RFLink, P1, OTGW, MySensors
USB: RFXCom, ZWave, Sonoff 3
MQTT: ZIgbee2MQTT,
ZWAVE: Zwave-JS-UI
WIFI: Mi-light, Tasmota, Xiaomi Shelly
Solar: Omnik, PVOutput
Video: Kodi, Harmony HUB, Chromecast
Sensors: You name it I got 1.
User avatar
bbqkees
Posts: 407
Joined: Sunday 17 August 2014 21:01
Target OS: Linux
Domoticz version: 4.1x
Location: The Netherlands
Contact:

Re: [122] Philips Hue support

Post by bbqkees »

sincze wrote: Should I wait a little longer before buying my first starter kit ?? :D
Or is everybody convinced about the current possibilities?
Just buy the starter kit already.
The integration with Domoticz is not complete but it is workable, but aside from this it is a very nice toy to play with.

Control via the Hue app is inconvenient for me, so I bought an additional LivingColors remote (24 euro) for manual control and I use Domoticz for sunset on/off scenario etc.
Bosch / Nefit / Buderus / Junkers / Worcester / Sieger EMS bus Wi-Fi MQTT Gateway and interface boards: https://bbqkees-electronics.nl/
Edgar
Posts: 24
Joined: Saturday 13 September 2014 23:26
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Netherlands, -6 NAP
Contact:

Re: [122] Philips Hue support

Post by Edgar »

@bbqkees

That's interesting. Which starterkit do you have? I thought the LivingColors Remote only worked with the Bloom type of devices. Does it work with the bulbs or led string as well?

I bought the HueTap in addition but am not really happy with the way it works. They should have made it battery powered (but that's an other discussion).
Only Robinson Crusoe had everything done by Friday
User avatar
bbqkees
Posts: 407
Joined: Sunday 17 August 2014 21:01
Target OS: Linux
Domoticz version: 4.1x
Location: The Netherlands
Contact:

Re: [122] Philips Hue support

Post by bbqkees »

I have a GU10 starterkit, one E27 bulb and some LivingWhite dimmers.
The LivingWhite dimmers have a switch to toggle between 'dimmer' and 'on/off only' function, so you can also control other stuff than lighting.
But the LivingWhite dimmers are hard to get nowadays because Philips stopped making them a while ago.

The remotes work on all Hue products, LivingWhites and all LivingColors from gen2 or 3 onwards.
The only thing that wont work is the gen.1 LivingColors.

You first need to pair the remote with all the lights you want to control with that remote, and after that pair the remote with the bridge.
All information on LivingColor remotes etc is available on http://www.everyhue.com/

After you paired everything correctly, all remote functions will work like the color wheel, scene, and on/off.

You can buy the remote here:
http://philips.e-shoppers.eu/Product_Sp ... ueID=10376
Bosch / Nefit / Buderus / Junkers / Worcester / Sieger EMS bus Wi-Fi MQTT Gateway and interface boards: https://bbqkees-electronics.nl/
Derik
Posts: 1601
Joined: Friday 18 October 2013 23:33
Target OS: Raspberry Pi / ODroid
Domoticz version: BETA
Location: Arnhem/Nijmegen Nederland
Contact:

Re: [122] Philips Hue support

Post by Derik »

@ bbqkees.

Why a heu remote?
You can switch whit a KaKu ....
Xu4: Beta Extreme antenna RFXcomE,WU Fi Ping ip P1 Gen5 PVOutput Harmony HUE SolarmanPv OTG Winddelen Alive ESP Buienradar MySensors WOL Winddelen counting RPi: Beta SMAspot RFlinkTest Domoticz ...Different backups
User avatar
bbqkees
Posts: 407
Joined: Sunday 17 August 2014 21:01
Target OS: Linux
Domoticz version: 4.1x
Location: The Netherlands
Contact:

Re: [122] Philips Hue support

Post by bbqkees »

Derik wrote:@ bbqkees.

Why a heu remote?
You can switch whit a KaKu ....
The LivingColors remote offers direct color control via a color wheel and it has brightness and saturation buttons.
With the KaKu achieving something similar is not so straight forward with a single remote.
While I have the Hue for over a year now, I do not own a RFXcom yet, so switching with KaKu is not possible at the moment.

I do have plans to get a RFXcom and some KaKu devices, but for the moment I still have other projects to be done first (bunch of Arduino's etc). :)
Bosch / Nefit / Buderus / Junkers / Worcester / Sieger EMS bus Wi-Fi MQTT Gateway and interface boards: https://bbqkees-electronics.nl/
simonrg
Posts: 329
Joined: Tuesday 16 July 2013 22:54
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.8807
Location: North East England
Contact:

Re: [122] Philips Hue support

Post by simonrg »

I have written a couple of Lua scripts which interact with Philips Hue light scenes.

The scripts utilise two switches to control the lights:
  • first switch when pressed captures the current state of my Philips Hue lights and stores this scene in a new variable in Domoticz
    second switch when pressed retrieves the next previously stored scence from Domoticz and sets the Philips Hue lights
So pressing the second switch cycles through all the scenes previously captured, this would be rather slow for 10s of scenes, but works well for a few i.e. relax, concentrate, warm glow.

I have put the scripts on the wiki on the Philips Hue page - http://www.domoticz.com/wiki/Philips_Hue_Lights - I plan to document a bit more when I get some time.
Raspberry Pi 2 B - 2A@5V PSU - Raspbian + Domoticz + RFXtrx(89), LightwaveRF House(dimmers, sockets, wireless/mood switches), Owl CM113, 4 LaCross Temp / Humidity Sensors, 4 Siemens PIR, Smappee, Solaredge, ESP8266
User avatar
gizmocuz
Posts: 2350
Joined: Thursday 11 July 2013 18:59
Target OS: Raspberry Pi / ODroid
Domoticz version: beta
Location: Top of the world
Contact:

Re: [122] Philips Hue support

Post by gizmocuz »

Upcoming beta has native philips hue support ! (Thanks to Kees for lending a bridge and a hue light !!!)

#2079+
Quality outlives Quantity!
simonrg
Posts: 329
Joined: Tuesday 16 July 2013 22:54
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.8807
Location: North East England
Contact:

Re: [122] Philips Hue support

Post by simonrg »

Great fantastic can't wait, thanks :D :D :D :D
Raspberry Pi 2 B - 2A@5V PSU - Raspbian + Domoticz + RFXtrx(89), LightwaveRF House(dimmers, sockets, wireless/mood switches), Owl CM113, 4 LaCross Temp / Humidity Sensors, 4 Siemens PIR, Smappee, Solaredge, ESP8266
simonrg
Posts: 329
Joined: Tuesday 16 July 2013 22:54
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.8807
Location: North East England
Contact:

Re: [122] Philips Hue support

Post by simonrg »

So went to Hardware - added the Philips Hue Bridge - filled in ip address and username - clicked register after having pressed the link button on the bridge and magically under devices I then had 3 Lighting Limitless / Applamp devices.

2 of my lamps are labelled Hue Lamp 2 (id 00000002, unit 1) and Hue Lamp 3(id 00000003, unit 1), the other is labelled as Unknown (id 1, unit 1). So moved devices from unused to used, renamed Unknown to Hue Lamp 1, went to switches and edited type of Hue Lamp 1.

Lamps 2 and 3 work fully with colour control, on/off etc, but lamp 1 basically works, apart from the Domoticz display does not change correctly saying lamp is Off when it is On. Something funny going on, will delete everything and try again.

Great, many thanks.

As the Philips Hue lights can be controlled by the bridge from many different apps, the colour displayed in Domoticz appears to be that last set by Domoticz, not the current colour of the lamp. Is that correct? Is there a way to update the Domoticz display to show the current colour, so it can then be captured into scenes? Thanks.
Raspberry Pi 2 B - 2A@5V PSU - Raspbian + Domoticz + RFXtrx(89), LightwaveRF House(dimmers, sockets, wireless/mood switches), Owl CM113, 4 LaCross Temp / Humidity Sensors, 4 Siemens PIR, Smappee, Solaredge, ESP8266
Derik
Posts: 1601
Joined: Friday 18 October 2013 23:33
Target OS: Raspberry Pi / ODroid
Domoticz version: BETA
Location: Arnhem/Nijmegen Nederland
Contact:

Re: [122] Philips Hue support

Post by Derik »

@simon..
are the hue lights active?
Or do you have old light in the Hue bridge?
Xu4: Beta Extreme antenna RFXcomE,WU Fi Ping ip P1 Gen5 PVOutput Harmony HUE SolarmanPv OTG Winddelen Alive ESP Buienradar MySensors WOL Winddelen counting RPi: Beta SMAspot RFlinkTest Domoticz ...Different backups
simonrg
Posts: 329
Joined: Tuesday 16 July 2013 22:54
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.8807
Location: North East England
Contact:

Re: [122] Philips Hue support

Post by simonrg »

@Derik, all 3 Hue lights are the same, come from a starter pack I purchased December 2012, does that make them active? I belive they are Zigbee lighs which communicate with the Hue bridge.
Raspberry Pi 2 B - 2A@5V PSU - Raspbian + Domoticz + RFXtrx(89), LightwaveRF House(dimmers, sockets, wireless/mood switches), Owl CM113, 4 LaCross Temp / Humidity Sensors, 4 Siemens PIR, Smappee, Solaredge, ESP8266
Derik
Posts: 1601
Joined: Friday 18 October 2013 23:33
Target OS: Raspberry Pi / ODroid
Domoticz version: BETA
Location: Arnhem/Nijmegen Nederland
Contact:

Re: [122] Philips Hue support

Post by Derik »

Ok.

My light make a lot of sound..
So i get 3 new lights, only the old one remove is not easy.
You have the resset the bridge, and then you lost all settings..
ScreenShot068.jpg
ScreenShot068.jpg (62.62 KiB) Viewed 11640 times
Xu4: Beta Extreme antenna RFXcomE,WU Fi Ping ip P1 Gen5 PVOutput Harmony HUE SolarmanPv OTG Winddelen Alive ESP Buienradar MySensors WOL Winddelen counting RPi: Beta SMAspot RFlinkTest Domoticz ...Different backups
simonrg
Posts: 329
Joined: Tuesday 16 July 2013 22:54
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.8807
Location: North East England
Contact:

Re: [122] Philips Hue support

Post by simonrg »

Deleted Hue Lamp devices and Hue Bridge hardware, reinstalled hardware - still 2 lamps recognised correctly and 1 not.
Screen shot 2014-10-25 at 18.41.16.png
Screen shot 2014-10-25 at 18.41.16.png (38.06 KiB) Viewed 11634 times
All 3 lamps are original with no differences showing up in any other context.
So I guess this must be a bug?
Raspberry Pi 2 B - 2A@5V PSU - Raspbian + Domoticz + RFXtrx(89), LightwaveRF House(dimmers, sockets, wireless/mood switches), Owl CM113, 4 LaCross Temp / Humidity Sensors, 4 Siemens PIR, Smappee, Solaredge, ESP8266
Blueone
Posts: 130
Joined: Friday 29 November 2013 11:30
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: [122] Philips Hue support

Post by Blueone »

Thanks for the extra functionality!

I found a little bug, the livingcolor lights within hue aren't recognized properly. It is in domoticz always in the off state, but I can control it. Also the living color shows as normal switch instead of a dimmer like the e27 hue lights.
Post Reply

Who is online

Users browsing this forum: No registered users and 0 guests