Allow for multiple labels/areas in label_entities/area_entities

Currently, if you want to list all entities in a given area or all entities having a given label, you do this:

{{ area_entities('kitchen') }}

{{ label_entities('kitchen') }}

If you want all entities in multiple given areas you can do this:

{{ ['kitchen', 'bedroom', 'bathroom']
   | map('area_entities')
   | sum(start=[]) }}

(Practical example shown here).

Similarly, to get all entities having multiple given labels, you would do this:

{{ ['outdoor', 'battery', 'temperature']
   | map('label_entities')
   | sum(start=[]) }}

This Feature Request proposes simplifying the process by enhancing area_entities and label_entities so they support multiple entities as arguments.

{{ area_entities('kitchen', 'bedroom', 'bathroom') }}

{{ label_entities('outdoor', 'battery', 'temperature') }}

In effect, this FR asks to have them behave like expand which, when used as a function, can accept a comma-separated string or a list:

{{ ['group.doors_exterior', 'group.doors_interior'] | expand | list }}
{{ expand(['group.doors_exterior', 'group.doors_interior']) | list }}
{{ expand('group.doors_exterior', 'group.doors_interior') | list }}

2 posts were split to a new topic: Enhanced way to pass arguments

You can also use a + operator:

{{ area_entities('kitchen') 
  + area_entities('bedroom') 
  + area_entities('bathroom') }}

Or several other inelegant ways which is why this FR was created

1 Like

especially useful for iterating over combined sets of label_entities, an aspect not yet mentioned.
eg

{%- for s in ['binnenlamp','buitenlamp']
   | map('label_entities')
   | sum(start=[])
    |select('is_state','on') %}
{%- if states[s].last_changed.day == now().day %}
{%- set stamp ='%H:%M' %}
{%- else %} {% set stamp = '%d/%m - %H:%M' %}
{%- endif %}
{%- if loop.first %} {% else %}, {% endif -%}
{{states[s].entity_id}} ({{as_local(states[s].last_changed).strftime(stamp)}})
{%- endfor %}

it does work like this, but a

{{ label_entities('binnenlamp','buitenlamp') }}

would be so much cleaner.

adding my support here since the linked/split topic seems to be focussing on introducing new functions, while this is an enhancement of the current.

Also because the suggested

label_entities('bedroom') + label_entities('kitchen')

is no solution for the use case I showed above, cant be done like that

thx for opening it

next challenge:

select certain areas (dynamically later on, but for now have enumerated them) in several floors, then select individual lights that are on…

{{['dorm','aanbouw','woongedeelte']|map('floor_areas')|sum(start=[])
|reject('search',['dorm_marte','logeerkamer','studenten']|join('|'))

|map('area_entities')|sum(start=[])
|select('match','light')

|select('is_state','on')|list}}

this works alright, except this also targets light groups. so, I would like to replace the

|map('area_entities')|sum(start=[])
|select('match','light')

with a selector using the

label_entities('binnenlamp')

or

['binnenlamp']|map(label_entities)

for that matter

but I can’t get the correct syntax.

please have a look what I could do here?
thx

edit

this seems to work:

{{['dorm','aanbouw','woongedeelte']|map('floor_areas')|sum(start=[])
|reject('search',['dorm_marte','logeerkamer','studenten']|join('|'))
|map('area_entities')|sum(start=[])
|select('in',label_entities('binnenlamp'))
|select('is_state','on')|list}}

though I am wondering why I can not map the label_entities directly, and first have to map the area_entities

It’s a bit opaque, but all groups have an entity_id attribute you can filter for.

yeah, please read down that post above, I already found the way to select the label_entities() using

|select('in',label_entities('binnenlamp'))

or, even use the same technique to again select from several labels:

{{['dorm','aanbouw','woongedeelte','buiten']|map('floor_areas')|sum(start=[])
|reject('search',['dorm','logeerkamer','studenten']|join('|'))
|map('area_entities')|sum(start=[])
|select('in',['binnenlamp','buitenlamp']|map('label_entities')|sum(start=[]))
|select('is_state','on')|list}}

so, select a few floors, and
find the areas in those floors,
reject a few areas, then
find the entities in those selected areas, and
find the correct label_entities in those areas, and
finally select those of the desired state :wink: