no message

This commit is contained in:
2025-12-05 07:55:46 +01:00
parent 0f338763e1
commit 6b1b841e0f

View File

@@ -129,16 +129,19 @@ private function CalculatePowerCosts($powerMeters, $tariffs, $userId, $from, $to
IPS_LogMessage("ABR", "====================== START CalculatePowerCosts ======================"); 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) . ")"); 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:"); IPS_LogMessage("ABR", "Geladene Tarife:");
foreach ($tariffs as $t) { foreach ($tariffs as $t) {
IPS_LogMessage("ABR", " Tarif: type=" . ($t['unit_type'] ?? '?') . IPS_LogMessage(
"ABR",
" Tarif: type=" . ($t['unit_type'] ?? '?') .
" price=" . ($t['price'] ?? '?') . " price=" . ($t['price'] ?? '?') .
" start=" . json_encode($t['start']) . " 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 = "<table border='1' cellspacing='0' cellpadding='3' width='100%' style='font-size:8px;'> $html = "<table border='1' cellspacing='0' cellpadding='3' width='100%' style='font-size:8px;'>
<tr style='background-color:#f0f0f0;'> <tr style='background-color:#f0f0f0;'>
<th>Zähler</th> <th>Zähler</th>
@@ -157,19 +160,18 @@ private function CalculatePowerCosts($powerMeters, $tariffs, $userId, $from, $to
$acc = []; $acc = [];
$meters = []; $meters = [];
// ---- Stromzähler suchen ----
IPS_LogMessage("ABR", "Suche nach Stromzählern für userId=$userId"); IPS_LogMessage("ABR", "Suche nach Stromzählern für userId=$userId");
foreach ($powerMeters as $m) { 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) if ($m['user_id'] != $userId) continue;
continue;
$name = $m['name']; $name = $m['name'];
$meters[$name] = [ $meters[$name] = [
'name' => $name, 'imp' => $m['var_import'] ?? null,
'imp' => $m['var_import'] ?? null, 'exp' => $m['var_export'] ?? null
'exp' => $m['var_export'] ?? null
]; ];
IPS_LogMessage("ABR", " → Zugeordnet: $name | ImportVar=" . $meters[$name]['imp'] . " ExportVar=" . $meters[$name]['exp']); 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)) { 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 .= "<tr><td colspan='11' align='center'>Keine Stromzähler für diesen Benutzer</td></tr></table><br>"; $html .= "<tr><td colspan='11' align='center'>Keine Stromzähler für diesen Benutzer</td></tr></table><br>";
return ['html' => $html, 'sum' => 0.0]; return ['html' => $html, 'sum' => 0.0];
} }
IPS_LogMessage("ABR", "Gefundene Stromzähler: " . implode(", ", array_keys($meters))); 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) { 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); $pGrid = $this->getTariffPriceAt($tariffs, ['Netztarif'], $ts);
$pSolar = $this->getTariffPriceAt($tariffs, ['Solartarif'], $ts); $pSolar = $this->getTariffPriceAt($tariffs, ['Solartarif'], $ts);
$pFeed = $this->getTariffPriceAt($tariffs, ['Einspeisetarif'], $ts); $pFeed = $this->getTariffPriceAt($tariffs, ['Einspeisetarif'], $ts);
IPS_LogMessage("ABR", " Tarife: Grid=" . var_export($pGrid, true) . 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; $impTotal = 0.0;
$expTotal = 0.0; $expTotal = 0.0;
$slot = []; $slot = [];
// --- Zählerwerte lesen --- // ---- Zähler auslesen ----
foreach ($meters as $name => $mm) { 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; $impDelta = 0.0;
if (!empty($mm['imp']) && IPS_VariableExists((int)$mm['imp'])) { $expDelta = 0.0;
$impDelta = $this->readDelta((int)$mm['imp'], $ts, $slotEnd);
IPS_LogMessage("ABR", " Import-Delta = $impDelta"); if (!empty($mm['imp'])) {
} else { $impDelta = $this->saferReadDelta((int)$mm['imp'], $ts, $slotEnd);
IPS_LogMessage("ABR", " Import-Variable existiert nicht!"); IPS_LogMessage("ABR", " Import-Delta: $impDelta");
} }
// Export delta if (!empty($mm['exp'])) {
$expDelta = 0.0; $expDelta = $this->saferReadDelta((int)$mm['exp'], $ts, $slotEnd);
if (!empty($mm['exp']) && IPS_VariableExists((int)$mm['exp'])) { IPS_LogMessage("ABR", " Export-Delta: $expDelta");
$expDelta = $this->readDelta((int)$mm['exp'], $ts, $slotEnd);
IPS_LogMessage("ABR", " Export-Delta = $expDelta");
} else {
IPS_LogMessage("ABR", " ⚠ Export-Variable existiert nicht!");
} }
if ($impDelta > 0 || $expDelta > 0) { if ($impDelta > 0 || $expDelta > 0) {
$slot[$name] = ['imp' => $impDelta, 'exp' => $expDelta]; $slot[$name] = ['imp' => $impDelta, 'exp' => $expDelta];
$impTotal += $impDelta; $impTotal += $impDelta;
$expTotal += $expDelta; $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"); IPS_LogMessage("ABR", " SUMME Intervall: impTotal=$impTotal | expTotal=$expTotal");
if ($impTotal == 0 && $expTotal == 0) { if ($impTotal == 0 && $expTotal == 0) {
IPS_LogMessage("ABR", "Intervall übersprungen (keine Werte)"); IPS_LogMessage("ABR", "Übersprungen (keine Werte)");
continue; continue;
} }
// ---- Verhältnis bestimmen ---- // Verhältnis bestimmen PV vs Netz
if ($impTotal <= $expTotal && $expTotal > 0) { if ($impTotal <= $expTotal && $expTotal > 0) {
$ratio = $impTotal / $expTotal; $ratio = $impTotal / $expTotal;
IPS_LogMessage("ABR", " Fall: solar deckt Verbrauch → ratio=$ratio"); IPS_LogMessage("ABR", " Fall Solar deckt alles → ratio=$ratio");
} elseif ($impTotal > 0) {
$ratio = $expTotal / $impTotal;
IPS_LogMessage("ABR", " Fall: teilweiser Netzbezug → ratio=$ratio");
} else { } else {
IPS_LogMessage("ABR", " Fall: keine klare Zuordnung, ratio=0"); $ratio = ($impTotal > 0) ? ($expTotal / $impTotal) : 0;
$ratio = 0.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) { foreach ($slot as $name => $v) {
$imp = $v['imp']; $imp = $v['imp'];
$exp = $v['exp']; $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]['imp'] += $imp;
$acc[$name]['exp'] += $exp; $acc[$name]['exp'] += $exp;
if ($impTotal <= $expTotal) { if ($impTotal <= $expTotal) {
IPS_LogMessage("ABR", " PV deckt alles"); // PV deckt alles
$acc[$name]['solar_bezug'] += $imp; $acc[$name]['solar_bezug'] += $imp;
$acc[$name]['solareinspeisung'] += (1 - $ratio) * $exp; $acc[$name]['solareinspeisung'] += (1 - $ratio) * $exp;
$acc[$name]['solarverkauf'] += $ratio * $exp; $acc[$name]['solarverkauf'] += $ratio * $exp;
if ($pSolar !== null) if ($pSolar !== null) $acc[$name]['cost_solar'] += ($imp * $pSolar) / 100;
$acc[$name]['cost_solar'] += ($imp * $pSolar) / 100.0; 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 { } else {
IPS_LogMessage("ABR", " → Netzbezug notwendig"); // Netzbezug
$acc[$name]['solar_bezug'] += $ratio * $imp; $acc[$name]['solar_bezug'] += $ratio * $imp;
$acc[$name]['netz_bezug'] += (1 - $ratio) * $imp; $acc[$name]['netz_bezug'] += (1 - $ratio) * $imp;
$acc[$name]['solarverkauf'] += $exp; $acc[$name]['solarverkauf'] += $exp;
if ($pGrid !== null) if ($pGrid !== null) $acc[$name]['cost_grid'] += ((1 - $ratio) * $imp * $pGrid) / 100;
$acc[$name]['cost_grid'] += ((1 - $ratio) * $imp * $pGrid) / 100.0; if ($pSolar !== null) $acc[$name]['cost_solar'] += ($ratio * $imp * $pSolar) / 100;
if ($pFeed !== null) $acc[$name]['rev_feedin'] += ($exp * $pFeed) / 100;
if ($pSolar !== null)
$acc[$name]['cost_solar'] += ($ratio * $imp * $pSolar) / 100.0;
if ($pFeed !== null)
$acc[$name]['rev_feedin'] += ($exp * $pFeed) / 100.0;
} }
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 ===="); IPS_LogMessage("ABR", "==== END 15-Minuten Analyse ====");
// ---- Tabelle aufbauen ----
$sum = 0.0; $sum = 0.0;
foreach ($acc as $name => $a) { 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)); IPS_LogMessage("ABR", "FINAL $name: " . json_encode($a));
$subtotal = $a['cost_grid'] + $a['cost_solar'] - $a['rev_feedin'];
$sum += $subtotal;
$html .= "<tr> $html .= "<tr>
<td>{$name}</td> <td>{$name}</td>
<td align='right'>" . number_format($a['imp'], 3) . "</td> <td align='right'>" . number_format($a['imp'], 3) . "</td>