Listing entities for a notification in YAML

Hi. I found this little snippet to make a list of entities in a certain state. How can I expand upon this a bit? I’d like it to be device_class door, or motion, or sound or smoke. Thanks!

{{ states.binary_sensor
  | selectattr('attributes.device_class', 'defined')
  | selectattr('attributes.device_class', 'eq', 'door')
  | selectattr('state', 'eq', 'on')
  | map(attribute='attributes.friendly_name')
  | sort
  | join(', ') }}

Alternately, I also found this to do it by integration. What’s the syntax to add the entity state after the friendly name?

{% for entity in integration_entities('envisalink')-%}
  {{ state_attr(entity, 'friendly_name') }}
{% endfor %}

HA Docs - Building Templates


You can use the in test to test the device class against a list of classes:

{% set d_classes = ['door','motion','sound','smoke']%}
{{ states.binary_sensor
| selectattr('attributes.device_class', 'defined')
| selectattr('attributes.device_class', 'in', d_classes)
| selectattr('state', 'eq', 'on')
| map(attribute='attributes.friendly_name')
| sort
| join(', ') }}

The function states() is used to return a state from an entity ID:

{% for entity in integration_entities('envisalink')-%}
  {{ state_attr(entity, 'friendly_name') }} {{ states(entity) }}
{% endfor %}
1 Like

Fabulous, thank you!