Philips Hue as a virtual module in the Fibaro HC2
The Philips Hue lights as first of their kind meanwhile enjoy more and more popularity despite their high price. The Smartphone Control is a nice gadget but at the same time a curse as well. The colored pears are nice to be used as “Appcessories”, but who wants to have to look for their smartphone every time they step into a room, only to turn on the light.
Philips had opened the API for the Hue for a while now, so that one is able to integrate the colored pears in various systems. RWE Smart Home showed how to do it (we reported) and integrated the Philips Hue into their home automation.
What would it be like if you were able to integrate the Philips Hue lights into your Fibaro Home Center 2?
Sounds like a nice gadget at first, but at the second glance it is much more than that. Integrated into the Fibaro HC2 as a virtual module, it is possible to address the Philips Hue like a physical hardware component. You can turn it on or off via switch, remote or motion sensor. You can also integrate it into scenes, for instance to get an open window signaled visually.
This is how you integrate the Philips Hue into the Fibaro HC2. Unfortunately it is not suitable for the Fibaro HCL, due to the fact that a LUA Code is necessary.
Create a User on the Philips Hue Bridge
First of all, you need a user name on the Philips Hue Bridge, to be able to address the Philips Hue API with it later on. In order to do that, go to the web surface of the Hue Bridge, via your browser, under the address:
http://<philips-hueip-address>/debug/clip.html
The easiest way for you to find the IP address of the Hue Bridge is via the web surface of your router.
On the web surface of the Philips Hue Bridge, next to the buttons GET, PUT, POST, DELETE, you can find three boxes:
- URL
- Message Body
- Command Response
To create a new user, write the following in the URL line:
http://ip address>/api
Following belongs in the Message Body:
{"devicetype":"test user","username":"newdeveloper"}
Thereby, the user “newdeveloper” is created. Of course, you can name him Fibaro or HeinzHorst as well. Before pressing the button POST, you shortly need to press the (physical) button on the Philips Hue Bridge. After you now pressed POST, you should receive a “success” notification. A Step by Step instruction for the Philips Hue API can be found on “Getting started” as well.
Creating a virtual Module
You can switch to the web surface of your Fibaro HC2 now. On there, you create a new virtual module under “Module” > Add or remove > Add a virtual module. Name the module freely. This module will control a Philips Hue Bulb. Therefore I name my modules Philips Hue Bulb 1 to 3. Assign another room. The field for the IP address and the TCP Port can stay free.
Go to the tab “Advanced” and add a slider.
This slider is later on going to adjust the brightness. Preferably name it “Brightness”, assign “sldBrightness” as ID and select “LUA Code” for the slider.
Upload Icon Set
In order for the virtual module to later on receive a matching icon depending on the brightness of the Philips Hue, the next thing you need is the Icon Set. You can download it on here:
Unpack it on your computer after the download. You will find 13 icons in this file. Upload them via the button “change symbol” in numbered order.
As soon as those icons are uploaded, you are going to receive an ID from the Fibaro Home Center. The first icon usually receives the ID 1000, the next the ID 1001 and so on. To make sure that your IDs start at 1000 as well, since you could have uploaded icons before, check it once more. The easiest way to do that is via the Google Chrome Browser or when using Firefox via the developer-tools. With a right-click on the icon > then “examine element” the assigned ID can be found in the source text.
You can find the assigned ID in the source text. You need it in the script.
Slider LUA Code
The earlier added slider is supposed to receive the script now, which adjusts the brightness of the Philips Hue. Since the value of the Philips Hue light for the full brightness is 255, and the slider can only adopt a value of 0-100, we always need to multiply it with 2.55. You can copy the following code into the slider (sldBrightness) like that:
1 2 | hueBrightness = math.floor(_sliderValue_ * 2.55) hueOn = true |
The three values, which have to be adjusted, can be found in the head of the LUA Script.
1 2 3 4 | hueIP = fibaro:get(selfId, "IPAddress") huePort = fibaro:get(selfId, "TCPPort") hueUser = "newdeveloper" hueLightID = 1 |
This is the IP address of your Philips Hue Bridge. I you named your user name other than “newdeveloper” in the first step you need to replace it as well. The value “hueLightID” shows, which Hue Bulb is supposed to be controlled. The starter package of the Philips Hue includes three lights. This means the value in this case could adopt 1-3.
If your Fibaro HC2 did not start at 1000 by assigning IDs to the icons, you might adjust them – beginning at the function:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 | selfId = fibaro:getSelfId() hueIP = fibaro:get(selfId, "IPAddress") huePort = fibaro:get(selfId, "TCPPort") hueUser = "newdeveloper" hueLightID = 1 -- set the id of the HC2 Virtual Device vDeviceID = fibaro:getSelfId(); -- function to select the correct icon based on the slider value -- NOTE: change the icon IDs if needed function getIcon(sValue) if(sValue >= 90) then return 1010 -- 90%-99% 11_s_light100.png elseif(sValue >= 80) then return 1009 -- 80%-89% 10_s_light90.png elseif(sValue >= 70) then return 1008 -- 70%-79% 09_s_light80.png elseif(sValue >= 60) then return 1007 -- 60%-69% 08_s_light70.png elseif(sValue >= 50) then return 1006 -- 50%-59% 07_s_light60.png elseif(sValue >= 40) then return 1005 -- 40%-49% 06_s_light50.png elseif(sValue >= 30) then return 1004 -- 30%-39% 05_s_light40.png elseif(sValue >= 20) then return 1003 -- 20%-29% 04_s_light30.png elseif(sValue >= 10) then return 1002 -- 10%-19% 03_s_light20.png elseif(sValue >= 1) then return 1001 -- 1%-9% 02_s_light10.png else return 1000 end -- 0% 01_s_light0.png end -- define the new values for hueBrightness and hueOn if (_sliderValue_ == 0) then hueBrightness = _sliderValue_ hueOn = false else -- multiply the slider value with 2.55 and round it DOWNWARDS -- to get the hueBrightness value (0-255) hueBrightness = math.floor(_sliderValue_ * 2.55) hueOn = true end -- connect to the Hue bridge Hue = Net.FHttp(hueIP,huePort) -- HTTP PUT to set the new values response, status, errorCode = Hue:PUT('/api/'..hueUser..'/lights/'..hueLightID..'/state', '{"on":'..tostring(hueOn)..',"bri":'..hueBrightness..'}') -- continue if HTTP status code is 200 if (tonumber(status) == 200) then -- change the icon based on the new slider value fibaro:call(vDeviceID, "setProperty", "currentIcon", getIcon(_sliderValue_)) end |
Main loop LUA Code – Requesting Status

Fibaro HC2 – inserting of the Main Loop LUA Code which requests the status of the Philips Hue steadily
The Main Loop Code is going to update the status of your Philips Hue Bulb steadily, to update the status of the slider, the icon and the information bar. In case you should use the Philips Hue App from time to time, to adjust the brightness, the status slider in the virtual module of the Fibaro HC2 is updated automatically. The Fibaro HC2 gets these information every 3 seconds (Standard, can be changed in the script) from the Philips Hue Bridge. Copy the following script into the field “Main loop”. Here again, you need to adjust the values “hueIP”; “hueUser” and the “hueLightID” in the head of the script.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 | -- set the Hue bridge properties -- http://developers.meethue.com/gettingstarted.html -- go to http:///api//lights to get the ID of all your Hue lights selfId = fibaro:getSelfId() hueIP = fibaro:get(selfId, "IPAddress") huePort = fibaro:get(selfId, "TCPPort") hueUser = "newdeveloper" hueLightID = 1 -- set the id of the HC2 Virtual Device vDeviceID = fibaro:getSelfId(); -- set the id of the icons used -- NOTE: change the icon IDs if needed iconUnreachable = 1011 iconError = 1012 -- specify the delay in milliseconds (3000 = 3 seconds) -- the lower the delay, the more traffic is send to the Hue bridge delay = 3000 -- function to select the correct icon based on the slider value -- NOTE: change the icon IDs if needed function getIcon(sValue) if(sValue >= 90) then return 1010 -- 90%-99% 11_s_light100.png elseif(sValue >= 80) then return 1009 -- 80%-89% 10_s_light90.png elseif(sValue >= 70) then return 1008 -- 70%-79% 09_s_light80.png elseif(sValue >= 60) then return 1007 -- 60%-69% 08_s_light70.png elseif(sValue >= 50) then return 1006 -- 50%-59% 07_s_light60.png elseif(sValue >= 40) then return 1005 -- 40%-49% 06_s_light50.png elseif(sValue >= 30) then return 1004 -- 30%-39% 05_s_light40.png elseif(sValue >= 20) then return 1003 -- 20%-29% 04_s_light30.png elseif(sValue >= 10) then return 1002 -- 10%-19% 03_s_light20.png elseif(sValue >= 1) then return 1001 -- 1%-9% 02_s_light10.png else return 1000 end -- 0% 01_s_light0.png end -- connect to the Hue bridge Hue = Net.FHttp(hueIP,huePort) -- HTTP GET to get all the info from the light response ,status, errorCode = Hue:GET('/api/'..hueUser..'/lights/'..hueLightID); -- continue if HTTP status code is 200 if (tonumber(status) == 200) then jsonTable = json.decode(response) -- check if error table exists if (jsonTable[1] ~= nil) then -- show error information errorType = jsonTable[1].error.type errorDescription = jsonTable[1].error.description fibaro:debug("Error type = "..errorType) fibaro:debug("Error description = "..errorDescription) if (errorType == 1) then fibaro:log("Hue: Unauthorized user") elseif (errorType == 3) then fibaro:log("Hue: Incorrect light ID") end -- set slider value to 0 fibaro:call(vDeviceID, "setProperty", "ui.sldBrightness.value", 0) -- change the icon to iconError fibaro:call(vDeviceID, "setProperty", "currentIcon", iconError) else -- get the on state hueOn = jsonTable.state.on fibaro:debug("hueOn = " .. tostring(hueOn)); -- get the brightness value (0-255) hueBrightness = tonumber(jsonTable.state.bri) fibaro:debug("hueBrightness = "..hueBrightness); -- get the reachable state hueReachable = jsonTable.state.reachable fibaro:debug("hueReachable = " .. tostring(hueReachable)); if (hueReachable == true) then if (hueOn == false) then hueSlider = 0; else -- devide the hueBrightness value by 2.55 and round it UPWARDS -- to get the hueSlider value (0-100) hueSlider = math.ceil(hueBrightness / 2.55); end fibaro:debug("hueSlider = "..hueSlider); -- show brightness and state info on the bottom bar of the Virtual Device fibaro:log("brightness: "..hueBrightness.." | on: "..tostring(hueOn)); -- set the actual slider value fibaro:call(vDeviceID, "setProperty", "ui.sldBrightness.value", hueSlider) -- change the icon of the Virtual Device based on the hueSlider value fibaro:call(vDeviceID, "setProperty", "currentIcon", getIcon(hueSlider)) else fibaro:log("Hue: Light not reachable"); -- set slider value to 0 fibaro:call(vDeviceID, "setProperty", "ui.sldBrightness.value", 0) -- change the icon to unreachable fibaro:call(vDeviceID, "setProperty", "currentIcon", iconUnreachable) end end end -- delay the Main loop fibaro:sleep(delay); |
In case your icons should not start with the ID 1000 and up, you need to change that in the “function getIcon” as well.
This is where the color comes in
Until this point, you have a finished virtual module, which can turn your Philips Hue on and off, as well as dim it. If you now want to change the color in the virtual module as well, you need another slider. Therefore, just add another slider and, for instance, name it “Color”.
You can freely assign the ID. I named it color in this example. Add the following code:
1 2 3 4 5 6 7 8 9 | selfId = fibaro:getSelfId() hueIP = fibaro:get(selfId, "IPAddress") huePort = fibaro:get(selfId, "TCPPort") hueUser = "newdeveloper" hueLightID = 1 HueGtw = Net.FHttp(hueIP,huePort) vHue = math.floor(_sliderValue_ * 655.35) HueGtw:PUT('/api/'..hueUser..'/lights/'..hueLightID..'/state', '{"hue":'.. vHue .. '}') |
On here, you need to adjust the variables “hueIP”; “hueUser”; and “hueLightID”, just like you had to with the other scripts as well.
Now you are not only able to vary the brightness with the virtual module, but also the color. You can address both sliders, even in scenes, and for instance turn on the Philips Hue with the Fibaro Motion Sensor, if motions were recognized. You could change the color of the Philips Hue to a pre-defined color, if for example the window contact recognized an open window.
Another tip, assign a fixed IP address to the Philips Hue Bridge, therefore you do not have to adjust the IP in the virtual module that often. In case you are using a FRITZ!Box, you can check the field “always assign the same IP address to this device”.
You can also download the virtual module here, and import it into your Fibaro Home Center 2. You have to reload the icon set, since it unfortunately cannot be exported into the virtual modules.
Virtual module – Philips Hue lights
************** Update August 16, 2014 **************
Thank you very much, Simon! He has undergone the virtual module for the Philips Hue a fine tuning.
– Saturation adjustable via slider
– State: on|off can only be affected through the brightness (no more also through brightness)
– IP und TCP Port are read from the module
This means, the IP address does not have to be changed in the LUA Code directly, but can also be endorsed in the virtual module in the tab “General” under IP Address.
Simon fills the variable “hueIP” with the following order
1 2 3 | selfId = fibaro:getSelfId() hueIP = fibaro:get(selfId, "IPAddress") huePort = fibaro:get(selfId, "TCPPort") |
Thank you, Simon, for the advancement of the virtual module.
You can download the update on here:

This text was translated by Linda
Hi! My name is Linda and I graduated high school with great interest in English language. I help with mein-cleveres-haus.de and translate texts for siio.de in my spare time.
You can read the original in German on siio.de.
Hi. Great post! When I try to copy the LUA code, a lot of (CSS?) formatting is included in the code. Is there a way to copy the “clean” code?
Thanks!
Thank you so much for this post! Do you have the LUA codes without all the formatting? It’s a lot of
font-family: Courier, ‘Courier New’, sans-serif; font-size:……..
to get rid of :)
oh! Sorry for the problems. We have corrected the code now.
Hi, it works correctly, but i have a problem with the app for iPhone, it not change the icon of the bulb when the state change. Can you help me?
Hello,
Still no chance to use Philips Hue with Fibaro HCL?
Do I have to upgrade do HC2 ?
Thank you very much
Hello,
Yesterday I wrote a message, that has been deleted.
I asked if nowadays is there a chance to use Philips Hue with Fibaro HCL, otherwise I have to upgrade to HC2.
I was sure that it wasn’t offtopic, so I rewrite the question.
Thank you very much
Alessandro
Hi,
no, at the moment there is no way to use it. :( Maybe Fibaro puts it in the next firmware-release.
Hi!
This post now needs some updates:
Philips has change the way to get username, you can not make your own username anymore and will have to use a code something like this:
{“devicetype”: “test user”}
And get a long random username.
Also FHttp command doesnt seem to work with HC2 anymore? I only get this error
[ERROR] 22:52:22: line FHttp:
[ERROR] 22:52:37: line FHttp:
The same here… can you please update the post?
[ERROR] 22:52:22: line FHttp: