Author Topic: Instructions and Steps to Implement LIFX API  (Read 11437 times)

Offline joek

  • Sr. Newbie
  • *
  • Posts: 48
  • Karma: +7/-0
Instructions and Steps to Implement LIFX API
« on: March 20, 2016, 09:43:38 am »
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
  • 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
JSON Support on Vera U15 and U17
  • Rename downloaded file "json.lua" to "myjson.lua"
  • 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.
  • Example command:  scp myjson.lua  root@veralite:/etc/cmh-ludl/
Install lifx_ctrl.lua (code is in attachment below)
  • Make sure your private LIFX API token is added to lifx_ctrl.lua code, then
  • Copy lifx_ctrl.lua to Vera using GUI.  APPS → Develop APPS → Luup files.
  • Add 'require("lifx_ctrl.lua")'  to Vera startup lua:   APPS → Develop APPS → Edit Startup Lua


USAGE

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

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.
« Last Edit: January 05, 2017, 06:09:51 pm by joek »

Offline jsingle

  • Sr. Newbie
  • *
  • Posts: 27
  • Karma: +0/-0
Re: Instructions and Steps to Implement LIFX API w/ Vera U15
« Reply #1 on: March 21, 2016, 12:28:42 am »
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.

Offline joek

  • Sr. Newbie
  • *
  • Posts: 48
  • Karma: +7/-0
Re: Instructions and Steps to Implement LIFX API w/ Vera U15
« Reply #2 on: March 21, 2016, 11:33:09 am »
@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

Offline ilikelife

  • Full Member
  • ***
  • Posts: 129
  • Karma: +2/-0
Re: Instructions and Steps to Implement LIFX API w/ Vera U15
« Reply #3 on: March 22, 2016, 03:19:36 pm »
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?

Instructions and Steps to Implement LIFX API w/ Vera U15 - lifx_ctrl()
   
  • Install lifx_ctrl.lua (see attachment below)
    • Make sure your private LIFX API token is added to lifx_ctrl.lua code, then
    • Copy lifx_ctrl.lua to Vera using U15 GUI.  APPS → Develop APPS → Luup files.  Make sure to check the button "restart Luup after upload"
    • Add 'require("ctrl_lifx.lua")'  to Vera startup lua:   APPS → Develop APPS → Edit Startup Lua

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)

Quote
  • JSON Support on Vera U15
    • Download the json lib from: https://gist.github.com/tylerneylon/59f4bcf316be525b30ab
    • 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.
    • Example command:  scp json.lua  root@veralite:/etc/cmh-ludl/

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.
Quote
[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
« Last Edit: March 22, 2016, 07:02:31 pm by ilikelife »

Offline joek

  • Sr. Newbie
  • *
  • Posts: 48
  • Karma: +7/-0
Re: Instructions and Steps to Implement LIFX API w/ Vera U15
« Reply #4 on: March 22, 2016, 09:17:55 pm »
@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
« Last Edit: March 22, 2016, 09:39:12 pm by joek »

Offline ilikelife

  • Full Member
  • ***
  • Posts: 129
  • Karma: +2/-0
Re: Instructions and Steps to Implement LIFX API w/ Vera U15
« Reply #5 on: March 22, 2016, 11:49:40 pm »
@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

Offline majorcuddles

  • Newbie
  • *
  • Posts: 12
  • Karma: +1/-0
Re: Instructions and Steps to Implement LIFX API
« Reply #6 on: March 25, 2016, 11:15:19 am »
Hey guys... I just wanted to say thanks for your efforts here... :)

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.

Offline mpalensh

  • Newbie
  • *
  • Posts: 14
  • Karma: +0/-0
Re: Instructions and Steps to Implement LIFX API
« Reply #7 on: May 14, 2016, 03:26:14 pm »
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

« Last Edit: May 14, 2016, 03:47:36 pm by mpalensh »

Offline Systmworks

  • Newbie
  • *
  • Posts: 2
  • Karma: +0/-0
Re: Instructions and Steps to Implement LIFX API
« Reply #8 on: June 01, 2016, 05:21:07 am »
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 ?

Offline joek

  • Sr. Newbie
  • *
  • Posts: 48
  • Karma: +7/-0
Re: Instructions and Steps to Implement LIFX API
« Reply #9 on: June 07, 2016, 07:47:43 am »
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 ?

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

Offline JasonJoel

  • Full Member
  • ***
  • Posts: 123
  • Karma: +22/-109
Re: Instructions and Steps to Implement LIFX API
« Reply #10 on: June 15, 2016, 11:26:25 am »
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.

Offline Systmworks

  • Newbie
  • *
  • Posts: 2
  • Karma: +0/-0
Re: Instructions and Steps to Implement LIFX API
« Reply #11 on: June 19, 2016, 02:10:09 am »
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.

Offline joek

  • Sr. Newbie
  • *
  • Posts: 48
  • Karma: +7/-0
Re: Instructions and Steps to Implement LIFX API
« Reply #12 on: June 21, 2016, 09:58:11 am »
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.

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.  :-)

I will update code and instructions soon. 

Offline mcblah

  • Newbie
  • *
  • Posts: 4
  • Karma: +0/-0
Re: Instructions and Steps to Implement LIFX API
« Reply #13 on: July 15, 2016, 01:16:01 pm »
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.

Offline stibbzy

  • Sr. Newbie
  • *
  • Posts: 29
  • Karma: +1/-0
Re: Instructions and Steps to Implement LIFX API
« Reply #14 on: July 15, 2016, 08:09:24 pm »
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:
Quote
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.