Change function of camera when changing SmartThings location mode

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
User avatar
swolsen
Posts: 31
Joined: Sat Mar 20, 2021 9:39 pm
Location: Schenectady, NY, USA

Change function of camera when changing SmartThings location mode

Post by swolsen »

Not sure if anyone is interested in this but I wrote a PHP CLI script to update certain (inside) monitors to change from [Monitor] to [Modect] and back depending on Smarthings location mode changing between [Home] and [Away]. I run it under a cron job every 5 minutes.

Code: Select all

#!/usr/bin/php

<?php
/************************************************************************************************************************
	Uses curl to get Smartthing's location mode (away or home) and set specified zoneminder camera id's function
	(Monitor or Modect)	based on ST mode. Requires personal authorization token (PAT) from your Smarthings account
	https://account.smartthings.com/tokens

	Scott W. Olsen
	2021-04-28
*************************************************************************************************************************/
$Monitors = array("1", "3"); // Zoneminder monitor id's for monitor function to set
$ZM_Host = "https://ZONEMINDERHOST"; // Zoneminder's host url
$ZM_Login = "user=USER&pass=PASSWORD"; // Zoneminder's login info
$ST_LocationID = "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX"; // your SmartThings Location ID
$ST_PAT = "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX"; // Your SmartThings personal access token

writelog("Initiating check for monitor id's : " . implode(", ", $Monitors));
$LocationMode = NULL;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://api.smartthings.com/v1/locations/$ST_LocationID/modes/current");
$header = array("Authorization: Bearer $ST_PAT");
curl_setopt($ch, CURLOPT_POST, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_VERBOSE, false);
$result = curl_exec($ch);
if($result != false) {
	$obj = json_decode($result);
	//echo "\nbegin output\n$result\nend output\n";
	//displayObject($obj);
	$LocationMode = $obj->name;
	writelog("SmartThings Location mode = $LocationMode");
}
else writelog("No Results from ST");
if(!is_null($LocationMode)) {
	// get API Key from Zoneminder.
	curl_setopt($ch, CURLOPT_URL, "$ZM_Host/zm/api/host/login.json");
	curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); // Used to accept self signed SSL cert if using SSL
	curl_setopt($ch, CURLOPT_POST, true);
	curl_setopt($ch, CURLOPT_POSTFIELDS, $ZM_Login);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
	curl_setopt($ch, CURLOPT_VERBOSE, false); // Set to true to see output of curl data when communicating with host.
	$result = curl_exec($ch);
	if($result != false) {
		$API_key = json_decode($result);
		//displayObject($API_key);

		foreach($Monitors as $id) {
			curl_setopt($ch, CURLOPT_POST, false);
			curl_setopt($ch, CURLOPT_URL, "$ZM_Host/zm/api/monitors/$id.json?token=$API_key->access_token");
			$result = curl_exec($ch);
			$mobj = json_decode($result); // need error checking here.
			//displayObject($mobj);
			//print $result;
			$MonitorFunction = $mobj->monitor->Monitor->Function;
			writelog("Zoneminder Monitor $id function = [$MonitorFunction]");
			$setMF = false;
			if($LocationMode == "Home" && $MonitorFunction != "Monitor") {
					$MonitorFunction = "Monitor";
					$setMF = true;
			}
			elseif($LocationMode == "Away" && $MonitorFunction != "Modect") {
					$MonitorFunction = "Modect";
					$setMF = true;
			}
			if($setMF) {
				writelog("Setting monitor $id function to [$MonitorFunction]");
				curl_setopt($ch, CURLOPT_POST, true);
				curl_setopt($ch, CURLOPT_POSTFIELDS, "Monitor[Function]=$MonitorFunction&Monitor[Enabled]=1");
				curl_setopt($ch, CURLOPT_URL, "$ZM_Host/zm/api/monitors/$id.json?token=$API_key->access_token");
				$result = curl_exec($ch);
				writelog($result);
			}
		}

	}
	else writelog('Curl error: ' . curl_error($ch));

}

curl_close($ch);


function writelog($w) {
	$file = fopen("/var/log/zm-setMonitorFunction.log", 'a');
	fwrite($file, date("Y-m-d H:i:s") . " $w\n");
	fclose($file);
}


// testing purposes
function displayObject($a) {
	foreach($a as $k => $v) {
			echo "[$k] => ";
			if(is_array($v) || is_object($v)) {
				echo "\n";
				displayObject($v);
			}
			else echo "$v\n";
	}
}

?>
Post Reply