Reed sensor only working for short period of time

Hello everyone,

I am new to ESP boards and just got my hands on an ESP32. I attached a reed sensor to it on my garage door and looked up for several tutorials out there on the net.
The ESP is added to HA through ESPHome and I am getting a correct value from the sensor in the first minutes.

After a while without any changes on the sensor it gets “frozen” to closed state and is not updated in HA.

After some digging I found that maybe the device gets disconnected because going in “sleep”. So I added the line below.

power_save_mode: none

After that the ESP does not show offline anymore, but my sensor value still doesn’t get updated.

I tried adding some Wifi sensors to check the connection status, it get updated every minute so there is some communication between HA and my ESP.
Weird behavior is that if I put my finger on the inside of the reed sensor it “reactivates” for some time before getting unresponsive again. I thought the sensor couldn’t detect my magnet and therefore does not change state but when “active” it works flawlessly.

My configuration file is the following:

esphome:
  name: esp32-garage-door
  friendly_name: esp32-garage_door

esp32:
  board: esp32dev
  framework:
    type: arduino

# Enable logging
logger:

# Enable Home Assistant API
api:
  encryption:
    key: "REDACTED"

ota:
  password: "REDACTED"

wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password
  power_save_mode: none

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "Esp32-Garage-Door"
    password: "REDACTED"

captive_portal:


binary_sensor:
  - platform: gpio
    name: "Garage Door is Open"
    pin:
      number: 2
      inverted: true
      mode: INPUT_PULLUP

sensor:
  - platform: wifi_signal # Reports the WiFi signal strength/RSSI in dB
    name: "WiFi Signal dB"
    id: wifi_signal_db
    update_interval: 60s
    entity_category: "diagnostic"

  - platform: copy # Reports the WiFi signal strength in %
    source_id: wifi_signal_db
    name: "WiFi Signal Percent"
    filters:
      - lambda: return min(max(2 * (x + 100.0), 0.0), 100.0);
    unit_of_measurement: "Signal %"
    entity_category: "diagnostic"

The log file when in “sleep” is:

INFO Reading configuration /config/esphome/esp32-garage-door.yaml...
WARNING GPIO2 is a Strapping PIN and should be avoided.
Attaching external pullup/down resistors to strapping pins can cause unexpected failures.
See https://esphome.io/guides/faq.html#why-am-i-getting-a-warning-about-strapping-pins
INFO Starting log output from esp32-garage-door.local using esphome API
INFO Successfully connected to esp32-garage-door.local
[22:06:38][I][app:102]: ESPHome version 2023.4.2 compiled on May  5 2023, 09:29:44
[22:06:38][C][wifi:505]: WiFi:
[22:06:38][C][wifi:363]:   Local MAC: 58:BF:25:38:09:F8
[22:06:38][C][wifi:364]:   SSID: [redacted]
[22:06:38][C][wifi:365]:   IP Address: 192.168.1.69
[22:06:38][C][wifi:367]:   BSSID: [redacted]
[22:06:38][C][wifi:368]:   Hostname: 'esp32-garage-door'
[22:06:38][C][wifi:370]:   Signal strength: -38 dB ▂▄▆█
[22:06:38][C][wifi:374]:   Channel: 1
[22:06:38][C][wifi:375]:   Subnet: 255.255.255.0
[22:06:38][C][wifi:376]:   Gateway: 192.168.1.1
[22:06:38][C][wifi:377]:   DNS1: 192.168.1.1
[22:06:38][C][wifi:378]:   DNS2: 0.0.0.0
[22:06:38][C][logger:294]: Logger:
[22:06:38][C][logger:295]:   Level: DEBUG
[22:06:38][C][logger:296]:   Log Baud Rate: 115200
[22:06:38][C][logger:297]:   Hardware UART: UART0
[22:06:38][D][api.connection:961]: Home Assistant 2023.4.6 (192.168.1.35): Connected successfully
[22:06:38][C][gpio.binary_sensor:015]: GPIO Binary Sensor 'Garage Door is Open'
[22:06:38][C][gpio.binary_sensor:016]:   Pin: GPIO2
[22:06:38][C][copy.sensor:015]: Copy Sensor 'WiFi Signal Percent'
[22:06:38][C][copy.sensor:015]:   Device Class: 'signal_strength'
[22:06:38][C][copy.sensor:015]:   State Class: 'measurement'
[22:06:38][C][copy.sensor:015]:   Unit of Measurement: 'Signal %'
[22:06:38][C][copy.sensor:015]:   Accuracy Decimals: 0
[22:06:38][C][captive_portal:088]: Captive Portal:
[22:06:38][C][mdns:108]: mDNS:
[22:06:38][C][mdns:109]:   Hostname: esp32-garage-door
[22:06:38][C][ota:093]: Over-The-Air Updates:
[22:06:38][C][ota:094]:   Address: esp32-garage-door.local:3232
[22:06:38][C][ota:097]:   Using Password.
[22:06:38][C][api:138]: API Server:
[22:06:38][C][api:139]:   Address: esp32-garage-door.local:6053
[22:06:38][C][api:141]:   Using noise encryption: YES
[22:06:38][C][wifi_signal.sensor:009]: WiFi Signal 'WiFi Signal dB'
[22:06:38][C][wifi_signal.sensor:009]:   Device Class: 'signal_strength'
[22:06:38][C][wifi_signal.sensor:009]:   State Class: 'measurement'
[22:06:38][C][wifi_signal.sensor:009]:   Unit of Measurement: 'dBm'
[22:06:38][C][wifi_signal.sensor:009]:   Accuracy Decimals: 0
[22:06:38][W][api.connection:087]: Home Assistant 2023.4.6 (192.168.1.35): Connection closed
[22:06:38][D][api:102]: Accepted 192.168.1.35
[22:06:39][D][api.connection:961]: Home Assistant 2023.4.6 (192.168.1.35): Connected successfully
[22:06:39][W][api.connection:087]: Home Assistant 2023.4.6 (192.168.1.35): Connection closed
[22:06:39][D][api:102]: Accepted 192.168.1.35
[22:06:39][D][api.connection:961]: Home Assistant 2023.4.6 (192.168.1.35): Connected successfully
[22:06:40][D][api:102]: Accepted 192.168.1.35
[22:06:40][W][api.connection:087]: Home Assistant 2023.4.6 (192.168.1.35): Connection closed
[22:06:40][D][api.connection:961]: Home Assistant 2023.4.6 (192.168.1.35): Connected successfully
[22:06:40][D][api:102]: Accepted 192.168.1.35
[22:06:40][W][api.connection:087]: Home Assistant 2023.4.6 (192.168.1.35): Connection closed
[22:06:40][D][api.connection:961]: Home Assistant 2023.4.6 (192.168.1.35): Connected successfully
[22:06:40][W][api.connection:087]: Home Assistant 2023.4.6 (192.168.1.35): Connection closed

To be honest I am kinda lost here. Some help would be appreciated.

It appears the api connection breaks. Perhaps wifi is poor? Perhaps try a fixed IP address?

The device is literally 2m max away from router.

I fixed the IP and didn’t change anything unfortunately.

Below are the logs from the moment I pass my finger in front of the sensor. It seems it is getting woken up. But why isn’t the magnet producing the same results?


INFO Reading configuration /config/esphome/esp32-garage-door.yaml...
WARNING GPIO2 is a Strapping PIN and should be avoided.
Attaching external pullup/down resistors to strapping pins can cause unexpected failures.
See https://esphome.io/guides/faq.html#why-am-i-getting-a-warning-about-strapping-pins
INFO Starting log output from esp32-garage-door.local using esphome API
INFO Successfully connected to esp32-garage-door.local
[16:43:20][I][app:102]: ESPHome version 2023.4.4 compiled on May  7 2023, 11:00:56
[16:43:20][C][wifi:505]: WiFi:
[16:43:20][C][wifi:363]:   Local MAC: 58:BF:25:38:09:F8
[16:43:20][C][wifi:364]:   SSID: [redacted]
[16:43:20][C][wifi:365]:   IP Address: 192.168.1.69
[16:43:20][C][wifi:367]:   BSSID: [redacted]
[16:43:20][C][wifi:368]:   Hostname: 'esp32-garage-door'
[16:43:20][C][wifi:370]:   Signal strength: -42 dB ▂▄▆█
[16:43:20][C][wifi:374]:   Channel: 1
[16:43:20][C][wifi:375]:   Subnet: 255.255.255.0
[16:43:20][C][wifi:376]:   Gateway: 192.168.1.1
[16:43:20][C][wifi:377]:   DNS1: 192.168.1.1
[16:43:20][C][wifi:378]:   DNS2: 0.0.0.0
[16:43:20][C][logger:294]: Logger:
[16:43:20][C][logger:295]:   Level: DEBUG
[16:43:20][C][logger:296]:   Log Baud Rate: 115200
[16:43:20][C][logger:297]:   Hardware UART: UART0
[16:43:20][C][gpio.binary_sensor:015]: GPIO Binary Sensor 'Garage Door is Open'
[16:43:20][C][gpio.binary_sensor:016]:   Pin: GPIO2
[16:43:20][C][copy.sensor:015]: Copy Sensor 'WiFi Signal Percent'
[16:43:20][C][copy.sensor:015]:   Device Class: 'signal_strength'
[16:43:20][C][copy.sensor:015]:   State Class: 'measurement'
[16:43:20][C][copy.sensor:015]:   Unit of Measurement: 'Signal %'
[16:43:20][C][copy.sensor:015]:   Accuracy Decimals: 0
[16:43:20][C][captive_portal:088]: Captive Portal:
[16:43:20][C][mdns:108]: mDNS:
[16:43:20][C][mdns:109]:   Hostname: esp32-garage-door
[16:43:20][C][ota:093]: Over-The-Air Updates:
[16:43:20][C][ota:094]:   Address: esp32-garage-door.local:3232
[16:43:20][C][ota:097]:   Using Password.
[16:43:20][C][api:138]: API Server:
[16:43:20][C][api:139]:   Address: esp32-garage-door.local:6053
[16:43:20][C][api:141]:   Using noise encryption: YES
[16:43:20][C][wifi_signal.sensor:009]: WiFi Signal 'WiFi Signal dB'
[16:43:20][C][wifi_signal.sensor:009]:   Device Class: 'signal_strength'
[16:43:20][C][wifi_signal.sensor:009]:   State Class: 'measurement'
[16:43:20][C][wifi_signal.sensor:009]:   Unit of Measurement: 'dBm'
[16:43:20][C][wifi_signal.sensor:009]:   Accuracy Decimals: 0
[16:43:26][D][binary_sensor:036]: 'Garage Door is Open': Sending state ON
[16:43:31][D][sensor:110]: 'WiFi Signal dB': Sending state -40.00000 dBm with 0 decimals of accuracy
[16:43:31][D][sensor:110]: 'WiFi Signal Percent': Sending state 100.00000 Signal % with 0 decimals of accuracy
[16:43:52][D][binary_sensor:036]: 'Garage Door is Open': Sending state OFF
[16:44:31][D][sensor:110]: 'WiFi Signal dB': Sending state -39.00000 dBm with 0 decimals of accuracy
[16:44:31][D][sensor:110]: 'WiFi Signal Percent': Sending state 100.00000 Signal % with 0 decimals of accuracy

If it’s an ESP32, use another GPIO instead of the current GPIO2. ESPHome gives you the warning, letting you know that GPIO2 is not recommended.
WARNING GPIO2 is a Strapping PIN and should be avoided.

I tried changing to pin GPIO4 which should be fine.
I am still getting the same message even though it should be totally open according to my board’s documentation.

https://randomnerdtutorials.com/esp32-pinout-reference-gpios/

https://www.mischianti.org/wp-content/uploads/2020/11/ESP32-DOIT-DEV-KIT-v1-pinout-mischianti.png

In fact I tried using pins 2 / 4 / 16 / 17.
Only pin 2 is giving me results from my sensor with the same results as before. While the other pins seems to be idle.

Just noticed that you have 0.0.0.0 specified as a DNS server. Why?

To be honest I don’t know.
It’s DNS2. DNS1 is my internet provider’s router.
I am using basic internet parameters that came with the router, I just strapped an NGINX reverse proxy on top of that.

1 Like

To add to the list of oddities (at least from my point of newb’s view) GPIO4 is sending the sensor status … if I link it to GPIO2.

To explain:

Sensor linked to GPIO2 only = sensor status updated correctly

Sensor linked to GPIO4 only = no sensor status updated

Sensor wired to GPIO4 and GPIO2 and GPIO4 linked together = both pins are reporting the sensor status

The more I deep dive in this, the more confused I am :sweat_smile:

Might it be a flaky reed switch? The really low cost ones you can find out there do sometimes have problems with reliability. I have had those just quit working for me.

If you have access to a volt/ohm meter, you can verify that the reed is actually working reliably.

Another possibility is the that location of the reed is close to some ferrous metal that is magnetized just enough to holding the contact closed at times.

I unmounted the ESP from its location. And it seems to stay up.
Your second option might be it. I’ll keep it stashed on the desk for few days and check it regularly to validate the theory.

The next problem will be to find non metalic parts on the garage door / frame. :grin:

1 Like

Ok, problem NOT solved :upside_down_face:

I left the ESP on my desk running two scenarios:

  • First: “Garage door” is open and I only close it from time to time => No issues, it runs smoothly. Everytime I am “Closing” the garage door it get updated and I receive the value.

  • Second: “Garage door” is closed by default (which should be the case in real scenario) and I only open the reed sensor from time to time => It works in the beginning. After leaving it for few hours it gets “stale” and unrespoding on the reed sensor. The wifi sensor defined is still periodically updating its value.

I really don’t get it.