Nested Repeat for_each

I’m trying to understand nested repeat loops for a script I’m working on. When I try to nest a repeat for_each: loop inside another like this:

mode: parallel
alias: banana

sequence:
  - repeat:
      for_each:
        - "living_room1"
        - "kitchen1"
        - "office1"
      sequence:
        - delay:
            seconds: 1
        - service: light.turn_off
          data:
            transition: 0
          target:
            entity_id: light.office_lamp
        - service: light.turn_on
          data:
            transition: 0
          target:
            entity_id: light.office_lamp
        - repeat:
            for_each:
              - "living_room2"
              - "kitchen2"
              - "office2"
          sequence:
            - delay: 
                seconds: 1
            - service: light.turn_off
              data:
                transition: 0
              target: 
                entity_id: light.office_lamp
            - service: light.turn_on
              data:   
                transition: 0
              target: 
                entity_id: light.office_lamp


I get the error:

2024-03-29 06:08:45.430 ERROR (MainThread) [homeassistant.components.script] Script with alias 'banana' could not be validated and has been disabled: extra keys not allowed @ data['sequence'][0]['repeat']['sequence'][3]['sequence']. Got [{'delay': {'seconds': 1}}, {'service': 'light.turn_off', 'data': {'transition': 0}, 'target': {'entity_id': 'light.office_lamp'}}, {'service': 'light.turn_on', 'data': {'transition': 0}, 'target': {'entity_id': 'light.office_lamp'}}]
required key not provided @ data['sequence'][0]['repeat']['sequence'][3]['repeat']['sequence']. Got None

After commenting out the nested loop the script validates and runs fine, flashing my light three times.

mode: parallel
alias: banana

sequence:
  - repeat:
      for_each:
        - "living_room1"
        - "kitchen1"
        - "office1"
      sequence:
        - delay:
            seconds: 1
        - service: light.turn_off
          data:
            transition: 0
          target:
            entity_id: light.office_lamp
        - service: light.turn_on
          data:
            transition: 0
          target:
            entity_id: light.office_lamp
        #- repeat:
        #    for_each:
        #      - "living_room2"
        #      - "kitchen2"
        #      - "office2"
        #  sequence:  
        #    - delay: 
        #        seconds: 1
        #    - service: light.turn_off
        #      data:
        #        transition: 0
        #      target:
        #        entity_id: light.office_lamp
        #    - service: light.turn_on
        #      data:  
        #        transition: 0
        #      target:
        #        entity_id: light.office_lamp

Why is my nested loop invalid? What have I missed in the documentation?

This script is just for debugging and understanding the issue. In the real script I’m working on, the repeat for_each: at line 1091 is nested inside the repeat for_each: started at line 930. So I know it’s possible to do, but I can’t get add a third nest loop. I must be missing something, so I’m trying to simply the problem with this script.

…and while you’re here, do you happen to know how to reference the outer repeat.item from inside the second loop? Would {{ super() }} give me “living_room1” or is it something like {{ super.repeat.item }} to get that?

The sequence of the second repeat needs to be indented to the same level as its corresponding for_each.

1 Like

That’s embarrassing. Spent 4hrs on that.

Define it as a new variable in the outer loop.

mode: parallel
alias: banana

sequence:
  - repeat:
      for_each:
        - "living_room1"
        - "kitchen1"
        - "office1"
      sequence:
        - delay:
            seconds: 1
        - service: light.turn_off
          data:
            transition: 0
          target:
            entity_id: light.office_lamp
        - service: light.turn_on
          data:
            transition: 0
          target:
            entity_id: light.office_lamp
        - variables:
            outer: "{{ repeat.item }}"
        - repeat:
            for_each:
              - "living_room2"
              - "kitchen2"
              - "office2"
            sequence:
              - service: light.turn_on
                data:
                  transition: foo
                  brightness: bar
                target:
                  entity_id: "light.{{ outer }}"
              - delay: 
                  seconds: 1
              - service: light.turn_off
                data:
                  transition: 0
                target: 
                  entity_id: light.office_lamp
              - service: light.turn_on
                data:   
                  transition: 0
                target: 
                  entity_id: light.office_lamp
1 Like

Right; that’s simple. Thank you for the help!