diff --git a/Energy_Pie/module.php b/Energy_Pie/module.php index 4f07bef..57513ca 100644 --- a/Energy_Pie/module.php +++ b/Energy_Pie/module.php @@ -100,34 +100,59 @@ class Energy_Pie extends IPSModule /* ===================== CORE LOGIC ======================== */ /* ========================================================= */ - private function RecalculateAndPush(): void - { - $range = $this->ReadAttributeString(self::ATTR_RANGE); - $date = $this->ReadAttributeString(self::ATTR_DATE); +private function RecalculateAndPush(): void +{ + $range = $this->ReadAttributeString(self::ATTR_RANGE); + $date = $this->ReadAttributeString(self::ATTR_DATE); - [$tStart, $tEnd] = $this->getRange($range, $date); + [$tStart, $tEnd] = $this->getRange($range, $date); - $prod = $this->readDelta($this->ReadPropertyInteger('VarProduction'), $tStart, $tEnd); - $cons = $this->readDelta($this->ReadPropertyInteger('VarConsumption'), $tStart, $tEnd); - $feed = $this->readDelta($this->ReadPropertyInteger('VarFeedIn'), $tStart, $tEnd); - $grid = $this->readDelta($this->ReadPropertyInteger('VarGrid'), $tStart, $tEnd); + // Deltas aus dem Archiv + $prod = $this->readDelta($this->ReadPropertyInteger('VarProduction'), $tStart, $tEnd); + $feed = $this->readDelta($this->ReadPropertyInteger('VarFeedIn'), $tStart, $tEnd); + $grid = $this->readDelta($this->ReadPropertyInteger('VarGrid'), $tStart, $tEnd); - $payload = [ - 'range' => $range, - 'date' => $date, - 'tStart' => $tStart, - 'tEnd' => $tEnd, - 'values' => [ - 'Produktion' => $prod, - 'Verbrauch' => $cons, - 'Einspeisung' => $feed, - 'Netz' => $grid - ] - ]; + // Hausverbrauch = Produktion - Einspeisung + Netz + $house = $prod - $feed + $grid; - $this->UpdateVisualizationValue(json_encode($payload, JSON_THROW_ON_ERROR)); + // optionaler Schutz (z.B. wenn Logs/Resets Mist bauen) + if ($house < 0) { + $house = 0.0; } + // Debug ins Log, damit du die Rechnung sofort prüfen kannst + $this->LogMessage( + sprintf( + "EnergyPie | Range=%s Date=%s | %s -> %s | Prod=%.3f Feed=%.3f Grid=%.3f House=%.3f", + $range, + $date, + date('Y-m-d H:i:s', $tStart), + date('Y-m-d H:i:s', $tEnd), + $prod, + $feed, + $grid, + $house + ), + KL_NOTIFY + ); + + $payload = [ + 'range' => $range, + 'date' => $date, + 'tStart' => $tStart, + 'tEnd' => $tEnd, + 'values' => [ + 'Produktion' => (float)$prod, + 'Einspeisung' => (float)$feed, + 'Netz' => (float)$grid, + 'Hausverbrauch'=> (float)$house + ] + ]; + + $this->UpdateVisualizationValue(json_encode($payload, JSON_THROW_ON_ERROR)); +} + + /* ========================================================= */ /* ===================== TIME RANGES ======================= */ /* ========================================================= */