Counts the lights on

hi petro, mind helping me in post 188 here?
I’m really stuck and can’t figure it out by myself.

thanks a lot!

None of the above examples were perfect in my case so I share what was my solution. If I missunderstood something please correct me.

Background: I’m using IKEA tradfri bulbs and zigbee switches to switch the light, only when I want to change the brightness / color temp then I configure the bulbs. For the sake of convenience I organized the bulbs in groups.
Problem:
The common | rejectattr('attributes.entity_id', 'defined') statement is not filtering out my groups nor my bulbs. I guess because I’m using ZHA groups and in these groups there is no entity_id attribute. It neither filters out my bulbs so I ended up counting a lot more light then I wanted :slight_smile:
Example:

min_color_temp_kelvin: 2202
max_color_temp_kelvin: 4000
min_mireds: 250
max_mireds: 454
supported_color_modes:
  - color_temp
  - xy
color_mode: color_temp
brightness: 252
color_temp_kelvin: 2702
color_temp: 370
hs_color:
  - 28.391
  - 65.659
rgb_color:
  - 255
  - 166
  - 87
xy_color:
  - 0.526
  - 0.387
off_with_transition: false
off_brightness: null
friendly_name: dresden elektronik ConBee II Bulb.feriroom_bulbs_zha_group_0x0002
supported_features: 40

Solution:
I had to find an attribute which exist in the ZHA groups and in my bulbs but not in my switches:
Switch attributes:

supported_color_modes:
  - onoff
off_with_transition: false
off_brightness: null
friendly_name: Kitchen.zbmini.switch Light
supported_features: 8

Sidenote: first I wanted to use supported_color_modes and separate by the value of this attribute, but this is not a real attribute DO NOT waste your time with this :wink:

Solution:
I ended up using the min_mireds attribute to filter out the groups and bulbs because its a common attribute for bulbs and is not populated for switches.

{{ 
states.light
              | rejectattr('attributes.min_mireds', 'defined')
              | selectattr('state', 'eq', 'on')
              | list
              | count
}}

I hope this might help someone.

Got to be an easier way to show just switches and light on?

It’s states.light :slight_smile:


{{ expand(states.switch, states.light)
|selectattr('state', 'eq', 'on') |list |count }}

Hello,

I’m trying to create an entity to count the lights that are on. Based on what I’ve read above, I’ve created the following yaml in the sensors.yaml file. However, the entity does not show up. What am I doing wrong?

- platform: template
  sensors:
    current_lights_on:
      friendly_name: Lichten aan op dit moment
      unit_of_measurement: "on"
      value_template: >
        {% set lights = [
        'states.light.woonkamer',
        'states.light.slaapkamer',
        ] %}
        "{{ expand(lights) | selectattr('states','eq','on') | list | count }}"

Cheers,

Ruben

Remove the double-quotes from the last line.
Change the word states to state.
Just use the entity_ids directly.

- platform: template
  sensors:
    current_lights_on:
      friendly_name: Lichten aan op dit moment
      unit_of_measurement: "on"
      value_template: >
        {% set lights = [
        'light.woonkamer',
        'light.slaapkamer'
        ] %}
        {{ expand(lights) | selectattr('state','eq','on') | list | count }}

Thanks for your reply! I did what you said, but the entity does not show up in the entities list.

What could be the problem here?

PS: I ‘manually’ added the sensors.yaml file by going into configuration.yaml and writing:

sensor: !include sensors.yaml 

Then I proceeded by creating a new file called sensors.yaml

Was this the correct way? (I restarted HA afterwards)

Check the log for reported errors.

No errors found, but I wrote the code from scratch, restarted the system and the entity showed up in the list. Thanks for your help!

Hy everyone

I am quite confused with this behavior by counting 2 light groups.
When I am executing the following code in the jinja2 engine the sum up is correctly. But in the template sensor (code itself), it seems that home assistant is unable to parse it (not presente in my entity list).

{{  expand(state_attr('light.bureau', 'entity_id') )
| expand(state_attr('light.sejour', 'entity_id') )
| selectattr('state','eq','on') | list | count }}

I add a workaround by sumup the 2 groupe separatelly, but the first scenario would have be proper and factorized


{{  expand(state_attr('light.bureau', 'entity_id') )
| selectattr('state','eq','on') | list | count 
+  expand(state_attr('light.sejour', 'entity_id') )
| selectattr('state','eq','on') | list | count }}

If you have any reflexion to share… Thanx :wink:

{{  expand(state_attr('light.bureau', 'entity_id'), state_attr('light.sejour', 'entity_id'))
  | selectattr('state','eq','on') | list | count }}
1 Like

Is there a way to get the list of entities? I would like to use this type of logic in an entities card, so the number of lights shows up on the button, then then I tap, a popup will show a entities card with the list of the entities each in a button?
Right now I just have it show all the lights. Not only the one that are on.
I would even do it as a text, just letting me know which are on using their friendly names. I have a lot of lights, so that’s the reason I am trying to do that.

It’s easy to get a list of the lights that are on. In some of the examples posted in this topic, remove the final count filter and you will get a list of entity_ids.

As for what you what the cards to do, someone else will have to help you with that.

Can someone please tell me what i am doing wrong ?
This below is my sensors.yaml, it says unavailable in tools/status.
I mean the last part, the Total lights on section.
(Sorry first time i paste my code here)

- platform: command_line 
    name: CPU Temperature 
    command: "cat /sys/class/thermal/thermal_zone3/temp" 
    unit_of_measurement: "°C" 
    value_template: '{{ value | multiply(0.001) | round(1) }}'

  - platform: systemmonitor
    resources:
      - type: disk_use_percent
        arg: /config
      - type: memory_free
      - type: processor_use
      - type: last_boot
      - type: disk_free
      - type: memory_use_percent
      - type: throughput_network_in
        arg: eno1
      - type: throughput_network_out
        arg: eno1
        
  - platform: template
    sensors:
      current_lights_on:
        friendly_name: Lichten aan op dit moment
        unit_of_measurement: "on"
        value_template: >
           {% set lights = [ -
           'states.light.bank_links',
           'states.light.bank_rechts',
           'states.light.eetkamer_links',
           'states.light.eetkamer_rechts'
           ] %}
           {{ expand(lights) | selectattr('state','eq','on') | list | count }}

Compare your code with the example in post #204.

My post is number #204 ?

this is 204

  - platform: template
    sensors:
      current_lights_on:
        friendly_name: Lichten aan op dit moment
        unit_of_measurement: "on"
        value_template: >
          {{ expand('light.bank_links', 'light.bank_rechts', 'light.eetkamer_links', 'light.eetkamer_rechts')
            | selectattr('state','eq','on') | list | count }}
1 Like

Ok sorry, my telefoon says 204 was my post.
Wil look into it when im home.

Nevermind…