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

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

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