How to apply multiple filter to a list?

I’m trying to get a list of entities filtered by area and label but this

{{ area_entities(‘kitchen’) | label_entities(‘main’) | list }}

is not working, what I’m doing wrong?

Hi ,

A little searching in the forum here finds a lot of stuff.
About jinja filtering.

Put this in your favorite search engine this if that one doesn’t help you enough.

site:home-assistant.io filter list jinja

thank for the heads up! I’ve searched all the morning about this, but even with the template documentation I can’t understand how to make it works.
I thought that jinja pipe filtering was processing the previous output, isn’t like that?

You have to use the filter commands to tell it what to do . You just piped one list into the other and returned the result as a list or something there.
It can’t read your mind my friend.

You start with a list, select or reject things, and the output to a list.

you’re correct, I’ve explained myself poorly: my goal is to set the automation action to turn on a list of all lights that

  • are in the same area of the triggered entity
  • have the label main
action: light.turn_on
metadata: {}
data: {}
target:
  entity_id: {{ label_entities('main') | selectattr('entity_id', 'in', area_entities('trigger.area_name')) | select('match', 'light') | list }}

Once you have an idea like that, if you want to test it and adjust, use the dev - template sandbox to get it correct.

Open your Home Assistant instance and show your template developer tools.

that’s what I’m doing; can you please suggest me where to find some docs and examples on select(), selectattr(), reject() and rejectattr() ? because my actual shotgun troubleshooting is not helping me to get out of this syntax problem

Tips on Searching for Answers & Duplicate Topics in the Forum.

Your template has three issues coming from the following filter function:

| selectattr('entity_id', 'in', area_entities('trigger.area_name'))

Issues:

  1. trigger.area_name is not one of the available trigger values.
  2. Unless you have an Area with the name “trigger.area_name” this part will never work. Remove the quote marks to use the variable instead.
  3. The preceding function, label_entities('main'), returns a list of entity IDs. The items in the list are just strings, they don’t have attributes for you to select by. Use select instead of selectattr.

The following uses trigger.entity_id to get the area since it is the most commonly available trigger datum that can easily be used to get an Area. If you are using a trigger type that does not supply a value for trigger.entity_id you will need to adjust the template:

{% set area = area_name(trigger.entity_id) %}
{{ label_entities('main') 
| select('in', area_entities(area)) 
| select('match', 'light') | list }}

thank you so much for your help