Need help with Deep Sleep mode

I’ve set up a car battery monitor for my first ESPHome project, and I’d like to have it sleep between updates.

I struggle a bit with YAML, but I have managed to cobble together the following code from online searches, which works with the sleep mode lines disabled. The logs show the ‘consider_deep_sleep’ script runs, and I see the correct results in the logs depending on whether my helper is on or off in Home Assistant.

esphome:
  name: car-battery-monitor
  on_boot:
    then:
      - script.execute: consider_deep_sleep

esp8266:
  board: d1_mini

-- irrelevant code removed for this topic posting -- 

# This is the sensor which monitors the battery voltage
sensor:
  - platform: adc
    pin: A0
    name: "Car Battery Level"
    device_class: voltage
    update_interval: 300s
    filters:
      - multiply: 14.4217

# This is a binary Helper in Home Assistant to enable/disable sleep mode
binary_sensor:
  - platform: homeassistant
    id: deep_sleep_mode
    name: Deep Sleep Mode
    internal: True
    entity_id: input_boolean.car_battery_monitor_sleep_mode

# Deep Sleep
#deep_sleep:
#  id: deep_sleep_control
#  run_duration: 10s
#  sleep_duration: 290s

# Script to check Binary Helper to enable/disable deep sleep mode
script:
  - id: consider_deep_sleep
    mode: queued
    then:
      - delay: 290s
      - if:
          condition:
            binary_sensor.is_on: deep_sleep_mode
          then:
#            - deep_sleep.enter: deep_sleep_control
            - logger.log: 'Deep Sleep mode Activated!'
          else:
#            - deep_sleep.prevent: deep_sleep_control
            - logger.log: 'Skipping sleep, per prevent_deep_sleep'
      - script.execute: consider_deep_sleep

But when I enabled the deep sleep, it goes offline and is no longer working. I read somewhere that I need to connect the GPIO16 pin to the RST pin on an ESP8266 (which I am using). Can someone tell me if this is the cause of my issue, or if there is any other errors in my code?

You have # the lines to tell it how long to sleep for before waking

1 Like

Yes, as I mentioned, I have the deep sleep lines commented out. When I had them active, the device would not come back online.

Looks ok to me from an eyeball.

Sounds like it is likely the GPIO16 thing but I’m not sure.

I’m just learning deep sleep ins and outs myself.

I’m using esp32’s and it’s straight forward. Sounds like a little more of a hassle on an esp8266.

1 Like

When you say it works without deep sleep I take it you mean the esp sends a voltage reading. I will also assume you’re using app rather than mqtt in the removed code. I suggest to first try removing your script and just check you can get the device into deep sleep in the simple fixed time way with your code uncommented . Increase the runtime duration a little so you can ota easily. If you are using api it takes longer for wifi connections to complete and communications to begin.

1 Like

Yes. This is how the timer (which never sleeps) wakes the ESP.
If your board doesn’t have this jumper pad it’s not a genuine Wemos product. Just short D0 to RST.

2 Likes

Yes, sorry, the removed code is just the base code added when creating a ESPHome project.

The voltage reading is working, as is the script (I get the log entries indicating that it is correctly reading the state of the binary helper in both states)

I’ll try your suggestion of increasing the run time. I’ll add an on_shutdown to alert me that it’s going into deep sleep.

Thanks for the suggestions.

Mine doesn’t look quite like that, I can’t tell if it has a trace between those 2 pins.

If it does, does it need to be activated somehow in the code, or is that automatic when ‘Deep Sleep’ mode is activated?

Does what need to be activated?
Sleep in the ESP is really simple.
You provide a sleep duration then tell it to go to sleep.
When duration ends, pin D0 (GPIO16) goes low.
You connect D0 to RST so that D0 going low pulls RST low, restarting the ESP.

When the ESP reboots, the first thing you do is to check the status of the helper.

1 Like

I meant he short between the GPIO16 and RST, are those 2 pins always connected on the Wemos,or does that ‘short’ need to be done in code? Is that an ESP32? Mine is an ESP8266

Mine isn’t a Wemos, there is no continuity between the 2 pins. From what you are saying, it sounds like I do need to add a jumper for it to work. This sounds like it is the issue in my case.

I’ll try to add a jumper this weekend when I have the time.

Thanks again to everyone.

So, I decided to move this to a ESP32 to simplify this project, but now I have another problem I can’t figure out. The exact same script that works on the ESP8266 doesn’t work on the ESP32.

On the ESP8266, when the Binary Helper is on, the script correctly reads the value from HA and logs the ‘Deep Sleep mode Activated’ line, but on the ESP32, the exact same code logs the ‘Skipping sleep, per prevent_deep_sleep’ line.

# This is a Binary Helper in Home Assistant
binary_sensor:
  - platform: homeassistant
    id: deep_sleep_mode
    name: Deep Sleep Mode
    entity_id: input_boolean.car_battery_monitor_sleep_mode
# Script to check Binary Helper to enable/disable deep sleep mode
script:
  - id: consider_deep_sleep
    mode: queued
    then:
      - delay: 20s
      - if:
          condition:
            binary_sensor.is_on: deep_sleep_mode
          then:
            - logger.log: 'Deep Sleep mode Activated!'
          else:
            - logger.log: 'Skipping sleep, per prevent_deep_sleep'
      - script.execute: consider_deep_sleep

Is there any reason why the ESP32 wouldn’t read the state of the Binary helper?