LIFX

From Domoticz
Jump to: navigation, search

LIFX in Domoticz

Background

LIFX® is a wifi enabled, multi-color, energy efficient LED light bulb.
While significantly more expensive than limitless/milight bulbs, this is a 17W bulb and the lumen output of the bulb is several times higher.
Also, the bulb supports white temperature control as well as RGB with saturation control and thus offers a far more flexible setup.
If you are planning to change all lights in your house with smart lights, it would be worthwhile to consider a few LIFX bulbs for key locations coupled with cheaper milight/limitless for other locations

Setting up your LIFX

The guide assumes that you have the LIFX bulb setup on your network and you can control it through the official iOS/Android app already

Integrating it with your domoticz installation is essentially an indirect two step process (hopefully we may see official integration in domoticz like Hue and limitless soon)

Keep in mind that this method is a workaround only and while you will have on/off control, you will need to define individual scenes for each kind of color state you want

1) Create a dummy switch for each of your LIFX bulbs

2) While there are multiple libraries available to control LIFX, I zeroed on the excellent arrian/lifx-python library (https://github.com/arrian/lifx-python)

3) Install dependencies - sudo apt-get install python git

4) cd /home/pi/

5) git clone https://github.com/arrian/lifx-python.git

6) test that it works by using one of the examples by

sudo python /home/pi/lifx-python/example_gatsby.py

You should be able to see the bulb change color states

7) Now comes the tedious part - Creating scripts for all the desired states

8) the library relies on a simple format for switching the LIFX on/off and setting color - example below - change the text in the parentheses to suit your needs

from time import sleep
import lifx
lights = lifx.Lifx()
lights.on()
lights.set_colour(hue (0-50000) , staturation (0-50000), brightness(0-50000), Kelvin , transitionduration)

9) Save a few sample scripts , would suggest creating a basic lifx_on.py and lifx_off.py to begin with

10) since these are python scripts, use the following format

- #!/usr/bin/python

from time import sleep

import lifx

lights = lifx.Lifx()

lights.on()

lights.set_colour(1 , 0, 45000, 5000)

11) In the above example, feel free to change hue, saturation brightness and tempertaure to suit your needs

12) save the script and change it to an executable - chmod +x lifx_on.py

13) Now go back to the dummy switch you created and add the path to the script in the on action

14) Do the same for the off action

That should be it

Next steps

1) Create scenes and link them to their custom scripts (e.g. for scene Warm hue, set color temperature to 3000 and brightness to 20000)
2) You can add sleep between set color arguments to create custom transitions

Use Of The Beta LIFX Cloud API

Following the recent release of the V2.0 LIFX firmware lifx have exposed a beta version of api control. You can interact with your bulbs via the lifx cloud. The documentation for this is based on swagger & can be found at https://api.lifx.com

Authentication is via a unique key that is embedded in the html header request.

To obtain a key at this point involves e-mailing lifx as per the following post: https://github.com/chendo/lifx-http/issues/27#issuecomment-72760322

On/Off & Dimming using the cloud API

1) Create a dummy switch of type dimmer - I used a lightwaveRF dummy device for this with the title Lifx

2) Copy the following code and save as domoticz/scripts/lua/script_device_Lifx.lua - the name in the title must match the name of the device created in step 1

Change both the lifxlabel parameter & apikey parameter to the name of the bulb you wish to control & your individual api key provided by lifx. If your bulb name includes a space, use %20 instead: for example replace "Bulb Name" with "Bulb%20Name".

commandArray = {}
 
DomDevice = 'Lifx'
lifxlabel = '<INSERT THE LABEL OF THE BULB YOU WANT TO CONTROL HERE>'
apikey = '<INSERT YOUR OWN API KEY HERE>'
 
 if devicechanged[DomDevice] then
   if(devicechanged[DomDevice]=='Off') then
   print ("Turning off " .. DomDevice);
   runcommand = "curl -H \"Authorization: Bearer " .. (apikey) .. "\" -XPUT https://api.lifx.com:443/v1beta1/lights/label:" .. (lifxlabel) .. "/power.json?state=off";
   os.execute(runcommand);
   return commandArray
   elseif(devicechanged[DomDevice]=='On') then
   print ("Turning on " .. DomDevice);
   runcommand = "curl -H \"Authorization: Bearer " .. (apikey) .. "\" -XPUT https://api.lifx.com:443/v1beta1/lights/label:" .. (lifxlabel) .. "/power.json?state=on";
   os.execute(runcommand);
   return commandArray
   else
   DomValue = (otherdevices_svalues[DomDevice]);
   end
   CalcValue = DomValue / 31.25;
   print ("Dimming "  .. (DomDevice) .. " to " .. (CalcValue) .. " ");
   runcommand = "curl -H \"Authorization: Bearer " ..
(apikey) .. "\" -XPUT https://api.lifx.com:443/v1beta1/lights/label:" .. (lifxlabel) .. "/color.json?color=brightness:" .. (CalcValue) .. " ";
   os.execute(runcommand);
 end
return commandArray

Notes:

The dimming function of the dummy switch only seems to allow 32 levels of operation so this is calculated by the script to the approximate percentage value before passing to the lifx cloud. For more than one bulb, you can repeat the steps above for each bulb, using unique names.