C:\Smart-thinks\waschmaschine.json # Timer als Windows-Aufgabe auf benötigte Zeit zur aktualisierung einrichten ################################################################################################################# $jsonPfad = 'C:/Smart-thinks/waschmaschine.json'; $targetId = $_IPS['SELF']; // 1. LOKALE JSON-DATEI EINLESEN if (!file_exists($jsonPfad)) { echo "Fehler: Die Datei $jsonPfad existiert noch nicht.\n"; return; } $response = file_get_contents($jsonPfad); $data = json_decode($response, true); if (json_last_error() !== JSON_ERROR_NONE) { echo "Fehler beim Parsen der JSON-Datei. Inhalt ist kein gültiges JSON."; return; } // ========================================== // 2. DATEN VERARBEITEN & FILTERN // ========================================== $flatData = []; if (isset($data['components'])) { flattenArray($data['components'], '', $flatData); } else { flattenArray($data, '', $flatData); } // Restlaufzeit berechnen $calculatedMinutes = null; foreach ($flatData as $key => $value) { if (strpos($key, 'remainingTime_value') !== false) { $calculatedMinutes = convertToMinutes($value); break; } } if ($calculatedMinutes !== null) { $flatData['Betriebsdaten_Restlaufzeit_In_Minuten'] = $calculatedMinutes; } // Variablen in Symcon aktualisieren foreach ($flatData as $key => $value) { $ident = preg_replace('/[^a-zA-Z0-9]/', '', $key); if (strlen($ident) > 100) $ident = substr($ident, -100); $varId = @IPS_GetObjectIDByIdent($ident, $targetId); if ($varId === false) { $varType = getSymconVarType($value); if ($varType === null) continue; $varId = IPS_CreateVariable($varType); IPS_SetParent($varId, $targetId); $friendlyName = str_replace('_', ' ➔ ', $key); IPS_SetName($varId, $friendlyName); IPS_SetIdent($varId, $ident); if (strpos($key, 'energyConsumption_value') !== false) IPS_SetVariableCustomProfile($varId, '~Electricity'); if ($key === 'Betriebsdaten_Restlaufzeit_In_Minuten') IPS_SetVariableCustomProfile($varId, '~Minute'); } if (strpos($key, 'energyConsumption_value') !== false) $value = $value / 1000; if (GetValue($varId) !== $value) { SetValue($varId, $value); } } echo "Waschmaschine erfolgreich Aktualisiert am: " . date('d.m.Y H:i:s') . "\n"; // ========================================== // HILFSFUNKTIONEN // ========================================== function flattenArray($array, $prefix, &$result) { foreach ($array as $key => $value) { if (strpos($key, 'supported') !== false || strpos($key, 'optional') !== false || strpos($key, 'timestamp') !== false) { continue; } $newKey = $prefix === '' ? $key : $prefix . '_' . $key; if (is_array($value)) { if (empty($value)) continue; if (count($value) == 1 && isset($value['value'])) { $result[$newKey . '_value'] = $value['value']; } else { flattenArray($value, $newKey, $result); } } else { if ($value === null) $value = ''; $result[$newKey] = $value; } } } function convertToMinutes($rawTime) { if (preg_match('/PT(?:(\d+)H)?(?:(\d+)M)?/', $rawTime, $matches)) { return ((isset($matches[1]) ? (int)$matches[1] : 0) * 60) + (isset($matches[2]) ? (int)$matches[2] : 0); } elseif (preg_match('/(\d+):(\d+):(\d+)/', $rawTime, $matches)) { return ((int)$matches[1] * 60) + (int)$matches[2]; } return (int)$rawTime; } function getSymconVarType($value) { if (is_bool($value)) return 0; if (is_int($value)) return 1; if (is_float($value)) return 2; if (is_string($value)) return 3; return null; } ########################### 01.08.26 ################################################### // ========================================== // 3. GEZIELTE DATENEXTRAKTION // ========================================== // Hinweis: Die genauen JSON-Pfade können je nach Waschmaschinen-Modell leicht variieren. // Standardmäßig nutzt Samsung für Waschmaschinen folgende Strukturen im "main"-Cluster: $extractedData = [ 'Status' => isset($data['components']['main']['washboardMachineState']['machineState']['value']) ? $data['components']['main']['washboardMachineState']['machineState']['value'] : 'Unbekannt', 'Programm' => isset($data['components']['main']['washerCycle']['washerCycle']['value']) ? $data['components']['main']['washerCycle']['washerCycle']['value'] : 'Unbekannt', 'Restlaufzeit' => isset($data['components']['main']['remoteControlStatus']['remainingTime']['value']) ? $data['components']['main']['remoteControlStatus']['remainingTime']['value'] : (isset($data['components']['main']['dryerJobStatus']['remainingTime']['value']) ? $data['components']['main']['dryerJobStatus']['remainingTime']['value'] : '00:00:00'), 'Energieverbrauch'=> isset($data['components']['main']['powerConsumptionReport']['energyConsumption']['value']) ? ($data['components']['main']['powerConsumptionReport']['energyConsumption']['value'] / 1000) // Umrechnung von Wh in kWh : 0.0 ]; echo 'Restlaufzeit'; echo " ".PHP_EOL; echo 'Energieverbrauch'; echo " ".PHP_EOL; // ========================================== // 4. VARIABLEN IN SYMCON AKTUALISIEREN // ========================================== foreach ($extractedData as $name => $value) { // Identifikator ohne Sonderzeichen $ident = preg_replace('/[^a-zA-Z0-9_]/', '', $name); // Prüfen, ob die Variable existiert $varId = @IPS_GetObjectIDByIdent($ident, $targetId); // Wenn nicht, neu anlegen if ($varId === false) { $varType = getSymconVarType($value); $varId = IPS_CreateVariable($varType); IPS_SetParent($varId, $targetId); IPS_SetName($varId, $name); IPS_SetIdent($varId, $ident); // Zuweisung von Standard-Profilen für schönere Darstellung in Symcon if ($name === 'Energieverbrauch') { IPS_SetVariableCustomProfile($varId, '~Electricity'); // Fügt "kWh" als Einheit an } echo "Variable angelegt: $name\n"; } // Wert bei Änderung aktualisieren if (GetValue($varId) !== $value) { SetValue($varId, $value); } } echo "Die 4 Kern-Werte wurden erfolgreich aktualisiert.\n";