Help: automation with data_template and for-loop

I would like to iterate through entities to switch them of, but I cannot get it to work:
The aim is to switch off all entities except for the one that triggerd the automation.

alias: Toggle Input Booleans for Dashboard
description: ""
trigger:
  - platform: state
    entity_id:
      - input_boolean.toggle_abfall
      - input_boolean.toggle_eingang
      - input_boolean.toggle_kvb
      - input_boolean.toggle_lichtdg
      - input_boolean.toggle_lichteg
      - input_boolean.toggle_luftfeuchte
      - input_boolean.toggle_mediendg
      - input_boolean.toggle_medieneg
      - input_boolean.toggle_pflanzen
      - input_boolean.toggle_steckdosen
      - input_boolean.toggle_vacuum
    to: "on"
condition: []
action:
  - service: input_boolean.turn_off
    data_template:
      entity_id: >
        {% set triggered_entity = trigger.entity_id %}  {% for entity in
        states.input_boolean 
          if entity.entity_id.startswith('input_boolean.toggle') and entity.entity_id != triggered_entity %}
        {{ entity.entity_id }},  {% endfor %} 
mode: restart

That leeds to: Fehler: not a valid value for dictionary value @ data[‘entity_id’]

The list generated by the template-code seems to be correct (in the trace)

BTW:
Is there some way to also simplify the trigger?

Not sure that it works but when you have a list of entities they need a - and a space before the entity name and no comma.

        {% set triggered_entity = trigger.entity_id %}  
        {% for entity in states.input_boolean %}
          {% if entity.entity_id.startswith('input_boolean.toggle') and entity.entity_id != triggered_entity %}
            - {{ entity.entity_id }}
          {% endif %}
        {% endfor %} 
{{ states.input_boolean | map(attribute='entity_id') | select('search', '^input_boolean.toggle') | reject('eq', trigger.to_state.entity_id) | list }}
1 Like

Thank you!! That works fine now :slight_smile: