-
Notifications
You must be signed in to change notification settings - Fork 17
Fix Reduce DB Reporting freezing DPS310 pressure and ambient sensors #85
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
TrevorSchirmer
merged 2 commits into
ApolloAutomation:beta
from
bharvey88:fix/reduce-db-heartbeat
Jul 15, 2026
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🎯 Functional Correctness | 🔴 Critical | ⚡ Quick win
Fix
NANhandling to prevent database spam and delayed error reporting.The new lambda logic mishandles
NANvalues (which ESPHome uses to indicate a failed sensor read) in two critical ways:last_reported_valuebecomesNAN,isnan(last_reported_value)will evaluate totrueon every subsequent poll. This bypasses the filter entirely, spamming Home Assistant withNANupdates every 60 seconds and defeating thereduce_db_reportingtoggle.NAN,abs(NAN - last_reported_value) >= thresholdevaluates tofalse. The error won't be sent to Home Assistant until the 1-hour heartbeat expires, leaving stale data in the UI.To fix this, track the initial run explicitly with a boolean and handle
NANstate transitions separately.Integrations/ESPHome/Core.yaml#L702-L726: Update the DPS310 Pressure filter to usestatic bool first_runand explicitly check forNANtransitions.Integrations/ESPHome/Core.yaml#L429-L446: Apply the same logic to the ESP Temperature filter (using its5.0delta).Integrations/ESPHome/Core.yaml#L649-L668: Apply the same logic to the LTR390 Light filter (using its5.0delta).Integrations/ESPHome/Core.yaml#L669-L691: Apply the same logic to the LTR390 UV Index filter (using its1.0delta).🛠️ Proposed fix for the DPS310 Pressure filter (adapt for the other 3 sensors)
static float last_reported_value = NAN; static uint32_t last_report_time = 0; + static bool first_run = true; float current_value = x; float offset = id(dps310_pressure_offset).state; if (!isnan(offset)) { current_value += offset; } // Reduce DB Reporting: report on a meaningful change, but at least hourly so the sensor never looks frozen. // Barometric pressure normally drifts only 1-3 hPa a day, so the old 5 hPa delta could freeze the reading for hours. if (id(reduce_db_reporting).state) { uint32_t now = millis(); - if (isnan(last_reported_value) || - abs(current_value - last_reported_value) >= 0.5 || + bool is_nan_transition = isnan(current_value) != isnan(last_reported_value); + + if (first_run || + is_nan_transition || + (!isnan(current_value) && abs(current_value - last_reported_value) >= 0.5) || (now - last_report_time) >= 3600000UL) { last_reported_value = current_value; last_report_time = now; + first_run = false; return current_value; } return {}; // Within delta and under the hourly heartbeat -> discard } else { last_reported_value = current_value; last_report_time = millis(); + first_run = false; return current_value; }📝 Committable suggestion
📍 Affects 1 file
Integrations/ESPHome/Core.yaml#L702-L726(this comment)Integrations/ESPHome/Core.yaml#L429-L446Integrations/ESPHome/Core.yaml#L649-L668Integrations/ESPHome/Core.yaml#L669-L691🤖 Prompt for AI Agents