ZoneMinder: Automatically Change States When Home or Away Using A Shell Script and Cron

Date: 3 September 2020

Table of Contents

Introduction

I needed a method to automatically change my ZoneMinder (ZM) Run State throughout the day. I wanted to recieve automatic email alerts when ZM detected motion and I was out of the house, but didn't want any email alerts when I was home. There are a few threads about this topic on ZoneMinder's forums, but I couldn't find anything immediately usable. The solution that I created relies on my mobile phone. I carry my phone almost every time I leave the house. Therefore, it's a pretty reasonable assumption that if the phone is no longer on the local network with ZoneMinder, then nobody is home.

Solution Workflow

Initial Steps

The script assumes the mobile phone has the same IP address every time it connects to the network. Static IPs can be set in most consumer/SOHO routers somehow - I am not going to elaborate on the dozens of varieties that could exist. I used pfSense's Static IP Mappings feature. Be sure to record the IP used, as it will be needed in the shell script below.

Shell Script

Either manually copy the contents of the shell script to a file /root/zm/zm_sentry.sh or just download it directly to your server:

sudo wget -O /root/zm/zm_sentry.sh https://www.chrono101.com/guides/zm_sentry.txt
Then set the permissions correctly:
sudo chown root:root /root/zm/zm_sentry.sh
sudo chmod 0700 /root/zm/zm_sentry.sh
Now modify the script using Vim or some other CLI editor:
	ZM_DEV1=192.168.xxx.xxx
	ZM_DEV2=192.168.xxx.xxx
	ZM_HOMESTATE="YourHomeState"
	ZM_AWAYSTATE="YourAwayState"
	

Cron Job

Modify root's crontab to run the script every 3 minutes:

	sudo crontab -e
	

Add this to the bottom of the crontab:

	*/3 * * * * /root/zm/zm_sentry.sh > /dev/null 2>&1
	

Conclusion

ZM should now be changing states whenever your phone disconnects or reconnects from WiFi (with a little bit of lag time, due to running the cron job every 3 minues).