diff --git a/Energy Counter Child/form.json b/Energy Counter Child/form.json index 9c90e10..d5e5f44 100644 --- a/Energy Counter Child/form.json +++ b/Energy Counter Child/form.json @@ -42,6 +42,12 @@ ] } ], - "actions": [], + "actions": [ + { + "type": "Button", + "caption": "Set total energy to current meter value", + "onClick": "ECC_SetTotalEnergyToCurrentSource($id);" + } + ], "status": [] } diff --git a/Energy Counter Child/locale.json b/Energy Counter Child/locale.json index 72e3332..04e977a 100644 --- a/Energy Counter Child/locale.json +++ b/Energy Counter Child/locale.json @@ -3,6 +3,7 @@ "de": { "Energy Counter Display": "Energiezähler Anzeige", "Values are calculated and reset only by the associated main module. This form controls visibility only; hidden values remain available in the background.": "Die Werte werden ausschließlich durch das zugehörige Hauptmodul berechnet und zurückgesetzt. Hier wird nur ihre Sichtbarkeit gesteuert; ausgeblendete Werte bleiben im Hintergrund vorhanden.", + "Set total energy to current meter value": "Gesamtenergie auf aktuellen Zählerstand setzen", "Display variables": "Anzuzeigende Variablen", "Show total energy": "Gesamtenergie anzeigen", "Show total cost": "Gesamtkosten anzeigen", diff --git a/Energy Counter Child/module.php b/Energy Counter Child/module.php index 0f80529..30fabd4 100644 --- a/Energy Counter Child/module.php +++ b/Energy Counter Child/module.php @@ -13,6 +13,7 @@ class EnergyCounterChild extends IPSModule // nicht im Formular angeboten. Die Childinstanz bleibt reine Anzeige. $this->RegisterPropertyInteger('ParentInstanceID', 0); $this->RegisterPropertyString('CounterKey', ''); + $this->RegisterPropertyInteger('SourceVariableID', 0); $this->RegisterPropertyBoolean('ShowTotalEnergy', true); $this->RegisterPropertyBoolean('ShowTotalCost', true); @@ -35,6 +36,25 @@ class EnergyCounterChild extends IPSModule $this->SetStatus(102); } + /** Setzt die Gesamtenergie über das Hauptmodul auf den aktuellen Zählerstand. */ + public function SetTotalEnergyToCurrentSource(): bool + { + $parentInstanceID = $this->ReadPropertyInteger('ParentInstanceID'); + $counterKey = $this->ReadPropertyString('CounterKey'); + if ($parentInstanceID <= 0 || $counterKey === '') { + IPS_LogMessage('EnergyCounterChild', 'Gesamtenergie kann nicht gesetzt werden: Zuordnung zum Hauptmodul fehlt.'); + return false; + } + + $function = 'ECM_SetCounterTotalEnergyFromSource'; + if (!function_exists($function)) { + IPS_LogMessage('EnergyCounterChild', 'Gesamtenergie kann nicht gesetzt werden: Hauptmodul-Funktion ist nicht verfügbar.'); + return false; + } + + return $function($parentInstanceID, $counterKey); + } + private function RegisterValueVariables() { $this->RegisterVariableFloat('TotalEnergy', $this->Translate('Total Energy'), self::ENERGY_PROFILE, 10); diff --git a/Energy Counter Main/module.php b/Energy Counter Main/module.php index ea2ebee..cb4bddb 100644 --- a/Energy Counter Main/module.php +++ b/Energy Counter Main/module.php @@ -83,7 +83,7 @@ class EnergyCounterMain extends IPSModule } IPS_SetName($childID, $counter['Name']); - $this->ConfigureChild($childID, $key); + $this->ConfigureChild($childID, $key, $counter['VariableID']); $children[$key] = $childID; } @@ -177,6 +177,60 @@ class EnergyCounterMain extends IPSModule $this->ResetEnergy('monthEnergy'); } + /** + * Übernimmt den aktuellen Rohzählerstand als Gesamtenergie eines einzelnen + * Counters. Diese Methode wird über den Initialisierungsbutton des Childs + * aufgerufen, die Zustandsführung bleibt aber im Hauptmodul. + */ + public function SetCounterTotalEnergyFromSource(string $counterKey): bool + { + // Noch nicht verarbeitete Änderungen sollen Tages- und Monatswerte + // weiterhin korrekt fortschreiben, bevor nur der Gesamtwert gesetzt wird. + $this->UpdateValues(); + if (!$this->EnterLock()) { + return false; + } + + try { + $counter = null; + foreach ($this->GetCounters() as $configuredCounter) { + if ($configuredCounter['Key'] === $counterKey) { + $counter = $configuredCounter; + break; + } + } + if ($counter === null || !IPS_VariableExists($counter['VariableID'])) { + $this->Log('Gesamtenergie konnte nicht gesetzt werden: Quellzähler für ' . $counterKey . ' ist nicht verfügbar.'); + return false; + } + + $sourceValue = GetValue($counter['VariableID']); + if (!is_numeric($sourceValue)) { + $this->Log('Gesamtenergie konnte nicht gesetzt werden: Quellzähler für ' . $counter['Name'] . ' enthält keinen numerischen Wert.'); + return false; + } + + $states = $this->ReadJsonAttribute('CounterState', []); + $state = $this->NormalizeState($states[$counterKey] ?? [], date('Y-m-d'), date('Y-m')); + $currentValue = (float) $sourceValue; + $state['initialized'] = true; + $state['lastRaw'] = $currentValue; + $state['totalEnergy'] = max(0.0, $currentValue); + $states[$counterKey] = $state; + $this->WriteAttributeString('CounterState', json_encode($states)); + + $children = $this->ReadJsonAttribute('CounterChildren', []); + $childID = isset($children[$counterKey]) ? (int) $children[$counterKey] : 0; + if ($childID > 0 && IPS_InstanceExists($childID)) { + $this->WriteChildValues($childID, $state, $counter['PricePerKWh']); + } + + return true; + } finally { + $this->LeaveLock(); + } + } + private function ResetEnergy(string $field) { // Vor dem Reset wird ein möglicherweise noch nicht verarbeiteter Anstieg @@ -333,7 +387,7 @@ class EnergyCounterMain extends IPSModule return ($instance['ModuleInfo']['ModuleID'] ?? '') === self::CHILD_MODULE_ID; } - private function ConfigureChild(int $childID, string $key) + private function ConfigureChild(int $childID, string $key, int $sourceVariableID) { $configuration = json_decode(IPS_GetConfiguration($childID), true); if (!is_array($configuration)) { @@ -341,6 +395,7 @@ class EnergyCounterMain extends IPSModule } $configuration['ParentInstanceID'] = $this->InstanceID; $configuration['CounterKey'] = $key; + $configuration['SourceVariableID'] = $sourceVariableID; IPS_SetConfiguration($childID, json_encode($configuration)); IPS_ApplyChanges($childID); } diff --git a/README.md b/README.md index 530dc8f..55b977b 100644 --- a/README.md +++ b/README.md @@ -12,6 +12,8 @@ Die Bibliothek enthält zwei Symcon-Module: 3. Übernehmen. Für jeden Tabellen-Eintrag wird eine Childinstanz in der Zielkategorie erstellt. 4. Bei Bedarf in jeder Childinstanz die nicht benötigten Werte ausblenden. +Über den Button **Gesamtenergie auf aktuellen Zählerstand setzen** in einer Childinstanz kann deren Gesamtenergie einmalig mit dem Wert ihrer verlinkten Quellvariable initialisiert werden. Die Tages- und Monatsenergie bleibt dabei unverändert. + Die Quellvariable wird nie verändert. Beim ersten Auslesen wird ihr Wert als Referenz übernommen; die berechneten Gesamtwerte beginnen daher bei `0 kWh`. Sinkt ein Hardwarezählerstand, wird er als Zählerreset behandelt, ohne bereits ermittelte Verbrauchswerte zu verringern. Entfernte Tabellen-Einträge löschen keine Childinstanzen oder historischen Werte automatisch. Das verhindert unbeabsichtigten Datenverlust; nicht mehr benötigte Childinstanzen können bei Bedarf manuell gelöscht werden. diff --git a/library.json b/library.json index d577063..092dd93 100644 --- a/library.json +++ b/library.json @@ -7,7 +7,7 @@ "version": "6.0", "date": 1491343200 }, - "version": "1.1", - "build": 2, + "version": "1.2", + "build": 3, "date": 0 }