From cd9209f8002d6678f69e18af26f16b739fe759a8 Mon Sep 17 00:00:00 2001 From: DanielHaefliger Date: Thu, 11 Dec 2025 14:23:26 +0100 Subject: [PATCH] no message --- Abrechnung/module.php | 60 ++++++++++++++++++++++++------------------- 1 file changed, 34 insertions(+), 26 deletions(-) diff --git a/Abrechnung/module.php b/Abrechnung/module.php index 3e6edb9..2238545 100644 --- a/Abrechnung/module.php +++ b/Abrechnung/module.php @@ -607,34 +607,42 @@ foreach ($meters as $name => $mm) { // ====================== Hilfsfunktionen ====================== - private function GetValueAt($varId, $timestamp, $nearestAfter = true) - { - $archiveID = @IPS_GetInstanceListByModuleID('{43192F0B-135B-4CE7-A0A7-1475603F3060}')[0]; - if (!$archiveID || !IPS_VariableExists($varId)) { - return null; - } - - // 30 Tage Fenster um den Zeitpunkt - $values = @AC_GetLoggedValues($archiveID, $varId, $timestamp - 30 * 86400, $timestamp + 30 * 86400, 0); - if (empty($values)) { - return (float)GetValue($varId); - } - - $closest = null; - foreach ($values as $v) { - if ($nearestAfter && $v['TimeStamp'] >= $timestamp) { - // erster Wert NACH oder GENAU zum Timestamp - $closest = $v['Value']; - break; - } elseif (!$nearestAfter && $v['TimeStamp'] <= $timestamp) { - // letzter Wert DAVOR oder GENAU zum Timestamp - $closest = $v['Value']; - } - } - - return $closest ?? (float)GetValue($varId); +private function GetValueAt($varId, $timestamp, $nearestAfter = true) +{ + $archiveID = @IPS_GetInstanceListByModuleID('{43192F0B-135B-4CE7-A0A7-1475603F3060}')[0]; + if (!$archiveID || !IPS_VariableExists($varId)) { + return null; } + if ($nearestAfter) { + // Erster Wert NACH oder GENAU ab Timestamp + $values = @AC_GetLoggedValues( + $archiveID, + $varId, + $timestamp, // start + time(), // end + 1 // LIMIT + ); + } else { + // Letzter Wert DAVOR oder GENAU bis Timestamp + $values = @AC_GetLoggedValues( + $archiveID, + $varId, + 0, // start + $timestamp, // end + 1 // LIMIT + ); + } + + if (!empty($values)) { + return (float)$values[0]['Value']; + } + + // Fallback → Live-Wert + return (float)GetValue($varId); +} + + private function getDeltaFromArchive(int $varId, int $tStart, int $tEnd): float { // Werte holen