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

Usually yes, but not this one

This looks exactly like what I´m looking for. Is this adaptable to a Wemos D1 Pro? I´m still in the beginning of the ESP stuff :slight_smile:

nope. afaik no (good) deep sleep support for the esp8266. but an esp32 isn’t expensive too…

See? I wasn’t even aware that there is a difference :slight_smile:

As I want to put it into my metal mailbox, do you have any recommendation for boards where I can connect a external WiFi antenna?

I just finished a build for a mailbox sensor, I could not find an ESP32 that supported external antenna and had good low currrent draw in deep sleep.

I ended up getting an ESP32Thing which also has the advantage of a built in LIPO charger, then mounting the electronics in a waterproof(ish) box in the junk mail section of the mailbox, with the reed switch plugged in using cable and an audio jack (to make it easy to remove the electronics without disturbing the sensor, for recharging).

The back of the junk mail portion is open and I get a reasonable wifi connection from the nearest AP in the house, about 60 - 70 feet away. The disadvantage is if someone looked hard enoungh they could find it and steal it.

Been going a month now on initial charge.

That said, every other reed switch device in the house is 433Mhz, connected via multiple Sonoff bridges. I could have gone down that route and it would have been a lot cheaper.

Hi, found this, and thought I would share what I will be using (still waiting for parts).

Apparently it will last years and years on battery as no power consumption between it wakes up. See details here: https://randomnerdtutorials.com/latching-power-switch-circuit-auto-power-off-circuit-esp32-esp8266-arduino/

My code is here (small problem with notification though): Automation with notification is not firing

Enjoying this thread, immensely, as I also want to battery power and deep sleep the device. Rather than a reed/magnet switch, I chose to buy a few SPST buttons manufactured specifically for installation in door frames/jambs:
https://www.amazon.com/Gardner-Bender-GSW-SK-Electrical-Normally/dp/B00004WLKL

Great Project!
I was wondering…how long does it take for the device to wake from sleep when the door opens? I mean, is the whole process fast enough so that you get the notifications within seconds? I mean the device has to wake, then connect to the wifi then post the notifications so I’m afraid the delay would be too important.

Thanks

in my case the status is displayed in less than 3 secs. if the door is opened/closed within the run_period the update is instant

1 Like

hello Scott

can share your whole codes, esphome and homeAssistant sides ?

best reggards

Here it is:

esphome:
  name: bedford
  platform: ESP32
  board: esp32dev
  on_boot:
    # If MQTT cant connect in 30s will deep sleep
    priority: 500
    then:
      - delay: 30s
      - if:
          condition:
            binary_sensor.is_off: bedford_status
          then:
            - deep_sleep.enter: deep_sleep_1

wifi:
  ssid: 'OpenWrt'
  password: **********
  fast_connect: true

mqtt:

  broker: hass
  port: 1883
  username: scott
  password: **********
  discovery: false
  birth_message:
  will_message:
  on_message:
    # Prevent deep sleep for OTA
    - topic: bedford/ota_mode
      payload: 'ON'
      then:
        - deep_sleep.prevent: deep_sleep_1
    # Home Assistant Automation Puts to sleep after measurements
    - topic: bedford/sleep_mode
      payload: 'ON'
      then:
        - deep_sleep.enter: deep_sleep_1

logger:
  level: NONE

ota:
  password: *******

i2c:
  sda: 16
  scl: 17

binary_sensor:
  - platform: status
    name: "bedford_status"
    id: bedford_status

sensor:
  - platform: uptime
    name: uptime
    update_interval: 6s
  - platform: bme280
    temperature:
      name: "bford temp"
      id: bford_temp
      oversampling: 2x
#    pressure:
#      name: "bford pres"
#      id: bford_pres
#      oversampling: 2x
    humidity:
      name: "bford humi"
      id: bford_humi
      oversampling: 2x
    address: 0x76
    update_interval: 6s



deep_sleep:
  id: deep_sleep_1
  # If MQTT is connected but Home Assistant is down this will be reached
  run_duration: 10s
  sleep_duration: 60min

The automation that runs when sensors readings that get received by HA. Its a while since I did this, I think the second mqtt.publish cancels any retained message:

alias: bedford sleep after mqtt receipt
trigger:
  - platform: mqtt
    topic: bedford/sensor/bford_humi/state
action:
  - delay: '00:00:01'
  - data:
      payload: 'ON'
      topic: bedford/sleep_mode
    service: mqtt.publish
  - delay: '00:00:10'
  - data:
      topic: bedford/sleep_mode
    service: mqtt.publish
mode: single

For a door with reed switch you need to have esp32 wake up when reed is triggered and send state to HA. Much of what I did was because my esp was on another network and accessed over internet so I wanted to cover instances where connection failed and esp stayed awake wasting battery. To do an OTA flash I just send a manual bedford/ota_mode message and esp does not sleep. Empty birth and will config stops the sensors from being not available when its sleeping, not sure if that is needed with a reed switch.

1 Like

Hi. When the door changes its state from closed to open while the ESP is in deep sleep, how long does it take the ESP to wake-up and report the state to HA? I’m currently implementing a battery powered solution but timing is critical in my situtation. Thanks!

I’d say less than 5 seconds. But my wifi AP is next to the door.

1 Like

hi i understand, is the 18650 the reed relay and the esp32 enough? is the voltage of the 18650 enough for ESP?

Sorry. I don’t get it. What are you asking?

The ESP32 works fine with the battery, the voltage is regulated.

1 Like

what PIN where connect? Can you take a photo from this? Sorry my bad english :slight_smile:

how is voltage regulated? Do you use a dc-dc converter?

Most ESP32 boards have a VIN pin which will accept a range of voltages and regulate that to 3.3V. What board do you have?

Note that the VIN normally requires at least 6V.

There are some boards (ESP32 Thing for instance) that happily accept 3.7 volts on a dedicated LiPo input.

I have a espressif esp32-wroom-32

Hello.
Why no voltage divider? Is it not necessary? I thought that full charged 18650 battery with its 4.0V or more could damage the GPIO pin.