Starting with a usb webcam, a Pi or similar (Odroids are great for this, better than a Pi) running Raspbian or Ubuntu Server.
Install Motion:
sudo apt-get install motion
Then edit the motion configuration file:
sudo nano /etc/motion/motion.conf
If you’ve never used Motion before at a minimum you’ll need to change this setting so you can view the camera from another pc and use it in Home Assistant.
# Restrict webcam connections to localhost only (default: on)
webcam_localhost on
Change the ‘on’ to ‘off’.
Settings such as frames-per-second, camera dimensions, etc. should also be tweaked but if you’re new to Motion you can leave them as they are for now except the above. Tweak them later after having a look at the github guide, and checking you can connect to the camera.
Scroll back up the config file, and look for this:
# Detect motion in predefined areas (1 - 9). Areas are numbered like that: 1 2 3
# A script (on_area_detected) is started immediately when motion is 4 5 6
# detected in one of the given areas, but only once during an event. 7 8 9
# One or more areas can be specified with this option. (Default: not defined)
;area_detect value
I want to trigger events on movement in area 4 and 5, then run a script which is on the same device that Motion is installed on named call_hass.sh
, so the above changes to this:
# Detect motion in predefined areas (1 - 9). Areas are numbered like that: 1 2 3
# A script (on_area_detected) is started immediately when motion is 4 5 6
# detected in one of the given areas, but only once during an event. 7 8 9
# One or more areas can be specified with this option. (Default: not defined)
area_detect value 4,5
on_area_detected /home/pi/call_hass.sh
call_hass.sh
uses a curl command to run a script on Home Assistant, and the script increases a variable by one every time something happens in area 4 or 5:
#!/bin/sh
curl -X POST -H "Content-Type: application/json" -d '{"entity_id": "script.increment_cam2_zone45_variable"}' http://192.168.1.23:8123/api/services/script/turn_on
Don’t forget to make the script executable:
chmod u+x call_hass.sh
To get Motion running in the background simply type:
motion &
or edit /etc/rc.local
and place that command in there so it runs automatically. We are finished with the camera set-up
So now in Home Assistant, we need to create a variable & the script to increase the variable’s value by one.
For variables in HA I use this:
Follow the handy instructions there, and add the following into your configuration.yaml
variable:
times_motion_cam2_zone45_triggered:
value: 0
attributes:
friendly_name: 'Times Camera Two - Zone 4 or 5 Triggered'
icon: mdi:cctv
restore: true
In your scripts.yaml
add this, it is what the bash curl command is calling:
increment_cam2_zone45_variable:
alias: Increase Cam2 Detection Counter by One
sequence:
- service: variable.set_variable
data:
variable: times_motion_cam2_zone45_triggered
value_template: '{{ (variable.state | int) + 1 }}'
Finally, we need an automation that will trigger every time that variable changes, in your automation.yaml
:
- alias: Camera 2 Zone 45 Motion Detected
trigger:
platform: state
entity_id: variable.times_motion_cam2_zone45_triggered
condition:
- condition: time
after: '09:00:00'
before: '21:00:00'
- condition: state
entity_id: 'input_boolean.motion_detection_alerts'
state: 'on'
action:
- service: light.toggle
entity_id: switch.whatever_light_you_want_to toggle
- delay: '00:00:05'
- service: light.toggle
entity_id: switch.whatever_light_you_want_to toggle
You will, of course, have your own time settings, and actions.
I’ll cover the PC notification bit further below.
Also create an entry in your input_boolean.yaml
, so you can turn on & off this automation from a HA page so you’re not bothered by it at times you’ll be in & out across that zone, e.g. gardening etc.
motion_detection_alerts:
name: Motion Detection Alerts
initial: on
icon: mdi:video-switch
Here’s a group.yaml entry for them:
Cam2 Zone45 Motion:
- variable.times_motion_cam2_zone45_triggered
- input_boolean.motion_detection_alerts
If you want the counter total to reset at midnight, add this to your automation.yaml
:
- alias: Reset Camera Zone Counter at Midnight
trigger:
platform: time
at: '23:59:59'
action:
- service: variable.set_variable
data:
variable: times_motion_cam2_zone45_triggered
value: 0
And that’s the main Home Assistant part covered
So, for those interested here’s how to have Home Assistant (in this case Hassio) send a pop-up notification to your Linux PC:
First up, on the pc:
sudo apt-get install notify-osd
Having the Home Assistant logo is a nice touch, and it’s here:
https://github.com/home-assistant/home-assistant-assets/blob/master/logo-pretty.png
Save it somewhere on your pc.
I’ve used a command_line switch for the next bit, there are other ways. In the switch version, add this to your switches.yaml
:
- platform: command_line
switches:
notify_laptop_of_motion_detection:
command_on: ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -i /config/.ssh/id_rsa [email protected] "notify-send 'Motion Detected' 'Driveway Activity Zone 45' -i /home/homelaptop/logo-pretty.png -t 5000"
Hassio needs a little extra for an ssh into another machine, I reckon for Hassbian the command_on would be simpler:
ssh [email protected] "notify-send 'Motion Detected' 'Driveway Activity Zone 45' -i /home/homelaptop/logo-pretty.png -t 5000"
In the above automation entry you could now add this:
- service: switch.turn_on
entity_id: switch.notify_laptop_of_motion_detection
It will look like this, though saying 45 not 4:
I think that’s everything, and it’s certainly a bit longer than I thought it would be when I started typing