amdean / motion-notify

Notification system for Linux Motion providing upload to Google Drive and email notificaiton
GNU General Public License v3.0
32 stars 12 forks source link

motion-notify

Motion Notify Version 1.0.3

Motion Notify 1.0 is a major release which is a significant rewrite from the previous 0.3 release. The latest version provides a platform which is more stable, more configurable and more extensible. Focus has been made on providing a platform that is easily extended by allowing developers to add new Actions and Detectors. See the section at the end of the readme if you want to know more about developing additional actions and detectors.

Overview

Motion Notify is a notification system for Linux Motion providing upload to Google Drive and email notification when you're not home.

Take an event, carry out some detections to see if you're home and then carry out a series of actions.

This allows you to carry out the following:

Upgrades

If you're upgrading from an older version note the following:

Events, Detectors and Actions

Motion Notify works on the concept of Events, Detectors and Actions.

The framework and configuration is designed to allow developers to easily develop and integrate their own Detectors and Actions

Event Action Rules

Each "Event Type" has a set of Event Action Rules allowing you to specify a different series of actions to be called at the start of an event than at the end of an event.

Currently support Actions are:

Currently support Event Types are passed to Motion Notify via the command which is trigger by Linux Motion (or any other event trigger)

Actions may be configured to trigger:

Detection Rules

Detection rules can be specified in groups to indicate whether they all detectors should indicate that the system is active or whether an individual detector can determine that the system is active

IpBasedDetector

This detector checks whether you're at home by checking the network for the presence of certain devices by IP address.

It's highly recommended to use this rather than ArpBasedDetector which looks for MAC addresses. If you choose to use ArpBasedDetector you will need to run Motion Notify (and Motion) as root as it uses ARP - this isn't recommended.

IP detection uses ping so will run as a regular user.

Specify a comma separated list of IP addresses which will be checked - if any of those addresses are active then the system assumes that you're at home.

Note that mobile phones often don't retain a constant connection to the wireless network even though they show that they are connected. They tend to sleep and then just reconnect occasionally to reduce battery life. This means that you might get a lot of false alarms if you just use a mobile phone IP address.

Adding lots of devices that are only active when you're at home will reduce false alarms - try things like your Smart TV, desktop PC etc as well as any mobile phones. It's highly recommended to configure your devices to use static IP's to prevent the IP addresses from changing.

You can specify the timeout for ping requests in the config file - this forces ping to exit and assume that the device is offline rather than wait a long time for each response. If you're on a poor wireless connection or experiencing slow ping responses this should be increased.

The switches for ping are not consistent between operating systems. If you're running on a Debian based system such as Raspbian then ping -w2 127.0.0.1 causes a timeout of 2 seconds. On a BSD based system such as OSX you need ping -t2 127.0.0.1 so set the IpBasedDetector.ping_timeout_switch in the config file to -w or -t according to your operating systems ping command.

ArpBasedDetector

This detector checks the network for the presence of certain MAC addresses. It requires the script and Linux Motion to be run as root so it's use is only recommended when the IpBasedDetector can't be used (such as when static IP addresses are not possible).

TimeBasedDetector

This detector allows times of the day to be specified at which the system should always be considered active.

Installation

Install Python Libraries

sudo apt-get update
sudo apt-get install python-pip
sudo pip install PyDrive
sudo pip install enum34
sudo pip install oauth2client (may already be installed)
sudo pip install google-api-python-client (may already be installed)
sudo apt-get install python-openssl (may already be installed)

Clone the Git repo into the directory

cd /etc/
sudo apt-get install git
sudo git clone https://github.com/amdean/motion-notify.git
sudo chown -R motion.motion /etc/motion-notify

Create the log file and lock file and set the permissions

sudo touch /var/tmp/motion-notify.log
sudo chown motion.motion /var/tmp/motion-notify.log
sudo chmod 664 /var/tmp/motion-notify.log
sudo touch /var/tmp/motion-notify.lock.pid
sudo chmod 664 /var/tmp/motion-notify.lock.pid
sudo chown motion.motion /var/tmp/motion-notify.lock.pid

Update Configuration (see Configuration Section)

Change the File permissions

sudo chown motion.motion /etc/motion-notify/motion-notify.py
sudo chown motion.motion /etc/motion-notify/motion-notify.cfg
sudo chmod 744 /etc/motion-notify/motion-notify.py
sudo chmod 600 /etc/motion-notify/motion-notify.cfg

Create the entry in the Motion conf file to trigger the motion-notify script when there is an alert

sudo cat /etc/motion-notify/create-motion-conf-entries.txt >> /etc/motion/motion.conf
rm /etc/motion-notify/create-motion-conf-entries.txt

Motion will now send alerts to you when you're devices aren't present on the network

Configuration

SmtpEmailNotifyAction config

Enter the following configuration for emails:

TimeBasedNotification Config

GoogleDriveUploadAction Config

Google drive authentication is done using a service account via key authentication. The service account is given access only to the folder you specify in Google Drive.

Next you need to get some account credentials from the Google Developers console - this will allow motion-notify to upload files to Google Drive.

Once the project is created you'll be take to the project dashboard for that project.

You'll receive a download containing a JSON file.

The service account has an email address associated with it which will @developer.gserviceaccount.com. Copy that email address and enter it into the "service_user_email" field in the config file.

You now need to allow the service account access to your Google Drive folder.

Sub Folder Config

To Create Date Level Folders update the date format config. By Default this is set to roll each day. (See Permissions)

Permissions Config

Using the Service User OAuth (p12 file) means that the files are created and owned by the service user
When using folders that means we need to set the folder permission explicitly.
By Default the gmail/user from config is set as a writer. To add any other users set the read_users and write_users values in config. These values should be the email addresses of the users you wish to permission.

Usage

Motion Notify is designed to work with Linux Motion but can be used to handle other applications.

To run Motion Notify call motion-notify.py and pass in the following arguments:

If you're using Motion Notify with Linux Motion motion detection software you need the following lines at the end of your /etc/motion/motion.conf file:

on_picture_save /etc/motion-notify/motion-notify.py /etc/motion-notify/motion-notify.cfg %f on_picture_save %s %v %n
on_movie_end /etc/motion-notify/motion-notify.py /etc/motion-notify/motion-notify.cfg %f on_movie_end %s %v %n
on_event_start /etc/motion-notify/motion-notify.py /etc/motion-notify/motion-notify.cfg None on_event_start %s %v None

Troubleshooting

The first three steps to troubleshooting are:

Trigger an "on_event_start" event

sudo /etc/motion-notify/motion-notify.py /etc/motion-notify/motion-notify.cfg None on_event_start 123 456 None

Trigger an "on_picture_save" event

sudo /etc/motion-notify/motion-notify.py /etc/motion-notify/motion-notify.cfg /home/pi/test.jpg on_picture_save 123 456 test.jpg

Developers

Motion Notify is designed to be extended. Create your own actions or detectors. Please contribute to the project by submitting any changes to https://github.com/amdean/motion-notify

Actions

Simply create a new Python module in the actions package, add the action to the EventActionRules section of the config file and that's it - no need to modify any code in the Motion Notify core.

Rules:

Detectors

Simply create a new Python module in the detectors package, add the detector to the detector_rules line of the config file and that's it - no need to modify any code in the Motion Notify core.

TODO

SSDP Installation

sudo pip install requests