Repeating Telldus 433 MHz device commands

I’m kind of new to automations and HA, so please advice better solutions

Since 433MHz is just one way, you never know if the device has been configured (switch turned an or off), and sometimes only half my house is turned off. To fix this I decided to create an automation that repeats the command a couple of times without having to create a separate automation for each switch.

The final automation looks like below with one initial config and additional conditional configurations with some time delays between:

The reason for time delays is that as far as I can tell (looking in the Telldus logs), Telldus are ignoring consecutive requests.

We also need to make sure that if you (once you’re in the loop) want to change the state of the switch. Then the previous loop needs to be inactivated so that it doesn’t create an internal loop that will re-trigger the automation. I solved this, by in the first activity in the loop, return false if I the state of the switch is not the same as the state I want to set. Here I would instead want to break the loop, but I haven’t understood how to do this.

alias: Repeat telldus
description: This automation repeats configurations sent to Telldus 433 MHz devices
trigger:
  - platform: state
    entity_id:
      - switch.TelldusSwitch1
      - switch.TelldusSwitch2
      - switch.TelldusSwitch3
      - switch.TelldusSwitch4
      - switch.TelldusSwitch5
      - switch.TelldusSwitchEtc
condition: []
action:
  - service: switch.turn_{{trigger.to_state.state}}
    enabled: true
    target:
      entity_id: "{{ trigger.entity_id }}"
  - delay: >-
      00:00:{{ '{:02d}'.format(range(1, 2) | random) }}.{{range(1, 999) |
      random}}
    enabled: true
  - repeat:
      count: 5
      sequence:
        - condition: template
          value_template: "{{ trigger.to_state.state == states[trigger.entity_id].state }}"
          enabled: true
        - service: switch.turn_{{trigger.to_state.state}}
          enabled: true
          target:
            entity_id: "{{ trigger.entity_id }}"
        - service: notify.notify
          enabled: false
          data:
            message: |-
              {{ repeat.index }} 
              {{ trigger.entity_id }} 
              {{trigger.to_state.state}}
            title: Telldus repeat diagnostics
            data: {}
        - delay: >-
            00:00:{{ '{:02d}'.format(range(1, 2) | random) }}.{{range(1, 999) |
            random}}
          enabled: true
mode: parallel
max: 50

I’m very interested in suggestions to make the code better and firstly a better solution to break the complete loop instead of just creating a false statement that will stop the current sequence