Counts the lights on

I asked Airyphyla :wink:

1 Like

That goes in the template section of configuration.yaml

when placed into configuration.yaml, it should look like this:

template:
  - sensor:
      - name: current_lights_on
        friendly_name: Lights at this moment
        unit_of_measurement: 'on'
        state: "{{ states.light | selectattr('state', 'eq', 'on') | rejectattr('attributes.is_hue_group') | list | count }}"
2 Likes

Note to myself: stop answering while in a semi boring Teams meeting :wink:

3 Likes

this is how i’ve got it setup in config.yaml, when i paste your example it wont let me save because i’ve got template: as duplicate showing up.

#templates

template:
  - sensor:
      - name: current_lights_on
        friendly_name: Lights at this moment
        unit_of_measurement: 'on'
        state: "{{ states.light | selectattr('state', 'eq', 'on') | rejectattr('attributes.is_hue_group') | list | count }}"

Hi Taras. Have come accross this one and would like a advise on how to remove a number of lightgroups in the counting.

It works if I put in this

value_template: >
          {% set reject = ['ganglightgroup','gang_dor_lamper'] %}
          {{ states.light | rejectattr('object_id', 'in', reject) | selectattr('state','eq','on') | list | length }}

however i have a number of light.groups ending on “lightgroup” or “_lamper”.

I have tried several versions of a code like this one, however can not make it work. It is like it will not accept a wildchar at all.

value_template: >
          {% set reject = ['*lightgroup','*_lamper'] %}
          {{ states.light | rejectattr('object_id', 'in', reject) | selectattr('state','eq','on') | list | length }}

I could make it by including all the groups, one by one. However I will make some error in this over time.

Any idea?

reject entities that contain the attribute ‘entity_id’

| rejectattr('attributes.entity_id', 'defined')
3 Likes

Works perfectly, very nice solution. Would not have come up with it my self. Thanks.

If others look for this later, the full template looks like this

    count_lights_on:
      friendly_name: "# Lights on"
      unit_of_measurement: 'on'
      value_template: "{{ states.light | selectattr('state','eq','on') | rejectattr('attributes.entity_id', 'defined') | list | length }}"

Can You show how card yaml code looks like?
I try to do something similiar with more groups like persons, doors opened and other.

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: