Check if the device has an assigned area [Jinja2 template]

Which filter should be added to this record to select those devices to which the area is assigned
{{ states.light|selectattr('state','eq','on')|map(attribute='entity_id')| list }}

I should check what area_id(‘device_id’) in areas(), but I don’t see how to do it in a nice way.

Any solution on how to get a list of working lights (with assigned area) will also do.

Copy-paste the following template into the Template Editor and experiment with it.

{% set x =  states.light
  | selectattr('state', 'eq', 'on') 
  | map(attribute='entity_id')
  | list %}
{% set y = x | map('area_name') | list %}
{{ zip(x, y) | list }}

If you want to display the results in a particular format then you’ll need to use a for-loop.

Perhaps my question was not worded accurately enough.

I will clarify the general task - to find out if there are switched on lights in the house, excluding various indicators to which no area is assigned ( A sub-variant of the task, the indicators are assigned an auxiliary area. And when checking, we exclude them based on that area).
Your example does not add any checks and returns objects without area

Correct, because your first post didn’t mention those requirements.

Do you have any other unmentioned requirements? It’s easier to compose a solution when all requirements are known.


EDIT

If you simply want to reject any lights that are on but have no assigned area, you can use rejectattr.

{{ zip(x, y)
 | rejectattr(1, 'eq', none)
 | list }}

The same technique can be used to reject lights from a specific area (should you need that).

Quite explicit in my opinion, but I could be wrong as I’m not a native speaker.
Anyway, thank you very much.

1 Like