Door reed sensor on Raspberry Pi with phone notifications
Wire a magnetic door contact to GPIO, push alerts via ntfy and optional Home Assistant MQTT discovery—gpiozero on Pi 4 and Pi 5.
AI-generated illustration
Wire a magnetic door contact to GPIO, push alerts via ntfy and optional Home Assistant MQTT discovery—gpiozero on Pi 4 and Pi 5.
Also available in French.
Know when the front door, shed or cupboard opens: a two-wire reed switch needs no sensor battery— the Pi detects open/close. This script sends ntfy push (free, open source) and optionally publishes a door entity in Home Assistant via MQTT.
| Time | Level | Budget |
|---|---|---|
| ~45 min | Beginner | Reed switch is inexpensive—check retailers |
Reed (normally open, closes with magnet): UK · US
Internal pull-up—no external resistor.
| Reed | Pi (physical) |
|---|---|
| Wire 1 | 11 (GPIO17) |
| Wire 2 | 9 (GND) |
Door closed = magnet near = contact closed = GPIO low. Never apply external voltage to the reed line.
Same venv pattern as our other Pi sensor guides (Bookworm, gpiozero + lgpio):
sudo apt update
sudo apt install -y python3-venv python3-gpiozero python3-lgpio
mkdir -p ~/capteurs && cd ~/capteurs
python3 -m venv --system-site-packages venv
source venv/bin/activate
pip install paho-mqtt # if using HA
ntfy: install the app, subscribe to a long random topic on ntfy.sh (public topics are readable if guessed) or self-host. Set NTFY_URL=https://ntfy.sh/your-secret-topic.
Script: porte_reed_notification.py.
Mosquitto + MQTT integration; dedicated user; discovery creates binary_sensor door entity.
Instead of every open, notify only if open 5 minutes:
alias: Front door left open
triggers:
- trigger: state
entity_id: binary_sensor.raspberry_pi_door_front_door
to: "on"
for: "00:05:00"
actions:
- action: notify.notify
data:
message: "Front door open for 5 minutes."
Run the script without NTFY_URL if you only want this HA automation.
porte-reed.service with Environment=NTFY_URL=... and MQTT_HOST=... — enable on boot.
when_pressed / when_released for normally-closed reeds.bounce_time to 0.2–0.3 s.curl -d test https://ntfy.sh/your-topic from the Pi.Sources: gpiozero Button · ntfy publish · HA MQTT binary sensor.
Also: garage door ESP32 · PIR motion.
Torn between two models or stuck on a build? Drop us a line and we will get back to you by email.