Hi,
ich habe gerne übersichtliche Tabellen von allen Messwerten. Bisher hatte ich alle Variablen mit dem WebFront einzeln verlinkt, so wie es ein Anfänger (<2 Jahre dabei) halt macht. Meine smarten Anfänge liegen aber schon relativ weit zurück und es gibt ein Sammelsurium an Sensorwerten von Dect, Z-Wave, Wetter-Station, Feinstaubsensor und nicht zuletzt Zigbee.
Vor einigen Wochen wollte ich die Instanzen rekursiv nach Temperaturen absuchen aber paresy hat mich abgehalten und mir einen besseren Weg gezeigt:-) IPS_GetVariableList war meine Rettung.
Vielleicht hilft das Ergebnis ja auch anderen Benutzern.
Dieses Script durchsucht alle Variablen nach den (Custom)Profilen ‚~Temperature‘ und merkt sich Parent (Sensornamen), GParent (bei mir Zimmername), Wert und letzte Aktualisierung bzw. Änderung. Sortiert wird die Tabelle zuerst nach Zimmer und dann nach Sensor. Bei der ersten Ausführung des Scriptes wird ein zyklisches 15 Minuten Ereignis und eine HTML-Variable erzeugt die man jetzt nur noch mit dem Web-Front verlinken muss.
Dank HTML kann man auch Zusatzinformationen in die Werte einbauen. In diesem Beispiel ist es so das Temperaturen < 0 und > 40 Grad in Rot und < 10 Grad in Gelb dargestellt werden. Bei den Aktualisierungen wird Gelb dargestellt wenn eine Variable > 1 Tag nicht aktualisiert wurde und Rot bei > 7 Tagen. So kann man leicht sehen welche Sensoren Probleme haben könnten und in welchen Zimmern es Temperaturprobleme gibt.
<?php
$UpdateList = array();
$VariableIDsList = IPS_GetVariableList();
foreach ($VariableIDsList as $VariableID) {
$Variable = IPS_GetVariable($VariableID);
if (($Variable['VariableProfile'] == '~Temperature') || ($Variable['VariableCustomProfile'] == '~Temperature')){
$Parent = IPS_GetParent($VariableID);
$GParent = IPS_GetParent($Parent);
$Index = count($UpdateList);
$UpdateList[$Index]['VariableID'] = $VariableID;
$UpdateList[$Index]['Name'] = IPS_GetName($VariableID);
$UpdateList[$Index]['ParentName'] = IPS_GetName($Parent);
$UpdateList[$Index]['GParentName'] = IPS_GetName($GParent);
$UpdateList[$Index]['LastChanged'] = $Variable['VariableChanged'];
$UpdateList[$Index]['LastUpdated'] = $Variable['VariableUpdated'];
$UpdateList[$Index]['Value'] = $Variable['VariableValue'];
}
}
$Zimmer = array_column($UpdateList, 'GParentName');
$Name = array_column($UpdateList, 'Name');
array_multisort($Zimmer, SORT_ASC, $Name, SORT_ASC, $UpdateList);
// HTML ereugen
$HintergrundFarbcode = '000000';
$TextFarbcode = 'FFFFFF';
$TextFarbcodeOK = '00FF00';
$TextFarbcodeNOK = 'FF0000';
$TextFarbcodeACHTUNG = 'FFFF00';
$TextSize = 14;
$TextSizeTitle = $TextSize + 2;
$TextWeight = 'normal';
$Textausrichtung = 'text-align:left;';
$HTML_CSS_Style = '<style type="text/css">
.bt {border-collapse: separate;border-spacing: 2px;}
.bt td {font-family:Arial, sans-serif;font-size:' . $TextSize . 'px;color:#' . $TextFarbcode . ';' . $Textausrichtung . 'padding:1px 10px;border-style:solid;border-width:1px;overfNOK:hidden;word-break:normal;}
.bt th {font-family:Arial, sans-serif;font-size:' . $TextSize . 'px;color:#' . $TextFarbcode . ';' . $Textausrichtung . 'padding:1px 10px;border-style:solid;border-width:1px;overfNOK:hidden;word-break:normal;}
.bt .tb-title {font-size:' . $TextSizeTitle . 'px;padding:2mm;' . $Textausrichtung . 'background-color:#' . $HintergrundFarbcode . ';color:#' . $TextFarbcode . '}
.bt .tb-contentNORMAL {font-size:' . $TextSize . 'px;font-weight: ' . $TextWeight . ';padding:1mm;text-align:left;background-color:#' . $HintergrundFarbcode . ';color:#' . $TextFarbcode . '}
.bt .tb-content {font-size:' . $TextSize . 'px;font-weight: ' . $TextWeight . ';padding:1mm;' . $Textausrichtung . 'background-color:#' . $HintergrundFarbcode . ';color:#' . $TextFarbcode . '}
.bt .tb-contentOK {font-size:' . $TextSize . 'px;font-weight: ' . $TextWeight . ';padding:1mm;' . $Textausrichtung . 'background-color:#' . $HintergrundFarbcode . ';color:#' . $TextFarbcodeOK . '}
.bt .tb-contentNOK {font-size:' . $TextSize . 'px;font-weight: ' . $TextWeight . ';padding:1mm;' . $Textausrichtung . 'background-color:#' . $HintergrundFarbcode . ';color:#' . $TextFarbcodeNOK . '}
.bt .tb-contentACHTUNG {font-size:' . $TextSize . 'px;font-weight: ' . $TextWeight . ';padding:1mm;' . $Textausrichtung . 'background-color:#' . $HintergrundFarbcode . ';color:#' . $TextFarbcodeACHTUNG . '}
</style>';
$HTML = '<html>' . $HTML_CSS_Style;
$HTML .= '<table class="bt"';
$Titel = array();
$Titel = array('Instanz-ID');
$Titel[] = 'Sensor';
$Titel[] = 'Zimmer';
$Titel[] = 'Name';
$Titel[] = 'Letzte Var-Änderung';
$Titel[] = 'Letzte Var-Aktualisierung';
$Titel[] = 'Temperatur';
$HTML .= '<tr>';
$colspan = 1;
foreach ($Titel as $TitelEntry) {
$HTML .= '<th class="tb-title"><b>' . $TitelEntry . '</b></th>';
$colspan++;
}
$HTML .= '</tr><tr>';
foreach ($UpdateList as $Update) {
$HTML .= '<th class="tb-content">' . $Update['VariableID'] . '</th>';
$HTML .= '<th class="tb-contentNORMAL">' . $Update['ParentName'] . '</th>';
$HTML .= '<th class="tb-contentNORMAL">' . $Update['GParentName'] . '</th>';
$HTML .= '<th class="tb-contentNORMAL">' . $Update['Name'] . '</th>';
$HTML .= '<th class="tb-content">' . date('d.m.Y H:i', $Update['LastChanged']) . '</th>';
if ($Update['LastUpdated'] > 0){
if ((time() - $Update['LastUpdated']) > 7*86400){
$HTML .= '<th class="tb-contentNOK">' . date('d.m.Y H:i', $Update['LastUpdated']) . '</th>';
}
else if ((time() - $Update['LastUpdated']) > 1*86400){
$HTML .= '<th class="tb-contentACHTUNG">' . date('d.m.Y H:i', $Update['LastUpdated']) . '</th>';
}
else $HTML .= '<th class="tb-content">' . date('d.m.Y H:i', $Update['LastUpdated']) . '</th>';
}
if (($Update['Value'] < 0) || ($Update['Value'] > 40)){
$HTML .= '<th class="tb-contentNOK">' . number_format($Update['Value'], 1) . 'C°</th>';
}
else if ($Update['Value'] < 10){
$HTML .= '<th class="tb-contentACHTUNG">' . number_format($Update['Value'], 1) . 'C°</th>';
}
else{
$HTML .= '<th class="tb-content">' . number_format($Update['Value'], 1) . 'C°</th>';
}
$HTML .= '</tr>';
}
$HTML .= '</table></html>';
$vid = @IPS_GetVariableIDByName("Temperatur-Tabelle", $_IPS['SELF']);
if ($vid == false){
$vid = IPS_CreateVariable(3);
IPS_SetParent($vid, $_IPS['SELF']); //Variable zuordnen
IPS_SetName($vid, "Temperatur-Tabelle");
IPS_SetVariableCustomProfile($vid, "~HTMLBox");
}
SetValue( $vid, $HTML);
if (@IPS_GetEventIDByName("CheckTemperatur", $_IPS['SELF']) == false){
$eid = IPS_CreateEvent(1); //Zyklisches Ereignis
IPS_SetEventCyclic($eid, 2, 1, 0, 0, 2, 15); //Jeden Tag alle 15 Minuten
IPS_SetParent($eid, $_IPS['SELF']); //Ereignis zuordnen
IPS_SetEventActive($eid, true); //Ereignis aktivieren
IPS_SetName($eid, "CheckTemperatur");
}
/*
foreach ($UpdateList as $Update){
var_dump($Update);
}
*/
?>
Nach dem selben Prinzip habe ich auch Tabellen für Luftdruck, Luftfeuchtigkeit, Stromverbrauch und Zigbee-Geräte erstellt.
Anbei noch ein Bild wie die Tabelle bei mir aussieht:
Ach ja die Mittelwerte in der Tabelle stammen von Scripten die bei einigen Zimmern den Mittelwert der Sensoren berechnen.
Viel Spaß Ralf