Counts the lights on

Ok, now I do see the count of ‘1’

You need to write the code to select multiple states from all the devices if that is a scenario that will exist.

Sorry I’m not following. I repeat the code multiple times with different state each time?

The code I initially used was provided by @ValMarDav for lights.

You have multiple states across your HVAC devices so you may want to see the count by state or do you want the count for any active state combined?

Heating: {{ states.climate | rejectattr('attributes.entity_id', 'defined') | selectattr('state', 'eq', 'heat') | list | count }}
Cooling: {{ states.climate | rejectattr('attributes.entity_id', 'defined') | selectattr('state', 'eq', 'cool') | list | count }}

image

Lights are easier because they are simply on or off.

A battery example

{% set batteries = states.sensor
                    | selectattr('attributes.device_class', 'defined')
                    | selectattr('attributes.device_class', 'eq', 'battery')
                    | rejectattr('state', 'in', ['unknown', 'unavailable'])
                    | selectattr('state', 'ne', '100')
                    | selectattr('state', 'le', '90')
                    | selectattr('state', 'ge', '0')
%}
{% for battery in batteries -%}
- {{ state_attr(battery.entity_id, 'friendly_name') }}: {{ states(battery.entity_id) }}%
{% endfor %}

Yeah, any active state combined. If it’s on I’d like to see that as part of the count.

These are the attributes for the HVAC as reported under Developer Tools.

hvac_modes:
  - heat_cool
  - heat
  - dry
  - fan_only
  - cool
  - "off”

I only use cool, heat, dry.

I think this will work for you, let me know.

{{ states.climate | rejectattr('attributes.entity_id', 'defined') | selectattr('state', 'in', ['cool', 'heat', 'dry']) | list | count }}
1 Like

You my friend, are a dead set legend. Thank you so much!

1 Like

You can now accomplish this a bit easier using Floors (introduced in HA 2024.4):

{{ states.light | selectattr('state', 'eq', 'on')
  | rejectattr('name', 'search')
  | map(attribute='entity_id') | map('area_id')
  | select('in', floor_areas("Downstairs"))
  | list | count }}

This is a small modification to @123’s template

1 Like

besides the reject, I would think it to be more efficient to start with the floor (and not with states, which probably are many more entities to work with)

{{floor_areas('Downstairs')|map('area_entities')|sum(start=[])
  |select('match','light')|select('is_state','on')|list}}

and add |count for the counter ofc.

want to reject certain names/object_id’s? do

|reject('search','not_this')

Thanks Marius and Matt I was trying to find out how to count lights in a floor area. In case it helps this one excludes hue rooms & zones.

template:
  - sensor:
      - name: "Count Ground Floor Lights On"
        state: >
{{floor_areas('Ground Floor')
|map('area_entities')
|sum(start=[])
  |select('match','light')
  |select('is_state','on')
  |reject('search','hue_zone')
  |reject('search','hue_room')
  |list
  |count
}}

best to reject first, make it as small as possible as soon as possible.
might depend on what to reject though
just a thought

That works only if the entity’s entity_id contains the string hue_zone or hue_room.

A light entity that is either a Hue room or zone has an attribute named is_hue_group.

image

Therefore you can exclude them by detecting if the entity has this attribute.

{{ floor_areas('Downstairs')
  | map('area_entities')
  | sum(start=[])
  | select('match','light')
  | expand
  | rejectattr('attributes.is_hue_group', 'defined')
  | selectattr('state', 'eq', 'on')
  | map(attribute='entity_id')
  | list
  | count
}}

Good point, thankyou

Hi all,
I’m currently using the following code to count the lights

  - sensor:
      - name: "Lights turned on"
        unique_id: lights_turned_on
        icon: mdi:light-flood-up
        state: |
          {{ states.light 
            | rejectattr('attributes.entity_id', 'defined') 
            | rejectattr('attributes.is_hue_group', 'true') 
            | selectattr('state', 'eq', 'on') 
            | reject('search', 'screen')
            | reject('search', 'segment')            
            | list
            | count }}

But it shows to many lights. I now marked all light that should be counted with the label “lamp” and all not to count with the label “nolamp”. How can I make use of the label to only count lamps that are supposed to be counted?

See this video.

that video is missing out on the main feature label offers. Check the docs on the Label

no more need to iterate over all states, if one can use the label_entities() function

2 Likes