Instructions and Steps to Implement LIFX API

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: https://cloud.lifx.com/settings[/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: http://api.developer.lifx.com/docs/list-lights[/li][/ul]
JSON Support on Vera U15 and U17

[ul][li]Download the json lib from: https://gist.github.com/tylerneylon/59f4bcf316be525b30ab[/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: http://api.developer.lifx.com/docs/ 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]

Joek,

Thank you for taking the time to document and post this. Do you foresee any issues with this process and UI7?

I’m using VeraPlus. I’ll give it a go in a few days, but after two resets of the VeraPlus and two full network rebuilds (inclusion and all), I want to make sure the core network is stable before attempting anything new.

@jsingle

The lifx_ctrl lua code is very basic/simple and would not expect any difficulties, but I did not test on U17 since I am not running that version. Please report back when you have it running on U17.

Regards,
Joe

Hi @joek,

I can report some progress on UI7, but because of my lack of knowledge, I’m not sure where I’ve screwed up.

First, I believe think there is a typo in the line to add to startup lua. Shouldn’t it be require(“lifx_ctrl.lua”) instead of ctrl_lifx.lua?

[quote=“joek, post:1, topic:191679”][font=calibri]Instructions and Steps to Implement LIFX API w/ Vera U15 - lifx_ctrl()[/font]

[ul][li][font=calibri]Install lifx_ctrl.lua (see attachment below)[/font]
[list]
[li][font=calibri]Make sure your private LIFX API token is added to lifx_ctrl.lua code, then
[/font][/li]
[li][font=calibri]Copy lifx_ctrl.lua to Vera using U15 GUI. APPS → Develop APPS → Luup files. Make sure to check the button “restart Luup after upload”[/font][/li]
[li][font=calibri]Add ‘require(“ctrl_lifx.lua”)’ to Vera startup lua: APPS → Develop APPS → Edit Startup Lua[/font][/li]
[/list] [/li][/ul][/quote]

By just doing those 3 steps we were able to get “all” or selected lights to turn off or toggle, but could not get the “on” command to work. (The friend who owns these bulbs has only “monochrome” bulbs, not color, so we were not able to check the color command.) We were not able to get “pulse” to work trying lifx_ctrl(“id:xxx”, “pulse”, NULL, .5, 5)

[ul][li][font=calibri]JSON Support on Vera U15[/font] [list] [li][font=calibri]Download the json lib from: [url=https://gist.github.com/tylerneylon/59f4bcf316be525b30ab]https://gist.github.com/tylerneylon/59f4bcf316be525b30ab[/url][/font][/li] [li][font=calibri]Copy the downloaded file "json.lua" to the Vera by using scp (secure copy) to the "/etc/cmh-ludl/" directory. Do not use the U15 GUI to copy the file because it automatically compresses the file and won't work.[/font][/li] [li][font=calibri]Example command: scp json.lua root@veralite:/etc/cmh-ludl/[/font] [/li] [/list] [/li][/ul]

Because your instructions say this is to add json support to UI5, we did NOT load the json lib in Vera and as a result did get an error at line 68 in your lua.

[string "--[[..."]:68: attempt to call field 'parse' (a nil value)

I just don’t know enough to be comfortable copying that version of json lib to Vera running UI7. Any thoughts/suggestions you or others can offer on how to proceed from here would be appreciated.

(My friend was VERY pleased to get this far!)

Thanks,

EDIT: We did get “on” working by adding a value in the color parm so lifx_ctrl(“all”, “on”, “#ffffff”, 1, NULL) works
Still have the error because it can’t find the parse command, but that is expected

@ilikelife

Glad you made some progress. Fixed the typo - thanks for pointing that out.

I only have color LIFX lights, so not able to test monochrome bulbs. Did you try “#ffffff” for the pulse color value? You could also go to the bottom of the following website and try different values for the monochrome bulb and let me what works: http://api.developer.lifx.com/docs/pulse-effect

Also, you can find your bulbs id from the bottom of this webpage: http://api.developer.lifx.com/docs/list-lights Put all in the selection box.

U17 includes a json lua lib where U15 doesn’t - which is the reason to have to copy one to the Vera. If there isn’t another json.lua file in U17 then should be okay to copy the file to the Vera. Or, if you really don’t care about the return values from lifx_ctrl.lua, then you could just delete lines 24 and 68-89 in the code. Those lines are just parsing the json results from the LIFX API and returning the values.

Thanks for the feedback!

Regards,
Joe

@joek,

Yes, pulse works with “#ffffff” and “kelvin:2500” (to 9000) works as the color value in the “on” example. So everything does work on UI7

There is already a json.lua so I’m still reluctant to put that in. For now we’ve commented out the return values section, but will play more to see what we can come up with.

Thanks again for building and documenting this!

Mike

Hey guys… I just wanted to say thanks for your efforts here… :slight_smile:

I love my (22) Lifx’s but was getting frustrated at lack of straightforward integration…

Please let me know if y’all need additional testers: I have Lifx Original, Lifx White 800, & Lifx Color 1000 bulbs.

Currently running Vera Lite on UI5 with a Vera Plus enroute.

Is it possible to pass the ID of a LIFX Group, instead of an individual bulb? I’ve been trying, but no luck so far. Thanks so much for this - I’ve been really disappointed that I couldn’t properly integrate my LIFX bulbs with my Vera scenes.

Now, if I could only get them to consistently stay on the WiFi network! One problem at a time, though.

EDIT: Figured it out - I had to pass the name, and not the ID.

For anyone else, here’s a sample group off command to add to the examples above:

lifx_ctrl(“group:Great Room”, “off”) – turn off selected group

Thanks joek for sharing this excellent way of controlling our LIFX lights from Vera.

The script is working fine for me - the only odd thing I notice is when I try testing a command in “APPS → Develop APPS → Test Luup code (Lua)” the command successfully does what its supposed to (turn a light on, off, etc), but I get a “Code failed” popup box every time. It looks like a Vera dialog box, with green OK button to close. Is this normal ?

[quote=“Systmworks, post:9, topic:191679”]Thanks joek for sharing this excellent way of controlling our LIFX lights from Vera.

The script is working fine for me - the only odd thing I notice is when I try testing a command in “APPS → Develop APPS → Test Luup code (Lua)” the command successfully does what its supposed to (turn a light on, off, etc), but I get a “Code failed” popup box every time. It looks like a Vera dialog box, with green OK button to close. Is this normal ?[/quote]

I am guessing perhaps it may have to do with parsing the json response in the code. May want to verify the json.lua file is installed on your Vera. Are you using U15 or U17? For U17 I recommend changing the json.lua filename to something like myjson.lua because U17 already has a file named json.lua - U15 does not. I will update code and instructions soon.

I find ALTUI to be much more helpful when testing lua code because you can see error messages, return values and print output.

Joe

Has anyone actually done this with UI7? I will be mainly controlling the LIFX via my Harmony remote but I would like on off capability from Vera as well.

Thanks for your reply joek.

I am using the VeraEdge (UI7). Your script works great as-is, its only the test code part (works) but cause an error message, so its no biggie.

@JasonJoel - yes this script is working for me on UI7.

Yes script works fine with U17 - I’ve since upgraded to VeraPlus (U17). I’d recommend changing the json.lua filename to something like myjson.lua because U17 already has a file named json.lua - U15 does not. Obviously, the lifx_ctrl.lua code would also have to reflect the file name change. :slight_smile:

I will update code and instructions soon.

Just an FYI if you havent “claimed” your bulbs in the mobile app then the api won’t work as the bulbs won’t be connected to the cloud. I overlooked this as they were working from the app and didnt notice the option to claim them when you select the individual bulb.

Hello,

Since updating to the latest vera firmware version 1.7.830, your wonderful lifx service has stopped working for me. It was working very well prior to the upgrade. Now, whenever I attempt to use it in a scene, I get this error in the logs:

50 07/16/16 10:02:59.464 luup_log:0: lifx: {"results":[{"id":"d073d503a71f","label":"Main Bed Light","status":"offline"}]} <0x2fc45680> 08 07/16/16 10:03:02.158 JobHandler_LuaUPnP::HandleActionRequest device: 0 service: urn:micasaverde-com:serviceId:HomeAutomationGateway1 action: RunScene <0x2fc45680> 08 07/16/16 10:03:02.159 JobHandler_LuaUPnP::HandleActionRequest argument serviceId=urn:micasaverde-com:serviceId:HomeAutomationGateway1 <0x2fc45680> 08 07/16/16 10:03:02.159 JobHandler_LuaUPnP::HandleActionRequest argument action=RunScene <0x2fc45680> 08 07/16/16 10:03:02.160 JobHandler_LuaUPnP::HandleActionRequest argument SceneNum=81 <0x2fc45680> 08 07/16/16 10:03:02.161 Scene::RunScene running 81 Toggle LIFX Lights <0x2fc45680> 50 07/16/16 10:03:02.612 luup_log:0: lifx: {"results":[{"id":"d073d503a71f","label":"Main Bed Light","status":"offline"}]} <0x2fc45680>

I’ve double checked the entry in startup lua, and done a few lua restarts and vera reboots. Note that the bulb is still completely controllable with the Lifx app. Am I doing anything wrong?

Thanks, Mark.

Hmm, it’s now started working again… Is it possible that the cloud service wasn’t woking, and that is why my Lifx app would work on my local network?

Based on your log entry the lifx bulb may have been offline:

50 07/16/16 10:02:59.464 luup_log:0: lifx: {“results”:[{“id”:“d073d503a71f”,“label”:“Main Bed Light”,“status”:“offline”}]} <0x2fc45680>

Glad is working again.

Joe

yeah it seems like the cloud service was offline. I was able to use the Lifx app locally.

Anyway, thanks so much for the plugin!

This sounds like a great feature, but I’m a little hesitant to set up anything that relies fully on the cloud.
What are the main reasons why this plugin cannot be modified to work over the LAN when required?
I could see that as being very frustrating if you have temporary ISP issues and cannot turn on your lights to find your way to the bathroom at night (for example).

Thanks for putting this together, as well as for helping me understand this limitation.

[quote=“Quixote, post:19, topic:191679”]This sounds like a great feature, but I’m a little hesitant to set up anything that relies fully on the cloud.
What are the main reasons why this plugin cannot be modified to work over the LAN when required?
I could see that as being very frustrating if you have temporary ISP issues and cannot turn on your lights to find your way to the bathroom at night (for example).

Thanks for putting this together, as well as for helping me understand this limitation.[/quote]

Local LAN access to LIFX is possible via their LAN Protocol: https://lan.developer.lifx.com/docs However, I if recall correctly it requires a local server. There are folks that created python libs for LIFX that use local LAN, but still requires a local server to operate.

Using the LIFX HTTP API was easy to implement, but you are correct that it requires internet access to their servers per their API.

Joe

© 2019 Vera Control Ltd., All Rights Reserved. Terms of Use | Privacy Policy