Can delay in automation be cancelled?

I have a problem with ESP32 modules connection - occasionally they disconnect from HA and mostly after a while they reconnect, but not always. I have yet to discover why this is happening, but for now i’ve set up LED THIS WAY for monitoring purposes. When module is offline in HA my LED blinks as it’s still connected to wifi,but it doesn’t ping, so i guess it’s kinda “stuck”. ESP32 is powered from router’s USB port, so first try is a separate power supply in case that USB is not powerfull enough.

But my main question is different: i’d like to do a reboot automation. “reboot_timeout” in esphome section doesn’t work because, as said, module thinks that it’s connected and doesn’t reboot module.

So i’d like to set up an interval to check api.connected,something like:

interval:
  - interval: 10s
    then:
      if:
        condition:
          api.connected:
        then:

        else:
          - delay: 2min
          - switch.turn_on: esp_module_reboot

What i wonder is: say that automation starts and delay begins countdown from 2min. If in the meantime api gets connected again will be automation cancelled or will reboot still happen? How to cancel automation progress? I guess i have to put something after my “then”, correct?

No it will not be cancelled as the condition happens previously to the delay.
so order is

  1. condition checked
  2. delay
  3. continue with other tasks

Add a second condition which checks once again on the api.connected after the delay.
This still makes the task to keep active even if a reconnect appears, but stops after the dleay.

Alternatively you could use a wait_for_trigger with a timeout of 2min:

Add a second condition which checks once again on the api.connected after the delay.
This still makes the task to keep active even if a reconnect appears, but stops after the dleay.

That’s a great idea, thanks! A second testing solves my question.

Tinkering with Script Sytnax in HA wouldn’t do much help, because esp module can’t be rebooted from HA if it’s not connected, except if you meant scripting inside ESPHome…i did some more research and i think it might be possible to do with scripts in ESPHome - supposely scripts can be stopped. But above simple solution will do just fine.

did you find any solution for your problem? I have the same one!

sorry I cannot follow - I did not have a problem here at all … just recommended possible solution to the thread owner :smiley:

Use a script - put delay & following action in a script.
And then You can call script.stop to cancel it.

interval:
  - interval: 10s
    then:
      if:
        condition:
          api.connected:
        then:
          - script.stop: delay_before_reboot
        else:
          - script.execute: delay_before_reboot

script:
  - id: delay_before_reboot
    mode: single
    then:
      - delay: 2min
      - switch.turn_on: esp_module_reboot
1 Like