DIY Smart Door Sensor with ESP32 and Home Assistant: A Step-by-Step Guide

April 14, 2025 · smart home, ESP32, Home Assistant, DIY, home automation, IoT

DIY Smart Door Sensor with ESP32 and Home Assistant

Ever walked out the door and wondered, Did I close it? I’ve been there too—until I built a DIY smart door sensor using an ESP32 and Home Assistant. Now, my phone buzzes if the door’s left ajar, and I’ve even set up automations to turn off the AC when the door’s open for too long (goodbye, wasted energy!). Here’s how you can build your own.

Why Build a Smart Door Sensor?

Commercial door sensors work great, but they’re often pricey and locked into proprietary ecosystems. With an ESP32 and a magnetic reed switch, you can build a customizable, privacy-friendly alternative that integrates seamlessly with Home Assistant. Plus, it’s a fun weekend project!

Materials You’ll Need

Here’s what I used (with affiliate-free links for transparency):

Pro Tip: If you’re placing the sensor outdoors, consider a waterproof reed switch and an ESP32 enclosure.

Step 1: Wiring the Reed Switch to the ESP32

The reed switch is a simple device: it closes (completes the circuit) when near a magnet and opens when the magnet moves away. Here’s how to wire it:

  1. Connect one end of the reed switch to GPIO 4 on the ESP32 (you can use any GPIO, but avoid pins 6-11 if using SPI).
  2. Connect the other end to the 3.3V pin on the ESP32.
  3. Add a 10kΩ pull-down resistor between GPIO 4 and GND to prevent floating signals.

Here’s a quick diagram:

ESP32 GPIO 4 → Reed Switch → 3.3V
                |
            10kΩ Resistor
                |
               GND

Step 2: Flashing the ESP32 with ESPHome

ESPHome makes it dead simple to program the ESP32 for Home Assistant. Here’s the configuration:

  1. Install ESPHome via Home Assistant’s add-on store or Docker.
  2. Create a new device (smart_door_sensor.yaml) and paste this code:
esphome:
  name: smart-door-sensor
  platform: ESP32
  board: esp32dev

wifi:
  ssid: "YOUR_WIFI_SSID"
  password: "YOUR_WIFI_PASSWORD"

# Enable logging for debugging
logger:

# Enable Home Assistant API
api:
  password: "YOUR_API_PASSWORD"

# Reed switch sensor
binary_sensor:
  - platform: gpio
    pin: GPIO4
    name: "Front Door Sensor"
    device_class: door
  1. Flash the ESP32 by running:
esphome run smart_door_sensor.yaml
Troubleshooting: If the ESP32 won’t connect, double-check your WiFi credentials and ensure your router isn’t blocking new devices.

Step 3: Integrating with Home Assistant

Once flashed, the sensor should auto-appear in Home Assistant under Settings > Devices & Services. Now, let’s set up a notification:

  1. Go to Settings > Automations & Scenes.
  2. Create a new automation with this trigger:
trigger:
  - platform: state
    entity_id: binary_sensor.front_door_sensor
    to: "on" # Door is open
  1. Add an action to notify your phone (replace with your notification service):
action:
  - service: notify.mobile_app_your_phone
    data:
      message: "Front door is open!"

Step 4: Advanced Automations

Here’s where it gets fun. Try these ideas:

Example automation to turn off AC:

alias: "Turn off AC when door is open"
trigger:
  - platform: state
    entity_id: binary_sensor.front_door_sensor
    to: "on"
    for:
      minutes: 5
action:
  - service: climate.turn_off
    target:
      entity_id: climate.living_room_ac

Troubleshooting Common Issues

FAQ

Can I use an ESP8266 instead of an ESP32?

Yes! The wiring and ESPHome config are nearly identical.

How do I power this battery?

For battery use, swap to an ESP32 with deep sleep support and a LiPo battery. Here’s a great guide.

Can I monitor multiple doors?

Absolutely! Just add more reed switches and configure them in ESPHome.

Next Steps

Now go forth and never second-guess your door’s status again! 🚪💡