ESP32 Reed Switch Door/Window Sensor for Home Assistant (ESPHome & MQTT) – Complete 2025 Guide

A reed switch is one of the simplest and most reliable ways to detect when a door or window opens. When combined with an ESP32, it becomes a fast, local, battery-free, and cloud-free door sensor that integrates perfectly with Home Assistant.

This guide covers both ESPHome and MQTT methods, along with wiring diagrams and automations.


1. What Is a Reed Switch?

A reed switch contains two tiny metal contacts sealed inside a small glass tube.
When a magnet comes close, the contacts pull together (closed).
When the magnet moves away, the contacts separate (open).

Perfect for:

  • Doors
  • Windows
  • Cabinets
  • Garage doors
  • Mailboxes
  • Safes

2. Hardware Required

  • ESP32 DevKit
  • Reed switch module OR bare magnetic reed switch
  • Small magnet
  • Jumper wires
  • Resistor (10k pull-up if using bare reed switch)
  • USB cable

3. Wiring Diagram

Using a reed switch module (recommended)

Most modules include a pull-up resistor.

ESP32 → Reed Module
3.3V → VCC
GND  → GND
GPIO27 → OUT

Bare reed switch wiring (requires resistor)

ESP32 → Reed Switch
GPIO27 → Reed Switch → GND
GPIO27 → 10k → 3.3V

GPIO27 is used as an example. Any GPIO except 34–39 can be used.


METHOD 1 — ESPHome Integration

ESPHome provides a simple binary_sensor configuration.


4. ESPHome Reed Switch YAML

esphome:
  name: esp32-reed
  platform: ESP32
  board: esp32dev

wifi:
  ssid: "YOUR_WIFI"
  password: "YOUR_PASSWORD"

logger:
api:
ota:

binary_sensor:
  - platform: gpio
    name: "Front Door"
    pin:
      number: 27
      mode:
        input: true
        pullup: true
    device_class: door
    filters:
      - delayed_off: 50ms

Notes

  • pullup: true ensures stable readings with bare reed switches
  • device_class: door gives correct icons in Home Assistant
  • delayed_off prevents bouncing

ESPHome devices appear automatically in Home Assistant.


METHOD 2 — MQTT Integration (Matches Your Style)

This method uses Home Assistant’s MQTT binary_sensor block, just like your PIR and relay articles.


5. Home Assistant configuration.yaml — MQTT Door Sensor

Add under:

mqtt:
  binary_sensor:

MQTT YAML Block

mqtt:
  binary_sensor:
    - name: "Front Door"
      state_topic: "home/door/front"
      payload_on: "OPEN"
      payload_off: "CLOSED"
      device_class: door

6. ESP32 MQTT Code for Reed Switch

#include <WiFi.h>
#include <PubSubClient.h>

#define REED_PIN 27
#define WIFI_SSID "YOUR_WIFI"
#define WIFI_PASS "YOUR_PASSWORD"
#define MQTT_SERVER "192.168.0.10"

WiFiClient espClient;
PubSubClient client(espClient);

int lastState = -1;

void setup() {
  Serial.begin(115200);

  pinMode(REED_PIN, INPUT_PULLUP);

  WiFi.begin(WIFI_SSID, WIFI_PASS);
  while (WiFi.status() != WL_CONNECTED) delay(500);

  client.setServer(MQTT_SERVER, 1883);
}

void loop() {
  if (!client.connected()) {
    while (!client.connected()) client.connect("ESP32_Door");
  }

  int state = digitalRead(REED_PIN);

  if (state != lastState) {
    lastState = state;

    if (state == LOW)
      client.publish("home/door/front", "OPEN");
    else
      client.publish("home/door/front", "CLOSED");
  }

  client.loop();
  delay(50);
}

The ESP32 now sends "OPEN" and "CLOSED" events to Home Assistant instantly.


7. Example Home Assistant Dashboard

type: entities
entities:
  - entity: binary_sensor.front_door


8. Useful Automations

Notify when front door opens

automation:
  - alias: "Front Door Open Alert"
    trigger:
      - platform: state
        entity_id: binary_sensor.front_door
        to: "on"
    action:
      - service: notify.mobile_app
        data:
          message: "Front door just opened."

Turn on lights when door opens at night

  - alias: "Hallway Lights on Door Open"
    trigger:
      - platform: state
        entity_id: binary_sensor.front_door
        to: "on"
    condition:
      - condition: sun
        after: sunset
    action:
      - service: light.turn_on
        target:
          entity_id: light.hallway

Send an alert if the door stays open for too long

  - alias: "Door Left Open Warning"
    trigger:
      - platform: state
        entity_id: binary_sensor.front_door
        to: "on"
        for: "00:02:00"
    action:
      - service: notify.mobile_app
        data:
          message: "Front door has been open for 2 minutes!"

9. Troubleshooting

Sensor reads OPEN all the time

  • Magnet placed too far away
  • Reed switch wired backwards (use NC/NO pins correctly on modules)

Sensor flickers

  • Add delayed_off: 50ms filter in ESPHome
  • Use INPUT_PULLUP in Arduino code

Door is detected as CLOSED instead of OPEN

  • Reverse logic:

ESPHome:

inverted: true

MQTT (Arduino code):

if (state == LOW) → CLOSED  
if (state == HIGH) → OPEN

10. Tips for Reliable Installation

  • Mount magnet and reed switch parallel
  • Keep gap < 1 cm
  • Use 3M tape + screws for stability
  • Keep ESP32 away from metal door frames if WiFi is weak
  • For high-security doors, use two reed switches for redundancy

Keywords

esp32 reed switch
esp32 door sensor
home assistant door sensor
mqtt reed switch
esphome reed switch
esp32 window sensor
reed switch esp32 wiring
magnetic door sensor home assistant

Newsletter Updates

Enter your email address below and subscribe to our newsletter

Leave a Reply

Your email address will not be published. Required fields are marked *