Trying to understand deepsleep options

Is it for example, possible to make a ESP deepsleep from

Sleep: 07:00
Wake: 19:00

then again,

Sleep 00:00
Wake: 05:00

and, would be be possible to just set one set of wake and sleep instructions based on time, kind of like sunset / sunrise.

Sleep: 07:00
Wake: 19:00

Thanks!

Well, ESP32 can be scheduled to wake up at specific time. And you can use the sun component.

To enter deep sleep at sunset is pretty easy. You just have to get sunrise time and replace the placeholder.

time:
  - platform: homeassistant
    id: ha_time

sun:
 # your location
  latitude: 48.8584°
  longitude: 2.2945°

  on_sunset:
    then:
      - deep_sleep.enter:
          id: deep_sleep_1
          until: <sunrise>
          time_id: ha_time

deep_sleep:
  id: deep_sleep_1

Wow, thanks this is exactly what I was looking for.

So, just as your code is there it will work? Obv change the long and lat.

How does the ESP32 know when the sun is set or not? Just based on longitude and latitude?

Thanks again!

Actually I did not test it, since not at PC. But due to thedocs, it should work. I just look up a solution to get the sunrise.

The Esphome component just needs coordinates to calculate sun position. More it not necessary.

Ok im testing now. The only part that the editor is complaining about is

until: <sunrise>

Doesnt like the sunrise bit.

Ok, I added a modification that should insert sunrise as wakeup time. Hope it compiles, since I could but test it.

time:
  - platform: homeassistant
    id: ha_time

sun:
 # your location
  latitude: 48.8584°
  longitude: 2.2945°
  id: sun_id

  on_sunset:
    then:
      - deep_sleep.enter:
          id: deep_sleep_1
          until: !lambda |-
                      auto sunrise = id("sun_id").sunrise();
                      if (!sunrise.has_value())
                        return "00:00:00;
                      char str[9];
                      return strftime(str, sizeof(str), "%H:%M:%S", sunrise.value().timestamp);
          time_id: ha_time

deep_sleep:
  id: deep_sleep_1

x is the current sun component and the lambda function should insert the sunrise time as timestamp for wakeup.

Ok im testing now. The only part that the editor is complaining about is…

Yes, I asked you to replace this placeholder. :wink:

But I think/hope, I found a solution to insert the value.

Maybe indentation is out?

image

Hmm, ok. I fixed one mistake. You need a string for until - not a timestamp. Can you change to corrected version and try to compile?

Same issue :frowning:

Something is not happy :stuck_out_tongue_winking_eye:

Ok, give me a moment. And a semicolon is missing after auto sunrise = x.sunrise().
I check indentation.

Are you using esp32 or esp8266? Later won’t work and show this error.

This is on a ESP32 devkit board.

Did you try with semicolon added and the lambda source intended 2 spaces below the !lambda?

I tried to correct indentation, but on mobile it’s pretty hard. Now it’s more than 2 spaces. :grin:

I can play with the indentation and make it better, but something, usually on 1 thing always ends up being incorrect and preventing compiling.

okay. So when you replace the whole until lambda with:

until: "06:00:00"

It should work? Then you device deep sleeps at sunset till 6:00am.

Maybe somebody else can get the sunrise part for wake up getting fixed. Or tomorrow when I have compiler and tools.

Yea no worries, thats a good starting point anyway. I can do some testing and work from there.

Would be good to get the Sunset and Sunrise part working but at least I can play and test now. Definitely much further off when I started.

Thanks for your efforts much appreciated!

I’ve compiled and updates that one.

So if im correct, when I upload it, it should just go to sleep and become offline and unreachable in HA?

The code:

  ap:
    ssid: "Rf-Transmitter Fallback Hotspot"
    password: "dDlGa9Mi8cXf"

captive_portal:
    
time:
  - platform: homeassistant
    id: ha_time

sun:
 # your location
  latitude: -36.848461
  longitude: 174.763336

  on_sunset:
    then:
      - deep_sleep.enter:
          id: deep_sleep_1
          until: 07:00:00
          time_id: ha_time

deep_sleep:
  id: deep_sleep_1

Just one thing, are you sure this is correct? I want the device to SLEEP during the day and awake at night. Looks like it is going to sleep at night with the sunset.

Should I change that?