added yearly Energy and costs

This commit is contained in:
jhartworks committed 2026-10-21 13:23:40 +02:00
1 parent ac0621ffe4
commit 989d58ad1a
8 files changed
+54 -12

No files matched your search

+22 -6
View File
@@ -109,6 +109,7 @@ class EnergyCounterMain extends IPSModule
$children = $this->ReadJsonAttribute('CounterChildren', []);
$today = date('Y-m-d');
$month = date('Y-m');
$year = date('Y');
foreach ($this->GetCounters() as $counter) {
$key = $counter['Key'];
@@ -124,7 +125,7 @@ class EnergyCounterMain extends IPSModule
continue;
}
$state = $this->NormalizeState($states[$key] ?? [], $today, $month);
$state = $this->NormalizeState($states[$key] ?? [], $today, $month, $year);
$currentValue = (float) $sourceValue;
// Die erste Messung setzt nur die Referenz. Dadurch wird kein bereits
@@ -138,6 +139,7 @@ class EnergyCounterMain extends IPSModule
$state['totalEnergy'] += $delta;
$state['dayEnergy'] += $delta;
$state['monthEnergy'] += $delta;
$state['yearEnergy'] += $delta;
} else {
// Ein zurückgesetzter/überlaufener Hardwarezähler darf den
// bereits berechneten Verbrauch nicht negativ verändern.
@@ -177,6 +179,12 @@ class EnergyCounterMain extends IPSModule
$this->ResetEnergy('monthEnergy');
}
/** Setzt ausschließlich die im Hauptmodul berechneten Jahreswerte zurück. */
public function ResetYearEnergy()
{
$this->ResetEnergy('yearEnergy');
}
/**
* Übernimmt den aktuellen Rohzählerstand als Gesamtenergie eines einzelnen
* Counters. Diese Methode wird über den Initialisierungsbutton des Childs
@@ -211,7 +219,7 @@ class EnergyCounterMain extends IPSModule
}
$states = $this->ReadJsonAttribute('CounterState', []);
$state = $this->NormalizeState($states[$counterKey] ?? [], date('Y-m-d'), date('Y-m'));
$state = $this->NormalizeState($states[$counterKey] ?? [], date('Y-m-d'), date('Y-m'), date('Y'));
$currentValue = (float) $sourceValue;
$state['initialized'] = true;
$state['lastRaw'] = $currentValue;
@@ -245,7 +253,7 @@ class EnergyCounterMain extends IPSModule
$children = $this->ReadJsonAttribute('CounterChildren', []);
foreach ($this->GetCounters() as $counter) {
$key = $counter['Key'];
$state = $this->NormalizeState($states[$key] ?? [], date('Y-m-d'), date('Y-m'));
$state = $this->NormalizeState($states[$key] ?? [], date('Y-m-d'), date('Y-m'), date('Y'));
$state[$field] = 0.0;
$states[$key] = $state;
@@ -328,7 +336,7 @@ class EnergyCounterMain extends IPSModule
return $counters;
}
private function NormalizeState(array $state, string $today, string $month): array
private function NormalizeState(array $state, string $today, string $month, string $year): array
{
$normalized = [
'initialized' => (bool) ($state['initialized'] ?? false),
@@ -336,8 +344,10 @@ class EnergyCounterMain extends IPSModule
'totalEnergy' => max(0.0, (float) ($state['totalEnergy'] ?? 0)),
'dayEnergy' => max(0.0, (float) ($state['dayEnergy'] ?? 0)),
'monthEnergy' => max(0.0, (float) ($state['monthEnergy'] ?? 0)),
'yearEnergy' => max(0.0, (float) ($state['yearEnergy'] ?? 0)),
'dayStamp' => (string) ($state['dayStamp'] ?? $today),
'monthStamp' => (string) ($state['monthStamp'] ?? $month)
'monthStamp' => (string) ($state['monthStamp'] ?? $month),
'yearStamp' => (string) ($state['yearStamp'] ?? $year)
];
if ($normalized['dayStamp'] !== $today) {
@@ -348,6 +358,10 @@ class EnergyCounterMain extends IPSModule
$normalized['monthEnergy'] = 0.0;
$normalized['monthStamp'] = $month;
}
if ($normalized['yearStamp'] !== $year) {
$normalized['yearEnergy'] = 0.0;
$normalized['yearStamp'] = $year;
}
return $normalized;
}
@@ -360,7 +374,9 @@ class EnergyCounterMain extends IPSModule
'DayEnergy' => $state['dayEnergy'],
'DayCost' => $state['dayEnergy'] * $pricePerKWh,
'MonthEnergy' => $state['monthEnergy'],
'MonthCost' => $state['monthEnergy'] * $pricePerKWh
'MonthCost' => $state['monthEnergy'] * $pricePerKWh,
'YearEnergy' => $state['yearEnergy'],
'YearCost' => $state['yearEnergy'] * $pricePerKWh
];
foreach ($values as $ident => $value) {