Use of for loop in automation action

Hi,
I’m new to HA and getting on well, building all sorts of things and mostly working it all out using these forums. I’m though stuck on getting an action to loop through a variable set as part of a trigger.
The automation is designed to turn heating off in specific zones when a swtich is turned off in HA for 5 seconds. This works fine when I have one climate entity in a zone, but breaks when I try adding a for loop.

This is the working code which handles just one climate entity:

alias: Turn Heating Off
description: ""
trigger:
  - platform: state
    entity_id:
      - input_boolean.heating_lounge_schedule_switch
    to: "off"
    for:
      hours: 0
      minutes: 0
      seconds: 5
    variables:
      climate_ids:
        climate_id_1:
          name: lounge
  - platform: state
    entity_id:
      - input_boolean.heating_study_schedule_switch
    to: "off"
    for:
      hours: 0
      minutes: 0
      seconds: 5
    variables:
      climate_ids:
        climate_id_1:
          name: study
  - platform: state
    entity_id:
      - input_boolean.heating_bathrooms_schedule_switch
    to: "off"
    for:
      hours: 0
      minutes: 0
      seconds: 5
    variables:
      climate_ids:
        climate_id_1:
          name: main_bathroom
        climate_id_2:
          name: second_bathroom
condition: []
action:
  - if:
      - condition: template
        value_template: "{{ trigger.to_state.state == 'off' }}"
    then:
      - action: climate.set_temperature
        metadata: {}
        data:
          temperature: 10
        target:
          entity_id: 
            - climate.{{ climate_ids['climate_id_1'].name }}
mode: single

I was then trying to update the target entity_id to:

      entity_id: >
        {% for climate_id in climate_ids %}
        - climate.{{ climate_ids['climate_id'].name }}
        (% endfor %}

However, this generates an error:

Message malformed: not a valid value for dictionary value @ data[‘action’][0][‘then’][0][‘target’][‘entity_id’]

Any ideas?

If you have several climate entities, why not use several triggers?

Is there a reason why you have chosen to define the value of climate_ids as a complex dictionary?

To get the job done, it only needs to be a list.

    variables:
      climate_ids:
        - climate.main_bathroom
        - climate.second_bathroom

Then the action can simply reference the variable’s list directly (no need for a for-loop).

 - action: climate.set_temperature
   data:
     temperature: 10
   target:
     entity_id: '{{ climate_ids }}'
1 Like

You can have more than one trigger in an automation…

(Don’t forget we can read your deleted posts!) :grin: