no message

This commit is contained in:
dh
2025-11-05 08:03:27 +01:00
parent 5387326d7c
commit 1f80404765
+25 -9
View File
@@ -225,27 +225,43 @@ private function GetValueAt(int $varId, int $timestamp, bool $nearestAfter = tru
return null; return null;
} }
$maxDays = 365; // 🔹 maximal 1 Jahr zurück / vor suchen
$stepDays = 7; // 🔹 Schrittweise Erweiterung (jeweils 7 Tage)
$valueFound = null;
if ($nearestAfter) { if ($nearestAfter) {
// 🔹 Suche den ersten Wert nach dem gegebenen Zeitpunkt for ($offset = 0; $offset <= $maxDays; $offset += $stepDays) {
$values = @AC_GetLoggedValues($archiveID, $varId, $timestamp, $timestamp + 7 * 86400, 1000); $from = $timestamp + $offset * 86400;
$to = $from + $stepDays * 86400;
$values = @AC_GetLoggedValues($archiveID, $varId, $from, $to, 1000);
if (!empty($values)) { if (!empty($values)) {
foreach ($values as $v) { foreach ($values as $v) {
if ($v['TimeStamp'] >= $timestamp) { if ($v['TimeStamp'] >= $timestamp) {
return floatval($v['Value']); $valueFound = floatval($v['Value']);
break 2;
}
} }
} }
} }
} else { } else {
// 🔹 Suche den letzten Wert vor dem gegebenen Zeitpunkt for ($offset = 0; $offset <= $maxDays; $offset += $stepDays) {
$values = @AC_GetLoggedValues($archiveID, $varId, $timestamp - 7 * 86400, $timestamp, 1000); $from = $timestamp - ($offset + $stepDays) * 86400;
$to = $timestamp - $offset * 86400;
$values = @AC_GetLoggedValues($archiveID, $varId, $from, $to, 1000);
if (!empty($values)) { if (!empty($values)) {
return floatval($values[count($values) - 1]['Value']); $valueFound = floatval($values[count($values) - 1]['Value']);
break;
}
} }
} }
// Fallback – aktuellen Variablenwert nehmen if ($valueFound !== null) {
return $valueFound;
}
// ❗ Fallback, wenn gar nichts im Archiv gefunden wird
$fallback = floatval(GetValue($varId)); $fallback = floatval(GetValue($varId));
IPS_LogMessage('Abrechnung', "⚠ Kein Archivwert für $varId gefunden, nutze aktuellen Wert ($fallback)"); IPS_LogMessage('Abrechnung', "⚠ Kein Archivwert für $varId im Umkreis von $maxDays Tagen gefunden, nutze aktuellen Wert ($fallback)");
return $fallback; return $fallback;
} }
@@ -350,7 +366,7 @@ private function AddMeterToPDFRow($meter, $tariffs, $from, $to, $type)
// Archivwerte lesen // Archivwerte lesen
$startVal = $this->GetValueAt($varId, $segmentStart, false); $startVal = $this->GetValueAt($varId, $segmentStart, true);
$endVal = $this->GetValueAt($varId, $segmentEnd, true); $endVal = $this->GetValueAt($varId, $segmentEnd, true);
if ($startVal === null || $endVal === null) { if ($startVal === null || $endVal === null) {