Battery powered ESP-01 smart switch

Hello everyone!

I am new here and I am looking for some advice.

I have a few LED book nooks in my home and I want to be able to turn them on and off from Home Assistant. They are battery (AA and AAA) powered and only consume power when on.

I was thinking of using ESP Home on an ESP-01 device per book nook. The idea is that I could click in Home Assistant and the ESP would turn on the book nooks.

But to ensure battery longevity, I would like to use deep sleep and I wanted some advice on how to best approach that.

My initial idea was to use MQTT and have Home Assistant post a message on the broker when I toggle the virtual switch. Then, the ESP devices which spend most of their time in deep sleep would check the MQTT queue on wake up and turn the LEDs on if there’s a message to do so.

Of course, such an approach would mean that it would not be instantaneous. Indeed, the LED would only turn on when the ESP next wake up and notice the message in the queue. But I don’t really need them to turn on immediately and that’s really the only way I could think of minimizing power consumption and ensuring the batteries last longer.

What is your opinion? Would that work? Do you have a better idea?

Thank you!

Esp-01 can be woken up from deep sleep without some fine soldering. ESP 12F much easier to work with for deep sleep.
Still AA won’t last long (prob less than 1 day) in the use you are suggesting.

I saw some tutorials online about soldering XDP_DCDC / GPIO-16 to the RST pin to enable deep sleep on the ESP-01. I think I should manage it, but I also have a couple of ESP-12F, so I might use them instead for this project. Thank you for pointing this out to me, I didn’t know that when I initially purchased the ESP-01.

I’m more interested however in what you said about battery life. I have to admit that I was hoping to get at least a month of battery life with deep sleep, based on the following assumptions and calculations:

  • ESP device stays in deep sleep most of the time
  • Once every 10 minutes, only between 12 pm and 12 am, the device wakes up
  • Upon wake up, it connects to wifi, check the MQTT topic and goes back to deep sleep

That would mean that the ESP wakes up 72 times per day.

  • Assumption #1: A wake up cycle is 20 seconds
  • Assumption #2: In deep sleep, the ESP consumes 50 uA
  • Assumption #3: During wake up, the ESP consumes 100 mA
  • Assumption #4: AA batteries can provide 1500mAh of power

Full deep-sleep consumption per day (12am → 12pm)
50 uA * 12 h = 0.6 mAh

Mixed activity period consumption per day (12pm → 12pm)

  • 1 cycle = 600s
  • 72 cycles total

Wake up consumption per cycle: 100 mA * 20 s = 2000 mAs = 0.55 mAh
Deep sleep consumption per cycle: 50 uA * 580 s = 29mAs = 0.008 mAh
Total consumption per cycle = 0.55 +0.008 = 0.558 mAh
Total consumption per day for mixed activity period: 0.558 * 72 = 40.18mAh

Total run time on battery
1500 mAh (battery capacity) / 40.18 (consumption per day) = 37 days

Did I make a mistake somewhere?

Am I being too optimistic about power consumption or how long the ESP needs to be awake?

I realize that it doesn’t take into account the periods when the ESP will be active and providing voltage to a mosfet to trigger the “ON” switch to light up the book nook, but I estimate it will only run for a few hours per months, and the ESP is not the one providing power to the display, it is only switching it on.

Thank you again for your help and advice!

Your mileage may vary. AA cell will probably crash esp chip when it drop much below 3v. Also important how you intend to switch your book nook device on. Relays or transistors to switch. Relays use more juice.
Perhaps a soft latching switch could allow esp to go asleep and leave power running to book nook.

My current plan it to use 3 rechargeable AA batteries (rated 1.2V, 2500mAh), but I will see depending on what makes more sense for powering the ESP. I have to admit I haven’t yet tested anything there and I am open to suggestions. I saw some projects from other people online mentioning having up to 5-6 months of autonomy with such solutions when using the ESP to take infrequent sensor readings, so I’m hopeful it will work out.

For switching the book nook, my intention was to use a mosfet to limit how much current it takes from the ESP to switch on (and therefore preserve battery life). But I only have IRF-520 ones, which are not adapted for a 3.3V circuit, so I would need to buy some logic-level mosfet to test.

I had not considered a soft latching switch, I will look into it, thanks for the suggestion! My idea was to leave the ESP awake while the book nook is on, so that it could continue supplying voltage for the mosfet, but turn off everything that I don’t need (wifi radio, modem, ADC, UART, I2C…). However your suggestion might be better for battery life.

What do you think of my assumptions in my previous post, especially with regards to cycle lengths and power consumption in different modes? Do you think they are in the right ballpark?

I honestly couldn’t say about the calculations. My suspicion would be when there is a load on the aa batteries and the current spikes when it wakes and probably during wifi turn on it can peak at 430mA and the voltage will drop and it will crash. You will never get 1800mAh from the cells. Test it and see how long it works without adding the switch. Just sleep and wake cycles. With power to wifi turned down. Learning is the fun part. For practical purposes I’d use a usb and mains power adapter to run the whole show book nook and all. But do the expirement first and report back.

1 Like

I couldn’t agree more. I’ll experiment and update here :slight_smile:

I would too, but unfortunately batteries are the practical solution in my case, as there are no outlet anywhere near the bookshelf, and it’s impossible to hide ugly wires with concrete walls.

What about a small powerbrick (phone charger type thing) and then run the ESP from USB?

You can get 10000mAh, 20000mAh cheaply and they could be mounted neatley.

Power banks are usually bad match with deep sleep. They often have circuit to switch off the boost converter when current draw is below a threshold. So Esp goes to sleep, power bank turns off and good night…

Not just with deep-sleep. I experimented with an old Xiaomi USB powerbank and it switches off on its own because power draw is too small to keep it awake. And you have to step down the voltage, so not ideal either.

You do have a point regarding capacity, a bunch of lithium batteries are probably a much better solution for long autonomy. But I’m wary about lithium and I’m not ready to DIY something with them, especially if it’s supposed to stay powered 24/7. A lithium fire in flammable bookshelf is not my idea of fun.

In other news, I successfully modified one of the ESP-01 to work with deep sleep following the online tutorial I mentioned earlier. I made a bit of a mess because I didn’t have enameled wire and I just coated a very thin wire with nail polish instead, but it works just fine, so I’m pretty happy about that.

Behold my (not) masterwork!

I probably won’t be using this specific ESP in the final product, but it’s useful to have for prototyping at this stage.

Normal Li-ion or LiPo have too high voltage for Esp, full battery is 4.2V.
But LiFePo4 cell has nominal voltage 3.2V, perfect match for Esp without any converters. And they are safer as well.

1 Like

Alright, as advised, I am starting an autonomy experiment based on the parameters discussed above and that I am summarizing below:

  • ESP-01s modded for deep sleep
  • Powered by 3 rechargeable Duracell AA batteries rated for 2500mAh at 1.2V
  • Cycles last 10 minutes, with 20s of activity and 580s of deep sleep
  • The cycles are the same 24/7 instead of spending the whole night in deep sleep. It should make the battery life shorter
  • Upon wake up, the ESP connects to WiFi and sends a MQTT heartbeat message

The test was started on Dec 20th 2024, at approximately 13:00 UTC

One thing I should note is that I didn’t fully charge the batteries before starting the test, and I have been using the ESP for a few hours on them since I took them of their packaging. But the idea is not to get a precise measurement of battery life, just a ballpark figure.

I think the max you can deep sleep an ESP8266 is 71 mins. The closer I programmed to 60mins the more variable the time it actually stayed asleep.

mqtt:
  broker: *********
  username: *******
  password: ********
 
  on_message:
    - topic: device/ota_mode
      payload: 'ON'
      then:
        - logger.log: 'OTA Mode ON - Deep sleep DISABLED'
        - deep_sleep.prevent: deep_sleep_1

    - topic: device/sleepearly
      payload: 'ON'
      then:        
        - deep_sleep.enter: deep_sleep_1	

In order to keep it awake as short a time as possible I have an automation on HA that as soon as it receives an MQTT message (in most of my cases a sensor reading) it replies back on the topic of device/sleepearly and ESP8266 goes straight back to deep sleep. Typically this means the ESP is only connected to HA for about 2 seconds. It takes about 6 seconds for WIFI to establish first.

1 Like

Oh, I had not realized that it could not sleep for that long. In my initial plan, I was hoping to have it sleep for 12 hours. I guess I will have to work around this.

Thank you for sharing your configuration, that’s one way to do it. I found someone on reddit talking about following a similar approach, but on the controller side, before WiFi even turns on. Let me quote the relevant part:

The simplest way to go longer than 3.5 hours in Deep Sleep is to store the current state in the RTC RAM, and then check it when you awake (before turning the WiFi on). If it’s not time to wake up fully yet, update the RTC RAM and go back into Deep Sleep. You can perform that sequence in about 100ms at 35mA, most of that the boot time. That’s about 0.0068mAh per day, if I did the math right. It’s pretty minimal.

Source: https://www.reddit.com/r/esp8266/comments/m4v252/comment/gsd1q09/?utm_source=share&utm_medium=web3x&utm_name=web3xcss&utm_term=1&utm_content=share_button

I’ll be honest, I don’t have a clue how to do that, and it’s possible that you can’t do it through ESPHome. I will try to experiment with another ESP-01 device (time to go back to soldering a tiny wire)

You could us global variable to count the “dumb wakeups” and set option restore_value: true to keep it in memory.
If global is 0 do full wakeup, otherwise dumb wakeup.
During “full wakeups” check the internet time and if it’s time to go for 12h sleep, set global to 1. Next wakeups increment that global every time and go straight back to sleep. If global is >11, set it to 0 and do the “full wakeup”.

1 Like

I read the documentation about the “restore_value” flag for global variables (here: Global Variables — ESPHome), but it is not clear to me where the value is stored. This sentence seem to imply that is stored on the flash/eeprom, which has very limited write endurance:

"Whether to try to restore the state on boot up. Be careful: on the ESP8266, you only have a total of 96 bytes available for this! "

The “on boot up” part makes me think it is stored in flash, but the 96 bytes limit makes me think it is stored in the RTC RAM. I don’t want to enable this option if it is stored in flash, because at 10k write endurance, writing every 10 minutes would mean the memory is dead after 7 days.

Do you know more about that? I found a GitHub issue where someone mentions the same problem with the documentation: Flesh writes Wear Leveling · Issue #6439 · esphome/issues · GitHub

AFAIK globals are saved in RTC and don’t survive complete power off, so you can easily test it to confirm.
Also, if you don’t set esp8266_restore_from_flash to true, how could it write there…

Anyway, Esp32 has default flash write interval 1 minute and I have never experienced flash failure. If you write one byte to flash 10k times, it doesn’t equal to writing entire flash 10k times.

1 Like

Well that’s really disappointing. It looks like the last message was received yesterday, which puts current autonomy under these test conditions at ~8 days, very short of the full month I had initially calculated. Sure, the test wasn’t exactly the same, with the ESP doing double the work it should have done (waking up all night long), but even 16 days would be much too short for me.

I will make actual power drain measurements next. I also noticed something weird during the test: a few times, when waking up, the ESP would stay on for a minute or more instead of going back to sleep after 20 seconds. I am not sure what could be causing that, I will need to dig into it.

On a more positive note, I found a really nice write up from someone who combined an ATTiny with a ESP-8266 to get ~ 2 years of autonomy on AA batteries, while taking sensor measurements every 2 minutes. Here’s the link for reference: ESP8266 on batteries for years – part 1 – Cron Blog – My personal findings

That obviously destroys the battery life. Wake time should be some milliseconds if no wifi needed and few seconds when needed.
Low power MCU as wake up trigger is useful to cut down sleep power, but it doesn’t help a bit if your problem is wake time!

ESPhome is not suitable for this kind of usage.
Have you considered the RF433 solution?
Just add a module to your ESPhome.
Buy a transmitter. I think this solution is simpler.