Counts the lights on

Sorry for late replay. Does this answer your question?

The code below will give me this result. Note that I use templates for some of the button config
image

             - type: custom:button-card
                template: button_toggle
                entity: light.light_basement
                icon: mdi:home-floor-0
                action: toggle
                show_label: true
                label: |
                  [[[
                    if(states['sensor.lights_number_on_in_basement'].state == 1){
                      return states['sensor.lights_number_on_in_basement'].state + " light on"
                    }
                    return states['sensor.lights_number_on_in_basement'].state + " lights on"
                  ]]]

Code for buttom template

button_card_templates:
  button_default:
    color: '#4caf50'
    size: 25%
    state:
      - value: 'off'
        color: '#ff5252'
    styles:
      card:
        - border-radius: 5px
        - padding: 5%
        - font-size: 100%
  button_toggle:
    template: button_default
    show_state: false
    action: toggle
    hold_action:
      action: more-info

Hi Guys.

I have this in Yaml file. It is reporting that 4 lights are on when in fact only 3 are. Is it including the light group? If so can that be excluded?

sensor:
  - platform: template
    sensors:
      count_lights_on:
        friendly_name: "# Lights on"
        unit_of_measurement: "on"
        value_template: "{{ states.light | selectattr('state', 'eq', 'on') | rejectattr('attributes.is_hue_group', 'eq', true) | list | count }}"
1 Like

To add other light groups than Hue:


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

EDIT: đŸ„· by RodgerDodger

2 Likes

I use this from day 1 forwards
Works all the time

platform: template

sensors:
  taf_count_lights_on:
    friendly_name: Lights on (taf)
    icon_template: mdi:lightbulb-on-outline
    value_template: >
      {{ states.light
        | rejectattr('attributes.is_deconz_group', 'eq', true)
        | rejectattr('attributes.entity_id', 'defined')
        | selectattr('state', 'eq', 'on')
        | list | count }}
1 Like

Thanks, mate. I now have the correct number of lights showing. I could not get your example working but it gave me what I needed to get mine sorted.

sensor:
  - platform: template
    sensors:
      count_lights_on:
        friendly_name: "# Lights on"
        unit_of_measurement: "on"
        value_template: "{{ states.light | selectattr('state', 'eq', 'on') | rejectattr('attributes.is_hue_group', 'eq', true) | rejectattr('attributes.entity_id', 'defined') | list | count }}"
1 Like

Hi!

There is a way to use a wildcard to reject all the browser_mod light entities??

Now, i use something like this:

{% set filter = ['light.browser_mod_fc5f0259_d2d4d40a'] %}
{{ states.light | selectattr( 'state', 'eq', 'on') | rejectattr('entity_id', 'in', filter) | list | length }}

But, i would like to did it more auto.

Thx!

rejectattr('object_id', 'search', '^browser_mod_')

1 Like

Thank u so much dude!

Hi, it is possible to retrieve Device/Entity Name instead of ID?
I’m using the following code (found on this thread :slight_smile: ) but I can’t figure out how to modify the attribute :frowning:
{%- set search_state = 'on' %} {%- set ns = namespace(lights=[]) %} {%- for light in states.light | selectattr('state','eq', search_state) | rejectattr('attributes.entity_id', 'defined') %} {%- set ns.lights = ns.lights + [ light.entity_id ] %} {%- endfor %} {{ ns.lights }}

Replace this with [ light.name ]

1 Like

Thanks! I was trying with “_” and “entity_name” :frowning:

to me it always signals one more light on 

If I have 1 lit, it signals 2.

Then go into Developer Tools and look for other lights that are on.

For me it was the ambilight even if the TV was off.

There’s no need to use a for-loop in the template to simply list the lights, by name, that are on. It can be done using filters.

{{ states.light | selectattr('state', 'eq', 'on')
   | rejectattr('attributes.entity_id', 'defined')
   | map(attribute='name') | list }}

If you want the result to display the lights as a comma-separated string, add join(', ') to the end of the template.

{{ states.light | selectattr('state', 'eq', 'on')
   | rejectattr('attributes.entity_id', 'defined')
   | map(attribute='name') | list | join(', ') }}

They are all off, in fact it signals 0.
But if I turn on 1, it signals me 2 


That’s because you probably switch on a light group and you haven’t hidden the group members in the group settings.

Either do that or live with that and be able to handle them separately in other circumstances.

if you made a light group you need to filter out the light group in your template.

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

I just had this start happening and it took me a bit to figure out it was the ring light on my damn doorbell I just installed lol. I added it the the reject list and all was well.

hello,
I use this code to count the light are on:

  - platform: template
    sensors:
      count_lights_on:
        friendly_name: "anzahl_lichter_an"
        unit_of_measurement: "an"
        value_template: "{{ states.light | selectattr('state', 'eq', 'on') | rejectattr('attributes.entity_id', 'defined') | list | count }}"

But I have some lights, which do not have the entity light, but switch. How can I e.g. add individual switches?

for example “switch.lichter_box”