(Solved) Can't get automation to work with {{ repeat.index <= 5 }}

This is my automation, that’ll trigger some events after a door has been left open.

alias: Melding - Deuren te lang open
description: ''
trigger:
  - platform: state
    to: 'on'
    for:
      hours: 0
      minutes: 0
      seconds: 0
      milliseconds: 0
    from: 'off'
    entity_id:
      - binary_sensor.koelkast_vriezer_deur_contact
      - binary_sensor.voordeur_deur_contact
      - binary_sensor.achterdeur_deur_contact
condition: []
action:
  - delay:
      hours: 0
      minutes: 1
      seconds: 0
      milliseconds: 0
  - choose:
      - conditions:
          - condition: state
            entity_id: binary_sensor.koelkast_vriezer_deur_contact
            state: 'on'
        sequence:
          - repeat:
              while:
                - condition: state
                  entity_id: binary_sensor.koelkast_vriezer_deur_contact
                  state: 'on'
              sequence:
                - service: notify.telegram
                  data:
                    title: WAARSCHUWING!!!
                    message: >-
                      Waarschuwing! De {{trigger.to_state.name}} staat al enige
                      tijd open.
                - service: tts.cloud_say
                  data:
                    entity_id: media_player.woonkamer
                    message: >-
                      Waarschuwing! De {{trigger.to_state.name}} staat al enige
                      tijd open.
                - delay:
                    hours: 0
                    minutes: 1
                    seconds: 0
                    milliseconds: 0
      - conditions:
          - condition: state
            entity_id: binary_sensor.voordeur_deur_contact
            state: 'on'
        sequence:
          - repeat:
              while:
                - condition: state
                  entity_id: binary_sensor.voordeur_deur_contact
                  state: 'on'
              sequence:
                - service: notify.telegram
                  data:
                    title: WAARSCHUWING!!!
                    message: >-
                      Waarschuwing! De {{trigger.to_state.name}} staat al enige
                      tijd open.
                - service: tts.cloud_say
                  data:
                    entity_id: media_player.woonkamer
                    message: >-
                      Waarschuwing! De {{trigger.to_state.name}} staat al enige
                      tijd open.
                - delay:
                    hours: 0
                    minutes: 1
                    seconds: 0
                    milliseconds: 0
      - conditions:
          - condition: state
            entity_id: binary_sensor.achterdeur_deur_contact
            state: 'on'
        sequence:
          - repeat:
              while:
                - condition: state
                  entity_id: binary_sensor.achterdeur_deur_contact
                  state: 'on'
              sequence:
                - service: notify.telegram
                  data:
                    title: WAARSCHUWING!!!
                    message: >-
                      Waarschuwing! De {{trigger.to_state.name}} staat al enige
                      tijd open.
                - service: tts.cloud_say
                  data:
                    entity_id: media_player.woonkamer
                    message: >-
                      Waarschuwing! De {{trigger.to_state.name}} staat al enige
                      tijd open.
                - delay:
                    hours: 0
                    minutes: 1
                    seconds: 0
                    milliseconds: 0
    default: []
mode: single

But when I add:

          - condition: template
            value_template: '{{ repeat.index <= 5 }}'

or:

          - condition: template
            value_template: "{{ repeat.index <= 5 }}"

after:

          - condition: state
            entity_id: binary_sensor.koelkast_vriezer_deur_contact
            state: 'on'

It won’t run the vriezer-path when I open the freezer to log, but it’ll run the default-path.
I think I’ve followed the example on Script Syntax documentation. The trace-functionality has provided me with enough help in dealing with other issues, but this I could not solve and any help would be appreciated.

Can you paste the entire automation with the added repeat indices?

You need to switch you <= to >=… The repeat index is 0, which is less than (<) 5, so it stops like you told it to.

EDIT: Ignore this… for some reason I mistook it as a repeat-until

For a while loop that’s the way it should operate. If loop index (0) is <= 5 keep repeating.

I just copy/pasted it from the documentation and changed the number, if your solution works then the documentation is faulty.

            # Don't do it too many times
            - condition: template
              value_template: "{{ repeat.index <= 20 }}"

I found the error, the “repeat.index” has to be within the loop.

alias: Melding - Deuren te lang open
description: ''
trigger:
  - platform: state
    to: 'on'
    for:
      hours: 0
      minutes: 0
      seconds: 0
      milliseconds: 0
    from: 'off'
    entity_id:
      - binary_sensor.koelkast_vriezer_deur_contact
      - binary_sensor.voordeur_deur_contact
      - binary_sensor.achterdeur_deur_contact
condition: []
action:
  - delay:
      hours: 0
      minutes: 1
      seconds: 0
      milliseconds: 0
  - choose:
      - conditions:
          - condition: state
            entity_id: binary_sensor.koelkast_vriezer_deur_contact
            state: 'on'
        sequence:
          - repeat:
              while:
                - condition: state
                  entity_id: binary_sensor.koelkast_vriezer_deur_contact
                  state: 'on'
                - condition: template
                  value_template: '{{ repeat.index <= 5 }}'
              sequence:
                - service: notify.telegram
                  data:
                    title: WAARSCHUWING!!!
                    message: >-
                      Waarschuwing! De {{trigger.to_state.name}} staat al enige
                      tijd open.
                - service: tts.cloud_say
                  data:
                    entity_id: media_player.woonkamer
                    message: >-
                      Waarschuwing! De {{trigger.to_state.name}} staat al enige
                      tijd open.
                - delay:
                    hours: 0
                    minutes: 1
                    seconds: 0
                    milliseconds: 0
      - conditions:
          - condition: state
            entity_id:
              - binary_sensor.voordeur_deur_contact
              - binary_sensor.achterdeur_deur_contact
            state: 'on'
        sequence:
          - repeat:
              while:
                - condition: state
                  entity_id:
                    - binary_sensor.voordeur_deur_contact
                    - binary_sensor.achterdeur_deur_contact
                  state: 'on'
                - condition: template
                  value_template: '{{ repeat.index <= 3 }}'
              sequence:
                - service: notify.telegram
                  data:
                    title: WAARSCHUWING!!!
                    message: >-
                      Waarschuwing! De {{trigger.to_state.name}} staat al enige
                      tijd open.
                - service: tts.cloud_say
                  data:
                    entity_id: media_player.woonkamer
                    message: >-
                      Waarschuwing! De {{trigger.to_state.name}} staat al enige
                      tijd open.
                - delay:
                    hours: 0
                    minutes: 1
                    seconds: 0
                    milliseconds: 0
    default: []
mode: single