Watching your Packages

We recently had two packages ripped into. One they actually stole things, and the other they left (presumably because it was a giant bag of dog food that would be a pain to carry).

Pretty sure tape doesn’t just come apart like that on its own. Pretty sure…

We were at home at the time (and are pretty much always at home these days), so it was especially irritating that thieves got to these packages before we did. We talked about getting a Ring camera, but I don’t dig helping out the police state and don’t want to buy one more tech thing if I don’t have to (or it’s not a fun tech thing).

I glanced over and noticed that we had two cameras for our Play Stations. One for the Playstation 3 that is currently doing an impressive job farming dust.

“Surely, I can use an old webcam to watch my front porch”, I thought.

And this is a happy project. Pretty much everything you need is already built and works pretty well. First up:

Home Assistant

I’d heard good things about Home Assistant, but our house was all Google devices, so I didn’t really feel motivated to install software to manage my home devices. The Google software worked ok.
But then we got a pool. And the pool came with a horrid app to control it. The app crashes all the time. But it does have integration with Home Assistant. So a few weeks ago I installed it. I already had a Mac Mini running linux, but they also make it easy to install on a Raspberry Pi which will set you back $35. Here are the installation instructions

Getting Started with Home Assistant

This has led to fun integrations. Like when the Spa is an appropriate temperature the Google Home yells “Get in the Hottub“. But back to the camera.

You’ll probably want to spend some time adding integrations for the devices in your home before continuing on.

Installing Motion

So I started with a Playstation Eye that I had around the house. Although apparently you can buy them for like $10. I propped it up in my front window on two scraps of 2×4 on the window ledge. Remember, my goal is motion detection to grab packages before thieves get them, not providing the police with pictures of people’s faces. So this angle is just fine.

Added bonus, because this webcam was made to go in people’s living rooms it has a red and blue light to let you know when it has power and when it is recording. It looks especially good in a front window at night.

Here’s my camera angle.

You’ll notice there’s reflections from blinds and a lot of foliage. I worried about this, but turned out it wasn’t a big deal since a person walking through this image is very large and easy to track for a computer.

Ok, so I plugged the camera into the usb port on my machine. If it works properly you should see a device show up as /dev/video0.

Next I installed the motion package. This is available by default in more recent versions of ubuntu:

sudo apt-get install motion

I tweaked a few settings.

videodevice /dev/video0
# I increased the size of the images to make them nicer to look at
width 640
height 480

# this makes night time monitoring work better with the PS Eye
auto_brightness on

# I upped the number of pixels needed to signal motion because I have a lot plants that move in the wind. And an actual person is a huge proportion of the image. This is what you'll probably need to tweak up and down to get rid of false positives
threshold 2000
noise_tune on

# I save movies of people approaching the porch into the www folder of my home assistant configuration. This allows them to be accessed with /local/[filename] on the home assistant server.
target_dir /usr/share/hassio/homeassistant/www

# Because I'm running homeassistant in a docker container I needed to be able to access the stream from outside the host machine. I have firewall rules to keep people from accessing it externally. This allows you to view a stream of the camera on port 8081
stream_localhost off

# And finally a script that is called when motion is detected
on_event_start /usr/share/hassio/homeassistant/frontDoorStart.sh

Next I created the script to tell Home Assistant that motion has happened.

/usr/share/hassio/homeassistant/frontDoorStart.sh

You’ll need to go into your Home Assistant UI to get an Api Key. Go to your profile:
https://[Your Home Assistant IP]/profile

Scroll down to Long Lived Access Token, and click ‘Create Token’. Then take the token you get and insert it into the script below where it says PUT_TOKEN_HERE.

!/bin/sh
BEARER_TOKEN=PUT_TOKEN_HERE

curl -X POST \
-H "Authorization: Bearer ${BEARER_TOKEN}" \
-H "Content-Type: application/json" \
-d "{\"state\":\"on\"}" \
https://ha.tfamaustin.us/api/events/FRONT_DOOR

You’ll probably need to make that executable as well:

chmod +x /usr/share/hassio/homeassistant/frontDoorStart.sh

Once that’s all done go ahead and restart motion.

sudo service motion restart

OPTIONAL: While you’re here you can add your camera to be viewable in the Home Assistant UI. This isn’t necessary, but it’s nice, and easy to add.

View from my front door on my Home Assistant dashboard, this is also viewable on the Home Assistant app on my phone.

In your Home Assistant configuration.yaml add the following:

camera:
  platform: mjpeg
  mjpeg_url: http://[IP of your server]:8081
  verify_ssl: false
  name: Front Door cam

You may need to restart Home Assistant to make this show up. You can do that under Configuration->Server Controls->Restart.

Adding the Automation

Finally you just need to decide what to do when motion is detected. I have a couple integrations, but my main one is to have it say “A Package Is On The Porch” on my Google Home. Go to Configuration->Automations. Then click the Plus in the bottom right corner to add a new Automation.

The Automation UI does not always work properly, so I’m going to post the YAML for each section. You can cut and paste by selecting Edit as YAML from the little menu on the right of each card.

My automation is called “Broadcast when there’s a package on the porch”. It has a Trigger based upon the Event that is sent from the frontDoorStart.sh script.

event_data:
  state: 'on'
event_type: FRONT_DOOR
platform: event

Then I added a Condition. I don’t want this thing waking me up so I decided to tell it to only notify between 10am and 11pm.

after: '10:00'
before: '23:00'
condition: time

Then I have two conditions. I have it broadcast on my main Google Home and on my Google ChromeCast Audio in my Office/Bedroom.

data:
  entity_id: media_player.living_room_home
  message: Package on the porch
service: tts.google_translate_say

I have a separate automation that sends notifications at any time of day to my phone. The number of things you can do is pretty limitless.

If you have any questions or run into problems, please feel free to leave a comment and I’ll try to update this.