diff --git a/Integrations/ESPHome/Core.yaml b/Integrations/ESPHome/Core.yaml index 13f8313..acc9363 100644 --- a/Integrations/ESPHome/Core.yaml +++ b/Integrations/ESPHome/Core.yaml @@ -427,22 +427,24 @@ sensor: id: sys_esp_temperature filters: - lambda: |- - static float last_reported_value = -6.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 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) >= 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 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; } @@ -645,22 +647,24 @@ sensor: id: ltr390light filters: - lambda: |- - static float last_reported_value = -21.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 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) >= 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 20 - 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: @@ -668,22 +672,24 @@ sensor: id: ltr390uvindex filters: - lambda: |- - static float last_reported_value = -21.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 1 or more - if (abs(current_value - last_reported_value) >= 1.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) >= 1.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 20 - 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; } @@ -694,26 +700,29 @@ sensor: id: dps310pressure filters: - lambda: |- - static float last_reported_value = -6.0; + 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; } temperature: @@ -721,7 +730,7 @@ sensor: id: dps310temperature filters: - lambda: return x - id(dps310_temperature_offset).state; - update_interval: 30s + update_interval: 60s i2c_id: bus_a