For Each Loop with entity_id template in IF Statement

Hello guys,

I’m having a problem saving an automation that I try to implement with for each loop.

YAML is the following:

alias: For Each Room
repeat:
  for_each:
    - room: Bedroom
      status: sensor.bedroom_status
      heating: climate.bedroom
      cooling: climate.bedroom_ac
      manual_hvac: switch.bedroom_hvac_manual_mode
      manual_ac: switch.bedroom_ac_manual_mode
    - room: Kitchen
      status: sensor.kitchen_status
      heating: climate.kitchen
      manual_hvac: switch.kitchen_hvac_manual_mode
      manual_ac: switch.kitchen_ac_manual_mode
    - room: Livingroom
      status: sensor.livingroom_status
      heating: climate.livingroom
      cooling: climate.livingroom_ac
      manual_hvac: switch.livingroom_hvac_manual_mode
      manual_ac: switch.livingroom_ac_manual_mode
    - room: Office
      status: sensor.office_status
      heating: climate.office
      cooling: climate.office_ac
      manual_hvac: switch.office_hvac_manual_mode
      manual_ac: switch.office_ac_manual_mode
  sequence:
    - if:
        - condition: state
          entity_id: switch.someone_home
          state: "on"
          alias: Someone is Home
      then:
        - alias: Turn On Heating if Room Status is not Alert
          if:
            - alias: Room Status is Not Alert
              condition: not
              conditions:
                - condition: state
                  entity_id: "{{repeat.item.status}}"
                  state: Alert
                  alias: Room Status
          then:
            - alias: Turn On Heating if Manual Mode is Off
              if:
                - alias: Manual Mode Off
                  condition: state
                  entity_id: "{{repeat.item.manual_hvac}}"
                  state: "off"
              then:
                - service: climate.set_temperature
                  data:
                    temperature: "{{states.input_select.hvac_manual_heat_temperature.state}}"
                    hvac_mode: heat
                  target:
                    entity_id: "{{repeat.item.heating}}"
                  alias: Set Bedroom HVAC Temperature
          else:
            - alias: Turn Off Heating
              if:
                - condition: not
                  conditions:
                    - condition: state
                      entity_id: "{{repeat.item.heating}}"
                      state: "off"
                  alias: Heating is not Off
              then:
                - alias: Turn Off Heating
                  service: climate.turn_off
                  data: {}
                  target:
                    entity_id: "{{repeat.item.heating}}"

When I try to save, I get the error:
Message malformed: Entity {{repeat.item.status}} is neither a valid entity ID nor a valid UUID for dictionary value @ data[‘action’][0][‘else’][0][‘then’][0][‘then’][3][‘repeat’][‘sequence’][0][‘then’][0][‘if’][0][‘conditions’][0][‘entity_id’]

Any ideas on how to fix this?
Thanks

The YAML code you posted seems to be a script, not an automation. An automation has a trigger section, and an optional condition section.

A script looks like the example shown here. Note how the sequence: key appears immediately after the script’s name (it’s missing from your script example).

If your intention is to make an automation, not a script, then your example is missing a trigger section. I suggest you use the Automation Editor in visual mode to compose your automation. You can switch it to YAML mode to see the automation’s YAML code.

If your example is just a portion of an automation, please post the entire automation.

Hello,
It is only a portion of the automation. In the meanwhile I managed to find another solution for the IF conditions. I used templates.
Please see the section below…

alias: For Each Room - Turn On Heating and Turn Off Cooling
repeat:
  for_each:
    - room: Bedroom
      status: sensor.bedroom_status
      room_temperature: sensor.bedroom_temperature_sensor_temperature
      heating: climate.bedroom
      cooling: climate.bedroom_ac
      manual_hvac: switch.bedroom_hvac_manual_mode
      manual_ac: switch.bedroom_ac_manual_mode
      x_fan: switch.bedroom_ac_xfan
    - room: Kitchen
      status: sensor.kitchen_status
      room_temperature: sensor.kitchen_temperature_sensor_temperature
      heating: climate.kitchen
      manual_hvac: switch.kitchen_hvac_manual_mode
      manual_ac: switch.kitchen_ac_manual_mode
    - room: Livingroom
      status: sensor.livingroom_status
      room_temperature: sensor.livingroom_temperature_sensor_temperature
      heating: climate.livingroom
      cooling: climate.livingroom_ac
      manual_hvac: switch.livingroom_hvac_manual_mode
      manual_ac: switch.livingroom_ac_manual_mode
      x_fan: switch.livingroom_ac_xfan
    - room: Office
      status: sensor.office_status
      room_temperature: sensor.office_temperature_sensor_temperature
      heating: climate. Office
      cooling: climate.office_ac
      manual_hvac: switch.office_hvac_manual_mode
      manual_ac: switch.office_ac_manual_mode
      x_fan: switch.office_ac_xfan
  sequence:
    - alias: Turn On Heating and Turn Off Cooling
      if:
        - condition: state
          entity_id: switch.someone_home
          state: "on"
          alias: Someone is Home
      then:
        - alias: Turn On Heating System and Turn Off AC
          if:
            - condition: template
              value_template: "{{states(repeat.item.status) != \"Alert\" }}"
              alias: Room Status is not Alert
          then:
            - alias: Turn On Heating System
              if:
                - condition: template
                  value_template: "{{states(repeat.item.manual_hvac) == \"off\"}}"
                  alias: Heating Manual Mode is OFF
              then:
                - service: climate.set_temperature
                  data:
                    temperature: "{{states.input_select.hvac_manual_heat_temperature.state}}"
                    hvac_mode: heat
                  target:
                    entity_id: "{{repeat.item.heating}}"
                  alias: Set Heating Temperature
            - alias: Turn Off AC
              if:
                - condition: template
                  value_template: "{{states(repeat.item.manual_ac) == \"off\"}}"
                  alias: AC Manual Mode is Off
                - condition: template
                  value_template: "{{states(repeat.item.cooling) != \"off\"}}"
                  alias: AC is not Off
              then:
                - service: switch.turn_on
                  data: {}
                  target:
                    entity_id: "{{repeat.item.x_fan}}"
                  alias: Turn On XFan
                - service: climate.turn_off
                  data: {}
                  target:
                    entity_id: "{{repeat.item.cooling}}"
                  alias: Turn Off AC
          else:
            - alias: Turn Off Heating System
              if:
                - condition: template
                  value_template: "{{states(repeat.item.heating) != \"off\"}}"
                  alias: Heating is not OFF
              then:
                - alias: Turn Off Heating
                  service: climate.turn_off
                  data: {}
                  target:
                    entity_id: "{{repeat.item.heating}}"
            - alias: Turn Off AC
              if:
                - condition: template
                  value_template: "{{states(repeat.item.cooling) != \"off\"}}"
                  alias: AC is not Off
              then:
                - service: switch.turn_on
                  data: {}
                  target:
                    entity_id: "{{repeat.item.x_fan}}"
                  alias: Turn On XFan
                - service: climate.turn_off
                  data: {}
                  target:
                    entity_id: "{{repeat.item.cooling}}"
                  alias: Turn Off AC
      else:
        - alias: Turn On Heating System and Turn Off AC
          if:
            - condition: template
              value_template: "{{states(repeat.item.status) != \"Alert\" }}"
              alias: Room Status is not Alert
          then:
            - alias: Turn On Heating System
              if:
                - condition: template
                  value_template: "{{states(repeat.item.manual_hvac) == \"off\"}}"
                  alias: Heating Manual Mode is OFF
              then:
                - service: climate.set_temperature
                  data:
                    temperature: 18
                    hvac_mode: heat
                  target:
                    entity_id: "{{repeat.item.heating}}"
                  alias: Set Heating Temperature to 18 Degrees
            - alias: Turn Off AC
              if:
                - condition: template
                  value_template: "{{states(repeat.item.manual_ac) == \"off\"}}"
                  alias: AC Manual Mode is Off
                - condition: template
                  value_template: "{{states(repeat.item.cooling) != \"off\"}}"
                  alias: AC is not Off
              then:
                - service: switch.turn_on
                  data: {}
                  target:
                    entity_id: "{{repeat.item.x_fan}}"
                  alias: Turn On XFan
                - service: climate.turn_off
                  data: {}
                  target:
                    entity_id: "{{repeat.item.cooling}}"
                  alias: Turn Off AC
          else:
            - alias: Turn Off Heating System
              if:
                - condition: template
                  value_template: "{{states(repeat.item.heating) != \"off\"}}"
                  alias: Heating is not OFF
              then:
                - alias: Turn Off Heating
                  service: climate.turn_off
                  data: {}
                  target:
                    entity_id: "{{repeat.item.heating}}"
            - alias: Turn Off AC
              if:
                - condition: template
                  value_template: "{{states(repeat.item.cooling) != \"off\"}}"
                  alias: AC is not Off
              then:
                - service: switch.turn_on
                  data: {}
                  target:
                    entity_id: "{{repeat.item.x_fan}}"
                  alias: Turn On XFan
                - service: climate.turn_off
                  data: {}
                  target:
                    entity_id: "{{repeat.item.cooling}}"
                  alias: Turn Off AC

So this can be closed,
Thanks

In the future, please indicate that in your first post or supply the entire automation. Otherwise we don’t have a clear picture of what the example represents.

For future reference, a State Condition doesn’t support templates. That’s why both of these State Conditions are invalid.

                - condition: state
                  entity_id: "{{repeat.item.status}}"
                  state: Alert
                  alias: Room Status

                  condition: state
                  entity_id: "{{repeat.item.manual_hvac}}"
                  state: "off"

That’s why it was necessary to convert them to Template Conditions.

In addition, you should avoid using direct references to an entity’s state value like this:

states.input_select.hvac_manual_heat_temperature.state

The reason for it is explained in the Warning box of this section of the documentation.

It’s preferable to use the states() function:

states('input_select.hvac_manual_heat_temperature')

You can also use the is_state() function to reduce this:

value_template: "{{states(repeat.item.manual_hvac) == \"off\"}}"

To this:

value_template: "{{ is_state(repeat.item.manual_hvac, 'off') }}"