How do I get all entities NOT in ANY area?

area_entities() gives me a list of all entity IDs in a given area which is fine. (Although it would be better as a generator.)

How do I get a list of entity IDs that aren't assigned to an area? I create a lot of template entities and it would be useful to find those without an area so I can assign them.

The overview dashboard shows me devices unassigned to areas but not template entities.

This should give you a list of all entitty_id's which are not in in an area

{% set ns = namespace(in_area=[]) %}
{% for area in areas() %}
  {% set ns.in_area = ns.in_area + area_entities(area) %}
{% endfor %}
{{ states | map(attribute='entity_id') | reject('in', ns.in_area) | list }}

Ah. I hadn't thought of a Jinja2 loop. Thanks. I still think that it would be nice if area_entities(none) would do the same and it'd be a lot faster to do in the core Python. Feature request I guess. Thanks.

You can use the difference filter.

{{ difference(states|map(attribute='entity_id'), areas()|map('area_entities')|flatten) }}

When I tested it on my system, the resulting list contained entities that I would not normally assign to an area (i.e. automation, script, update, etc). You can reject them like this:

{{ difference(states | map(attribute='entity_id'), areas() | map('area_entities') | flatten)
   | reject('match', 'automation|calendar|notify|script|update')
   | sort
}}

That's a nice approach and perhaps fewer CPU cycles.

Thanks.

FYI, I assign everything to an area. If an automation affects only entities in a given area, it is assigned to that area.

I have a 'non-localized' area for things like weather, automations affecting or groups encompassing more than one area, etc.

Hence my wish for a 'no-area' filter.

On a similar topic, how do I select or reject only those entities that don't have unique IDs and hence can't be assigned to an area?

unique_id is one of several entity properties stored in the core.entity_registry file. To my knowledge, there's no templating function able to access an entity's unique_id property.

Shame. I'll have to keep using my ad hoc filters for those - Ethernet MAC router DHCP device trackers in known_devices.yaml use a very specific naming format for example. But other entities that HA creates (sun.sun, conversation.* etc.) aren't up for that and have to be excluded explicitly. Hey ho. Thanks anyway.

Be careful with putting automations in areas if you ever do things like 'turn_off' area. That will, perhaps unexpectedly, disable any automations assigned to the area.

Point noted thanks. But I don't do blanket turn-off/on/toggle whole areas as this would not make any sense for me since almost every area has devices (smart-plugs on servers, UPSes & network infrastructure) that should only ever be turned off 'on-demand' to force a crashed one to reboot etc.

You could use a label for that. Apply that label to the entities you'd expect to not have an area assigned and ignore the entities with that label.

Labels can't be assigned to entities that don't have unique IDs such as known_devices.yaml-based device trackers. And I'd have to remember to add the label which is another layer of maintenance task. (I do use labels for differentiation e.g. wifi signal strength vs Bluetooth signal strength, battery draw/charge power vs main device power and so on so I can use auto-entities cards etc.)

But regarding the issue with automation toggles, I've now moved them all to a separate 'system' area along with update entities for apps and so forth.

Yes, sure of course :slight_smile: Should've thought of that :slight_smile: