How to use the repeat index outside the loop?

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

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

See: https://www.home-assistant.io/docs/scripts#scope-of-variables

Thanks!
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, https://community.home-assistant.io/t/wth-cant-input-helpers-be-read-only-in-ui/467562/13?u=tom_l

1 Like

Great! Thank you very much.