Hi,
I’m trying to create a template sensor to check the number of lights in a group that are on.
I’ve created the group with all the lights in it and then put this in the sensor template state field
Number of lights currently on in group: {{ states.group.all_lights.attributes.light.all_lights | select('in', states.light | map(attribute='entity_id')) | selectattr('state', 'eq', 'on') | list | count }}
but the sensor appears to be always “unavailable”. I cannot understand what I’m doing wrong.
The group ID is light.all_lights, I’ve put it in the wrong way?
I’ve also tried to do it for the all home without the group with this code
Using expand on a group will expand recursively until there are only single entities, while using the entity_id attribute will still use light groups in case you nested them.
Agreed, I included it because the OP was seeing duplicate entities. As I am sure you already know, expand listed the entities individually as well. It felt like this was a group vs an individual light issue.
Thanks a lot to you both. If I use the group it work as expected, instead if I use the “all lights” strangely I have +1 light if > 0. Basically, everything off is correctly 0, after that first light give me 2 as results, 2 lights 3, and so on.
I really don’t understand why.
Some integrations, like Hue, add an extra light entity for each room or zone.
So, for example, if you have two lights in your “Living Room” zone, named “lamp1” and “lamp2”, you might find three entities in Home Assistant: light.lamp1, light.lamp2, andlight.living_room (or something similar depending on what you called your Living Room in the app). light.living_room might report ON if either of the 2 lamp_* entities is on.
In fact, even if you had just one single light in your Living Room, if your lighting configuration associates lights with zones, you might find that an extra light entity appears in HA. Or if you assigned that single light to multiple zones, you might find additional entities in HA for each zone.
Shooting from the hip here, but hopefully it’s helpful.
It should be something like that. I’ve a BTicino system and I don’t see any more light entity that the ones that I can on/off but well, no real issue. I’m using a group with inside all the lights I want to track and controlling that.