Repeat loop with a delay time condition

Hi guy, i have repeat loop until and i want to add to self stop after a number of time.
it just does the toggle one time, any idea why my script aint work?

alias: Test timer
description: ""
triggers:
  - trigger: event
    event_type: ""
    context:
      user_id:
        - (hidden)
conditions: []
actions:
  - repeat:
      sequence:
        - type: toggle
          device_id: (hidden)
          entity_id: (hidden)
          domain: switch
        - delay:
            hours: 0
            minutes: 0
            seconds: 5
            milliseconds: 0
      until:
        - condition: template
          value_template: "{{ now() >= stop_time }}"
mode: single
variables:
  stop_time: "{{ now() + timedelta(seconds=20) }}"

I think you need to declare your variable at the beginning of the action block.

i got this error while i try to put it in the actions bloc :
Message malformed: expected a dictionary for dictionary value @ data[‘actions’][0][‘variables’]

alias: Test timer
description: ""
triggers:
  - trigger: event
    event_type: ""
    context:
      user_id:
        - xxx
conditions: []
actions:
  - variables:
    stop_time: "{{ now() + timedelta(seconds=20) }}"
  - repeat:
      sequence:
        - type: toggle
          device_id: xxx
          entity_id: xxx
          domain: switch
        - delay:
            hours: 0
            minutes: 0
            seconds: 5
            milliseconds: 0
      until:
        - condition: template
          value_template: "{{ now() >= stop_time }}"
mode: single

Indentation?

  - variables:
      stop_time: "{{ now() + timedelta(seconds=20) }}"

i got no error but still only toggle 1 time

What does the automation trace tell you?

Use “repeat count”. The combination of the 5 second delay and 4 iterations provides you with a 20 second “self stop” (with less code than “repeat until”).

alias: Test timer
description: ""
triggers:
  - trigger: event
    event_type: ""
    context:
      user_id:
        - (hidden)
conditions: []
actions:
  - repeat:
      count: 4
      sequence:
        - type: toggle
          device_id: (hidden)
          entity_id: (hidden)
          domain: switch
        - delay:
            seconds: 5
mode: single

Hi, no I can’t, I want to use it in another script with a multiple condition in a until loop.

In fact, if the humidity of x >= 50% or if the automatization work for more than 2h that mean water is running out so I don’t want to use my switch by doing on and off cycle when I don’t have any more water.

I was testing the function on this script because I don’t want to wait 2h to know if it is working or not.
Here is the script maybe that can help you have an idea what to do for my exact situation.

alias: Humidifier With Outdoor Weather
description: Humidifier With Outdoor Weather
triggers:
  - entity_id:
      - sensor.thermometre_chambre_humidity
    below: sensor.desired_humidity_min_with_outdoor_weather
    trigger: numeric_state
conditions: []
actions:
  - type: turn_on
    device_id: xx
    entity_id: xx
    domain: switch
  - delay:
      hours: 0
      minutes: 15
      seconds: 0
      milliseconds: 0
  - type: turn_off
    device_id: xx
    entity_id: xx
    domain: switch
  - delay:
      hours: 0
      minutes: "{{ delay_off }}"
      seconds: 0
      milliseconds: 0
  - repeat:
      sequence:
        - type: turn_on
          device_id: xx
          entity_id: xx
          domain: switch
        - delay:
            hours: 0
            minutes: "{{ delay_on }}"
            seconds: 0
            milliseconds: 0
        - type: turn_off
          device_id: xxx
          entity_id: xxx
          domain: switch
        - delay:
            hours: 0
            minutes: "{{ delay_off }}"
            seconds: 0
            milliseconds: 0
      until:
        - condition: numeric_state
          entity_id: sensor.thermometre_chambre_humidity
          above: sensor.desired_humidity_max_with_oudoor_weather
mode: single
variables:
  delay_on: 7
  delay_off: 5

It’s inadvisable to create an automation that spends a long time executing its actions.

If Home Assistant restarts while an automation is busy executing its actions, the automation is immediately terminated (any remaining actions are not executed)

For example, let’s say we have an automation that opens a valve, waits for time to pass (minutes or hours), then closes the valve. If the automation is terminated while it is busy (waiting for time to pass), the valve will be left open. On restart, Home Assistant doesn’t resume any automations that were terminated, so the valve remains open.

To ensure an automation is robust and able to handle failures in a safe manner, it should spend very little time executing its actions. Creating such a failure-resistant automation becomes more challenging because it must become more event-based (for example, detecting wherever Home Assistant is restarted) and rely on timer entities, which can be resumed after a restart, instead of delay statements within a repeat.

FWIW, I had created an automation to control my irrigation system and if took just one failure (and the valve was left open for several hours) to make me redesign it to be more fail-safe.

Thank you for the advice, but my humidifier has a self-cut when it empty… because it was a humidifier do(stop when it is empty…), what it doesn’t do it manage the absolute humidity. So i have done a template for this purpose based on my room temperature.

What i don’t want is, when my humidifier is off because it runs out of water, i don’t want my smart switch nonstop to toggle for no reason.

And because my humidifier always reaches my humidity in a certain time, i want it to self stop if it reaches the humidity or after a delay.

Anyone can help with this function?

After checking all the documentation i found the problem and i find a way to bypass it.
So here we go.

Time
now(), time_since(), time_until(), today_at(), and utcnow() are not supported in limited templates.

That mean cannot be used in a template condition.
So i have finally use the timestamp that it actually works.
Here is the working code.

alias: Test timer
description: ""
triggers:
  - trigger: event
    event_type: ""
    context:
      user_id:
        - xx
conditions: []
actions:
  - repeat:
      sequence:
        - type: toggle
          device_id: xx
          entity_id: xx
          domain: switch
        - delay:
            hours: 0
            minutes: 0
            seconds: 5
            milliseconds: 0
      until:
        - condition: template
          value_template: "{{ as_timestamp(now()) >= stop_time }}"
mode: single
variables:
  stop_time: "{{ as_timestamp(now()) + 40 }}"