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

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

IMHO not a good solution.

1.) no deepsleep for the esp8266 with esphome.
2.) no additonal data send by the esp (rssi, temp. maybe)
3.) a CR2302 battery will last for about 20times in his case

dunno how often you close/open your door but 20 times sounds not that good for me…

compared to a 18650 rechargable battery that lasts in my case for more than 2 months…

I find this very interesting. I could have a use case for a PIR that would trigger a light. Would it be possible to hookup a charger to the battery so it will recharge while light in on and runs on battery when light is off (considering light would be on for 4 minutes maybe 4 to 5 time a day)? That could make the sensor running for months.

1 Like

why run it with batteries when you have ac power?

Because AC power would be given through the wires of a ceiling lamp that should be off most of the time

hey there!

tinkered a bit with the config and added an “OTA Mode”. When switched on the esp won’t enter deep sleep.

Thanks to @scote for sharing his setup in this post. Just added a template switch for the ui package (see below).

esphome config (click to expand):
substitutions:
  devicename: esp32_1


esphome:
  name: $devicename
  platform: ESP32
  board: lolin32


wifi:
  ssid: !secret ssid_iot
  password: !secret password_iot
  fast_connect: true
  manual_ip:
    static_ip: 192.168.0.110
    gateway: 192.168.0.1
    subnet: 255.255.255.0

mqtt:
  broker: !secret mqtt_broker
  username: !secret mqtt_user
  password: !secret mqtt_pasword
  discovery: False
  discovery_retain: False
  birth_message:
    topic: $devicename/status
    payload: online
  will_message:
    topic: $devicename/status
    payload: offline
  on_message:
    - topic: $devicename/ota_mode
      payload: 'ON'
      then:
        - deep_sleep.prevent: deep_sleep_1
        - mqtt.publish:
            topic: $devicename/ota_mode_status
            payload: "active"
    - topic: $devicename/sleep_mode
      payload: 'ON'
      then:
        - mqtt.publish:
            topic: $devicename/ota_mode_status
            payload: "disabled"
        - deep_sleep.enter: deep_sleep_1


logger:
  level: WARN

ota:
  password: !secret ota_password

text_sensor:
  - platform: wifi_info
    ip_address:
      name: ${devicename}_ip

deep_sleep:
  id: deep_sleep_1
  run_duration: 30s
  sleep_duration: 720min
  wakeup_pin: 14
  wakeup_pin_mode: INVERT_WAKEUP

switch:
  - platform: shutdown
    name: "${devicename}_shutdown"
    id: shutdown1
  - platform: restart
    name: "${devicename}_restart"

binary_sensor:
  - platform: gpio
    pin:
      number: 14
      mode: INPUT_PULLDOWN
      inverted: True
    name: "door"
    device_class: door




sensor:
#wifi signal
  - platform: wifi_signal
    name: ${devicename}_rssi
  - platform: adc
    pin: 34
    attenuation: 11db
    filters:
    name: "${devicename}_voltage"

Basically this template switch will send/remove a retained message to the MQTT Broker

  - platform: template
    switches:
      esp32_01_switch_ota:
        value_template: "{{ is_state('binary_sensor.esp32_01_ota_status', 'on') }}"
        turn_on:
          - service: mqtt.publish
            data:
              topic: "esp32_1/ota_mode"
              payload: "ON"
              retain: true
        turn_off:
          - service: mqtt.publish
            data:
              topic: "esp32_1/sleep_mode"
              payload: "ON"
          - service: mqtt.publish
            data:
              topic: "esp32_1/ota_mode"
              payload: ""
              retain: true

Full Package with switch and notification automation is here:
esp32_1_doorsensor_elw.yaml

Comments highly appreciated!

3 Likes

I have made a change deep sleep config on my setup. The run_duration timer only starts after MQTT is connected, so if there is a network issue you will use a lot of battery power. My sensor is in a not easily accessed location so I want to minimise the times I need to recharge it, So I added this:

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

jfyi, in the UK you have everything to power your device in the ceiling rose

I know it was long time ago but in case you’re still interested, I saw here a topic where a guy did exactly the same. Basically he attached two conductive pads (foil squares) to the fridge and one long strip of foil to the inside of the fridge door so when it closes it would connect the square pads. then connect one square to the ground and the other one to GPIO and configure it as for a switch.

1 Like

Well, in Canada, in my house, for that specific lamp I do have only 2 wires and no neutral. So if the switch is open, there is no power going through the wires to the ceiling lamp.

Oh well… no luck then.
Do you have neutral in your wall switches? :wink:

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