Da ist ein kleiner Fehler drin.
Ich habe mal etwas geschnitzt, was drei Variablen mit Profil anlegt. Fehler, Unknown und eine Tabelle als Übersicht mit Fehler und Unkown.
<?php
// =============================================================
// Profil anlegen (Grün bei 0, Rot bei >0, tatsächlicher Wert anzeigen)
// =============================================================
$profileName = "Module.Status";
if (!IPS_VariableProfileExists($profileName)) {
IPS_CreateVariableProfile($profileName, 1); // Integer
IPS_SetVariableProfileValues($profileName, 0, 50, 1); // Max 50 Fehler, mehr haben hoffentlich keinen Fehler
IPS_SetVariableProfileIcon($profileName, "Warning");
// 0 = Grün
IPS_SetVariableProfileAssociation($profileName, 0, "0", "", 0x00FF00);
// 1–50 = Rot
for ($i = 1; $i <= 50; $i++) {
IPS_SetVariableProfileAssociation($profileName, $i, (string)$i, "", 0xFF0000);
}
}
// =============================================================
// Variablen automatisch anlegen
// =============================================================
$parent = $_IPS['SELF']; // Script als Parent
$vErrors = CreateVariableByIdent($parent, "ModuleErrors", "Module Fehler (Offline)", 1, 10, $profileName);
$vUnknown = CreateVariableByIdent($parent, "ModuleUnknown", "Module Unknown", 1, 20, $profileName);
$vHtml = CreateVariableByIdent($parent, "ModuleStatusHtml", "Module Status Tabelle", 3, 30, "~HTMLBox");
// =============================================================
// JSON laden
// =============================================================
$status = json_decode(GetValue(36740), true);
// Ungültiges oder leeres JSON → 999 setzen
if (!is_array($status)) {
SetValue($vErrors, 999);
SetValue($vUnknown, 999);
SetValue($vHtml, "<p>Keine gültigen Daten</p>");
return;
}
// =============================================================
// Fehler getrennt zählen
// =============================================================
// Offline
$errors = count(array_filter(
$status,
static fn($value) => $value === 'Offline'
));
// Unknown
$unknown = count(array_filter(
$status,
static fn($value) => $value === 'Unknown'
));
SetValue($vErrors, $errors);
SetValue($vUnknown, $unknown);
// =============================================================
// HTML-Tabelle erzeugen (nur Offline + Unknown)
// =============================================================
$html = '<table style="width:100%; border-collapse:collapse;" border="1" cellpadding="4" cellspacing="0">';
$html .= '<tr><th>ID</th><th>Name</th><th>Status</th></tr>';
foreach ($status as $id => $value) {
if ($value !== 'Alive') {
$name = IPS_GetName((int)$id);
$html .= sprintf(
'<tr><td>%s</td><td>%s</td><td>%s</td></tr>',
$id,
htmlspecialchars($name),
$value
);
}
}
$html .= '</table>';
SetValue($vHtml, $html);
// =============================================================
// Hilfsfunktion
// =============================================================
function CreateVariableByIdent($parent, $ident, $name, $type, $position, $profile = "") {
$vid = @IPS_GetObjectIDByIdent($ident, $parent);
if ($vid === false) {
$vid = IPS_CreateVariable($type);
IPS_SetParent($vid, $parent);
IPS_SetName($vid, $name);
IPS_SetIdent($vid, $ident);
IPS_SetPosition($vid, $position);
}
if ($profile != "" && IPS_VariableProfileExists($profile)) {
IPS_SetVariableCustomProfile($vid, $profile);
}
return $vid;
}