Hi!
Could you please tell me if it’s possible to execute action for multiple entities using loop for selecting them?
I used following code, but it doesn’t work properly:
- alias: Switch presence
trigger:
platform: state
entity_id:
- input_boolean.room1
- input_boolean.room2
- input_boolean.room3
to: 'on'
action:
service: input_boolean.turn_off
data_template:
entity_id: >
{% for state in states.input_boolean %}
{%- if state | string != 'trigger.entity_id' -%}
{{ state.entity_id }},
{%- endif -%}
{% endfor %}
Loop forming string like “input_boolean.room1,input_boolean.room2,…”, but action don’t work.
Main idea is that if input_boolean in one room is on, then input_booleans in other rooms should be turned off.
I know about input_select which is more suitable for this situation, but in my config I need input_booleans.
Actually, entity_id works perfect with templated value (see HASS manuals). And with list of entities. But I can’t make it to work with list of templated values.
If that’s the case, I wonder if what you’re generating is getting interpreted as a long string rather than a list. I’m not sure how to append to lists in Jinja.
Automation Templating
There is no example in this man, but believe me, entity_id works well with templates, of course if data_template directive present. For example I have automation to turn on lights according to input boolean state, which uses templated entity_id. If you interested, I can post code example.
Well, I tried code that forms both long string input_boolean.room1, input_boolean_room2, … and list: - input_boolean.room1 - input_boolean.room2 …
Unfortunately, both ways don’t work. entity_id work with long string (without templates) and with list. But not with string/list generated by template.
Seems like i had a very similar use case to Alexander!
After banging my head against the wall on how this relatively simple task was kicking my ass, finally cracked it this morning. Sharing here to help others: