The best way of automation or script to check if something is running, if not, run the script again?

I have my broadlink rm4 run commands to turn on the window fan via IR since simply turning it on via smart switch won’t turn it back on. It has to run through many commands (turn on, wait 2 seconds, fan speed up twice).

Unfortunately, it’s not 100% in turning it on initially, for some reason. Running this script a couple more times does eventually get it to turn on correctly. I’ve tried to have the script wait 30 seconds after running the broadlink commands to see if power consumption is over 20W based on the smart switch that it’s on, and if not, rerun the script. Doing this causes an infinite loop and causes HA to eventually crash when I try to view the trace. Is there a better way to have HA check 30 seconds after a script is run if something is running, if not, rerun the script? I’ve also tried to implement something similar in the automation and also causes issues with repeating the script even if power consumption was under 20W (based on traces)

The script I tried:

    alias: Vornado Window Pull In
    sequence:
      - type: turn_off
        entity_id: switch.bedroom_window_fan_on_off
        domain: switch
      - delay:
          hours: 0
          minutes: 0
          seconds: 5
          milliseconds: 0
      - type: turn_on
        entity_id: switch.bedroom_window_fan_on_off
        domain: switch
      - delay:
          hours: 0
          minutes: 0
          seconds: 15
          milliseconds: 0
      - service: remote.send_command
        data:
          device: Vornado Window Fan
          command: power
          hold_secs: 5
        target:
          entity_id: remote.bedroom_broadlink_remote
      - delay:
          hours: 0
          minutes: 0
          seconds: 2
          milliseconds: 0
      - service: remote.send_command
        data:
          device: Vornado Window Fan
          command: speed_up
          num_repeats: 3
          delay_secs: 0.5
        target:
          entity_id: remote.bedroom_broadlink_remote
      - delay:
          hours: 0
          minutes: 0
          seconds: 30
          milliseconds: 0
      - condition: numeric_state
        entity_id: sensor.bedroom_window_fan_electrical_measurement
        below: '20'
      - service: script.vornado_window_fan_on_pull
    mode: single
    icon: mdi:fan-chevron-up

You should be able to use a “Repeat Until” action, but you may need to play around with the delay time after the broadlink signal is sent.

alias: Vornado Window Pull In
sequence:
  - type: turn_off
    entity_id: switch.bedroom_window_fan_on_off
    domain: switch
  - delay: 5
  - type: turn_on
    entity_id: switch.bedroom_window_fan_on_off
    domain: switch
  - delay: 15
  - alias: "Repeat the broadlink power sequence UNTIL above 20W"
    repeat:
      sequence:
        - service: remote.send_command
          data:
            device: Vornado Window Fan
            command: power
            hold_secs: 5
          target:
            entity_id: remote.bedroom_broadlink_remote
        - delay: 5     ### This is the one you might need to adjust 
      until:
        - condition: numeric_state
          entity_id: sensor.bedroom_window_fan_electrical_measurement
          above: '20'
  - service: remote.send_command
    data:
      device: Vornado Window Fan
      command: speed_up
      num_repeats: 3
      delay_secs: 0.5
    target:
      entity_id: remote.bedroom_broadlink_remote
mode: single
icon: mdi:fan-chevron-up

thanks! I actually did try repeat until before but I think I had didn’t either have a delay long enough or in the wrong spot, so it would keep repeating the actions (likely because the smart plug takes some time to update the power usage and report back to HA). I added a delay of 30 seconds to the end of the repeat and now it seems to work great based on your suggestion!

The script:

alias: Vornado Window Pull In Test
sequence:
  - type: turn_off
    entity_id: switch.bedroom_window_fan_on_off
    domain: switch
  - delay:
      hours: 0
      minutes: 0
      seconds: 5
      milliseconds: 0
  - type: turn_on
    entity_id: switch.bedroom_window_fan_on_off
    domain: switch
  - delay:
      hours: 0
      minutes: 0
      seconds: 15
      milliseconds: 0
  - repeat:
      until:
        - condition: numeric_state
          entity_id: sensor.bedroom_window_fan_electrical_measurement
          above: '20'
      sequence:
        - service: remote.send_command
          target:
            entity_id: remote.bedroom_broadlink_remote
          data:
            device: Vornado Window Fan
            command: power
            hold_secs: 5
        - delay:
            hours: 0
            minutes: 0
            seconds: 2
            milliseconds: 0
        - service: remote.send_command
          target:
            entity_id: remote.bedroom_broadlink_remote
          data:
            device: Vornado Window Fan
            command: speed_up
            num_repeats: 3
            delay_secs: 0.5
        - delay:
            hours: 0
            minutes: 0
            seconds: 30
            milliseconds: 0
mode: single
icon: mdi:fan-chevron-up