The repeat in automation is too limited for me, for starters it was again my first time to get this too work so first trying to find good example (and some were helpful)
Objective is to make a selection of entities with specific attributes.
this double selection/piping/filtering does not work and the output results in:
Executed: October 29, 2025 at 5:31:42 PM
/0
Iteration 1
Executed: October 29, 2025 at 5:31:49 PM
Result:
result: false
entities: []
I have used this selection method successful on the dashboard (and yes this selection will still give some entities).
As soon as I try to do this selection upfront the devil is in the syntax and I encounter the common error “Error: Repeat ‘for_each’ must be a list of items”. I continue to find further examples but if someone can direct me to a solution that will be most helpful.
Actually it did work. You designed it to find all binary sensors with a state value of stale. A binary sensor’s state value can be on or off (or potentially unavailable) but it cannot be stale so the result of your template is correct (an empty list).
A template can have double, triple, quadruple, etc selections. What is it that you want your second selection to do?
I don’t see how that’s possible for the posted template because stale is not a valid state value for Home Assistant’s binary sensor model.
It seems to me as you are developing your automation in the automation editor.
I would suggest you use the template tools to get the syntax of your template correct first then move it over to the automation.
I guess you can use | sort(attribute='last_changed') to sort the entities on changed time, but then you would have to do an if template on the entity if it is above a parameter inside the looping part I guess.
I want to thank you all for your input, when put those together I realised the desired solution:
- repeat:
for_each: >
{% set time = now() | as_timestamp | int %}
{% set type = states['input_select.meshcore_contact_type'].state[:1] | int %}
{% set days_old = states['input_number.meshcore_days_old'].state | int %}
{% set time = time - days_old*24*60*60 %}
{{ states.binary_sensor | selectattr('attributes.lastmod', 'defined')
| selectattr('state', 'eq', 'stale')
| selectattr('attributes.lastmod', 'lt', time )
| sort(attribute='attributes.lastmod')
| map(attribute='object_id')
| list
}}
I want to the add the following to your input:
Variables: it looks like that variables needs to be declared inside the repeat
Patience must be a virtue
Use the template editor for faster results.
For users of the Meshcore integration:
use lastmod not last_advert (because last_advert date comes from repeaters are potential not correctly set and still be 2024-something.
attribute='last_changed' is not an option due to the behavior of the integration.
sorry Andy my Mesh gets to big and I need to remove contacts.
FWIW, given that you’re using this successfully for a Binary Sensor:
selectattr('state','eq', 'stale')
It means the Meshcore integration doesn’t comply with Home Assistant’s Binary Sensor model because it permits non-standard state values (i.e. “stale”).
For future reference, when a custom integration is involved, it’s helpful to include its name in the topic’s first post. A custom integration can behave in ways that differ from official integrations.