[Guide] How to toggle ZoneMinder's Run State (home/away) based on your location using IFTTT on Android/iPhone

If you've made a patch to quick fix a bug or to add a new feature not yet in the main tree then post it here so others can try it out.
no1knows
Posts: 8
Joined: Sun Nov 20, 2016 2:04 pm

[Guide] How to toggle ZoneMinder's Run State (home/away) based on your location using IFTTT on Android/iPhone

Post by no1knows »

Hi everyone,

First post! I've managed to get geofencing working with my Android phone using IFTTT's Maker Channel to toggle my ZoneMinder Run State depending on whether I'm home or away. The same should work for iPhones. Here's how:

Note: You'll need to make sure that your ZoneMinder server is publicly accessible at a static IP or hostname so IFTTT's Maker Channel can send the "change Run Time" request to your ZoneMinder API. If you don't know what all that means, you'll find out in a moment!

1. Set up your ZoneMinder Run States. For me, that means turning the function of the monitors inside my house to "Monitor" when I get home (saved as "Home" Run Time), and to "Modect" when I go away (saved as "Away" Run Time). See "I" here for more info. Remember what you call your home/away Run Times, you'll need them in part 3 below.

2. Save the following code into a new file called "setruntime.php" in your zoneminder www folder (/usr/share/zoneminder/www for me). You'll need to replace USER/PASS with your ZoneMinder username and password. If you've set up apache to use https, change the $url1 and $url2 variables to "https://...".

Code: Select all

<?php

//variables
$tmpfname = tempnam ("/tmp", 'cookiename');
$runstate = htmlspecialchars($_GET["runstate"]);
$url1 = "http://localhost/zm/index.php";
$url2 = "http://localhost/zm/api/states/change/" . $runstate . ".json";
$post = "username=USER&password=PASS&action=login&view=console";

//
//Authenticate
//

//open connection
$ch = curl_init();

//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL, $url1);
curl_setopt($ch,CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch,CURLOPT_POSTFIELDS, $post);
curl_setopt( $ch, CURLOPT_COOKIEJAR,  $tmpfname );
curl_setopt( $ch, CURLOPT_COOKIEFILE, $tmpfname );
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

//execute post
$result = curl_exec($ch);

//close connection
curl_close($ch);

//
//Change Run State
//

//open connection
$ch = curl_init();

//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL, $url2);
curl_setopt($ch,CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt( $ch, CURLOPT_COOKIEJAR,  $tmpfname );
curl_setopt( $ch, CURLOPT_COOKIEFILE, $tmpfname );
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

//execute post
$result = curl_exec($ch);

//close connection
curl_close($ch);
?>
This php script will basically authenticate with your ZoneMinder server and then, via the ZoneMinder API, set your ZoneMinder Run State to whatever you put in "RUNSTATENAME" in the URL below:
http://YOURSERVER/zm/setruntime.php?run ... NSTATENAME

We need this because IFTTT's Maker Channel can only send standard HTTP requests/posts, and can't do any authentication or store cookies (which we need to do to access ZoneMinder's API).

3. Install IFTTT on your (Android/iPhone) phone. Sign in/up for an account.

4. In IFTTT go to "My Applets". Add a new Applet. The "this" should be your Android (or iPhone) location, the trigger being "You enter an area". Set the location of your home and continue. The "that" should be Maker Channel, the action being "Make a web request". In the action details, add the following URL, replacing YOURSERVER with your ZoneMinder server's IP address/hostname, and HOMERUNSTATENAME with whatever you've called your "I'm Home" Run State (in my case, that's "Home").

http://YOURSERVER/zm/setruntime.php?run ... NSTATENAME

You can leave the other settings as they are. Click on the tick and then Finish.

5. Repeat #4 but the location trigger should be "You exit an area" and the URL in the Maker action details should be the following, replacing YOURSERVER with your ZoneMinder server's IP address/hostname, and AWAYRUNSTATENAME with whatever you've called your "I'm Away" Run State (in my case, that's "Away").

http://YOURSERVER/zm/setruntime.php?run ... NSTATENAME

That's it. IFTTT should start toggling your Run Times when you arrive at/leave home.

Hope this helps some folks. Shout if you have any questions.
Last edited by no1knows on Sat Dec 31, 2016 6:38 pm, edited 2 times in total.
jeepnjeff
Posts: 8
Joined: Fri Sep 25, 2015 5:44 pm

Re: [Guide] How to toggle ZoneMinder's Run State (home/away) based on your location using IFTTT on Android/iPhone

Post by jeepnjeff »

I have a question regarding Run State setups. I would like to always keep cameras in Modect in Home/Away states to always have record of what happening outside. Would like to turn my email filter off or not running in background when in Home state. Is that possible or can you only alter camera functions.
alabamatoy
Posts: 349
Joined: Sun Jun 05, 2016 2:53 pm

Re: [Guide] How to toggle ZoneMinder's Run State (home/away) based on your location using IFTTT on Android/iPhone

Post by alabamatoy »

Experimenting with this (thanks much for the post!) I am getting following error:

Code: Select all

PHP Fatal error:  Uncaught Error: Call to undefined function curl_init()
but if I do a curl -V from console, I get:

Code: Select all

curl 7.47.0 (x86_64-pc-linux-gnu) libcurl/7.47.0 GnuTLS/3.4.10 zlib/1.2.8 libidn/1.32 librtmp/2.3
Protocols: dict file ftp ftps gopher http https imap imaps ldap ldaps pop3 pop3s rtmp rtsp smb smbs smtp smtps telnet tftp 
Is there a required config or tweak to php to allow it to use the curl_init call? Ubuntu 16.04, Apache version 2.4.18 ......
no1knows
Posts: 8
Joined: Sun Nov 20, 2016 2:04 pm

Re: [Guide] How to toggle ZoneMinder's Run State (home/away) based on your location using IFTTT on Android/iPhone

Post by no1knows »

alabamatoy wrote:Experimenting with this (thanks much for the post!) I am getting following error:

Code: Select all

PHP Fatal error:  Uncaught Error: Call to undefined function curl_init()
but if I do a curl -V from console, I get:

Code: Select all

curl 7.47.0 (x86_64-pc-linux-gnu) libcurl/7.47.0 GnuTLS/3.4.10 zlib/1.2.8 libidn/1.32 librtmp/2.3
Protocols: dict file ftp ftps gopher http https imap imaps ldap ldaps pop3 pop3s rtmp rtsp smb smbs smtp smtps telnet tftp 
Is there a required config or tweak to php to allow it to use the curl_init call? Ubuntu 16.04, Apache version 2.4.18 ......
This suggests that you need to run the following:

sudo apt-get install php5-curl
alabamatoy
Posts: 349
Joined: Sun Jun 05, 2016 2:53 pm

Re: [Guide] How to toggle ZoneMinder's Run State (home/away) based on your location using IFTTT on Android/iPhone

Post by alabamatoy »

no1knows wrote: This suggests that you need to run the following:

sudo apt-get install php5-curl
Yep, I had done that already. I used "php-curl" instead of "php5-curl" which is supposed to install the correct version, since Im already running V7. Still no workie. Something is whacked between php and curl, and I cant seem to figure out what it is.
no1knows
Posts: 8
Joined: Sun Nov 20, 2016 2:04 pm

Re: [Guide] How to toggle ZoneMinder's Run State (home/away) based on your location using IFTTT on Android/iPhone

Post by no1knows »

alabamatoy wrote:
no1knows wrote: This suggests that you need to run the following:

sudo apt-get install php5-curl
Yep, I had done that already. I used "php-curl" instead of "php5-curl" which is supposed to install the correct version, since Im already running V7. Still no workie. Something is whacked between php and curl, and I cant seem to figure out what it is.
I'm afraid I use Fedora so not much use to you. Do you have all of "curl libcurl3 libcurl3-dev php5-curl" installed? Seems to do the trick for most.
alabamatoy
Posts: 349
Joined: Sun Jun 05, 2016 2:53 pm

Re: [Guide] How to toggle ZoneMinder's Run State (home/away) based on your location using IFTTT on Android/iPhone

Post by alabamatoy »

I finally got this working. Curl was installed incorrectly was what was causing me problems.

There were 2 issues with the PHP script, as written:

1 - Curl will not allow SSL cert mismatches in name, so care must be taken in aligning the call to the webservers domain name to align with the name used in the cert. For commercial systems, this is probably no big deal, but for self-signed certs, it can be. Fix was to enter the domain name used in the self-signed cert into the servers etc/hosts file so that the made-up domain name would resolve.

2 - The script gave no response whatsoever - I added some very simple text response to show success or failure, and if there were curl or other issues, errors are written to the syslog.

The API documentation says you can use whatever.json or whatever.xml calls, but this didnt work for me, see viewtopic.php?f=36&t=25733 Making a call to states.xml (as opposed to states.json) resulted in error condition and no worky. So Ive either got something wankered or the API documentation is incorrect.
no1knows
Posts: 8
Joined: Sun Nov 20, 2016 2:04 pm

Re: [Guide] How to toggle ZoneMinder's Run State (home/away) based on your location using IFTTT on Android/iPhone

Post by no1knows »

alabamatoy wrote:I finally got this working. Curl was installed incorrectly was what was causing me problems.

There were 2 issues with the PHP script, as written:

1 - Curl will not allow SSL cert mismatches in name, so care must be taken in aligning the call to the webservers domain name to align with the name used in the cert. For commercial systems, this is probably no big deal, but for self-signed certs, it can be. Fix was to enter the domain name used in the self-signed cert into the servers etc/hosts file so that the made-up domain name would resolve.

2 - The script gave no response whatsoever - I added some very simple text response to show success or failure, and if there were curl or other issues, errors are written to the syslog.

The API documentation says you can use whatever.json or whatever.xml calls, but this didnt work for me, see viewtopic.php?f=36&t=25733 Making a call to states.xml (as opposed to states.json) resulted in error condition and no worky. So Ive either got something wankered or the API documentation is incorrect.
Well done - care to post the updated code?

Re SSL certificates - good point. Have you looked into https://letsencrypt.org/ ? Will save you some headaches in future I suspect.
davidma
Posts: 36
Joined: Fri Oct 16, 2015 1:40 am

Re: [Guide] How to toggle ZoneMinder's Run State (home/away) based on your location using IFTTT on Android/iPhone

Post by davidma »

Old topic but I just wanted to say thanks for your work and also report on a few things I found when trying this.

1. The script works well for me but takes about 20 seconds to complete so as a result I found the IFTTT maker would always end up skipped due to timeout.
2. IFTTT is also a little quirky for me and not working well with the geolocation.
3. I used another app for Android to do the geolocation and when combined with the script it is working beautifully! I'm using
EgiGeoZone Geofence. It even has an option to specify the radius for the home area which is better than what IFTTT location seems to provide. There are probably other geofencing apps one could use too.

Thanks again!
no1knows
Posts: 8
Joined: Sun Nov 20, 2016 2:04 pm

Re: [Guide] How to toggle ZoneMinder's Run State (home/away) based on your location using IFTTT on Android/iPhone

Post by no1knows »

davidma wrote: Sun May 28, 2017 1:41 am Old topic but I just wanted to say thanks for your work and also report on a few things I found when trying this.

1. The script works well for me but takes about 20 seconds to complete so as a result I found the IFTTT maker would always end up skipped due to timeout.
2. IFTTT is also a little quirky for me and not working well with the geolocation.
3. I used another app for Android to do the geolocation and when combined with the script it is working beautifully! I'm using
EgiGeoZone Geofence. It even has an option to specify the radius for the home area which is better than what IFTTT location seems to provide. There are probably other geofencing apps one could use too.

Thanks again!
Glad to hear it. I'd actually given up on IFTTT because the geofencing was so hit and miss. I've installed EgiGeoFence now and set it up so let's see how that goes. Thanks for the tip!
RHCPNG
Posts: 14
Joined: Sat Sep 09, 2017 9:40 am

Re: [Guide] How to toggle ZoneMinder's Run State (home/away) based on your location using IFTTT on Android/iPhone

Post by RHCPNG »

no1knows wrote: Sun Jan 15, 2017 3:20 pm
alabamatoy wrote:I finally got this working. Curl was installed incorrectly was what was causing me problems.

There were 2 issues with the PHP script, as written:

1 - Curl will not allow SSL cert mismatches in name, so care must be taken in aligning the call to the webservers domain name to align with the name used in the cert. For commercial systems, this is probably no big deal, but for self-signed certs, it can be. Fix was to enter the domain name used in the self-signed cert into the servers etc/hosts file so that the made-up domain name would resolve.

2 - The script gave no response whatsoever - I added some very simple text response to show success or failure, and if there were curl or other issues, errors are written to the syslog.

The API documentation says you can use whatever.json or whatever.xml calls, but this didnt work for me, see viewtopic.php?f=36&t=25733 Making a call to states.xml (as opposed to states.json) resulted in error condition and no worky. So Ive either got something wankered or the API documentation is incorrect.
Well done - care to post the updated code?

Re SSL certificates - good point. Have you looked into https://letsencrypt.org/ ? Will save you some headaches in future I suspect.
Thanks for the code. I'm also interested in the updated code, because I'm having some trouble debugging the code.

I've altered the first part of the code to be able to run the script from command line:

Code: Select all

//variables                                                                                  
$tmpfname = tempnam ("/tmp", 'cookiename');                                                  
if (defined('STDIN')) {                                                                      
  $runstate = $argv[1];                                                                      
} else {                                                                                     
  $runstate = htmlspecialchars($_GET["runstate"]);                                           
}                                                                                            
$url1 = "http://LOCALHOST/zm/index.php";                                              
$url2 = "http://LOCALHOST/zm/api/states/change/" . $runstate . ".json";               
$post = "username=USER&password=PASS&action=login&view=console";
You can run the script by running the following from the command line: php -f setruntime.php <runstate>

The script runs fine, but the state does not change in ZoneMinder. I'm not familiar with php, so I'm freewheeling a bit.
RHCPNG
Posts: 14
Joined: Sat Sep 09, 2017 9:40 am

Re: [Guide] How to toggle ZoneMinder's Run State (home/away) based on your location using IFTTT on Android/iPhone

Post by RHCPNG »

OK, I got it working. The problem was a character in the password.

I figured it out by running curl commands directly from the command line.

Also, this link helped me in debugging php:
https://stackoverflow.com/questions/170 ... rnal-error
placix
Posts: 1
Joined: Mon Oct 30, 2017 7:34 pm

Re: [Guide] How to toggle ZoneMinder's Run State (home/away) based on your location using IFTTT on Android/iPhone

Post by placix »

jeepnjeff wrote: Fri Dec 23, 2016 11:01 pm I have a question regarding Run State setups. I would like to always keep cameras in Modect in Home/Away states to always have record of what happening outside. Would like to turn my email filter off or not running in background when in Home state. Is that possible or can you only alter camera functions.
Hello jeepnjeff!

Did you find a solution to manage the email filter?
User avatar
snake
Posts: 337
Joined: Sat May 21, 2016 2:20 am

Re: [Guide] How to toggle ZoneMinder's Run State (home/away) based on your location using IFTTT on Android/iPhone

Post by snake »

placix wrote: Mon Oct 30, 2017 7:53 pm Hello jeepnjeff!

Did you find a solution to manage the email filter?
Yes, there is a solution in recent Zoneminder (I think starting with 1.31) where you can filter based on run state. This means you can set email filter to only fire when the 'away' run state is set. See https://wiki.zoneminder.com/SMS_Notifications#Notes.

If you have an earlier ZM, you can take the debian package source add the patch manually. It is not difficult. I used 1.30.4

Use caution with 1.31 as it is not stable yet.
CV8R
Posts: 4
Joined: Mon May 21, 2018 1:46 am

Re: [Guide] How to toggle ZoneMinder's Run State (home/away) based on your location using IFTTT on Android/iPhone

Post by CV8R »

Hi All,

Sorry to dig up an old thread but this is pretty much where I need to be.

Has anyone managed to get this working (via EgiGeoZone) but for multiple household phones?

I must confess that I haven't set it up yet but I assume from reading the implementation that whenever a phone enters or exits the geozone that ZM state will change. That means if I am at home and my wife exits home then ZM running state will be changed from "AtHome" to "NotHome".

My scripting skills are very, very limited. Ideally I was hopeful that someone has solved the issue when there are multiple phones and multiple running states. In my research I have found some solutions that write a file based on who is home then a cron job changes the ZM running state but I haven't seen something that addresses when there are multiple phones in the household. Something like the following scenarios:

1. Wife and husband and kids are all at home - file = "AtHome"
2. Wife and kids are at home and husband is not at home - file = "AtHome"
2. Wife and husband and kids are at not at home - file = "NotHome"

A cron job that then monitors this file can be used to push the required state to VM.

Mixed with time of day schedule in cron I would be able to enable the front gate cam at night and not have the house cam's being alarmed when one or more of us are at home.

My problem, I cannot find out how to modify the file from the pushed message from EgiGeoZone.

Thanks in advance for any guidance you can offer.

Cheers,
CV8R
Post Reply