Count all switches contains string in friendly name

I’m trying to count all switches contains specific string in the friendly name field.
For example, all switches that contains the words ‘room’. Is it possible?


This template will get you the count. You can make it in a template sensor. If you do that, make sure you attach an entity_id like sensor.time or or sun.sun so the template periodically updates.

{% set ns = namespace(count=0) %}
{% set iter = states.switch | map(attribute='attributes.friendly_name') | list %}
{% for name in iter %}
{% if 'room' in (name | string).lower() %}
{% set ns.count = ns.count + 1 %}
{% endif %}
{% endfor %}
{{ ns.count }}

The template returns all entities that contains “room” at the friendly_name attribute,
Can It return only switches?


never mind. I just added “.switch” to “states” at line 2.
Thank you!

Yep, you got it. I’ll edit it for others. :wink: