Skip to content

Fix Reduce DB Reporting freezing DPS310 pressure and ambient sensors#85

Merged
TrevorSchirmer merged 2 commits into
ApolloAutomation:betafrom
bharvey88:fix/reduce-db-heartbeat
Jul 15, 2026
Merged

Fix Reduce DB Reporting freezing DPS310 pressure and ambient sensors#85
TrevorSchirmer merged 2 commits into
ApolloAutomation:betafrom
bharvey88:fix/reduce-db-heartbeat

Conversation

@bharvey88

@bharvey88 bharvey88 commented Jul 14, 2026

Copy link
Copy Markdown
Contributor

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):

  • DPS310 Pressure: delta lowered 5.0 → 0.5 hPa, and added a 1-hour heartbeat so the value always refreshes at least hourly. Poll interval moved 30s → 60s.
  • Same delta-OR-hourly-heartbeat floor applied to the other reduce-gated ambient sensors (ESP Temperature, LTR390 Light, LTR390 UV) so none of them can freeze either. Their existing deltas are preserved.
  • Radar distance/energy filters are intentionally left as-is (they return NAN when no target and deliberately suppress high-rate motion noise — a heartbeat there would re-add noise).

esphome config validates.

Types of changes

  • Bugfix (fixed change that fixes an issue)
  • New feature (thanks!)
  • Breaking change (repair/feature that breaks existing functionality)
  • Dependency Update - Does not publish
  • Other - Does not publish
  • Website of github readme file update - Does not publish
  • Github workflows - Does not publish

Checklist / Checklijst:

  • The code change has been tested and works locally
  • The code change has not yet been tested

If user-visible functionality or configuration variables are added/modified:

  • Added/updated documentation for the web page

🤖 Generated with Claude Code

Summary by CodeRabbit

  • New Features

    • Added hourly heartbeat reporting for selected sensors when reduced database reporting is enabled.
    • Sensor updates now report when values change beyond sensor-specific thresholds or when the hourly interval is reached.
  • Improvements

    • Improved reporting behavior for temperature, light, UV index, and pressure sensors.
    • Reduced pressure sensor update frequency while maintaining configured offsets.
  • Chores

    • Updated the ESPHome device version.

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)
@github-actions github-actions Bot added the bugfix Bug fix label Jul 14, 2026
@coderabbitai

coderabbitai Bot commented Jul 14, 2026

Copy link
Copy Markdown

Review Change Stack

Warning

Review limit reached

@bharvey88, you've reached your PR review limit, so we couldn't start this review.

Next review available in: 14 minutes

Enable usage-based reviews in Billing to review now. Otherwise, wait until the next included review is available.
You're only billed for reviews past your plan's rate limits ($0.25/file).

How can I continue?

After more reviews become available, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

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 configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 6ef09a70-8b00-4157-a5af-30051abfec13

📥 Commits

Reviewing files that changed from the base of the PR and between 36f5a28 and fa05dba.

📒 Files selected for processing (1)
  • Integrations/ESPHome/Core.yaml

Walkthrough

ESPHome version 26.7.14.1 is configured. Reduced database reporting now emits sensor updates on first value, threshold changes, or hourly heartbeats. DPS310 polling changes to 60 seconds, with revised pressure filtering and preserved temperature offsets.

Changes

Sensor reporting updates

Layer / File(s) Summary
Hourly heartbeat filters
Integrations/ESPHome/Core.yaml
Internal temperature and LTR390 light/UV filters now track the last reported value and time, emitting first, threshold-crossing, or hourly updates.
DPS310 reporting cadence
Integrations/ESPHome/Core.yaml
DPS310 pressure filtering uses the hourly heartbeat and revised delta threshold; polling changes to 60s, while temperature offsets remain applied.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Possibly related PRs

Suggested reviewers: trevorschirmer

Poem

A rabbit watched the sensors glow,
With heartbeats spaced an hour slow.
New values hop when changes call,
DPS checks less often now—
And NAN starts the reporting show!

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly matches the main change: fixing Reduce DB Reporting behavior for DPS310 pressure and related sensors.
Linked Issues check ✅ Passed The PR adds hourly heartbeat reporting and lowers DPS310 pressure filtering, which addresses the linked issue's request for time-based pressure updates.
Out of Scope Changes check ✅ Passed The changes stay focused on reduce-gated sensor reporting and the version bump, with no obvious unrelated additions.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

📥 Commits

Reviewing files that changed from the base of the PR and between 4c0f31d and 36f5a28.

📒 Files selected for processing (1)
  • Integrations/ESPHome/Core.yaml

Comment on lines +702 to 726
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;
}

Copy link
Copy Markdown

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 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:

  1. Database Spam: Once last_reported_value becomes NAN, isnan(last_reported_value) will evaluate to true on every subsequent poll. This bypasses the filter entirely, spamming Home Assistant with NAN updates every 60 seconds and defeating the reduce_db_reporting toggle.
  2. Delayed Error Reporting: If the sensor fails and transitions from a valid reading to NAN, abs(NAN - last_reported_value) >= threshold evaluates to false. 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 use static bool first_run and explicitly check for NAN transitions.
  • Integrations/ESPHome/Core.yaml#L429-L446: Apply the same logic to the ESP Temperature filter (using its 5.0 delta).
  • Integrations/ESPHome/Core.yaml#L649-L668: Apply the same logic to the LTR390 Light filter (using its 5.0 delta).
  • Integrations/ESPHome/Core.yaml#L669-L691: Apply the same logic to the LTR390 UV Index filter (using its 1.0 delta).
🛠️ 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.

Suggested change
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-L446
  • Integrations/ESPHome/Core.yaml#L649-L668
  • Integrations/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.

@bharvey88 bharvey88 mentioned this pull request Jul 14, 2026
@TrevorSchirmer
TrevorSchirmer merged commit c9ff283 into ApolloAutomation:beta Jul 15, 2026
12 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bugfix Bug fix

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants