LIFX Developer Zone

Listening for light state changes?

Is there a way to listen for when the state changes on a light?

I’m wondering if there’s a WebHook or some LAN protocol method of listening to a light for when that light’s state changes.

I thought about this for a while, and I believe; when the light’s state changes, it actually sends a web request to LIFX telling it the latest state. That’s how the LIFX HTTP API quickly knows the state of each light.

I would like this functionality myself because there’s no official way to turn on and off a scene. Because of this, I’m pinging the lights over the LAN protocol, grabbing their state, and then telling them to change their state.

This process isn’t immediate. It’d be better to have my own data store of light states and calculate scene states based on that data. Pretty sure that’s how LIFX does it, so I’d like the same solution myself.

Is this something people have done before? Is there an official solution?


There isn’t a way to subscribe to changes on the devices at the moment. The devices do tell the cloud they’ve been changed (in certain conditions), but for most devices/changes we get the previous state rather than the new state. So, for most devices, the information in the API /v1/lights output is the cloud asking the device for its state and then caching the answers.

Over the LAN protocol, there’s no subscription capability, so to know when they’ve changed state, you need to poll them on a regular basis.

You can use this to activate a scene and you can use a scene selector with the State endpoint to power off the lights in a scene. Though currently the only way to make the scene in the first place is via the app.

Thanks for your response.

So you’re saying I can set state to turn off a scene, but that only works through the HTTP API. If I’m using LAN Protocol (for lower latency), I have to first check the state of every light, ensure they’re either equal to or not equal to the scene, then do the opposite.

That’s why I wanted to know from a LAN perspective how to listen for state changes. I deal almost exclusively with scenes so that’s a big issue for me. If I could setup a webhook for the HTTP API, then you don’t need to expose anything, I just need to put an endpoint for you guys to POST.

Depends what you mean by “turn off” a scene. A scene is by definition one way so turning it off depends entirely on the context of the scene.