Instructions and Steps to Implement LIFX API - lifx_ctrl()
The LUA code lifx_ctrl() provides ability to control LIFX lights via Vera U15 and U17. The implementation uses the LIFX API (RESTful) and therefore an internet connection is required for network access to LIFX servers. The LIFX control provides a basic framework so can easily be modified to enhance its functionality. It’s implemented as a LUA function and the function can be called by other LUA code or from LUUP within a scene on the Vera.
INSTALLATION
LIFX API Access Token
[ul][li]Create/generate an LIFX API access token from here: [url=https://cloud.lifx.com/settings]https://cloud.lifx.com/settings[/url][/li][/ul]
[ul][li]add your LIFX API personal/private access token to the LUA file lifx_ctrl.lua by replacing the existing one in the code.[/li][/ul]
[ul][li]optional - using your LIFX API access token test your ability to control your lights with the LIFX API using the following link below. At the bottom of the webpage there’s a place to try the API: [url=http://api.developer.lifx.com/docs/list-lights]http://api.developer.lifx.com/docs/list-lights[/url][/li][/ul]
JSON Support on Vera U15 and U17
[ul][li]Download the json lib from: [url=https://gist.github.com/tylerneylon/59f4bcf316be525b30ab]https://gist.github.com/tylerneylon/59f4bcf316be525b30ab[/url][/li][/ul]
[ul][li]Rename downloaded file “json.lua” to “myjson.lua”[/li][/ul]
[ul][li]Copy the file “myjson.lua” to the Vera by using scp (secure copy) to the “/etc/cmh-ludl/” directory. Do not use the GUI in U15/U17 to copy the file because it automatically compresses the file and won’t work.[/li][/ul]
[ul][li]Example command: scp myjson.lua root@veralite:/etc/cmh-ludl/ [/li][/ul]
Install lifx_ctrl.lua (code is in attachment below)
[ul][li]Make sure your private LIFX API token is added to lifx_ctrl.lua code, then[/li][/ul]
[ul][li]Copy lifx_ctrl.lua to Vera using GUI. APPS → Develop APPS → Luup files.[/li][/ul]
[ul][li]Add ‘require(“lifx_ctrl.lua”)’ to Vera startup lua: APPS → Develop APPS → Edit Startup Lua[/li][/ul]
[b]
USAGE[/b]
Use the following link to get all the LIFX API parameters: [url=http://api.developer.lifx.com/docs/]http://api.developer.lifx.com/docs/[/url] The lifx_ctrl function can be used inside a Vera scene luup (or from another luup code) as follows:
stat, pwr, conn, err, resp = lifx_ctrl (selector, mode, color, bright, cycles, period)
where:
selector - choose “all”, group or a specific LIFX light
mode - on, off, toggle, pulse, breathe or list
color - red, green, blue, pink, etc. or #RRGGBB format
bright - brightness setting (0.0 - 1.0) for “on” and “pulse” modes
cycles - no. of cycles to pulse the LIFX light(s) when in “pulse” mode
period - time in seconds for one cycles of the effect
return values:
stat - status of LIFX API command
pwr - power state of LIFX light
conn - LIFX light is connected to WiFi
err - error message if one exists
resp - string containing entire response from LIFX API command
- Note: lifx_ctrl() LUA code puts an entry into LuaUPnP.log Vera log file after it executes.
Some example usage:
lifx_ctrl(“id:f055d5005dde”, “pulse”, “blue”,.5, 5, 2) – pulse bulb blue 5 times at 1/2 brightness & cycle period of 2 secs
lifx_ctrl(“all”, “on”, “#00ff00”, 1) – turn on all lights green at full brightness
lifx_ctrl(“all”, “off”) – turn off all lights
lifx_ctrl(“all”, “toggle”) – toggle all lights
lifx_ctrl(“id:f055d5005dde”, “off”) – turn off selected light
lifx_ctrl(“label:bulb01”, “breathe”, “yellow”,1, 3,.5) – breathe blub yellow 3 times at full brightness & cycle period of .5 secs
lifx_ctrl (“label:bulb01” , “breathe” , “white” , 1 , 2 , .04) – a lightning effect
Example luup code used inside a Vera Scene:
function delaylifx()
lifx_ctrl(“label:bulb01”, “toggle”) – toggle lifx
end
luup.call_delay(‘delaylifx’, 1) – Call function after a delay of 1 seconds
Enjoy!!!
UPDATES
10/01/16 -
- changed instructions to support U15 and U17 releases
- rename json.lua to myjson.lua for U17 support
- added “period” parameter to lifx_ctrl()
- added “breathe” mode to lifx_ctrl
- updated instructions and examples
03/25/16 - added brightness parameter for pulse mode - thx to @ilikelife
[size=8pt]LiFi Labs if you are listening, please change the behavior of the LIFX bulbs after power interruption. Having an option to return to previous state as opposed to white at full brightness would be useful. It would also give LIFX an edge over your major competitors since they all do the same by turning on white at full brightness. It prevents me and others from using the bulbs throughout the house because power interruptions are common enough.[/size]