ESPhome / deepsleep / API: works when the device is plugged in but not when on battery

I am trying to build a “bluetooth low energy” data collector with ESP32 to collect data from a few BLE soil moisture sensors.
My device is an M5 atom lite. The config is trivial,

esphome:
  name: esphome-backyard
  platform: ESP32
  board: m5stack-core-esp32
  #board: nodemcu-32s


# Enable Home Assistant API
api:
  password: ""

ota:
  password: ""

wifi:
  ssid: "XYZ"
  password: "XYZ"

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "esphome-backyard"
    password: "...."

captive_portal:

logger:
  level: DEBUG # Required for the tracker to show the device

esp32_ble_tracker:

deep_sleep:
  run_duration: 120s
  sleep_duration: 10min

sensor:
  - platform: xiaomi_hhccjcy01
    mac_address: 'C4:7C:....'
    temperature:
      name: "Xiaomi HHCCJCY01 Temperature"
    moisture:
      name: "Xiaomi HHCCJCY01 Moisture"
    illuminance:
      name: "Xiaomi HHCCJCY01 Illuminance"
    conductivity:
      name: "Xiaomi HHCCJCY01 Soil Conductivity"
    battery_level:
      name: "Xiaomi HHCCJCY01 Battery Level"

this works perfectly with the device pluged to a power source.
With the device plugged to the laptop, I can see log message telling when the device enters deep-sleep, and when it comes out of deep sleep, as per the timers that I have set.

No problems here between deepsleep and “API” . I don’t mind leaving the device on 2-3 minutes so the API can come up and the data transmitted.

Also I noticed that the esphome deepsleep documentation does not say anything about API needing to be removed. Deep Sleep Component — ESPHome

I bought the “tailbat” battery module as I want this device to staty outdoors for a few weeks. When I connect the battery, I can see the Atom Lite turns on and it does connect to homeassistant and updates the sensor, all good.
Then it goes to sleep and it never wakes up. The only way to wake up the device is to press the button in the “tailbat” battery module, I see the LED light staying on for 2 minutes (== run_duration) and then it goes off afterwards and never comes back on. It’s like the ESP32 is turning off the battery module when it goes to sleep.

Any hint on why the atom lite+tailbat battery are unable to come back to life after deepsleep?

The documents should be updated then as it is well known that the API does not play nicely with deep sleep. Use mqtt instead.

See this comment by Otto: Create Native API Server · Issue #46 · esphome/feature-requests · GitHub

I am unsure how (or what) exactly a tailbat is, so I googled it. Please don’t assume knowledge, a good idea to post a link I think. I’ll do it now :slight_smile: ATOM TailBat - Battery Accessory for ATOM | m5stack-store

Anyway I see the tailbat has a power management chip on board, perhaps it is something to do with that?

Thanks @tom_l and @nickrout
I migrated from API to MQTT, it was a lot simpler than I thought, setting up mosquitto in the RPi4 that is running HA.

I notice turn-up is a lot faster than before, the sensors get updated much quicker in HA.

However, the deep sleep problem remains

mqtt:  # using mqtt instead of API due to deepsleep
  broker: 192.168.1.5

ota:
  password: ""

wifi:
  ssid: "xxx"
  password: "xxxxx"


logger:
  level: DEBUG # Required for the tracker to show the device

esp32_ble_tracker:

deep_sleep:
  run_duration: 120s
  sleep_duration: 10min

sensor:
  - platform: xiaomi_hhccjcy01
    mac_address: 'xxxxxx'
    temperature:
      name: "Xiaomi HHCCJCY01 Temperature"
    moisture:
      name: "Xiaomi HHCCJCY01 Moisture"
    illuminance:
      name: "Xiaomi HHCCJCY01 Illuminance"
    conductivity:
      name: "Xiaomi HHCCJCY01 Soil Conductivity"
    battery_level:
      name: "Xiaomi HHCCJCY01 Battery Level"

When on battery, after two minutes, I see the light in the battery going off, and it never turns back on again until I push the ON button that it has.

To @nickrout point, perhaps this battery turns itself off when it senses that the usb-c connector peer disappears and that is preventing the esp32 from turning itself on when the RTC timer expires. I will try to find some other simpler usb-c battery to test and compare.
Headsup to anybody thinking of using an m5 ‘atom tailbat’ battery with a deepsleep usecase

I would suspect that it iss not very useful to run a ble tracker on batteries

It will only work like intended (scanning each BLE channel for 320ms in cycles) when the esp32 is always powered. The ble beacons the thing should detect are only published once in a while (because they are typically from battery powered devices).

Probably quite impossible to “sync” the beacons emitted with the short on time (120s) from your configuration, you may catch a beacon by luck but most of them just get lost because they are emitted while the esp is sleeping (10min in your config)

What battery is this? Like a external power bank? They often need a minimum current draw to turn on (and often turn off under a certain threshold too).

But running a ble tracker on batteries is nonsense anyway :wink:

It’s a tiny 200mAh battery with an “on” button, part of the m5stack “atom” family of products, it’s very small and it plugs into the atom module (esp32), I think the use case they had in mind is to allow the esp32 to be turned on on-demand when you press the button, like a mini remote control, or to provide a few hours of backup power, it has a passthru connector.

To your point, this weekend I tested a “power bank” meant for cellphones. Exact same problem: the ESP works fine up to the point it enters deep sleep, and then it’s unable to wake up. This powerbank has a button to check battery level, as soon as I press it, the esp becomes pingable, i.e. it turns back on.
I wonder if as you said both batteries shut off for safety or other reasons when they see the load no longer drawing any current

My BLE-based sensor transmits values every 10 minutes based on my tests so far. I want to collect one sample every 120 minutes. I wanted to leave the esp inside a weatherized box outdoors close to the sensor, no AC nearby, in a tree actually. So for this use case it would be nice if deepsleep had been usable, so the battery can last all season.

I read that most of the power consumed by the tiny esp32 board goes to wifi. Is there a way to turn wifi on every 2 hours, for a few minutes? that could be an alternative approach

Any luck? I am also encountering exact same problem. I want to use esp32 to water some outdoor plant, no ble tracker. never wake up on power bank or solar charger controller with full battery, but it’s doing fine when it’s plugged into power socket.

Both of these have internal circuits which normally turn completely off to don’t drain the battery itself. Often a certain amount of current draw is needed to activate this circuit again and let the batteries “deliver” energy. :zap:

It looks like your esp isn’t drawing enough power to “active” your power bank usb ports. :battery:

Looks like we are out of luck. I tested two different models of ESP, two different batteries, deepsleep (or at least ESPhome’s deepsleep) just does not work while on battery. One day if time allows I’d like to try some other firmware just to rule out an ESPhome-specific problem.

I haven’t found a way to turn off wifi (and on, periodically, via automation) with ESPhome either. That could be an alternative, if feasible. Without this, ESPhome depletes a 5000mah battery in about 24 hours

As you have been told already, it is not an issue with esphome. It is an issue with your battery bank. It turns off when the current falls below a threshold.

You and @HSH both already proved in this thread that the cause of the problem is not related to the ESP/esphome:

The reason for this is that typical wall plugs are “always on” because they don’t need to care about any battery drain :battery: :zap:

well, I definitely knew what cause since the first article. I am asking here for any solution to solve or to prevent the power bank auto off.

A dummy load which permanently drains the battery can keep your power bank on if you want.

Another way (which might be “better” - depending of your scenario) could be to rip the power bank apart and “harvest” the batteries to use them together with a (good) ldo (and maybe some extra battery protection) which outputs 3.3V for your esp. You then just need to work with a extra charging circuit if you want to recharge your batteries.

still hoping to find a way to shutdown wifi and turn it on on demand and/or periodically but otherwise keep the esp running, as an alternative to deep sleep. I suspect the battery bank will last a few weeks that way, instead of 24 hours. That would fit the bill for many use cases that require untethered power

This (esp without wifi) might be even not enough current draw for some power banks to keep them permanently “alive”.

Anyway, you can just test it and report your findings:

Don’t use a power bank. Use a couple of protected 18650 cells in a battery holder and buy a charger you can put them in when needed.

1 Like

Thank you :slightly_smiling_face:

Thank you.

I’m now testing with solar charging battery starting with full 10 amps and then charge into the power bank (hoping that it add extra capacity to the batt.) And I programmed to update interval every 10 mins, though it seems to update more frequent like less than 5 minutes. Power save: high, fast connect: on. Now, two days past and it is still on. I will keep u post how long it will last. I will need it to last for 2 weeks.