Getting push notifications when motion is detected

I use a Raspberry Pi Zero attached camera to detect motion in our home when we are away. I have set up email notifications to let me know if any movements are detected. This is great, but I am not checking my emails too often. I started looking for a solution to set up push notifications to my phone. In fact I wanted to notify not only myself, but my wife as well. In the same time. Doing my research I have found Pushover which I found cheap and easy enough to work with. For a one time $5 USD payment you are allowed to send 7,500 push notifications per month to multiple devices on the same platform. It is available for iOS and Android as well.

Setting up your device and app on Pushover

Once you have purchased a licence for your platform you download the app and log in, so the device gets linked with your account. On the Pushover dashboard you can find your User Key and this is also the place where you will need to create a new Application for your camera. Once the Application has been created you are taken to its own page with statistics about daily (for the last month and a half) and monthly usage (for the last 11 months). This is also the place to find your API Token.

Creating a small Phyton script to send the notifications

To send the notifications I use the following script which is located in my home folder with the name pushover.py. Of course, you will need to use your own user key and api token to make it work.

import sys,requests, os.path

filefullpath=sys.argv[1]
fileextension = os.path.splitext(filefullpath)[1]
dirname=os.path.split(os.path.split(os.path.splitext(filefullpath)[0])[0])[1]
filename=os.path.split(filefullpath)[1]

if fileextension == ".jpg": 
    r = requests.post("https://api.pushover.net/1/messages.json", data={"token":"","user":"","message":"Motion detected!"}, files={"attachment":open(filefullpath,"rb")})
elif fileextension == ".mp4":
    r = requests.post("https://api.pushover.net/1/messages.json", data={"token":"","user":"","message":"Motion detected and video captured. Go to Dropbox to view it: "+dirname+"/"+filename})
else: 
    r = requests.post("https://api.pushover.net/1/messages.json",data={"token":"","user":"","message":"Something strange happening! A file got uploaded to Dropbox with the name: "+filename})

What this script does is

  • if you have enabled motion triggered still images, then you’ll get a message each time a new image has been captured and uploaded to your choosen file storage solution. The image itself will also come in the push notification.
  • if you have enabled motion triggered movies, then n you’ll get a message each time a new movie has been captured and uploaded to your choosen file storage solution. The push notification will also contain the directory and filename of the movie
  • if any other file gets uploaded by MotionEye to your choosen file storage solution you’ll get a message. This should never really happen, but covered it just in case…

The messages assume you’re using Dropbox, but feel free to customize them.

To test if it is working you just need to execute on your Raspberry Pi the following:

python3 pushover.py Test

You should get a push notification instantly with the message Something strange happening! A file got uploaded to Dropbox with the name: Test. Obviously there won’t be such file in your Dropbox, because we were just testing if the push notification works.

And finally to configure MotionEye

You need to go to File Storage section -> Run A Command. Toggle it to ON and enter to the Command field the following

python3 /home/[your Raspberry Pi user name]/pushover.py %f

This will pass onto our script the full file name, including the location (directory) which has been uploaded to the choosen file storage solution of yours.

That is all you need to get push notified.