From 284a38eea5f254f898d777f6c274bc2c0869c285 Mon Sep 17 00:00:00 2001 From: Brandon Harvey <8107750+bharvey88@users.noreply.github.com> Date: Tue, 14 Jul 2026 11:11:27 -0500 Subject: [PATCH] Add heartbeat floor to Reduce DB Reporting ambient sensors MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The reduce-gated ambient sensors (ESP temp, LTR390 light/UV) used a hardcoded delta with no time floor, so with Reduce DB Reporting on they could freeze and never show a value on boot. Give them a delta-OR-hourly-heartbeat floor and publish the first post-boot reading. Radar filters, BME280 pressure, and the toggle-off path are unchanged. Related to ApolloAutomation/MSR-2#70 🤖 Generated with [Claude Code](https://claude.com/claude-code) --- Integrations/ESPHome/Core.yaml | 62 +++++++++++++++++++--------------- 1 file changed, 34 insertions(+), 28 deletions(-) diff --git a/Integrations/ESPHome/Core.yaml b/Integrations/ESPHome/Core.yaml index 5b5cfc1..b1e371c 100644 --- a/Integrations/ESPHome/Core.yaml +++ b/Integrations/ESPHome/Core.yaml @@ -1,5 +1,5 @@ substitutions: - version: "26.7.9.1" + version: "26.7.14.1" device_description: ${name} made by Apollo Automation - version ${version}. # Default OTA password. Override in your device YAML by re-declaring # `substitutions: { ota_password: !secret _ota_password }` so each @@ -401,22 +401,24 @@ sensor: id: sys_esp_temperature filters: - lambda: |- - static float last_reported_value = 0.0; + static float last_reported_value = NAN; + static uint32_t last_report_time = 0; float current_value = x; - // 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 if (id(reduce_db_reporting).state) { - // Apply delta filter: only report if the value has changed by 2 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) >= 5.0 || + (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 2 - 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; } @@ -617,22 +619,24 @@ sensor: id: ltr390light filters: - lambda: |- - static float last_reported_value = 0.0; + static float last_reported_value = NAN; + static uint32_t last_report_time = 0; float current_value = x; - // 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 if (id(reduce_db_reporting).state) { - // Apply delta filter: only report if the value has changed by 2 or more - if (abs(current_value - last_reported_value) >= 20.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) >= 20.0 || + (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 2 - 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; } uv_index: @@ -640,22 +644,24 @@ sensor: id: ltr390uvindex filters: - lambda: |- - static float last_reported_value = 0.0; + static float last_reported_value = NAN; + static uint32_t last_report_time = 0; float current_value = x; - // 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 if (id(reduce_db_reporting).state) { - // Apply delta filter: only report if the value has changed by 2 or more - if (abs(current_value - last_reported_value) >= 20.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) >= 20.0 || + (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 2 - 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; }