Error using repeat..for_each in a script

Hello,

I’m attempting to use “for_each” in a script which was introduced in the 2022.5 release, I’m using 2022.6.5.

As “for_each” isn’t available as a repeat type in the visual editor, I’m using the example from here Script Syntax - Home Assistant in the script YAML editor as a starting point. Although the YAML editor doesn’t show any formatting errors, I get the following error when I save the script

Message malformed: extra keys not allowed @ data[‘repeat’]

Here’s my test script

alias: For Each Test
mode: single
repeat:
  for_each:
    - "living_room"
    - "kitchen"
    - "office"
  sequence:
    - service: light.turn_off
      target:
        entity_id: "light.{{ repeat.item }}"

I’ll be grateful for any suggestions please?

thanks in advance

Paul

If that is a script it needs to be formatted like this:

script:
  for_each_test:
    alias: For Each Test
    mode: single
    sequence: ### <------ You are missing this
      - repeat:
          for_each:
            - "living_room"
            - "kitchen"
            - "office"
          sequence:
            - service: light.turn_off
              target:
                entity_id: "light.{{ repeat.item }}"

There’s no need for a “for each” to do this though. You can just do:

script:
  lights_off:
    alias: Lights Off
    mode: single
    sequence:
      - service: light.turn_off
        target:
          entity_id:
            - light.living_room
            - light.kitchen
            - light.office
1 Like

Sorry for disturbing again regarding this topic.
Actually I understood everything and now I wonder, whether there is an error in Studio Code Server? When I type code like this:


licht_hell_aufwachen:
  sequence:
    - repeat:
        for_each:
          - "flur_group_deconz"
          - "kueche_group_deconz"
          - "gaestezimmer"
          - "toilette_2"
        sequence:
          - service: light.turn_on
            target:
              entity_id:
                - light.{{ repeat.item }}
            data_template:
              brightness_pct: 100
  icon: mdi:lightbulb-auto

I get an error that this would be an incorrect type. Expected “object“. The script is working, though.