Es gab mal ein Script aber das scheint verweist zu sein und war auch nicht mehr funktionsfähig, also mache ich mich hier jetzt selber an die Arbeit, die „API“ ist ja hier beschrieben
mPower, mFi Switch and mFi In-Wall Outlet HTTP API - Ubiquiti Networks Community
Die Anzahl der Steckdosen wird automatisch erkannt, also 1 (hoffentlich), 3 oder 6 (die habe ich)
Variablen werden Automatisch dem Script zugeordnet (unter dem Script erstellt)
<?
$user = "Admin"; // The Username is also case sensitive
$pw = "geheim";
$host = "192.168.178.58";
$debug = "1"; // 0 = Off // 1 = On
// Don't change anything below this line
$cookieID = "01234567890123456789012345678931";
$timeOut = 10;
$logintry = 0;
// Login
function login(){
global $user, $pw, $host, $timeOut, $cookieID, $logintry, $debug;
$logintry++;
echo "
Login Number: " . $logintry;
$curl = curl_init();
curl_setopt($curl, CURLOPT_TIMEOUT, $timeOut);
curl_setopt($curl, CURLOPT_URL, "http://" . $host . "/login.cgi");
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, "username=" . $user . "&password=" . $pw);
curl_setopt($curl, CURLOPT_COOKIE,"AIROS_SESSIONID=" . $cookieID);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec($curl);
echo "
Login:" . $data;
readData();
}
// Read the Data
function readData(){
global $user, $pw, $host, $timeOut, $cookieID, $logintry, $debug;
$curl = curl_init();
curl_setopt($curl, CURLOPT_TIMEOUT, $timeOut);
curl_setopt($curl, CURLOPT_URL, "http://" . $host . "/sensors");
curl_setopt($curl, CURLOPT_COOKIE,"AIROS_SESSIONID=" . $cookieID);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec($curl);
if($debug == "1"){
echo "
JSON-Data: " . $data;
}
// JSON decode
$result = json_decode($data);
// Count the numbers of "Steckdosen"
// Yes I know an @ is not the best way ;)
if(!$count = count(@$result->sensors)){
if($logintry > 0) {
echo "LogIn - Failure: something is wrong with the login.";
exit;
}
login();
}else{
if($debug == "1"){
echo "
Status:" . $result->status;
echo "
Count: " . $count;
echo "\Self: " . $_IPS['SELF'];
}
for($i = 0; $i < $count; $i++){
if($debug == "1"){
echo "
Steckdose Number: " . $result->sensors[$i]->port;
echo "
Output:" . $result->sensors[$i]->output;
echo "
Power:" . $result->sensors[$i]->power;
echo "
enabled:" . $result->sensors[$i]->enabled;
echo "
Current:" . $result->sensors[$i]->current;
echo "
Voltage:" . $result->sensors[$i]->voltage;
echo "
powerfactor:" . $result->sensors[$i]->powerfactor;
echo "
Relay:" . $result->sensors[$i]->relay;
echo "
lock:" . $result->sensors[$i]->lock;
echo "
prevmonth:" . $result->sensors[$i]->prevmonth;
echo "
thismonth:" . $result->sensors[$i]->thismonth;
}
// Prüfen ob die Dummy Instanz für dne Port vorhanden ist
$port = "";
$childs = IPS_GetChildrenIDs($_IPS['SELF']);
foreach ($childs as $child) {
$o = IPS_GetObject($child);
if (strpos($o["ObjectInfo"],"Port " . $result->sensors[$i]->port)!== false) {
$port = $child;
break;
}
}
if($port == ""){
// create a new Dummy Instance
$port = IPS_CreateInstance("{485D0419-BE97-4548-AA9C-C083EB82E61E}"); // Dummy Instance
IPS_SetName($port, "Port " . $result->sensors[$i]->port);
IPS_SetInfo($port, "Port " . $result->sensors[$i]->port);
IPS_SetParent($port, $_IPS['SELF']);
}
// Create or Update Variables
// updateOrCreateVariable(1, "port", $port, $result->sensors[$i]->port); // not nessessary
updateOrCreateVariable(0, "output", $port, $result->sensors[$i]->output); // Boolean
updateOrCreateVariable(2, "power", $port, $result->sensors[$i]->power); // Float
updateOrCreateVariable(0, "enabled", $port, $result->sensors[$i]->enabled); // Boolean
updateOrCreateVariable(2, "current", $port, $result->sensors[$i]->current); // Float
updateOrCreateVariable(2, "voltage", $port, $result->sensors[$i]->voltage); // Float
updateOrCreateVariable(2, "powerfactor",$port, $result->sensors[$i]->powerfactor); // Float
updateOrCreateVariable(0, "relay", $port, $result->sensors[$i]->relay); // Boolean
updateOrCreateVariable(1, "lock", $port, $result->sensors[$i]->lock); // Integer
updateOrCreateVariable(1, "prevmonth", $port, $result->sensors[$i]->prevmonth); // Integer
updateOrCreateVariable(1, "thismonth", $port, $result->sensors[$i]->thismonth); // Integer
}
}
}
function updateOrCreateVariable($type, $name, $port, $value){
// Check if the Variable available in Port Dummy Instance
$varID = "";
$childs = IPS_GetChildrenIDs($port);
foreach ($childs as $child) {
$o = IPS_GetObject($child);
if (strpos($o["ObjectInfo"], $name)!== false) {
$varID = $child;
break;
}
}
// Check if Profiles are available, create if not
if (!IPS_VariableProfileExists("mFi-Watt")) {
IPS_CreateVariableProfile("mFi-Watt", 2);
IPS_SetVariableProfileValues("mFi-Watt", 0.00, 0.00, 0.00);
IPS_SetVariableProfileIcon("mFi-Watt", "Electricity");
IPS_SetVariableProfileDigits("mFi-Watt", 2);
IPS_SetVariableProfileText ("mFi-Watt", "", " W");
}
if (!IPS_VariableProfileExists("mFi-Ampere")) {
IPS_CreateVariableProfile("mFi-Ampere", 2);
IPS_SetVariableProfileValues("mFi-Ampere", 0.00, 0.00, 0.00);
//IPS_SetVariableProfileIcon("mFi-Ampere", "Electricity");
IPS_SetVariableProfileDigits("mFi-Ampere", 2);
IPS_SetVariableProfileText ("mFi-Ampere", "", " A");
}
if($varID == ""){
$varID = IPS_CreateVariable($type); // 0:Boolean 1:Integer 2:Float 3:String
IPS_SetParent($varID, $port);
IPS_SetName($varID, $name);
IPS_SetInfo($varID, $name);
if($name == "output"){
IPS_SetVariableCustomProfile($varID, "~Switch");
IPS_SetVariableCustomAction($varID, $_IPS['SELF']);
IPS_SetName($varID, "Status"); // New Name
}
if($name == "power") IPS_SetVariableCustomProfile($varID, "mFi-Watt");
if($name == "current") IPS_SetVariableCustomProfile($varID, "mFi-Ampere");
if($name == "voltage") IPS_SetVariableCustomProfile($varID, "~Volt.230");
}
// Update variable
if($type == 0) SetValueBoolean($varID, ($value == 1) ? "true" : "false");
if($type == 1) SetValueInteger($varID, $value);
if($type == 2) SetValueFloat($varID, $value);
}
if ($_IPS['SENDER'] == "WebFront"){
$status = ($_IPS['VALUE'] == 1) ? 1 : 0; // 1 = On // empty = Off
$sender = IPS_GetObject($_IPS['VARIABLE']);
$parent = IPS_GetObject(IPS_GetParent($_IPS['VARIABLE']));
$portID = 0;
if (strpos($sender["ObjectInfo"], "output") !== false){
if (strpos($parent["ObjectInfo"], "Port 1") !== false) $portID = 1;
if (strpos($parent["ObjectInfo"], "Port 2") !== false) $portID = 2;
if (strpos($parent["ObjectInfo"], "Port 3") !== false) $portID = 3;
if (strpos($parent["ObjectInfo"], "Port 4") !== false) $portID = 4;
if (strpos($parent["ObjectInfo"], "Port 5") !== false) $portID = 5;
if (strpos($parent["ObjectInfo"], "Port 6") !== false) $portID = 6;
}else{
echo "Error: Wrong Sender";
}
if ($portID == 0) echo "Error: No port detected";
$curl = curl_init();
curl_setopt($curl, CURLOPT_TIMEOUT, $timeOut);
curl_setopt($curl, CURLOPT_URL, "http://" . $host . "/sensors/" . $portID);
curl_setopt($curl, CURLOPT_POSTFIELDS, "output=" . $status);
curl_setopt($curl, CURLOPT_COOKIE,"AIROS_SESSIONID=" . $cookieID);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec($curl);
if($debug == "1"){
echo "
WebFrontTest - Port: " . $portID . " - " . $status;
echo "
JSON-Data: " . $data;
}
// Refresh the status of all variables
$debug = 0; // Turn Debug off to show only the Messages for this action
readData();
}else{
readData();
}
?>
das Ganze ergibt wenn Debug aktiviert ist dann folgende Ausgabe im Debugger Fenster
JSON-Data: {"sensors":[{"port":1,"output":1,"power":0.0,"enabled":1,"current":0.0,"voltage":231.729263305,"powerfactor":0.0,"relay":1,"lock":0,"prevmonth":0,"thismonth":0},{"port":2,"output":1,"power":77.91257584,"enabled":1,"current":0.339102625,"voltage":231.698983192,"powerfactor":0.991635993,"relay":1,"lock":0,"prevmonth":92709,"thismonth":87126},{"port":3,"output":1,"power":78.093189597,"enabled":1,"current":0.347895681,"voltage":231.632225036,"powerfactor":0.969092302,"relay":1,"lock":0,"prevmonth":92731,"thismonth":87292},{"port":4,"output":1,"power":51.262525856,"enabled":0,"current":0.221684575,"voltage":231.659452438,"powerfactor":0.998192954,"relay":1,"lock":0,"prevmonth":38311,"thismonth":56686},{"port":5,"output":1,"power":0.0,"enabled":0,"current":0.0,"voltage":231.789358615,"powerfactor":0.0,"relay":1,"lock":0,"prevmonth":0,"thismonth":0},{"port":6,"output":1,"power":0.0,"enabled":0,"current":0.0,"voltage":231.862983703,"powerfactor":0.0,"relay":1,"lock":0,"prevmonth":0,"thismonth":0}],"status":"success"}
Status:success
Count: 6\Self: 12647
Steckdose Number: 1
Output:1
Power:0
enabled:1
Current:0
Voltage:231.729263305
powerfactor:0
Relay:1
lock:0
prevmonth:0
thismonth:0
Steckdose Number: 2
Output:1
Power:77.91257584
enabled:1
Current:0.339102625
Voltage:231.698983192
powerfactor:0.991635993
Relay:1
lock:0
prevmonth:92709
thismonth:87126
Steckdose Number: 3
Output:1
Power:78.093189597
enabled:1
Current:0.347895681
Voltage:231.632225036
powerfactor:0.969092302
Relay:1
lock:0
prevmonth:92731
thismonth:87292
Steckdose Number: 4
Output:1
Power:51.262525856
enabled:0
Current:0.221684575
Voltage:231.659452438
powerfactor:0.998192954
Relay:1
lock:0
prevmonth:38311
thismonth:56686
Steckdose Number: 5
Output:1
Power:0
enabled:0
Current:0
Voltage:231.789358615
powerfactor:0
Relay:1
lock:0
prevmonth:0
thismonth:0
Steckdose Number: 6
Output:1
Power:0
enabled:0
Current:0
Voltage:231.862983703
powerfactor:0
Relay:1
lock:0
prevmonth:0
thismonth:0
Die Variable Status (output) ist der Schalter
Manchmal dauert das Schalten leider 1-2 Sekunden bzw bis die Anzeige sich aktualisiert hat
Jetzt noch ein Zyklisches Ereignis auf das Script legen. (aber nicht zu klein, denke mal 10 Sekunden ist das Minimum)
Wie man eine „Live Überwachung“ einbaut weiß ich leider nicht da soweit ich weiß nur eine Abfrage der Daten möglich ist und kein „notify“