Template in for each loop does not work

Hello,

I have created a script that has a for each loop in it. I would like to pass a field value to that for each loop but that doesn’t seem to work.

This code works.

fields:
  group_covers:
    description: group of covers to move
    example: cover.raffstores_eg_west
  group_position:
    description: Position of the cover
    example: 60
  group_tilt_position:
    description: Tilt position (optional)
    example: 50
alias: group_cover_position_tilt
sequence:
  - repeat:
      for_each: "{{ state_attr('cover.raffstores_eg_wintergarten', 'entity_id') }}"
      sequence:
        - action: script.turn_on
          target:
            entity_id: script.cover_position_tilt
          data:
            variables:
              entity_id: "{{ repeat.item }}"
              position: "{{ group_position }}"
              tilt_position: "{{ group_tilt_position }}"
    enabled: true
description: ""
icon: mdi:blinds-horizontal

But if I change the for each command so it uses the field value, the whole things stops working.

fields:
  group_covers:
    description: group of covers to move
    example: cover.raffstores_eg_west
  group_position:
    description: Position of the cover
    example: 60
  group_tilt_position:
    description: Tilt position (optional)
    example: 50
alias: group_cover_position_tilt
sequence:
  - repeat:
      # Next line is change compared to the version above
      for_each: "{{ state_attr('{{ group_covers }}', 'entity_id') }}"
      sequence:
        - action: script.turn_on
          target:
            entity_id: script.cover_position_tilt
          data:
            variables:
              entity_id: "{{ repeat.item }}"
              position: "{{ group_position }}"
              tilt_position: "{{ group_tilt_position }}"
    enabled: true
description: ""
icon: mdi:blinds-horizontal

Any idea how to get this working?
Thanks and best regards,
Tim

Instead of an entity ID, what you have provide to the state_attr function is the following string '{{ group_covers }}'… the function doesn’t know what to do with that.

  • Don’t nest templates, just use the variable in place of the entity ID.
  • Mind how you use quote marks.
fields:
  group_covers:
    description: group of covers to move
    example: cover.raffstores_eg_west
  group_position:
    description: Position of the cover
    example: 60
  group_tilt_position:
    description: Tilt position (optional)
    example: 50
alias: group_cover_position_tilt
sequence:
  - repeat:
      for_each: "{{ state_attr(group_covers, 'entity_id') }}"
      sequence:
        - action: script.turn_on
          target:
            entity_id: script.cover_position_tilt
          data:
            variables:
              entity_id: "{{ repeat.item }}"
              position: "{{ group_position }}"
              tilt_position: "{{ group_tilt_position }}"
    enabled: true
description: ""
icon: mdi:blinds-horizontal

FWIW, some users have had issues using Repeat for each with some brands of blinds if they don’t add a short delay in the Repeat sequence.

1 Like

Thank you so much! This is a perfect example why I love this forum. No bullshitting, just a straight forward solution.
Thanks for the tipp with the short delay, was necessary here as well.

Here my final code:

  fields:
    group_covers:
      description: group of covers to move
      example: cover.raffstores_eg_west
    group_position:
      description: Position of the cover
      example: 60
    group_tilt_position:
      description: Tilt position (optional)
      example: 50
  alias: group_cover_position_tilt
  sequence:
  - repeat:
      for_each: '{{ state_attr(group_covers, ''entity_id'') }}'
      sequence:
      - action: script.turn_on
        target:
          entity_id: script.cover_position_tilt
        data:
          variables:
            entity_id: '{{ repeat.item }}'
            position: '{{ group_position }}'
            tilt_position: '{{ group_tilt_position }}'
      - delay:
          hours: 0
          minutes: 0
          seconds: 1
          milliseconds: 0
    enabled: true
  description: ''
  icon: mdi:blinds-horizontal
1 Like