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?

Hello,

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.