Wake from deep sleep at a specified time?

I might be missing something simple, but is there a way to have an ESP32 wake from deep sleep at a specific time each day? I am able to set a timed wakeup by pressing a button using this code, but is there a way to eliminate the need for the button press?

deep_sleep:
  id: deepsleep1
  run_duration: 
    default: 2h
    gpio_wakeup_reason: 5min
  wakeup_pin:
    number: 0
    inverted: true

time:
  - platform: homeassistant
    id: ntp

binary_sensor:

  - platform: gpio
    name: "Button 2"
    pin: 
      number: GPIO21
      inverted: true
    internal: false
    on_click: 
      then:
        - logger.log: "Entering Deep sleep until 10am"
        - deep_sleep.enter: 
                id: deepsleep1 
                until: "10:00:00"
                time_id: ntp

Read the documentation.

1 Like

So do you want to wake up at 10am for 5min and then go back to sleep?

One way to do it would be to trigger your deep_sleep.enter action you have there at 10:05am.

Or read the documentation so they familiarize themselves with all of deep_sleep and using the docs as a resource. They have examples that do exactly what this person is looking for and it sure doesnt look like they’ve even looked at the docs so, maybe its time they do.

thank you. i actually was trying to accomplish two tasks:
1, get the esp32 to exit deep sleep at 10am, stay awake for two hours, and then go back into deep sleep, no matter when the ESP 32 was initially powered on.
2. Wake when the button is pressed for 5 minutes, and then go into deep sleep until 10 am.

I was able to accomplish #2 with the button action by reading the deep sleep documentation, but was asking if the “until: 10:00:00” could also be incorporated into the deep_sleep section so that if the ESP32 is powered on at some arbitrary time, it would stay on for two hours (the default run duration) and then go into deep sleep until 10am.

My understanding (perhaps incorrect) after reading the time component instructions is that it would involve calling deep.sleep.enter with wakeup at 10 at some interval, but if that call was when the esp32 was in deep sleep (perhaps because it is past the 2 hour default run time) it would not be run and esp 32 would never wake up.

I suppose I could remove the default run time, and have time component run the enter deep sleep until 10 at noon, but that would mean that if the esp32 was powered on at 1pm, it would run for 23 hours before deep sleeping, and was hoping for a more elegant solution if one was possible.

I probably was not clear enough about what I was trying to accomplish, but I did read both the time component and deep sleep instructions and did not find a solution.

Yes, sort of.
An ESP awakens when the RST pin is pulled low. (It reboots, actually). The clock on the ESP still runs so you can set the sleep time for up to 4294967296 microseconds, about 71 minutes.

For longer times you need an external RTC, like the DS3231. You connect the output of the RTS to the reset pin of the ESP. I’ve never done this so I can’t help you with programming the RTC.

on_boot: you could wait_until: an uptime: sensor is 2hrs, then sleep until 10AM.

Remove the default duration.

Keep your existing button stuff.

I think that would work.

Might not work quite right for some edge cases.

Thank you. I hadn’t thought of using the on_boot to set the deep sleep. Too focused on trying to shoehorn a wake until into the deep_sleep component.

Thanks!

And, how would you do this?

There is an RTC in the ESP32, but I have seen nothing in the ESP documentation for setting the time.

Using sleep until.

Again, I have seen nothing in the ESP documentation to set the current time on the RTC.

No need for a RTC if the time can get sourced via network (ha server, ntp, …)

1 Like

Yes like Indeed said, I typically fetch time from Home Assistant or SNTP.

Acknowledge that wasn’t explict in my earlier response, but the deep_sleep.enter with until: docs example gives some hint towards the time_id: sntp_id.

https://esphome.io/components/time/sntp.html

https://esphome.io/components/time/homeassistant.html

That’s fine for Home Assistant, but the ESPHome device doesn’t need Home Assistant to function. Besides, if it’s asleep, how is it going to respond to an API call?

You can use sntp time if you don’t want a dependency on Home Assistant (I have some devices running at friends homes like that which need time).

If you don’t want a dependency on the internet, you might need some more hardware.
https://esphome.io/components/time/ds1307.html

When it’s asleep indeed it will not respond to an api call.

Time sync happens when awake.

Isn’t this exactly what I have been saying?