Counts the lights on

Can you please explain why the opening braces don’t need a matching set of closing braces?

Is it possible to exclude lights based on part of the entity name?

For example, I have 3 lights in the kitchen and a group ‘light.kitchenall’. If I turn on just one light in the kitchen, the kitchenall entity also shows as on so I get a count of two lights instead of just one.

How can I exclude an entity where the name includes ‘all’?

Its been corrected in the OP now. It was only that it previously read:

value_template: "{{ expand('group.luces', 'light.whatever') | selectattr('state','eq','on') | list | count) }}"

and should have read:

value_template: "{{ expand('group.luces', 'light.whatever') | selectattr('state','eq','on') | list | count }}"

I mentioned it only because people copy and pasting it in (like myself) would be tripped by the additional closing brace. All good now, and big thanks to @Taras for supplying the reference.

Thanks for explaining though!

You could add a reject to the template which searches for “all” in the name. Should be something like this:

{{ states.light | selectattr('state', 'eq', 'on') | rejectattr('attributes.friendly_name','search','all')| list | count }}

Brilliant. That’s done it. Your help is much appreciated.

Thanks man this is exactly what I was looking for

Given that the entities all have the same domain (light) you can shrink the reject list by simply listing each entity’s object_id.

        value_template: >
          {% set reject = ['home_lights', 'downstairs_lights', 'upstairs_lights', 'office_lights', 'study', 'living_room_lights', 'dining_room_lights', 'garage_lights'] %}
          {{ states.light | rejectattr('object_id', 'in', reject) | selectattr('state','eq','on') | list | length }}
3 Likes

Hey guys, i’ve been struggling making my own ‘lights-on count sensor’. Im using, from what i think is correct, a line of code. In dev-tools it also returns a positive value, however, the sensor doesn’t show up. I need a second pair of eyes on this; what am i doing wrong?

  - 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 }}"


Try this in configuration.yaml:

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

Where did you save this code? In configuration.yaml or somewhere else?

As mentioned above the code, yes, in configuration.yaml.

Works

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.