Secure Home Monitoring with Home Assistant on Raspberry Pi: Motion, Alarm, and Video Capture
Deploying a reliable home‑security system on a Raspberry Pi with Home Assistant (HA) is straightforward once you understand the key components: a motion sensor, an alarm panel, and camera‑based video capture. This guide walks you through the complete setup—from wiring the HC‑SR501 PIR sensor to configuring HA, launching mjpg‑streamer, and sending instant email alerts with photos.
Prerequisites
- Raspberry Pi running Home Assistant (recommended 0.101.3 or newer)
- HC‑SR501 PIR motion sensor
- USB or CSI camera (any camera that works with mjpg‑streamer)
- Internet‑connected email account (e.g., Gmail) for notifications
- Basic familiarity with YAML and Docker (if running HA in a container)
1. Wiring the HC‑SR501 to the Pi
The sensor has three pins: VCC (+), GND (–), and OUT. Connect them as follows:
- VCC → Pin 5 (3.3 V)
- GND → Pin 6 (Ground)
- OUT → Pin 7 (GPIO 4)
These connections allow HA to read the sensor’s binary state via the rpi_gpio platform.
2. Installing mjpg‑Streamer
Use this guide to set up mjpg‑streamer and the optional FFMpeg recorder. The scripts in the repository launch the stream and start/stop recording on demand.
3. Configuring Home Assistant
Open /usr/share/hassio/homeassistant/configuration.yaml (or your HA config folder). Add the following sections:
# Binary sensor for PIR
binary_sensor:
- platform: rpi_gpio
ports:
7: Sensor HC‑SR501
invert_logic: false
# Manual alarm panel
alarm_control_panel:
- platform: manual
name: Home Alarm
pending_time: 60
delay_time: 40
code: 1234
# Command‑line switches to control mjpg‑streamer
switch:
- platform: command_line
switches:
start_stop_motion_rec_timelapse:
friendly_name: 'Record motion timelapse'
command_on: "curl https://localhost/start_mjpgstrm.php && curl https://localhost/rec-motion-mjpg.php"
command_off: "curl https://localhost/stop_mjpgstrm.php && curl https://localhost/rec-motion-mjpg-stop.php"
# Shell commands for custom scripts
shell_command:
take_snapshot_webcam: '/config/scripts/takeSnapshotWebcam.sh'
start_mgpg_streamer: 'curl https://localhost/start_mjpgstrm.php'
stop_mgpg_streamer: 'curl https://localhost/stop_mjpgstrm.php'
start_motion_rec: 'curl https://localhost/rec-motion-mjpg.php'
stop_motion_rec: 'curl https://localhost/rec-motion-mjpg-stop.php'
# Email notifier (Gmail example)
notify:
- name: ha_sendmail
platform: smtp
server: smtp.gmail.com
port: 587
timeout: 15
sender: <your‑email>
encryption: starttls
username: <your‑email>
password: <app‑password>
recipient:
- <your‑email>
sender_name: My Home Assistant
After editing, validate the configuration via Configuration → Server Controls → Check Config, then reload automations and scripts.
4. Automation Logic
Create the following automations in automation.yaml to tie the sensor, alarm, and camera together.
# 1. Trigger when armed away and motion detected
- alias: 'Trigger alarm while armed away'
trigger:
- platform: state
entity_id: binary_sensor.sensor_hc_sr501
to: 'on'
condition:
- condition: state
entity_id: alarm_control_panel.home_alarm
state: armed_away
action:
- service: shell_command.start_mgpg_streamer
- service: shell_command.start_motion_rec
- service: alarm_control_panel.alarm_trigger
target:
entity_id: alarm_control_panel.home_alarm
# 2. Stop recording when alarm is disarmed
- alias: 'Stop recording when alarm disarmed'
trigger:
- platform: state
entity_id: alarm_control_panel.home_alarm
to: 'disarmed'
action:
- service: shell_command.stop_mgpg_streamer
- service: shell_command.stop_motion_rec
- service: persistent_notification.create
data:
message: 'The alarm is disarmed at {{ states(''sensor.date_time'') }}'
# 3. Send photo on alarm trigger
- alias: 'Email alert on alarm trigger'
trigger:
- platform: state
entity_id: alarm_control_panel.home_alarm
to: 'triggered'
action:
- service: persistent_notification.create
data:
message: 'Alarm triggered by motion sensor.'
- delay: { seconds: 4 }
- service: script.webcam_snapshot
- service: notify.ha_sendmail
data:
title: 'Intruder alert'
message: '{{now().strftime("%H:%M %Y-%m-%d")}}: Alarm triggered by motion sensor.'
data:
images:
- /config/camera/snapshot.jpg
The script.webcam_snapshot should invoke takeSnapshotWebcam.sh, which captures a single frame and stores it as /config/camera/snapshot.jpg for the email attachment.
5. Docker Volume Note
If HA runs inside Docker, remember that the container’s /config folder maps to /usr/share/hassio/homeassistant on the host. Use docker inspect homeassistant | less to confirm volume bindings.
6. Testing the System
- Arm the alarm to
armed_awayvia the HA UI. - Trigger the PIR sensor (e.g., by walking past it).
- Verify that mjpg‑streamer starts, the motion recorder captures a short clip, and an email with a snapshot is received.
- Disarm the alarm and confirm that recording stops and a disarm notification appears.
Conclusion
With these steps you have a self‑contained, low‑cost home‑security solution that blends motion detection, alarm control, and instant photo alerts—all orchestrated by Home Assistant on a Raspberry Pi.
Feel free to adapt the scripts and notification settings to your environment. Happy automating!
Manufacturing process
- Build a Raspberry Pi Temperature Logger with a $5 I2C Sensor
- Build a Raspberry Pi Weather Station that Emails Daily Weather Data
- Connecting HC‑SR04 Ultrasonic Sensor to Raspberry Pi 3 – A Complete Guide
- Build a DIY Infrared Motion Sensor for Raspberry Pi – Step‑by‑Step Guide
- Control an LED with a PIR Motion Sensor on Raspberry Pi
- Build a Low‑Cost Raspberry Pi Soil Moisture Sensor for Smart Irrigation
- Build a Raspberry Pi Home Temperature Monitor with MCP9808, InfluxDB & Grafana
- Master Raspberry Pi GPIO: Interfacing a PIR Motion Sensor on B+/Model 2
- Build a Raspberry Pi‑Powered Home Automation System for Remote Control
- How to Connect, Calibrate, and Program the HC‑SR501 PIR Motion Sensor with a Raspberry Pi