Unable to use repeat.item in a conditional

I’m trying to set up a pretty basic script to control the heat settings on my minisplits. I’m trying to use the repeat for each syntax to loop through my mini splits to perform a conditional check and if it passes, set the temperature. However when i run the script i get an “Invalid action” error.

Entity climate.{{ repeat.item }} is neither a valid entity ID nor a valid UUID for dictionary value @ data[0]['repeat']['sequence'][0]['if'][0]['entity_id']

This seems to be a quirk of the conditional block as if i skip the conditional and just set the temp, repeat.item is expanded as expected. I could work around this just by not using a loop but it would be nice to be able to. The conditional is kind of non negotiable because my LG minisplits make a very loud chirp anytime a setting is changed that cant be disabled. Full repeat block yaml below:

repeat:
  for_each:
    - computer_minisplit
  sequence:
    - if:
        - condition: numeric_state
          entity_id: climate.{{ repeat.item }}
          attribute: temperature
          below: 72
      then:
        - action: climate.set_temperature
          metadata: {}
          target:
            entity_id: climate.{{ repeat.item }}
          data:
            temperature: 72
            hvac_mode: heat

Most types of triggers and conditions do not allow templates, or allow them only in specific configuration keys. For Numeric State conditions (and triggers) templates are only supported in the value_template configuration key. To do what you are trying to do, you will need to use a Template condition.

repeat:
  for_each:
    - computer_minisplit
  sequence:
    - variables:
        entity: "{{ 'climate.'~ repeat.item }}"
    - if:
        - condition: template
          value_template: |
            {{ state_attr( entity, "temperature") | float(0) < 72 }}
      then:
        - action: climate.set_temperature
          metadata: {}
          target:
            entity_id: "{{ entity }}"
          data:
            temperature: 72
            hvac_mode: heat

Well that resolves it. I was messing around with value_templates but never would have thought I needed to manually reassign the repeat.item variable to another variable

It isn’t actually necessary to set a script variable, it’s just less repetitive. You can repeat the "climate."~ repeat.item if you want to:

repeat:
  for_each:
    - computer_minisplit
  sequence:
    - if:
        - condition: template
          value_template: |
            {{ state_attr("climate."~ repeat.item, "temperature") | float(0) < 72 }}
      then:
        - action: climate.set_temperature
          metadata: {}
          target:
            entity_id: "{{ 'climate.'~ repeat.item }}"
          data:
            temperature: 72
            hvac_mode: heat

Why is the tilde needed in "climate."~ repeat.item?

The tilde is the preferred operator for concatenation. You could do it the way you did originally for the entity_id value in the action, but in the condition’s template you need to do the concatenation inside the delimiters.

1 Like