no message

This commit is contained in:
2025-11-06 15:29:31 +01:00
parent eb0f6e05ed
commit 2c522454c0

View File

@@ -186,119 +186,82 @@ private function CalculatePowerCosts($powerMeters, $tariffs, $userId, $from, $to
} }
// 15-Minuten-Schritte // 15-Minuten-Schritte
for ($ts = $from; $ts < $to; $ts += 900) { for ($ts = $from; $ts < $to; $ts += 900) {
$slotEnd = min($to, $ts + 900); $slotEnd = min($to, $ts + 900);
// Tarife abrufen $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('Abrechnung', sprintf("⏱ %s %s | Tarife: Netz=%.3f Rp, Solar=%.3f Rp, Einspeise=%.3f Rp", $impTotal = 0.0;
date('d.m.Y H:i', $ts), date('H:i', $slotEnd), $expTotal = 0.0;
$pGrid ?? 0, $pSolar ?? 0, $pFeed ?? 0 $slot = [];
));
$impTotal = 0.0; foreach ($meters as $name => $mm) {
$expTotal = 0.0; $impDelta = $expDelta = 0.0;
$slot = [];
foreach ($meters as $name => $mm) { if (!empty($mm['imp']) && IPS_VariableExists((int)$mm['imp'])) {
$impDelta = 0.0; $impDelta = $this->readDelta((int)$mm['imp'], $ts, $slotEnd);
$expDelta = 0.0; }
if (!empty($mm['exp']) && IPS_VariableExists((int)$mm['exp'])) {
$expDelta = $this->readDelta((int)$mm['exp'], $ts, $slotEnd);
}
if (!empty($mm['imp']) && IPS_VariableExists((int)$mm['imp'])) { // Nur speichern, wenn wirklich Werte vorhanden
$impDelta = $this->readDelta((int)$mm['imp'], $ts, $slotEnd); if ($impDelta > 0 || $expDelta > 0) {
} $slot[$name] = ['imp' => $impDelta, 'exp' => $expDelta];
if (!empty($mm['exp']) && IPS_VariableExists((int)$mm['exp'])) {
$expDelta = $this->readDelta((int)$mm['exp'], $ts, $slotEnd);
}
$slot[$name] = ['imp'=>$impDelta, 'exp'=>$expDelta];
$impTotal += $impDelta; $impTotal += $impDelta;
$expTotal += $expDelta; $expTotal += $expDelta;
IPS_LogMessage('Abrechnung', sprintf(" %s: imp=%.4f, exp=%.4f", $name, $impDelta, $expDelta)); IPS_LogMessage('Abrechnung', sprintf("🔹 %s: imp=%.5f, exp=%.5f", $name, $impDelta, $expDelta));
} }
}
if ($impTotal <= 0 && $expTotal <= 0) continue; if ($impTotal == 0 && $expTotal == 0) {
IPS_LogMessage('Abrechnung', "⏭ Kein Verbrauch im Intervall " . date('H:i', $ts) . "" . date('H:i', $slotEnd));
continue;
}
// --- Verhältnis berechnen ---
if ($impTotal <= $expTotal && $expTotal > 0) {
$ratio = $impTotal / $expTotal;
IPS_LogMessage('Abrechnung', sprintf("⚙️ Fall 1: imp<=exp | imp=%.4f exp=%.4f ratio=%.4f", $impTotal, $expTotal, $ratio));
} elseif ($impTotal > 0) {
$ratio = $expTotal / $impTotal;
IPS_LogMessage('Abrechnung', sprintf("⚙️ Fall 2: imp>exp | imp=%.4f exp=%.4f ratio=%.4f", $impTotal, $expTotal, $ratio));
} else {
$ratio = 0;
IPS_LogMessage('Abrechnung', "⚙️ Keine gültige Division (impTotal/expTotal=0)");
}
// --- Verbrauch berechnen ---
foreach ($slot as $name => $v) {
$imp = $v['imp'];
$exp = $v['exp'];
// ===== Fall 1: import <= export =====
if ($impTotal <= $expTotal) { if ($impTotal <= $expTotal) {
$ratio = ($expTotal > 0) ? ($impTotal / $expTotal) : 0; // Fall 1
$acc[$name]['solar_bezug'] += $imp;
foreach ($meters as $name => $_) { $acc[$name]['solareinspeisung']+= (1 - $ratio) * $exp;
$imp = $slot[$name]['imp']; $acc[$name]['solarverkauf'] += $ratio * $exp;
$exp = $slot[$name]['exp']; if ($pSolar !== null) $acc[$name]['cost_solar'] += ($imp * $pSolar) / 100;
if ($pFeed !== null) $acc[$name]['rev_feedin'] += ((1 - $ratio) * $exp * $pFeed) / 100;
$acc[$name]['imp'] += $imp; } else {
$acc[$name]['exp'] += $exp; // Fall 2
$acc[$name]['solar_bezug'] += $ratio * $imp;
$acc[$name]['solar_bezug'] += $imp; $acc[$name]['netz_bezug'] += (1 - $ratio) * $imp;
$acc[$name]['netz_bezug'] += 0; $acc[$name]['solarverkauf'] += $exp;
$acc[$name]['solareinspeisung']+= (1 - $ratio) * $exp; if ($pGrid !== null) $acc[$name]['cost_grid'] += ((1 - $ratio) * $imp * $pGrid) / 100;
$acc[$name]['solarverkauf'] += $ratio * $exp; 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'] += ($imp * $pSolar) / 100;
if ($pFeed !== null) $acc[$name]['rev_feedin'] += ((1 - $ratio) * $exp * $pFeed) / 100;
}
IPS_LogMessage('Abrechnung', sprintf(" ⚙️ Fall 1 (imp<=exp): ratio=%.3f", $ratio));
} }
// ===== Fall 2: import > export ===== IPS_LogMessage('Abrechnung', sprintf(
else { "➡️ %s | imp=%.4f exp=%.4f ratio=%.4f grid=%.3f solar=%.3f feed=%.3f",
$ratio = ($impTotal > 0) ? ($expTotal / $impTotal) : 0; $name, $imp, $exp, $ratio, $pGrid ?? 0, $pSolar ?? 0, $pFeed ?? 0
));
foreach ($meters as $name => $_) {
$imp = $slot[$name]['imp'];
$exp = $slot[$name]['exp'];
$acc[$name]['imp'] += $imp;
$acc[$name]['exp'] += $exp;
$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;
if ($pSolar !== null) $acc[$name]['cost_solar'] += ($ratio * $imp * $pSolar) / 100;
if ($pFeed !== null) $acc[$name]['rev_feedin'] += ($exp * $pFeed) / 100;
}
IPS_LogMessage('Abrechnung', sprintf(" ⚙️ Fall 2 (imp>exp): ratio=%.3f", $ratio));
}
} }
}
// Ausgabe erzeugen
$grand = 0.0;
foreach ($acc as $name => $v) {
$sum = $v['cost_solar'] + $v['cost_grid'] - $v['rev_feedin'];
$grand += $sum;
$html .= "<tr>
<td>{$name}</td>
<td align='right'>" . number_format($v['imp'], 3) . "</td>
<td align='right'>" . number_format($v['exp'], 3) . "</td>
<td align='right'>" . number_format($v['solar_bezug'], 3) . "</td>
<td align='right'>" . number_format($v['netz_bezug'], 3) . "</td>
<td align='right'>" . number_format($v['solareinspeisung'], 3) . "</td>
<td align='right'>" . number_format($v['solarverkauf'], 3) . "</td>
<td align='right'>" . number_format($v['cost_solar'], 2) . "</td>
<td align='right'>" . number_format($v['cost_grid'], 2) . "</td>
<td align='right'>" . number_format($v['rev_feedin'], 2) . "</td>
<td align='right'>" . number_format($sum, 2) . "</td>
</tr>";
}
$html .= "<tr style='background-color:#f9f9f9; font-weight:bold;'>
<td colspan='10' align='right'>Total Stromkosten:</td>
<td align='right'>" . number_format($grand, 2) . "</td>
</tr></table><br>";
IPS_LogMessage('Abrechnung', sprintf("✅ Stromkosten fertig: %.2f CHF total", $grand));
return ['html'=>$html, 'sum'=>$grand];
} }