Fix Reduce DB Reporting freezing DPS310 pressure and ambient sensors#85
Conversation
When Reduce DB Reporting was on, the DPS310 pressure sensor only reported on a >= 5.0 hPa change. Barometric pressure normally drifts 1-3 hPa a day, so the reading could freeze for hours and look like a dead sensor. Give the reduce-gated ambient sensors (pressure, ESP temp, LTR390 light/UV) a delta-OR-hourly-heartbeat floor, and publish the first post-boot reading so a value always shows on boot. Lower the pressure delta to 0.5 hPa and move the DPS310 poll to 60s. Radar filters and the toggle-off path are unchanged. Fixes ApolloAutomation#70 🤖 Generated with [Claude Code](https://claude.com/claude-code)
|
Warning Review limit reached
Next review available in: 14 minutes Enable usage-based reviews in Billing to review now. Otherwise, wait until the next included review is available. How can I continue?After more reviews become available, a review can be triggered using the To avoid repeated limits, reduce automatic review volume by pausing incremental auto-reviews earlier, using label-based review opt-in, excluding WIP or generated PR titles, or requesting reviews manually when the PR is ready. If your team needs uninterrupted high-volume reviews, an organization admin can enable usage-based reviews. How do review limits work?CodeRabbit enforces per-developer PR review limits for each organization. Most developers receive the normal plan review availability. For paid Pro and Pro+ PR reviews, CodeRabbit uses adaptive limits for sustained high-volume activity. When a developer's recent PR review activity reaches the 95th percentile or higher among CodeRabbit users, additional reviews become available more gradually as earlier reviews age out of the rolling window. Please refer docs for additional details. Review details⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (1)
WalkthroughESPHome version ChangesSensor reporting updates
Estimated code review effort: 3 (Moderate) | ~20 minutes Possibly related PRs
Suggested reviewers: Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@Integrations/ESPHome/Core.yaml`:
- Around line 702-726: The sensor reporting filters mishandle NAN values,
causing repeated invalid updates and delayed failure reporting. In
Integrations/ESPHome/Core.yaml at lines 702-726, 429-446, 649-668, and 669-691,
update each filter to track initialization with a static first_run boolean and
explicitly detect NAN state transitions, reporting the initial value and
transitions between valid and NAN immediately while retaining normal delta and
hourly heartbeat behavior. Use deltas of 0.5 for DPS310 pressure, 5.0 for ESP
temperature and LTR390 light, and 1.0 for LTR390 UV index.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: 7d599e66-f2b0-45d0-af6d-005faeda12a4
📒 Files selected for processing (1)
Integrations/ESPHome/Core.yaml
| static float last_reported_value = NAN; | ||
| static uint32_t last_report_time = 0; | ||
| float current_value = x; | ||
| float offset = id(dps310_pressure_offset).state; | ||
| if (!isnan(offset)) { | ||
| current_value += offset; | ||
| } | ||
|
|
||
| // Check if the reduce_db_reporting switch is on | ||
| // 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) { | ||
| // Apply delta filter: only report if the value has changed by 5 or more | ||
| if (abs(current_value - last_reported_value) >= 5.0) { | ||
| last_reported_value = current_value; // Update the last reported value | ||
| uint32_t now = millis(); | ||
| if (isnan(last_reported_value) || | ||
| abs(current_value - last_reported_value) >= 0.5 || | ||
| (now - last_report_time) >= 3600000UL) { | ||
| last_reported_value = current_value; | ||
| last_report_time = now; | ||
| return current_value; | ||
| } else { | ||
| // Return the last reported value without updating if change is less than 5 | ||
| return {}; // Discard the update | ||
| } | ||
| return {}; // Within delta and under the hourly heartbeat -> discard | ||
| } else { | ||
| // If reduce_db_reporting is off, report the current value normally | ||
| last_reported_value = current_value; | ||
| last_report_time = millis(); | ||
| return current_value; | ||
| } |
There was a problem hiding this comment.
🎯 Functional Correctness | 🔴 Critical | ⚡ Quick win
Fix NAN handling to prevent database spam and delayed error reporting.
The new lambda logic mishandles NAN values (which ESPHome uses to indicate a failed sensor read) in two critical ways:
- Database Spam: Once
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. - Delayed Error Reporting: If the sensor fails and transitions from a valid reading to
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 NAN state 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
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| static float last_reported_value = NAN; | |
| static uint32_t last_report_time = 0; | |
| float current_value = x; | |
| float offset = id(dps310_pressure_offset).state; | |
| if (!isnan(offset)) { | |
| current_value += offset; | |
| } | |
| // Check if the reduce_db_reporting switch is on | |
| // 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) { | |
| // Apply delta filter: only report if the value has changed by 5 or more | |
| if (abs(current_value - last_reported_value) >= 5.0) { | |
| last_reported_value = current_value; // Update the last reported value | |
| uint32_t now = millis(); | |
| if (isnan(last_reported_value) || | |
| abs(current_value - last_reported_value) >= 0.5 || | |
| (now - last_report_time) >= 3600000UL) { | |
| last_reported_value = current_value; | |
| last_report_time = now; | |
| return current_value; | |
| } else { | |
| // Return the last reported value without updating if change is less than 5 | |
| return {}; // Discard the update | |
| } | |
| return {}; // Within delta and under the hourly heartbeat -> discard | |
| } else { | |
| // If reduce_db_reporting is off, report the current value normally | |
| last_reported_value = current_value; | |
| last_report_time = millis(); | |
| return current_value; | |
| } | |
| 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(); | |
| 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; | |
| } |
📍 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
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
In `@Integrations/ESPHome/Core.yaml` around lines 702 - 726, The sensor reporting
filters mishandle NAN values, causing repeated invalid updates and delayed
failure reporting. In Integrations/ESPHome/Core.yaml at lines 702-726, 429-446,
649-668, and 669-691, update each filter to track initialization with a static
first_run boolean and explicitly detect NAN state transitions, reporting the
initial value and transitions between valid and NAN immediately while retaining
normal delta and hourly heartbeat behavior. Use deltas of 0.5 for DPS310
pressure, 5.0 for ESP temperature and LTR390 light, and 1.0 for LTR390 UV index.
Version: 26.7.14.1
What does this implement/fix?
Fixes #70 — the DPS310 pressure sensor looked frozen when Reduce DB Reporting was on.
Root cause: with the toggle on, pressure only reported when it moved ≥ 5.0 hPa. Barometric pressure normally drifts only 1–3 hPa across a whole day, so the reading could sit unchanged for hours (sometimes effectively never), which reads as a dead sensor.
Changes (Reduce DB Reporting behaviour only — the toggle-off path is unchanged):
esphome configvalidates.Types of changes
Checklist / Checklijst:
If user-visible functionality or configuration variables are added/modified:
🤖 Generated with Claude Code
Summary by CodeRabbit
New Features
Improvements
Chores