Sensor to counts list on (detect if light is in group)

need some assistance.

i added a new sensor to configuration.yaml that counts the number of lights that are one.

however i don’t think i’ve got it quite right.

sensor:
- platform: template
  sensors:
    count_lights_on:
      friendly_name: "Lights On"
      unit_of_measurement: ''
      value_template: "{{ states.light | selectattr('state', 'eq', 'on') | list | count }}"

i have certain rooms where there are 4 bulbs but they are grouped into 1 “light” for the rooom, however the sensor is counting that as 5 lights “on” when i really only want it to count it as 1.

how do i adjust this so that it only counts any bulbs in a particular group as 1?

For a start you should be using the new template integration instead of the legacy template sensor platform for new template sensors. The legacy template sensor is still supported but it is not getting new features.

Secondly light groups have an attribute called entity_id that you can use to refine your selection. This will only count light groups that are on:

# configuration.yaml
template:
  - sensor:
      - name: "Count Light Groups On"
        unit_of_measurement: ''
        state: >
          {{ states.light 
            | selectattr('attributes.entity_id', 'defined') 
            | selectattr('state', 'eq', 'on')
            | list 
            | count 
          }}

And this will only count lights that are on ignoring light groups:

# configuration.yaml
template:
  - sensor:
      - name: "Count Individual Lights On"
        unit_of_measurement: ''
        state: >
          {{ states.light 
            | rejectattr('attributes.entity_id', 'defined') 
            | selectattr('state', 'eq', 'on')
            | list 
            | count 
          }}

However if you want a mix of groups and lights you are going to have to list them individually. The logic required to reject lights that are on in light groups that are on would be more complicated than doing it that way.

noted. i will look into how to use that

i have 2 rooms where there are 4 GUI10 bulbs grouped together to make 1 “light” for the room. the others rooms have a single bulb.

for the grouped bulbs there is never an instance where only “some” of the bulbs are on, it’s an “all or nothing” situation.

the outcome i need is that i have an icon on the dashboard that shows me how many “lights” are on, so it telling me that 5 lights are on (when in actual fact there is only 1 “light” on (4 grouped lights) is wrong for the display output.

Try this:

template:
  - sensor:
      - name: "Count Lights On"
        unit_of_measurement: ''
        state: >
          {% set group_count = states.light 
            | selectattr('attributes.entity_id', 'defined') 
            | selectattr('state', 'eq', 'on')
            | list 
            | count 
          %}
          {% set individual_count = states.light 
            | rejectattr('attributes.entity_id', 'defined') 
            | selectattr('state', 'eq', 'on')
            | list 
            | count 
          %}
          {{ group_count + individual_count }}

EDIT: also if you put this in the Developer Tools → Template editor you can see which lights it is counting:

{% set group_count = states.light 
  | selectattr('attributes.entity_id', 'defined') 
  | selectattr('state', 'eq', 'on')
  | map(attribute='entity_id')
  | list 
%}
{% set individual_count = states.light 
  | rejectattr('attributes.entity_id', 'defined') 
  | selectattr('state', 'eq', 'on')
  | map(attribute='entity_id')
  | list 
%}
## Group lights ## {{ group_count }}

## Individual lights ## {{ individual_count }}

hi

the first code you supplied still reports back 5, when all that is currently on is 1 group if lights (the group contains 4 bulbs) so that appears to be doing the same as my orginal code and reporting 4+1.

the code posted in the template editor produces this

## Group lights ## []

## Individual lights ## ['light.ikea_gui10_2_office', 'light.ikea_gui10_3_office', 'light.ikea_gui10_1_office', 'light.ikea_gui10_4_office', 'light.office_lights']

light.office_lights is the group i created that groups the 4 gui10 bulbs.
i grouped them in Zigbee2MQTT

How did you create the light group?

i grouped them in Zigbee2MQTT

In that case, the resulting group entity doesn’t indicate which lights are in its group.

  • Go to Developer Tools > States and find light.office_lights
  • Look at its attributes
  • There’s no information that it controls 4 other lights

Home Assistant doesn’t know it’s a Zigbee2MQTT group containing 4 lights. It sees it as just another light.

template will need to explicitly exclude light.office_lights. EDIT Exclude the 4 lights

wouldn’t i need to exclude the individual bulbs instead?

i want it to report back saying 1 and not 5 (which is the group plus the 4 bulbs).

My template will only work if you use home assistant light groups.

You’re absolutely correct; I said the exact opposite of what needs to be done. :man_facepalming:

That’ll teach me to reply before breakfast …

this seems to work as required - it brings back 1 when previously it was 5

{{ states.light | selectattr('state', 'eq', 'on') | rejectattr('entity_id','in',('light.ikea_gui10_1_office','light.ikea_gui10_2_office','light.ikea_gui10_3_office','light.ikea_gui10_4_office')) | list | count }}

Given that your 4 lights have a consistent naming scheme, you can reject them from the list using a regex pattern.

{{ states.light
  | selectattr('state', 'eq', 'on')
  | map(attribute='object_id')
  | reject('match', 'ikea_gui10_[1-4]_office')
  | list | count }}
1 Like