Hackster is hosting Hackster Holidays, Ep. 7: Livestream & Giveaway Drawing. Watch previous episodes or stream live on Friday!Stream Hackster Holidays, Ep. 7 on Friday!
Viat Chi
Published © GPL3+

Motion sensor, alarm, video recording in HA on Raspberry Pi

Connecting the motion sensor, alarm and video recording in Home Assistant on Raspberry pi.

IntermediateFull instructions providedOver 1 day4,548
Motion sensor, alarm, video recording in HA on Raspberry Pi

Things used in this project

Story

Read more

Schematics

spec_dURaNphSQ4.png

Code

Code snippet #1

Plain text
binary_sensor:
  - platform: rpi_gpio
    #name: HC-SR501
    ports:
      7: Sensor HC-SR501
    invert_logic: false

Code snippet #2

Plain text
  
alarm_control_panel:
  - platform: manual
    name: Home Alarm
    pending_time: 60
     delay time 40
     triggered:
    pending_time: 0
    code: 1234

Code snippet #3

Plain text
 - platform: command_line
   switches:
      start_stop_motion_rec_timelapse:
        friendly_name: 'Record motion timelapse video'
        command_on: 'curl http://localhost/start_mjpgstrm.php  && curl http://localhost//rec-motion-mjpg.php'
        command_off: 'curl http://localhost/stop_mjpgstrm.php && curl http://localhost/rec-motion-mjpg-stop.php'

Code snippet #5

Plain text
shell_command:
#start the script that generates a snapshot for the mailing list
  take_snapshot_webcam: '/config/scripts/takeSnapshotWebcam.sh'
#starting our mjpg-streamer 
  start_mgpg_streamer: 'curl http://localhost/start_mjpgstrm.php'
#stopping mjpg-streamer
  stop_mgpg_streamer: 'curl http://localhost/stop_mjpgstrm.php'
#start the script that generates snapshots for 5 seconds
  start_motion_rec: 'curl http://localhost/rec-motion-mjpg.php'
#stop it
  stop_motion_rec: 'curl http://localhost/rec-motion-mjpg-stop.php'

Code snippet #6

Plain text
notify:
  - name: ha_sendmail
    platform: smtp
#if mail is on gmail
    server: smtp.gmail.com
    port: 587
    timeout: 15
#from whom we send
    sender: user@gmail.com
    encryption: starttls
    username: user@gmail.com
    password: passwd
#to whom we send (you can use one box and send it to yourself, why not)
    recipient:
      - user@gmail.com
    sender_name: My Home Assistant

Code snippet #7

Plain text
#first action bundle (alias “triggered by sensor if security is enabled”
- alias: 'Trigger alarm while armed away'
#alias is triggered when the sensor is in the “on” position
  trigger: 
    - platform: state
      entity_id: binary_sensor.sensor_hc_sr501
      to: 'on'
#provided that the security mode is enabled in the absence of people at home “armed away”
  condition:
    - condition: state
      entity_id: alarm_control_panel.home_alarm
      state: armed_away
#and then performs the action 
  action:
#Running mjpg-streamer (if disabled)
    - service: shell_command.start_mgpg_streamer
#Running a script that records from the camera
    - service: shell_command.start_motion_rec
#Change the visualization of our "Home security" card on the HA panel to the “alarm triggered” position
    - service: alarm_control_panel.alarm_trigger
      entity_id: alarm_control_panel.home_alarm

Code snippet #8

Plain text
- alias: 'Send notification when alarm is 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') }}"

Code snippet #9

Plain text
- alias: 'Send notification when alarm triggered'
  trigger: 
   - platform: state
     entity_id: alarm_control_panel.home_alarm
     to: 'triggered'
  action:
    - service: persistent_notification.create
      data:
       message: Notification when alarm triggered. Motion sensor HC-SR501 detected.
    - delay:
       seconds: 4
#run our script for creating a single script
    - service: script.webcam_snapshot
#and refer to the described object in configuration.yaml: notify.ha_sendmail
    - service: notify.ha_sendmail
      data:
        title: 'Intruder alert'
        message: '{{now().strftime("%H:%M %Y-%m-%d")}}:Notification when alarm triggered. Motion sensor HC-SR501 detected.'
        data:
           images:
#link to the script already made by the script.webcam_snapshot
              - /config/camera/snapshot.jpg

Github file

https://github.com/cncjs/cncjs/wiki/Setup-Guide:-Raspberry-Pi-%7C-MJPEG-Streamer-Install-&-Setup-&-FFMpeg-Recording

Github

https://github.com/viatc/viamybox

Credits

Viat Chi
1 project • 0 followers

Comments