Toggle Power endpoint when existing state is mixed

The Toggle Power endpoint says:

Turn off lights if they are on, or turn them on if they are off.

That’s a bit ambiguous, but I was assuming it meant negating the power state on a light-by-light basis.

However, I’ve observed that if I have one light on and one light off, and I send a Toggle Power command to the two lights (combined as a comma-separated selector), the result is that both lights are off.

So, would it be more accurate to say:

Turn off lights if any of them are on, or turn them on if they are all off.

Or is something else going on?


Here is the code that handles the toggling:

if lights.any?(&:on?)
  lights.set_power(:off, wait: false, duration: duration)
  lights.set_power(:on, wait: false, duration: duration)

So I’ve gone ahead and updated the documentation to match your wording. :smiley:

Thanks for the feedback!

1 Like

So if you were to want to toggle all your lights to the opposite of their current power setting, your best bet is to loop through them all and send them individual toggle commands?

Yup, this is correct. Obviously keep in mind the rate limits.

A more efficient way, but a bit more work, would be to do a single List Lights, followed by a single Set States. You’d have to implement the logic for flipping the states yourself, but then you could do it in just 2 API calls for any number of lights (*), rather than 1 API call per light.

Since I believe the rate limit is based on the number of API calls, not the number of lights, this would avoid the rate limit problem.

(*) Technically, I think the limit would be around 1225 lights, because Set States allows up to 50 states, and each state could address up to 25 lights. But I’m figuring 25 * 49 instead of 25 * 50, to handle an arbitrary split between the number of lights that need to be “on” and the number that need to be “off”.

1 Like