From 6b1b841e0ff5e248daf6997d280457212a3caea2 Mon Sep 17 00:00:00 2001 From: DanielHaefliger Date: Fri, 5 Dec 2025 07:55:46 +0100 Subject: [PATCH] no message --- Abrechnung/module.php | 136 ++++++++++++++++++------------------------ 1 file changed, 58 insertions(+), 78 deletions(-) diff --git a/Abrechnung/module.php b/Abrechnung/module.php index 8cb67e9..7b3c922 100644 --- a/Abrechnung/module.php +++ b/Abrechnung/module.php @@ -129,16 +129,19 @@ private function CalculatePowerCosts($powerMeters, $tariffs, $userId, $from, $to IPS_LogMessage("ABR", "====================== START CalculatePowerCosts ======================"); IPS_LogMessage("ABR", "UserId: $userId | From: $from (" . date("d.m.Y H:i", $from) . ") | To: $to (" . date("d.m.Y H:i", $to) . ")"); - // ---- Tarife Debug ---- + // ---- Debug: Tarife ---- IPS_LogMessage("ABR", "Geladene Tarife:"); foreach ($tariffs as $t) { - IPS_LogMessage("ABR", " Tarif: type=" . ($t['unit_type'] ?? '?') . + IPS_LogMessage( + "ABR", + " Tarif: type=" . ($t['unit_type'] ?? '?') . " price=" . ($t['price'] ?? '?') . " start=" . json_encode($t['start']) . - " end=" . json_encode($t['end'])); + " end=" . json_encode($t['end']) + ); } - // ---- Stromzähler für diesen Benutzer sammeln ---- + // ---- HTML Tabellenkopf ---- $html = " @@ -157,19 +160,18 @@ private function CalculatePowerCosts($powerMeters, $tariffs, $userId, $from, $to $acc = []; $meters = []; + // ---- Stromzähler suchen ---- IPS_LogMessage("ABR", "Suche nach Stromzählern für userId=$userId"); foreach ($powerMeters as $m) { - IPS_LogMessage("ABR", " Prüfe Zähler: name=" . $m['name'] . " user_id=" . $m['user_id']); + IPS_LogMessage("ABR", " Prüfe Zähler: {$m['name']} (user_id={$m['user_id']})"); - if ($m['user_id'] != $userId) - continue; + if ($m['user_id'] != $userId) continue; $name = $m['name']; $meters[$name] = [ - 'name' => $name, - 'imp' => $m['var_import'] ?? null, - 'exp' => $m['var_export'] ?? null + 'imp' => $m['var_import'] ?? null, + 'exp' => $m['var_export'] ?? null ]; IPS_LogMessage("ABR", " → Zugeordnet: $name | ImportVar=" . $meters[$name]['imp'] . " ExportVar=" . $meters[$name]['exp']); @@ -188,146 +190,124 @@ private function CalculatePowerCosts($powerMeters, $tariffs, $userId, $from, $to } if (empty($meters)) { - IPS_LogMessage("ABR", "⚠ Keine Stromzähler für diesen Benutzer gefunden!"); + IPS_LogMessage("ABR", "❌ Keine Stromzähler für Benutzer gefunden!"); $html .= "
Zähler
Keine Stromzähler für diesen Benutzer

"; return ['html' => $html, 'sum' => 0.0]; } IPS_LogMessage("ABR", "Gefundene Stromzähler: " . implode(", ", array_keys($meters))); - // ==== 15-MINUTEN INTERVALL-BERECHNUNG ==== - IPS_LogMessage("ABR", "Beginne 15-Minuten Analyse..."); - + // ==================================================================================== + // 🔥 HAUPTSCHLEIFE: 15-Minuten-Abrechnung + // ==================================================================================== for ($ts = $from; $ts < $to; $ts += 900) { - $slotEnd = min($to, $ts + 900); - IPS_LogMessage("ABR", "----- Intervall " . date("d.m.Y H:i", $ts) . " - " . date("d.m.Y H:i", $slotEnd) . " -----"); - // Tarife für dieses Intervall + $slotEnd = min($to, $ts + 900); + + IPS_LogMessage("ABR", "----- INTERVALL " . date("d.m.Y H:i", $ts) . " → " . date("d.m.Y H:i", $slotEnd) . " -----"); + + // Tarife zu Zeitpunkt ermitteln $pGrid = $this->getTariffPriceAt($tariffs, ['Netztarif'], $ts); $pSolar = $this->getTariffPriceAt($tariffs, ['Solartarif'], $ts); $pFeed = $this->getTariffPriceAt($tariffs, ['Einspeisetarif'], $ts); IPS_LogMessage("ABR", " Tarife: Grid=" . var_export($pGrid, true) . - " Solar=" . var_export($pSolar, true) . " FeedIn=" . var_export($pFeed, true)); + " Solar=" . var_export($pSolar, true) . + " FeedIn=" . var_export($pFeed, true)); $impTotal = 0.0; $expTotal = 0.0; - $slot = []; + $slot = []; - // --- Zählerwerte lesen --- + // ---- Zähler auslesen ---- foreach ($meters as $name => $mm) { - IPS_LogMessage("ABR", " → Zähler $name: Lade Werte Import=" . $mm['imp'] . " Export=" . $mm['exp']); - // Import delta + IPS_LogMessage("ABR", " → Zähler: $name"); + $impDelta = 0.0; - if (!empty($mm['imp']) && IPS_VariableExists((int)$mm['imp'])) { - $impDelta = $this->readDelta((int)$mm['imp'], $ts, $slotEnd); - IPS_LogMessage("ABR", " Import-Delta = $impDelta"); - } else { - IPS_LogMessage("ABR", " ⚠ Import-Variable existiert nicht!"); + $expDelta = 0.0; + + if (!empty($mm['imp'])) { + $impDelta = $this->saferReadDelta((int)$mm['imp'], $ts, $slotEnd); + IPS_LogMessage("ABR", " Import-Delta: $impDelta"); } - // Export delta - $expDelta = 0.0; - if (!empty($mm['exp']) && IPS_VariableExists((int)$mm['exp'])) { - $expDelta = $this->readDelta((int)$mm['exp'], $ts, $slotEnd); - IPS_LogMessage("ABR", " Export-Delta = $expDelta"); - } else { - IPS_LogMessage("ABR", " ⚠ Export-Variable existiert nicht!"); + if (!empty($mm['exp'])) { + $expDelta = $this->saferReadDelta((int)$mm['exp'], $ts, $slotEnd); + IPS_LogMessage("ABR", " Export-Delta: $expDelta"); } if ($impDelta > 0 || $expDelta > 0) { $slot[$name] = ['imp' => $impDelta, 'exp' => $expDelta]; $impTotal += $impDelta; $expTotal += $expDelta; - - IPS_LogMessage("ABR", " → Intervall Messung: imp=$impDelta exp=$expDelta"); - } else { - IPS_LogMessage("ABR", " → Kein Verbrauch/Einspeisung im Intervall"); } } IPS_LogMessage("ABR", " SUMME Intervall: impTotal=$impTotal | expTotal=$expTotal"); if ($impTotal == 0 && $expTotal == 0) { - IPS_LogMessage("ABR", " → Intervall übersprungen (keine Werte)"); + IPS_LogMessage("ABR", " → Übersprungen (keine Werte)"); continue; } - // ---- Verhältnis bestimmen ---- + // Verhältnis bestimmen PV vs Netz if ($impTotal <= $expTotal && $expTotal > 0) { $ratio = $impTotal / $expTotal; - IPS_LogMessage("ABR", " Fall: solar deckt Verbrauch → ratio=$ratio"); - } elseif ($impTotal > 0) { - $ratio = $expTotal / $impTotal; - IPS_LogMessage("ABR", " Fall: teilweiser Netzbezug → ratio=$ratio"); + IPS_LogMessage("ABR", " Fall Solar deckt alles → ratio=$ratio"); } else { - IPS_LogMessage("ABR", " Fall: keine klare Zuordnung, ratio=0"); - $ratio = 0.0; + $ratio = ($impTotal > 0) ? ($expTotal / $impTotal) : 0; + IPS_LogMessage("ABR", " Fall Teil-Netzbezug → ratio=$ratio"); } - // ---- Werte pro Zähler verrechnen ---- + // ----- Werte auf einzelne Zähler herunterbrechen ----- foreach ($slot as $name => $v) { + $imp = $v['imp']; $exp = $v['exp']; - IPS_LogMessage("ABR", " → Verrechne für $name: imp=$imp exp=$exp ratio=$ratio"); + IPS_LogMessage("ABR", " → Verrechne $name: imp=$imp exp=$exp ratio=$ratio"); - // Summen Import/Export + // Summen Import/Export speichern $acc[$name]['imp'] += $imp; $acc[$name]['exp'] += $exp; if ($impTotal <= $expTotal) { - IPS_LogMessage("ABR", " → PV deckt alles"); - + // PV deckt alles $acc[$name]['solar_bezug'] += $imp; $acc[$name]['solareinspeisung'] += (1 - $ratio) * $exp; $acc[$name]['solarverkauf'] += $ratio * $exp; - if ($pSolar !== null) - $acc[$name]['cost_solar'] += ($imp * $pSolar) / 100.0; + if ($pSolar !== null) $acc[$name]['cost_solar'] += ($imp * $pSolar) / 100; + if ($pFeed !== null) $acc[$name]['rev_feedin'] += ((1 - $ratio) * $exp * $pFeed) / 100; - if ($pFeed !== null) - $acc[$name]['rev_feedin'] += ((1 - $ratio) * $exp * $pFeed) / 100.0; } else { - IPS_LogMessage("ABR", " → Netzbezug notwendig"); - + // Netzbezug $acc[$name]['solar_bezug'] += $ratio * $imp; $acc[$name]['netz_bezug'] += (1 - $ratio) * $imp; $acc[$name]['solarverkauf'] += $exp; - if ($pGrid !== null) - $acc[$name]['cost_grid'] += ((1 - $ratio) * $imp * $pGrid) / 100.0; - - if ($pSolar !== null) - $acc[$name]['cost_solar'] += ($ratio * $imp * $pSolar) / 100.0; - - if ($pFeed !== null) - $acc[$name]['rev_feedin'] += ($exp * $pFeed) / 100.0; + if ($pGrid !== null) $acc[$name]['cost_grid'] += ((1 - $ratio) * $imp * $pGrid) / 100; + if ($pSolar !== null) $acc[$name]['cost_solar'] += ($ratio * $imp * $pSolar) / 100; + if ($pFeed !== null) $acc[$name]['rev_feedin'] += ($exp * $pFeed) / 100; } - - IPS_LogMessage("ABR", " → Ergebnis $name: solar_bezug=" . $acc[$name]['solar_bezug'] . - " netz_bezug=" . $acc[$name]['netz_bezug'] . - " solarE=" . $acc[$name]['solareinspeisung'] . - " solarV=" . $acc[$name]['solarverkauf'] . - " costGrid=" . $acc[$name]['cost_grid'] . - " costSolar=" . $acc[$name]['cost_solar'] . - " revFeed=" . $acc[$name]['rev_feedin']); } - - IPS_LogMessage("ABR", "-------------------------------------------------------------------"); } + // ==================================================================================== + // 📊 Abschlusstabelle + // ==================================================================================== IPS_LogMessage("ABR", "==== END 15-Minuten Analyse ===="); - // ---- Tabelle aufbauen ---- $sum = 0.0; + foreach ($acc as $name => $a) { - $subtotal = $a['cost_grid'] + $a['cost_solar'] - $a['rev_feedin']; - $sum += $subtotal; IPS_LogMessage("ABR", "FINAL $name: " . json_encode($a)); + $subtotal = $a['cost_grid'] + $a['cost_solar'] - $a['rev_feedin']; + $sum += $subtotal; + $html .= " {$name} " . number_format($a['imp'], 3) . "