Count Lights that ar not a group

Hello,

I am trying to count the individual lights that are on in my house without including deconz_groups or other groups.

I figured out how to count all lights. But groups are still included. Anyone figured this out already?

- platform: template
  sensors:
    count_lights_on:
      friendly_name: 'Totaal aantal lampen aan'
      value_template: "{{ states.light | selectattr( 'state', 'eq', 'on') | list | count }}"

The deconz groups can be identified by “is_deconz_group: true”. Not sure how to detect light goup templates.

- platform: template
  sensors:
    count_lights_on:
      friendly_name: 'Totaal aantal lampen aan'
      value_template: "{{ states.light | rejectattr('attributes.is_deconz_group', 'eq', true) | selectattr('state', 'eq', 'on') | list | count }}"
2 Likes

This does filter out the deconz groups! Thank you for that!

Any idea on how to also filter out template light groups?

light:
  - platform: group
    name: Rail
    entities:
      - light.rail_1
      - light.rail_2
      - light.rail_3
      - light.rail_4
      - light.rail_5

A Light Group entity has an attribute called entity_id so we can reject any light entity that has this attribute defined.

- platform: template
  sensors:
    count_lights_on:
      friendly_name: 'Totaal aantal lampen aan'
      value_template: >
        {{ states.light 
          | rejectattr('attributes.is_deconz_group', 'eq', true)
          | rejectattr('attributes.entity_id', 'defined') 
          | selectattr('state', 'eq', 'on')
          | list | count }}
10 Likes

Thank you for your help!

1 Like
  - platform: template
    sensors:
      count_lights_on:
      friendly_name: "Totaal aantal lampen aan"
      value_template: "{{ states.light | rejectattr('attributes.is_Purifier_leds', 'eq', true) | selectattr('state', 'eq', 'on') | list | count }}"

I tried this also with my own settings (Purifier_leds)

But everytime when i add this in my configuration.yaml it says that i am not able to restart home assistant any more...

I just want to have a counter with all my light entities. But i am reading multiple solutions in the same value_template.
And mine are not working.

Probleem tijdens het herstarten van Home Assistant

argument of type 'NoneType' is not iterable

I feel i am doing something dumb because i don't understand those value_template stuff.

Would you be so kind and copy the code (not a picture) into the code editor here by using the </> button?

done that now

Thank you (I wasn’t sure if you had the right quotes in it). The indendation is wrong, it has to be like that:


  - platform: template
    sensors:
      count_lights_on:
        friendly_name: "Totaal aantal lampen aan"
        value_template: "{{ states.light | rejectattr('attributes.is_Purifier_leds', 'eq', true) | selectattr('state', 'eq', 'on') | list | count }}"

it does count all lights.

But somehow the exclusion for purifier_leds are not working or not used to count off the total. What is wrong.

Check the spelling in Developer Tools - States.

I’m trying to make this work not counting the hue groups. Hoping this was simular as the deconz_groups.

When I try the code given below in the developer tools i get
'UndefinedError: 'homeassistant.util.read_only_dict.ReadOnlyDict object' has no attribute 'is_hue_group' Does anyone has an idea?

- platform: template
  sensors:
    count_lights_on:
      friendly_name: 'Totaal aantal lampen aan'
      value_template: >
        {{ states.light 
          | rejectattr('attributes.is_hue_group', 'eq', true)
          | selectattr('state', 'eq', 'on')
          | list | count }}

      value_template: >
        {{ states.light 
          | rejectattr('attributes.is_hue_group', 'defined')
          | selectattr('state', 'eq', 'on')
          | list | count }}

1 Like

I’m trying to set up this with Z2M light groups. Unfortunately, the entities don’t include such an attribute like ‘light’ or ‘…group’.

Is there a way to get the attribute set by z2m or is there another way to exclude such z2m light groups from counting?

Edit:
Never mind… just noticed, that (light) groups don’t have the attribute ‘linkquality’. So I can check against this :slight_smile:

1 Like

Any chance you can tell me how you did it? I am also running into the issue that my z2m light groups are counted. Thanks!