Loop in action

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.

1 Like

I don’t believe that entity_id accepts templated values. I think the least-kludgy way to handle this would be with groups.

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.

Cool. Can you post a link?

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.

Did anyone found a solution, perhaps some sample code?

1 Like

put the list of entity into an array, instead.

1 Like

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:

automation:
  - id: enhanced_room_occupancy
    alias: 'Enhanced Room Occupancy'
    trigger:
      - platform: state
        entity_id: 
          - input_boolean.kitchen_occupied
          - input_boolean.livingroom_occupied
        to:  'on'
    condition:
        - condition: state
          entity_id: sensor.people_home
          state:  '1'
    action:
      - service: input_boolean.turn_off
        data_template: 
          entity_id: >
            {% set trigger_id = trigger.entity_id %}
            {{ expand(states.group.room_occupancy) | rejectattr('entity_id', 'eq', trigger_id) | map(attribute='entity_id') | join(',') }} 

As you can guess there is a group of input_booleans also required which i am sure someone smarter than me can template for the trigger too…

group:
  room_occupancy:
    name: Room Occupancy
    entities:
      - input_boolean.livingroom_occupied
      - input_boolean.kitchen_occupied