ESPHome and Deep Sleep

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