ESPHome offer a very useful option to flash our device over WI-FI (OTA), so without remove the thing from where is installed; this process can be very annoying if we have implemented a Deep Sleep functionality, especially for battery powered projects that needs to save energy.
During deep sleep the device is switched off and cannot be reached from ESPHome to upload the new firmware so we have only to options: do it with a USB cable or wait until the device came back online and quickly flash it while still awake.
To better manage this scenario we can implement a condition to disable/enable the deep sleep function so that first time the device will came back online will read this condition and remain online, according to the condition value, until we set off again the condition.
The condition is implemented using an Home Assistant Helper:
The Input Boolean helper integration allows you to define boolean values that can be controlled via the user interface and can be used within conditions of an automation. This can for example be used to disable or enable certain automations by using them in their conditions.
Home Assistant – Input Boolean
Define Input Boolean Helper
- Click Configuration
- Click Helpers

- Click Add Helper button
- Click Toggle type

- Type a Name, example “Disable Deep Sleep“
- Choose an icon, example “mdi:sleep-off“
- Click CREATE

- Add the new Entity “Disable Deep Sleep” to where you prefer on you HA Dashboard, I personally have a dedicated Dashboard to Server and System information and controls, so I’ve used this one
- When you will need to update your device with a new ESPHome firmware you will set this Toggle to ON. Next time the device will came online it will stay on until you will set this toggle OFF again.

Now we need to manage this condition inside the ESPHome firmware of each device.
Modify firmware to manage Helper condition
The condition will have to be evaluated using a script on each firmware to flash:
- Add the script execution at boot

- Add id to deep_sleep instruction
- Remove run_duration from deep_sleep instruction, you will place the same time value on delay attribute, see next step

- Add a binary_sensor for the Boolean Helper “Disable Deep Sleep” created before
- Add a script section with the following code
- The script will evaluate the Helper status and will disable or enable the deep sleep status according to its value

Following the code to add to your projects:
esphome:
name: plants-gelsomino-sensors
platform: ESP8266
board: d1_mini_lite
on_boot:
priority: -100.0
then:
- output.turn_on: gpio_d1
- script.execute: deep_sleep_evaluation
deep_sleep:
id: deep_sleep_enabled
#run_duration: 20sec # move this value to -delay- deep_sleep_evaluation script parameter
sleep_duration: 30min
binary_sensor:
- platform: homeassistant
id: disable_deep_sleep
entity_id: input_boolean.disable_deep_sleep
script:
- id: deep_sleep_evaluation
mode: queued
then:
- delay: 20s
- if:
condition:
binary_sensor.is_on: disable_deep_sleep
then:
- logger.log: 'Deep Sleep Disabled'
else:
- deep_sleep.enter: deep_sleep_enabled
- script.execute: deep_sleep_evaluation
Grazie per la condivisione. Ma ricevo questo messaggio da esphome prima della compilazione. “impossibile trovare l’azione con il nome ‘output.turn_on”. Mi sono perso a dichiarare qualcosa? Tutti i codici che hai postato li ho aggiunti a esphome
Ciao!
Puoi postare il tuo codice completo per verificare l’errore?
Thank you for this! It has helped me control the deep sleep behavior of my esp’s!
It has also given me the example of how to implement my own scripts 🙂
Really easy to follow guide!
Thanks for your feedback!
Hello! Why of the reason this section: – output.turn_on: gpio_d1
I skip this part and not kept in awake.But wake up regularly
Hello! That “output” was strictly related to the needs of my project using gpio to power a sensor, is not related to the deep sleep things.
Hallo Staff, great tutorial!
Any idea how to wake up via PIR sensor?
do I add the wakeup_pin to the deep_sleep section or do i adjust the script in some way?
thanks!
Hі there very cool ԝeb site!! Guy .. Excellent ..
Wonderful .. I’ll bookmark your web site and take the feeds
additionally? I’m glad to sеek out so many usefᥙl info right heгe in the ⲣost, we’d like ԁevelop
more strategies on this regard, thɑnk you fߋr sharing. .
. . . .
Thanks! Your solution to disable deep sleep is perfect. Used it for my sensor device