How to use the repeat index outside the loop?

How I save the index in a variable to use after “repeat”?

      - repeat:
            - service: scene.turn_on
              data: {}
                entity_id: scene.alarm_external
            - delay:
                seconds: 5
            - condition: template
              value_template: >-
                {{ is_state('switch.alarm_state', 'off') }}
            - condition: template
              value_template: "{{ repeat.index <= 5 }}"
      - service: notify.alexa_media_echo_room
          message: The alarm was armed in {{ attempts_variable }} attempts.
            type: tts

Variables are local to the block in which they are used. To store the variable for use outside the loop you would have to use a helper, e.g. input_number.


If there is more than one script running simultaneously, one helper per script will be needed… Is that right?

Yeah and it obviously would not work if the script was run in parallel mode.

If you have an MQTT broker you could also store it in an mqtt topic.

There was a post about storing stuff temporarily in template sensor attributes too but I can’t find it.

EDIT: Here,

1 Like

Great! Thank you very much.