Delay continue after deep sleep

Hello everyone, I am using ESPHome on an ESP32 for automation.

Every 15 minutes the ESP32 wakes up from deep_sleep and carries out measurements.
A switch should be triggered every 2 hours. Therefore I have a delay of 2 hours for the switch, a run_duration of 2 min for the measurements and a sleep_duration of 15 min.
The problem is that every time the ESP wakes up after 15 minutes, the delay of the switch starts all over again.

Is it possible that the delay continues to run during the deep_sleep so that the switch is only triggered after 2 hours (i.e. after 8 times deep_sleep mode)?

After 8 cycles you are at 2:16. 17 minutes each cycle. So even if the delay worked it would happen outside it’s on time. You could create a global number, increase that number by 1 each boot and after 8 reset the number and perform the action.

Something like

esphome:
  # ...
  on_boot:
    priority: 1000 #wait for every thing to start
    # ...
  then:
    - lambda: |-
        if (id(my_global_int) < 8) {
           id(my_global_int) += 1;
        } else {
          id(my_switch).turn_on();
          id(my_global_int) = 0;
        }
1 Like

Welcome.

Posting your config will help clarify what you have and help us help you. Also describing your use case a little more may help us suggest other ideas.

In addition to the approach Mikefila has suggested, you could also look at tracking the last switch time using a datetime sensor (you may need to confirm this survives deep sleep), and comparing that to the current time.

Datetime Component — ESPHome.

You may also be able to come up with an on_time trigger pattern which does the job.

Thank you very much for your response. I will try Mikefila’s suggestion.

The exact triggering of the switch after 2 hours is not important. When 2 hours have elapsed, the switch should simply trigger in the next cycle.

My goal is to control a watering system, which measures various values every 15 minutes but only activates the pump every 2 hours, of course, only when the limit values are reached. Since the control is autonomously powered by solar and battery, I need the deep sleep mode.

I have a script that should be started every 2 hours. The script triggers a check to see whether the limit value has been reached. If so, the switch of the pump is started.

My configuration looks like this so far:

esphome:
...
  on_loop:                                           # Starts scripts to verify if the soil moisture limit value has been reached
    then:
      - if:
          condition:
            - script.is_running: script_moisture
          else:
            - script.execute: script_moisture

...

script:
  id: script_moisture
  mode: single
  then:
    - delay: 15s                                 # Wait 15s to ensure that the new moisture value is measured
    - switch.turn_on: ckeck_moisture
    - delay: 1s
    - switch.turn_off: ckeck_moisture
    - delay: 7200s                              # Keep the script running for 2 hours. When the script is stopped, it gets restarted through the on_loop. 7200s => 2h

  - platform: gpio                                               
    pin: 27
    name: "Ckeck moisture"
    id: ckeck_moisture
    on_turn_on:
      - if:
          condition:
            lambda: |-
                if ((id(soil_moisture).state < id(limit_soil_moisture).state)){
                  return true;
                } else {
                  return false;
                }
          then:
            - switch.turn_on: Pumpe1
            - delay: !lambda 'return 1000 * id(runtime_pump1).state;'
            - switch.turn_off: Pumpe1


1 Like