Counts the lights on

{{  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…

I’m sorry for the inconvenience but I can’t find the solution.
I have 4 lights that I need to exclude from the count; if I create this sensor it doesn’t work (the sensor becomes unkown):

  • platform: template
    sensors:
    lights_on:
    friendly_name: ‘Lights On’
    icon_template: ‘mdi:lightbulb-group-outline’
    value_template: “{{ states.light | selectattr(‘state’, ‘eq’, ‘on’) | rejectattr(‘light.spotlight1’, ‘eq’, ‘on’) | rejectattr(‘light.spotlight2’, ‘eq’, ‘on’) | rejectattr(‘light.spotlight3’, ‘eq’, ‘on’) | rejectattr(‘light.spotlight4’, ‘eq’, ‘on’) | list | count }}”

Thanks in advance to whoever will help me.

Welcome to the party ,-). Can you try this?

value_template: |-
  {{ 
    states.light | selectattr(‘state’, ‘eq’, ‘on’) 
    | selectattr(‘entity_id’, ‘ne’, ‘light.spotlight1’) 
    | selectattr(‘entity_id’, ‘ne’, ‘light.spotlight2’) 
    | selectattr(‘entity_id’, ‘ne’, ‘light.spotlight3’) 
    | selectattr(‘entity_id’, ‘ne’, ‘light.spotlight4’) 
    | list | count
  }}

Please use code formatting in your next post.

1 Like

You can shorten your code a bit using


  {{ states.light |selectattr('state', 'eq', 'on') 
  |reject('search', 'spotlight1|spotlight2|spotlight3|spotlight4')
  |list |count }}

2 Likes

Ok perfect, it works. Thank you very much.

I take the opportunity to ask another question. If instead you had to count these sensors; I tried like this but it doesn’t work:

  doors_open:
    value_template: "{{ states.binary_sensor |selectattr('state', 'eq', 'on')
                       |selectattr('search', 'binary_sensor.sensordooring_contact|binary_sensor.sensordoorext_contact')
                       | list | count }}"

what am I doing wrong ?
How do I get the count of open ports called
“binary_sensor.sensordooring_contact” and “binary_sensor.sensordoorext_contact” ?

Thanks in advance to whoever will help me.

needs an attribute defined:


    value_template: "{{ states.binary_sensor |selectattr('state', 'eq', 'on')
                       |selectattr('entity_id', 'search', '_contact')
                       | list | count }}"

or


    value_template: "{{ states.binary_sensor |selectattr('state', 'eq', 'on')
                       |selectattr('object_id', 'search', '_contact')
                       | list | count }}"

object_id is what follows after the domain, for example

binary_sensor = domain

sensordooring_contact = object_id

1 Like

I have read the entire thread and I have listed the number of lights on.
image

I explored lovelace-auto-entities and I was able to create something like this: image
But it behaves like an entity, I have to click the entity, then click the dialog to turn off the light and close the dialog.

I want to have a card where the lights (that are ON) are displayed and I can turn them off in the card. Is that possible?

Probably you forgot to define the tap action.

You are right; now I have it working. Thank you
This is my code JIC:

type: custom:auto-entities
card:
  type: vertical-stack
card_param: cards
filter:
  include:
    - entity_id: light.*
      state: 'on'
      options:
        type: custom:mushroom-update-card
        entities:
          - this.entity_id
        tap_action:
          action: call-service
          service: homeassistant.turn_off
          service_data:
            entity_id: this.entity_id

1 Like

Now I want to take one step further and use a template because I already have a group with my lights. The problem is that I don’t know how to customize the card type or the tap_action. I have something like this:

type: custom:auto-entities
card:
  type: entities
filter:
  template: >-
    {{ expand('group.Lights_House') | selectattr('state','eq','on') |
    map(attribute='entity_id') | list }}

But it looks like this:
image

I prefer the previous look and feel of how to turn off the lights that are on.

Better to discuss here: 🔹 Auto-entities - Automatically fill cards with entities