[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.
Post Reply
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 » Sun Nov 20, 2016 2:59 pm

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: 7
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 » 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.

alabamatoy
Posts: 88
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 » Thu Dec 29, 2016 3:52 pm

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 » Sat Dec 31, 2016 6:32 pm

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: 88
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 » Sun Jan 01, 2017 4:08 am

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 » Mon Jan 02, 2017 1:17 pm

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: 88
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 » Sun Jan 15, 2017 2:57 pm

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 » 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.

davidma
Posts: 20
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 » 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!

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 » Thu Jun 01, 2017 8:31 pm

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 » Tue Sep 12, 2017 8:06 am

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 » Tue Sep 12, 2017 8:54 am

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 » Mon Oct 30, 2017 7:53 pm

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?

snake
Posts: 32
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 » Wed Nov 01, 2017 3:56 am

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.

Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 2 guests