Count all the lights that are on, but count groups of lights as one

I’ve made a template that reports how many lights are on. But I’ve got some lights that form a group (a ceiling light that has three bulbs), which I want to count as one.
Usually these lights are named 1 and 2. So that would perhaps be a starting point, although both of the bathrooms have the same naming: bulb 1 and bulb 2 for both rooms.
These lights all form a group, which would also be a way. But I am not really sure how to do this.

This is my code so far for all lights.

- sensor:
    - name: "Aantal Lampen Aan Totaal"
      unique_id: 72a18d9e-5996-43f7-8db4-9416568a64f7
      icon: mdi:lightbulb-group-outline
      state: >
        {{ states.light
          | rejectattr('entity_id', 'in', label_entities('Verlichting'))
          | rejectattr('entity_id', 'in', label_devices('HueRoomZone'))
          | rejectattr('entity_id', 'in', label_devices('DummyLight'))
          | selectattr('state', 'eq', 'on')
          | list | count }}
      attributes:
        light_names: >
          {{ states.light
            | rejectattr('entity_id', 'in', label_entities('Verlichting'))
            | rejectattr('entity_id', 'in', label_devices('HueRoomZone'))
            | rejectattr('entity_id', 'in', label_devices('DummyLight'))
            | selectattr('state', 'eq', 'on')
            | map(attribute='name')
            | list
            | sort }}

Any idea how to proceed?

The easy answer is to remove the labels from individual lights that are also in groups with the same label.

The more difficult answer would be to do it all manually. I’ve never had to look at the inside of groups, but the documentation says the expand() function/filter can be used to get the individual entities. Make a loop, use expand on each entity and see what comes out, if it is not the same entity as you put in, remove the resulting entities from the list.

I read about the expand() feature but that looked a bit difficult :smiley: So I save that for later and I am going for the labels.
But… I encountered a weird thing!

My code rejects entities with label HueRoomZone and DummyLight, but some are still visible! :frowning:

Look at Beneden (Zone) for example. It does have the label!

How can that be?!

Your code says to reject entity ids which are in a list of device ids, that won’t accomplish anything. You need to either use label_entities, like you already do in the first filter, or translate the list of device ids into entity ids.

label_devices('HueRoomZone') | map('device_entities') | sum(start=[])

Light groups have the attribute entity_id which holds a list of the entities contained in the group. So it can be used as selection criteria as follows:

{% set light_s = states.light %}
{% set in_a_group = light_s
| selectattr('attributes.entity_id', 'defined')
| map(attribute='attributes.entity_id')
| sum(start=[]) | list %}

{{ light_s
| map(attribute='entity_id')
| reject('in', in_a_group)
| select('is_state', 'on')
| list | count }}
2 Likes

Oh my… I didn’t even see that. I copied it from someone.

There fails my knowledge and overview of the possibilities.

          | rejectattr('entity_id', 'in', label_entities('Verlichting'))
          | rejectattr('entity_id', 'in', label_devices('HueRoomZone'))

So the first rejects entities id’s where the label is ‘Verlichting’? So if I change the second line to label_entities it should work?
I’m trying to learn what I’m doing here :slight_smile:

That would depend on what you have applied your labels to – on the entity level to individual entities, or on the device level to only the encapsulating devices.

HASS will only do exactly what you tell it to do. It is not an LLM/ChatGPT that will hallucinate and attempt to fill in any blanks by itself :stuck_out_tongue:

I noticed that, and that’s fine. I am not really fluent in that language yet. But learning every day.

Care to help if I applied the label to the entity?

Applying labels to entities is usually the safer way to go. For example, lots of devices are identified with the main entity type switch, but may also contain secondary switches for toggling device settings. So if you applied the action switch.turn_off to the entire device, it may not just turn off the switch/outlet/whatever type of device it was but also unexpectedly change some setting.

Correction, I applied it to the device I see.
Can also apply them to the entity (or both). I’ve got 290 devices with 2444 entity’s and I am loosing track as you can imagine.

So, let’s say I’ve applied it the the light-entities. How would my code then be?!