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 Hubitat 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 Hubitats's location mode (away or home) and set specified zoneminder camera id's function
(Monitor or Modect) based on the location mode. Requires access token from your Hubitat Maker API to access location
modes
Scott W. Olsen
2021-05-21
*************************************************************************************************************************/
//exit;
$Monitors = array("1", "3", "4"); // Zoneminder monitor id's for monitor function to set
$ZM_Host = "https://your.zoneminder.ip"; // Zoneminder's host url
$ZM_Login = "user=username&pass=password"; // Zoneminder's login info
writelog("Initiating check for monitor id's : " . implode(", ", $Monitors));
$LocationMode = NULL;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://hubitat_hub_ip/apps/api/32/modes?access_token=xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx");
curl_setopt($ch, CURLOPT_POST, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_VERBOSE, false);
$result = curl_exec($ch);
if($result != false) {
$obj = json_decode($result); // need error checking here.
//echo "\nbegin output\n$result\nend output\n";
//displayObject($obj);
foreach($obj as $mode)
if($mode->active == true) $LocationMode = $mode->name;
writelog("Hubitat Location mode = $LocationMode");
}
else print("No Results from Hubitat");
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);
}
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";
}
}
?>