How to create a condition for "are all lights in an area on/off"

Hello all!

I’m trying to figure out how to write a condition for “are all lights in an area on/off” without specifying the entities individually.


I have Zigbee lights set up through ZHA, but they are cheap ones that respond correctly to commands only 90% of the time (and often not at all to commands sent within seconds of eachother).

Right now what I do to turn on the lights is:

  1. Check for motion in a room
  2. If motion detected, run a script that selects the right light color based on time-of-day
  3. This script calls lights.on on the room variable that triggered the motion
  4. crucially I rerun the lights.on script after 5 seconds to make sure the lights actually responded

The approach of rerunning the script naively is both unelegant and annoying, and causes issues because:

  1. Someone might have changed the color of the lights (through voice assistant) in the wait window of the script that changes the color of the lights
  2. The bulbs sometimes respons slowly or not at all to Zigbee commands sent within seconds of eachother, so it creates an unpredictable queue of commands when I just rerun the script twice

Ideally I would like to:

  1. Run the script as above
  2. Check if any of the lights in the room are off (when they should be on) or on (when they should be off) and only rerun the command if that is the case

Is there any way to do this?

Hi, are you sure that the bulbs not responding 10% is because of being cheap?
Do you have other zigbee devices and do those work perfectly?

You might have a look at this: Guide for Zigbee interference avoidance and network range/coverage optimization

Prompted by your question, I stood next to a bulb and rapidly changed it’s colors in HA. And it seems to work fine, even with rapid changes :exploding_head:

I have other zigbee devices that work perfectly yes. Mostly switches and a few buttons. I’ve yet to experience issues with them.

I’ll read that post and see if I can fix it up.

That said, I’m still curious about the initial question for other reasons (ie if lights are already on, do not override them). Do you have an idea about the original question?

Not out of the top of my head but I think I have seen such a thing some time ago but can’t find it right now.
On the other hand, I think of 2 possible ideas:

  • in an automation you can specify a room for controlling the lights
  • you can create a group of the lights per room and check the status of that group.
    If 1 light of a group is on, the status for that group is on

You would need to set the “all entities” option for the group to “on” - that way the group would only be on if all the lights were on. (Which is what you want, I think? Or do you need to know which light has failed?)

Also have a look at this: How to get the state of lights (on/off) as a condition with the area_id?

This was a dirty solution to check if any light from a group is on and in that case, the “all entities” option has to be off.