How to get sequence index

Hello

How to get the index of a sequence and use it in a template?

alias: My script
sequence:
  - action: notify.persistent_notification
    metadata: {}
    data:
      message: "index: {{?}}"
  - action: notify.persistent_notification
    metadata: {}
    data:
      message: "index: {{?}}"
  - action: notify.persistent_notification
    metadata: {}
    data:
      message: "index: {{?}}"

AFAIK, that’s not available. What are you trying to do?

Script that runs 20 other scripts and field with which script to start with.

example:

alias: My script
sequence:
{# if start_with = 1 then
  - action: script.my_script
    metadata: {}
    data: {}
{# if start_with = 2 then
  - action: script.my_script
    metadata: {}
    data: {}
{# if start_with = 3 then
  - action: script.my_script
    metadata: {}
    data: {}
fields:
  start with:
    selector:
      number:
        min: 1
        max: 100
    name: Start with

I could use variables and increments, but after changing the position of the scripts in the main script I have to rewrite everything.

Do the scripts’ entity IDs or friendly names contain their “index”?

If so, you could do a Repeat for each by templating the for_each:

- repeat:
    for_each: "{{ range(start_with: 21) | list }}"
    sequence:
      - action: script.my_script_{{repeat.item}}
        metadata: {}
        data: {}

Or, if not, move the list of scripts in the order you want into a variable then do something like:

- variables:
    script_list:
      - script.my_script_a
      - script.my_script_b
      - script.my_script_c
      - script.my_script_d
- repeat:
    for_each: "{{ script_list[start_with-1:] }}"
    sequence:
      - action: "{{repeat.item}}"
        metadata: {}
        data: {}

No. The same script may be used multiple times or not used

I think it would be best to create helper, iterate in scripts and if ‘index’ < ‘counter.start_witch’ then stop sequence.