Reed Door Sensor (battery powered ESP32 with esphome(yaml))

Hi Folks!

I just finished my door sensor with esphome and some cheap reed sensors.

Total cost for this project is about 15 €.

I’m using a WEMOS LOLIN32 ESP32 running on a 18650 Battery soldered to a strip board. Positive output is connected with an ADC Pin (34) and the reed sensor itself is connected with +3V and Pin 14 .

Pin 14 is defined as a binary GPIO sensor and also as the wakeup pin for the ESP itself.

If no motion is detected the ESP32 will sleep for 6 hours and wake up for 30 seconds publishing the battery value and the current door state. If the door is opened the esp awakes from deep sleep and sends the current state. See the esphome source code for further details

If the voltage of the battery drops below 3V the esp performs a complete shutdown (in theory (still to be explored in detail)).
Some pictures:

The config files:

Package File for HomeAssistant

esphome config

As always any comments and/or improvements are highly welcome!

22 Likes

Nice project. Have you been happy with the performance? Thinking of making one.

Oh, and do you have updated links?

links and description updated.

performance is fine, I change the battery every 2 months.

the battery alert isn’t working as intended. right now I use a “update check script” like sensor warning.

if the door sensor (or the signal strength) isn’t updated for 6 hours I’ll get a notification.

Conclusion for all my battery sensor powered esps right now:
bme280 with a cheap old battery and update interval once per hour: approx. runtime 1 month
same with a good new battery: 2-3 months

the doorsensor (also cheap battery but never out in the cold): ~ 2 months

Great, thanks. I’m planning to adapt it to use a roller lever switch for my freezer for that often gets stuck very slightly open. Reed switches seem all to be too sensitive for the small gap.

Much appreciated!

Could you be as specific as possible on how you wired the battery ?

Thanks a million !!

I wired the postive and the negative to a PH-2 connector and connected it to the board?

Sorry can’t be more specific.

Another cabled is soldered to the positive outlet of the battery case and ping 34 on the ESP.

That’s all

1 Like

Batt+ and Batt- go to PH-2 on the board.
Batt+ also goes to GPIO34, and you get an analog V reading on GPIO34.

Is that right ?

yep, exactly.

On my (fake?) lolin32 board +/- is twisted so I had to solder black to plus and red to ground… but that depends as usual on your hardware :wink:

On word for the analog reading, never got any alarms for the voltage drop… so it is basically useless.

Maybe @ottoWinter could explain that better than I can…

I advise you to use some kind of template sensors that checks if the sensors has not been updated for an defined amount of time (in my case >6h). Works pretty well =)

just for completion here is my package if a sensor fails

help ? Why does this return nothing but [D][sensor.adc:087]: 'VBatt': Got voltage=0.00V

sensor:
  - platform: adc
    pin: 34
    attenuation: 11db
    name: "VBatt"
    update_interval: 5s

EP32 with Batt+ going to pin 34.

edit : nevermind, turns out there’s a bad solderjoint between the trace and P34.

I have tried the exact same model to automate my mailbox :slight_smile: I want a pushbullet to be sent to my phone when the postman drops a parcel … however i find that it takes about 10 secs for the esp32 to wake-up from deep-sleep and communicate to home assistant that the door was opened (by the postman). if it takes him (or her) less than 10 secs (ie. open door, drop parcel and close the door) then the esp 32 will wake-up but no change in status will be detected… I do not see how to fix that … while in deep-sleep is there any way for the esp32 to memorize the change that occured before wifi was fully reactivated? apart from leaving a note on the mailbox that the door should be left opened for more than 10 seconds of course :slight_smile:

check out the ESPHome docs for the general binary_sensor component. there is an option for “delayed_off”

I did a test with my mailbox too and a delay of 100ms fixed this =)

another thing to do is a static ip setup, wifi + dhcp takes some time…

example:

deep_sleep:
  run_duration: 30s
  sleep_duration: 120min
  wakeup_pin: 14
  wakeup_pin_mode: INVERT_WAKEUP
switch:
  - platform: shutdown
    name: "ESP32_4 Shutdown"
    id: shutdown1

binary_sensor:
  - platform: status
    name: "ESP32_4 Status"
  - platform: gpio
    filters:
      - heartbeat: 2s
      - delayed_off: 20ms
    pin:
      number: 14
      mode: INPUT_PULLDOWN
      inverted: True
    name: "door"
    device_class: door

great post. I tried it out on my esp32 and it seems to work. Although Im using a PIR sensor on pin for the wakeup…

Only one issue, Battery drains in less than one day (1 x 18650) the room im testing on is quite busy so the PIR would trigger a lot. Any advise. I would like to get it working on a battery.

see code for esphome:

esphome:
name: pir2
platform: ESP32
board: esp32dev

wifi:
ssid: “xxxxxx”
password: “xxxxxxxx”
manual_ip:
static_ip: 192.168.5.206
gateway: 192.168.5.1
subnet: 255.255.255.0
dns1: 192.168.5.1
dns2: 8.8.8.8

#Enable logging
logger:

api:
password: ‘xxx’

ota:
password: ‘xxx’

deep_sleep:
run_duration: 30s
sleep_duration: 120min
wakeup_pin: 15
#wakeup_pin_mode: INVERT_WAKEUP

switch:

  • platform: shutdown
    name: “esp32_1 Shutdown”
    id: shutdown1

binary_sensor:

  • platform: status
    name: “esp32_1 Status”
  • platform: gpio
    pin:
    number: 15
    #mode: INPUT_PULLDOWN
    #inverted: True
    name: “motion”
    device_class: motion

sensor:

  • platform: wifi_signal
    name: “esp32_1 WiFi Signal Sensor”
    update_interval: 30s
  • platform: adc
    pin: 34
    attenuation: 11db
    filters:
    name: “shutdown”
    on_value_range:
    below: 3.00
    then:
    - switch.turn_on:
    id: shutdown1

Busy rooms sounds like you could have a power source there? Maybe that’s better for your usecase…

Otherwise you could try to work with filters to force the minimum sleep time to a certain level (maybe need to experiment with a filter for the pir sensor too, to don’t retrigger a wake up) but this goes for sure on the detection ‘rate’. You will have kind of a dead zone after a detection - but maybe that fits your case, can’t tell?

Problem for you is that the device sucks most of it’s energy when wakin’ up (connecting to WiFi, api…) - which does it a lot’s of times in your case.

You could count the total runtime of the board (time in wake up) for a charge of battery (maybe with the duty_cycle sensor? The uptime sensor behaves weird when powered with battery…).

thanks for the tip… I think easiest will be just to plug it in… although i will look into your comments in regards to duty_cycle.

Kind regards

Andre

Getting 203.5% on the duty_cycle sensor reading… is that a lot?

Thanks.

Maybe you could decrease the runtime, 30 sec sounds a bit too long.

my battery lasts months but this sounds like the esp is always on (the load cycle)

ok will drop to 10sec, as the esp connects quickly to wifi, reserved IP.

does OTA update works? and can i use api instead of Mqtt? currently using custom code but having issue with mqtt thinking of moving to esphome.

currently the best method for connecting battery powered devices is MQTT

see:

Anyone tried this code with an esp8266, alot smaller and can be mounted with a small litho for windows, doors would need a larger battery. There is a guy on youtube that has done it, but he is using arduino code, I would like to be able to have a yaml file to use with esptool, but I am not familiar enough to be able to modify the code. here is a link to the youtube video, his printed case is perfect and just a bit larger than the old wired reed sensors.

1 Like