Filter / reject entities without an area, how?

Is there a way to filter out entities that don’t have an area? I.e. aread_id('entity_id') == None

I’m trying to get a list of temperature sensors and I’m only interested in those that have an area associated with them. I would have thought something like this would work select('area_id', 'ne', 'None'), but unfortunately it doesn’t. I also can’t find anything relevant in the new(ish) area filters.

This is the template which I currently have. It currently returns a mix of entities with and without area_ids.

{{
    states.sensor
    | expand
    | selectattr('attributes.device_class', 'defined')
    | selectattr('attributes.device_class', 'eq', 'tempature')
    | map(attribute='entity_id')
    | list
}}
{% set sensors = states.sensor
    | selectattr('attributes.device_class', 'defined')
    | selectattr('attributes.device_class', 'eq', 'temperature')
    | map(attribute='entity_id') | list %}
{% set ns = namespace(sensors = []) %}
{% for sensor in sensors if area_id(sensor) != none %}
{% set ns.sensors = ns.sensors + [sensor] %}
{% endfor %}
{{ ns.sensors }}

Thank you. That works perfectly.

1 Like