Deep Sleep Help Needed

Hi all

I am trying to create a simple sketch where by I can put a d1 mini esp32 in to deep sleep for a predetermined time by pressing a switch in home assistant. Once the time has elapsed and the mini wakes up I want to stay awake until the switch is pressed again.

The first bit works fine, the program loads a waiting until the switch is pressed but then the sleep process just cycles.

I think it is because the ‘switch on’ status is stored when the sleep starts so the program thinks the switch is pressed constantly.

Any ideas how to stop this would be gratefully appreciated .

> esphome:
>   name: battery_esp32
>   platform: ESP32
>   board: esp-wrover-kit
> 
> wifi:
>   ssid: ""
>   password: ""
> 
>   # Enable fallback hotspot (captive portal) in case wifi connection fails
>   ap:
>     ssid: "Battery Esp32 Fallback Hotspot"
>     password: ""
> 
> captive_portal:
> 
> # Enable logging
> logger:
> 
> # Enable Home Assistant API
> api:
>   password: ""
> 
> ota:
>   password: ""
>   
>   
> switch:
>   - platform: gpio 
>     pin: 5
>     name: sleep
>     on_turn_on:
>       - deep_sleep.enter: deep_sleep_1
>       - logger.log: "Switch Turned On!"
>     on_turn_off:
>       - logger.log: "Switch Turned Off!"
>       - deep_sleep.prevent: deep_sleep_1
>       
>       
> deep_sleep:
>     id: deep_sleep_1
>     sleep_duration: 20s

Two ideas…

First, add a turn off command as part of the turn on automation… not sure how well this will work. You’ll need to add an ID to the switch for local identification:

>     id: sleep_switch
>     on_turn_on:
>       - deep_sleep.enter: deep_sleep_1
>       - logger.log: "Switch Turned On!"
>       - switch.turn_off: sleep_switch

Alternatively, put the turn off command into the boot automation… this should turn it off during the boot process:

esphome:
  # ...
  on_boot:
    priority: -10
    # ...
    then:
      - switch.turn_off: sleep_switch

Cheers and good luck!
DeadEnd

Thanks DeadEnd,

I have used the first option you suggested I just had to change the order of the commands but it seems to be working so far.

I might put the second option in there as well to just to make double sure it works as it should.

Thanks again.

Now the deep sleep function is working correctly thanks to DeadEnd.

I have a new question. Is there a way to report the status ie sleep or awake back to HA. So it can be seen in a lovelace card and so when it wakes up it can trigger automations.

Thanks.

Yes, use platform “status”.

DeadEnd