Industrial manufacturing
Industrial Internet of Things | Industrial materials | Equipment Maintenance and Repair | Industrial programming |
home  MfgRobots >> Industrial manufacturing >  >> Manufacturing Technology >> Manufacturing process

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

1. Wiring the HC‑SR501 to the Pi

The sensor has three pins: VCC (+), GND (–), and OUT. Connect them as follows:

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

  1. Arm the alarm to armed_away via the HA UI.
  2. Trigger the PIR sensor (e.g., by walking past it).
  3. Verify that mjpg‑streamer starts, the motion recorder captures a short clip, and an email with a snapshot is received.
  4. 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

  1. Build a Raspberry Pi Temperature Logger with a $5 I2C Sensor
  2. Build a Raspberry Pi Weather Station that Emails Daily Weather Data
  3. Connecting HC‑SR04 Ultrasonic Sensor to Raspberry Pi 3 – A Complete Guide
  4. Build a DIY Infrared Motion Sensor for Raspberry Pi – Step‑by‑Step Guide
  5. Control an LED with a PIR Motion Sensor on Raspberry Pi
  6. Build a Low‑Cost Raspberry Pi Soil Moisture Sensor for Smart Irrigation
  7. Build a Raspberry Pi Home Temperature Monitor with MCP9808, InfluxDB & Grafana
  8. Master Raspberry Pi GPIO: Interfacing a PIR Motion Sensor on B+/Model 2
  9. Build a Raspberry Pi‑Powered Home Automation System for Remote Control
  10. How to Connect, Calibrate, and Program the HC‑SR501 PIR Motion Sensor with a Raspberry Pi