Instructions and Steps to Implement LIFX API - Part II
This topic is a continuation from the original topic “Instructions and Steps to Implement LIFX API” found here: http://forum.micasaverde.com/index.php?topic=36961.0
The LUA code lifx_ctrl() provides ability to control LIFX lights via Vera 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 - feel free. 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.
The code is pretty basic and does not do any error checking or handling. When time permits I’ll add that functionality.
LIFX API Access Token
- Create/generate an LIFX API access token from here: https://cloud.lifx.com/settings
- Add your LIFX API personal/private access token to the LUA file lifx_ctrl.lua by replacing the existing one in the code.
- 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: http://api.developer.lifx.com/docs/list-lights
Install lifx_ctrl.lua (download link below)
- Make sure your private LIFX API token is added to lifx_ctrl.lua code, then
- Upload lifx_ctrl.lua to Vera using the GUI. APPS → Develop APPS → Luup files.
require("lifx_ctrl.lua")to Vera startup lua: APPS → Develop APPS → Edit Startup Lua
The lifx_ctrl function can be used inside a Vera scene luup (or from another luup code) as follows:
lifx_ctrl (selector, mode, color, bright, cycles, period)
- 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
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:
lifx_ctrl("label:bulb01", "toggle") – toggle lifx
luup.call_delay('delaylifx', 1) – Call function after a delay of 1 seconds
You’ll find the lifx_ctrl lua code here: https://github.com/joekmd/lifx_ctrl/archive/master.zip