Skip to content

Instantly share code, notes, and snippets.

@paulgorman
Last active June 15, 2020 13:36
Show Gist options
  • Save paulgorman/57d520524e71a8d140d88d6aa8fab5cb to your computer and use it in GitHub Desktop.
Save paulgorman/57d520524e71a8d140d88d6aa8fab5cb to your computer and use it in GitHub Desktop.
Unifi PHP LED light control using “unifi-php-api”
<?php
/*
At sundown, turn off upstairs Unifi AP status LED
At sunrise, turn status LED on
Crontab:
* * * * * ~/unifi-php-api/upstairs-light.php > /dev/null 2>&1
- Presence
*/
require_once('Client.php');
$controlleruser = 'Username'; // the user name for access to the UniFi Controller
$controllerpassword = 'Password'; // the password for access to the UniFi Controller
$controllerurl = 'https://192.168.1.2:8443'; // full url to the UniFi Controller, eg. 'https://22.22.11.11:8443'
$controllerversion = '5.8.28'; // THE Version of the Controller software, eg. '4.6.6' (must be at least 4.0.0)
$debug = false;
$site_id = 'default';
$device_id = "5b89a97b35e2ac75d202bda7"; //upstairs AP
$unifi_connection = new UniFi_API\Client($controlleruser, $controllerpassword, $controllerurl, $site_id, $controllerversion);
/* Figure out timezone offset, in hours, from GMT, watching for DST and standard time */
date_default_timezone_set('America/Los_Angeles');
$offset = date_offset_get(date_create(date('Y-m-d'), timezone_open('America/Los_Angeles')))/60/60;
if (isset($argv[1])) {
echo "offset: $offset\n";
echo date("D M d Y"). ', sunrise time : ' .date_sunrise(time(), SUNFUNCS_RET_STRING, 36.019, -115.143, 90, $offset) . "\n";
echo date("D M d Y"). ', sunset time : ' .date_sunset(time(), SUNFUNCS_RET_STRING, 36.019, -115.143, 90, $offset) . "\n";
}
/* sunset in seconds */
$sunrise = strtotime(date_sunrise(time(), SUNFUNCS_RET_STRING, 36.019, -115.143, 90, $offset));
$sunset = strtotime(date_sunset(time(), SUNFUNCS_RET_STRING, 36.019, -115.143, 90, $offset));
$now = time();
$result = null;
if (@preg_match("/on/i",$argv[1])) {
$set_debug_mode = $unifi_connection->set_debug($debug);
$loginresults = $unifi_connection->login();
$result = $unifi_connection->led_override($device_id, "on");
} elseif (@preg_match("/off/i",$argv[1])) {
$set_debug_mode = $unifi_connection->set_debug($debug);
$loginresults = $unifi_connection->login();
$result = $unifi_connection->led_override($device_id, "off");
} elseif (($now >= $sunset) && ($now >= $sunrise)) {
if (isset($argv[1])) {
echo "go sunset\n";
}
$set_debug_mode = $unifi_connection->set_debug($debug);
$loginresults = $unifi_connection->login();
$result = $unifi_connection->led_override($device_id, "off");
} elseif (($now <= $sunset) && ($now <= $sunrise)) {
if (isset($argv[1])) {
echo "night\n";
}
$set_debug_mode = $unifi_connection->set_debug($debug);
$loginresults = $unifi_connection->login();
$result = $unifi_connection->led_override($device_id, "off");
} elseif (($now <= $sunset) && ($now >= $sunrise)) {
if (isset($argv[1])) {
echo "go sunrise\n";
}
$set_debug_mode = $unifi_connection->set_debug($debug);
$loginresults = $unifi_connection->login();
$result = $unifi_connection->led_override($device_id, "on");
}
if (isset($argv[1])) {
$data = json_decode(json_encode($result), true); // I want an array
print_r ($data);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment