diff --git a/WeatherPredictor/history.cpp b/WeatherPredictor/history.cpp index 0df389d..c83dafb 100644 --- a/WeatherPredictor/history.cpp +++ b/WeatherPredictor/history.cpp @@ -26,10 +26,15 @@ Sample historyLatest() { return historyGet(s_count - 1); } bool historyTrendDelta(float& outDelta) { if (s_count < 2) return false; Sample latest = historyLatest(); + if (latest.epoch < 10800UL) return false; uint32_t target = latest.epoch - 10800UL; // 3 h earlier + // Pick the newest sample at or before `target`. Scan all samples (do not + // assume epochs are strictly increasing: an NTP correction can shift them). int idx = -1; + uint32_t bestEpoch = 0; for (int i = 0; i < s_count; i++) { - if (historyGet(i).epoch <= target) idx = i; else break; + Sample s = historyGet(i); + if (s.epoch <= target && s.epoch >= bestEpoch) { bestEpoch = s.epoch; idx = i; } } if (idx < 0) return false; Sample past = historyGet(idx);