What's wrong with this template

I’m busys creating my own dashboard.
Thats going fine.
I did make a template for counting amount of lights on in a specific room.
This is working fine.
working template:

        {%- set ns = namespace(lightsensors=[]) -%}
        {%- set ns.lightsensors = states.light
          | selectattr('entity_id', 'in', area_entities('Woonkamer'))
          | selectattr('state', 'eq', 'on')
          | map (attribute='name') | list -%}
        {{ ns.lightsensors | count }}

Now i try to create a look a like template for count the amount of doors are open in a specific Area.
Not working template:

        {%- set ns = namespace(doorsensors=[]) -%}
        {%- set ns.doorsensors = states.door
          | selectattr('entity_id', 'in', area_entities('Woonkamer'))
          | selectattr('state', 'eq', 'on')
          | map (attribute='name') | list -%}
        {{ ns.doorsensors | count }}

What is wrong with this template?
Is “states.door” not correct?
Thanks

EDIT:

I did found the solution allready states.door needs to be states.binary sensor
and test on attributes.device_class also

        {%- set ns = namespace(doorsensors=[]) -%}
        {%- set ns.doorsensors =  states.binary_sensor 
          | selectattr('entity_id', 'in', area_entities('Woonkamer'))
          | selectattr('attributes.device_class', 'eq', 'door') 
          | selectattr('state', 'eq', 'on') 
          | map (attribute='name') | list -%}
        {{ ns.doorsensors | count }} 

Correct door sensors is not a device type.

Device Class can be device_class: door

Thanks. I did recognize that

If you’re interested, here’s how to find the number of open doors in a given Area.

{{ area_entities('Woonkamer')
  | select('match', 'binary_sensor') | expand
  | selectattr('attributes.device_class', 'eq', 'door')
  | selectattr('state', 'eq', 'on') | list | count }}

Instead of starting with all binary_sensors in your system (states.binary_sensor), it starts with all entities in Woonkamer (a much smaller number of entities), then selects just the binary_sensors. After that it proceeds similarly to your version, filtering by device_class and state. No for-loop is needed to produce the final count.

please also have a look at labels. they make life so much easier for these templates.

add a label ‘door’ to those binaries and all you need to search is the label_entities('door')

{{label_entities('deur')|select('is_state','on')|list|count}}

and, if you want to restrict to a certain area:


 {{ area_entities('library')
   |select('in',label_entities('deur'))
   |select('is_state','off')|list}}

if you need any of the attributes (like name) use |expand

{{label_entities('deur')
   |select('is_state','on')
   |expand
   |map(attribute='name')|join(', ')}}
1 Like

How would you constrain it to open doors in the Woonkamer area?

crossposted :wink:

currently we can only do this WTH, where are the floor_entities() for more than 1 targeted area, but Petro got his PR merged, so from 2025.4 on we finally will have floor_entities() !

This is more compact and much more smarter indeed.
Thanks

1 Like

This looks like a nice solution indeed.
But I don’t understand the way to use it?
Where can I find an explanation/ tutorial?