Filter area_entities

Hi,

I’m trying to filter out temperature entities for one area. The documentations says it can be used as a filter:

  • area_entities(area_name_or_id) returns the list of entity IDs tied to a given area ID or name. Can also be used as a filter.

Since the returned list is just a list of strings, not state objects, there doesn’t seem to be an easy way of filtering based on device_class.

Anyone have a suggestion?

The closest I can come up with is

{% set entities = namespace(list=[]) %}
{% for entity in area_entities('Living room') if state_attr(entity.entity_id, 'device_class') == 'temperature' %}
  {% set entitites.list = entities.list + [ entity.entity_id ]
{% endfor %}
{{ entities.list }}

But a one-liner using the selectattr would be much cleaner and easier to maintain.

Use expand.

expand(area_entities(xyz)) | selectattr(…

2 Likes

Bah, 1 1/2 with HA and I’ve just realized this one expands in this way. Great. Thank you @petro!

For anyone who stumbles across.
Expanding will remove groups.
I always thought they meant the filter like this:

{{ states.light
| selectattr('entity_id', 'in', area_entities('xyz')) }}

How to select objects in multiple areas?
For example: a list of temperature sensors placed in the kitchen and bedroom.

Lists can be combine with the plus sign.

{{ states.light 
| selectattr('entity_id', 'in', area_entities('kitchen') + area_entities('bedroom'))  }}
2 Likes