Home Assistant on Raspberry Piで安全なホーム監視 – モーション、アラーム、ビデオ撮影
Raspberry PiにHome Assistant(HA)を用いた信頼性の高いホームセキュリティシステムを導入するのは、主要コンポーネントを理解すれば簡単です:モーションセンサー、アラームパネル、カメラベースのビデオ撮影。本ガイドでは、HC-SR501 PIRセンサーの配線からHA設定、mjpg-streamer起動、写真付き即時メールアラート送信まで、完全なセットアップをウォークスルーします。
前提条件
- Home Assistant稼働中のRaspberry Pi(推奨0.101.3以降)
- HC-SR501 PIRモーションセンサー
- USBまたはCSIカメラ(mjpg-streamerで動作する任意のカメラ)
- インターネット接続されたメールアカウント(例:Gmail)で通知用
- YAMLとDockerの基本知識(HAをコンテナで実行する場合)
1. HC-SR501をPiに配線
センサーは3ピン:VCC(+)、GND(–)、OUT。以下のように接続:
- VCC → ピン5(3.3V)
- GND → ピン6(グランド)
- OUT → ピン7(GPIO 4)
これらの接続により、HAはrpi_gpioプラットフォーム経由でセンサーのバイナリ状態を読み取れます。
2. mjpg-Streamerのインストール
このガイドに従い、mjpg-streamerとオプションのFFMpegレコーダーをセットアップ。リポジトリ内のスクリプトでストリーム起動とオンデマンド録画開始/停止を行います。
3. Home Assistantの設定
/usr/share/hassio/homeassistant/configuration.yaml(またはHA設定フォルダ)を開き、以下のセクションを追加:
# PIR用バイナリセンサー
binary_sensor:
- platform: rpi_gpio
ports:
7: Sensor HC-SR501
invert_logic: false
# 手動アラームパネル
alarm_control_panel:
- platform: manual
name: Home Alarm
pending_time: 60
delay_time: 40
code: 1234
# 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_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'
# メール通知(Gmail例)
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
編集後、Configuration → Server Controls → Check Configで設定検証、オートメーションとスクリプトをリロード。
4. オートメーションロジック
automation.yamlに以下のオートメーションを作成し、センサー、アラーム、カメラを連携:
# 1. アウェイアーム中にモーション検知でアラーム発動
- 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. アラーム解除で録画停止
- 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. アラーム発動時に写真をメール送信
- 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
script.webcam_snapshotはtakeSnapshotWebcam.shを呼び出し、単一フレームを撮影して/config/camera/snapshot.jpgとして保存、メール添付用にします。
5. Dockerボリュームに関する注意
HAがDocker内で実行されている場合、コンテナの/configフォルダはホストの/usr/share/hassio/homeassistantにマッピングされます。docker inspect homeassistant | lessでボリュームバインディングを確認してください。
6. システムテスト
- HA UIでアラームを
armed_awayにアーム。 - PIRセンサーをトリガー(例:前を通過)。
- mjpg-streamer起動、モーションレコーダーが短いクリップを撮影、スナップショット付きメール受信を確認。
- アラームを解除し、録画停止と解除通知の出現を確認。
結論
これらのステップで、モーション検知、アラーム制御、即時写真アラートを融合した、自己完結型・低コスト・ホームセキュリティソリューションが完成します。すべてHome Assistant on Raspberry Piでオーケストレートされています。
スクリプトと通知設定を環境に合わせて自由に調整してください。快適な自動化を!
製造プロセス
- 5ドルI2C温度センサーで作るRaspberry Pi温度ロガー
- Raspberry PiとPythonでリモート温度センサーを構築 – ステップバイステップガイド
- Raspberry Piで気象観測ステーションを構築し、日次天気データをメール送信
- DS1820ワンワイヤー温度センサーをGPIOでRaspberry Piに接続する方法
- HC-SR04超音波距離センサーをRaspberry Pi 3に接続 – 完全ガイド
- Raspberry Pi用DIY赤外線モーションセンサー構築 - ステップバイステップガイド
- 家庭用の低コストワイヤレスセンサーネットワークを構築
- Raspberry PiでPIRモーションセンサーとLEDを制御
- Adafruit T-Cobbler PlusでヘッドレスRaspberry Pi 2水漏れアラームを構築
- Raspberry Piで低コスト土壌水分センサーを構築 - スマート灌漑に活用
- Raspberry Piでリニアホールセンサーを読み取り: JavaガイドとADC0832の知見