LIFX Developer Zone

Using Scenes in the API


#15

For something to do I took Nate’s add-on and forked it, I have added support for a TV and Movie scene using the new scenes in the HTTP API

Now if I could temporarily store the current settings as a scene that can be called later and automatically deleted it would be brilliant. I am not having issues with the add-on like I was before and it works fast enough really.


#16

Daniel how about the ability to set a fade in time for the scene ? It would be nice to fade to the scene rather than just switch over.


#17

Well, turns out that was already implemented and I just missed it when documenting it. To use it simply send a duration parameter, that same way you do for the power controls.

In the meantime I’ll go and correct the documentation… oops :confused:


#18

I’ve now documented this. You can now use the examples on the Activate Scene page.


#19

Brilliant thanks for that. Seems to be working well, mind you I am working from the office testing with lights at home.


#20

@rasputin303 @kubalicious @boxhead I’ve added an API endpoint that lets you set multiple states on multiple selectors in the one request. Check out HTTP API Changes - v1beta1 - 2015-08-28 for more information. There is no documentation just yet as we’ll gather feedback and make changes if necessary before documenting the new endpoints. Please put any feedback in that thread rather than this one!


#21

@tatey @chendo @daniel_hall I’m getting 404 Not Found with the v1beta1 endpoints. Is this happening to any of you too? I’m trying to make GET requests - http://api.developer.lifx.com/docs/list-scenes


#22

never mind, ignore the below, I didn’t get that you were testing using the documentation pages which has not been working for the last few days but is fixed now.

No problem for me, I am getting a list of scenes in my Kodi script with no issues. I have also tested the new state and states endpoints with no issues.
I am using

lifxCloud = 'https://api.lifx.com/v1beta1/'
apiKey = 'Bearer ’ + apiKey
header = {‘content-type’: ‘application/json’, ‘authorization’: apiKey}
getAvailableScenes = requests.get(lifxCloud + ‘scenes’, headers=header)


#23

Hi,

I am new here and I know most of you have experience with coding but I don’t and I would like to get started. I would like to have my two lifx lights turn off when a movie starts under kodi. The script that I found isn’t doing much (probably because I don’t know what I’m doing). Can someone point me in the right direction? Would it be best to use the LAN of HTTP option? Thanks


#24

Kodi uses Python scripts and this is good because you can easily add them to the system and this is all documented on the Kodi website.

If you just want to turn the power on and off on the bulbs then use the local protocol and Daniel has done all the hard work for you.
https://pythonhosted.org/lifx-sdk/index.html

and this example is key.

An example to turn the lights off (off the top of my head)

import time
import lifx

# Start the client
lights = lifx.Client()

# Give some time for discovery - waits one second
time.sleep(1)

# turn off the lights - state = Boolean True or False
for i in lights.get_devices():
  i.power = False

If you want to see my dodgy attempts at a Kodi script then see https://github.com/ceboxsell/NateKodilifx

Edit by @daniel_hall to fix @boxhead’s code example.


#25

If you don’t have much experience coding then the HTTP API is probably the best way to go. What programming languages do you have experiences with? I’m sure someone here can whip up a quick example in your preferred language.


#26

4 posts were split to a new topic: Kodi Integration with LIFX


#27

@rasputin303 any chance you can share the way you saved the state using the HTTP API and then reset it again? Looking for the first bit to be used in openHAB


#28

Here is a quick example I whipped up. It probably should be modified to use the set states endpoint to make the changes in parallel.

#!/usr/bin/env python

import requests
import time

token = '[YOUR TOKEN GOES HERE]'

headers = {
    "Authorization": "Bearer %s" % token,
}

def restore_state(state):
    selector = 'id:%s' % state['id']

    # Color is sent as one string, so we have to convert it
    color_items = state['color'].items()
    color_strings = map(lambda x:'%s:%s' % x, color_items)

    data = {
        'power': state['power'],
        'brightness': state['brightness'],
        'color': ' '.join(color_strings),
    }

    response = requests.put('https://api.lifx.com/v1/lights/%s/state' % selector, data=data, headers=headers)

def fetch_states(selector='all'):
    response = requests.get('https://api.lifx.com/v1/lights/' + selector, headers=headers)
    
    return response.json()

print 'Storing the current state of the bulbs'
orig_states = fetch_states()

print 'Waiting 10 seconds, change the color of the bulbs now'
time.sleep(10)

print 'Restoring lights to the state they were 10 seconds ago'
for state in orig_states:
    restore_state(state)

#29

Great stuff thanks

Any plans to expose other parts of the app in the API such as timers and effects?

Thanks
Andrew


#30

At the moment the only effect we support in the API are the Breathe and Pulse effects. We currently don’t have any plans to expose schedules or any other effects in the HTTP API.


#31

Ok is there a user voice section for development? I’d love to make some suggestions for your roadmap (such as bulbs to multiple groups)

Thanks
Andrew


#32

Feel free to post feature requests in these forums right here. I gather the items discussed on these forums and present them at our product planning meetings to help us decide what new features we add, and in what order.


#33

I have just got to trying this script - it is doing something - but I think that it isnt restoring the bulb state in the correct order - eg one bulb is set to pink before scriipt - 10 seconds later it is “restored” to a different color.

Any Ideas?

You also mentioned using states - mind changing the example to do this?

thanks
AJ


#34

in doing some more testing - I have isolated the selection to 1 lamp and am getting the same result, so its not the states collection.

If I set the original “color” to one of the whites, the script works as expected.

If I set the original color to say a green, the color is “restored” to 1% and 100% brightness yet the color_string is output as (for example)
hue:79.4103913939 saturation:0.74770733196 kelvin:3500

not sure why?