From 6151bbe5c806eea7a9160c0110b6660ee65c54ea Mon Sep 17 00:00:00 2001 From: Erik Montnemery Date: Tue, 21 Dec 2021 17:22:40 +0100 Subject: [PATCH] Improve debug log when warning about a dip in total_increasing sensor (#62501) --- homeassistant/components/sensor/recorder.py | 9 ++++++--- tests/components/sensor/test_recorder.py | 7 ++++--- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/homeassistant/components/sensor/recorder.py b/homeassistant/components/sensor/recorder.py index 9b6c07a323d..644719f00d7 100644 --- a/homeassistant/components/sensor/recorder.py +++ b/homeassistant/components/sensor/recorder.py @@ -298,7 +298,9 @@ def _suggest_report_issue(hass: HomeAssistant, entity_id: str) -> str: return report_issue -def warn_dip(hass: HomeAssistant, entity_id: str, state: State) -> None: +def warn_dip( + hass: HomeAssistant, entity_id: str, state: State, previous_fstate: float +) -> None: """Log a warning once if a sensor with state_class_total has a decreasing value. The log will be suppressed until two dips have been seen to prevent warning due to @@ -319,11 +321,12 @@ def warn_dip(hass: HomeAssistant, entity_id: str, state: State) -> None: return _LOGGER.warning( "Entity %s %shas state class total_increasing, but its state is " - "not strictly increasing. Triggered by state %s with last_updated set to %s. " + "not strictly increasing. Triggered by state %s (%s) with last_updated set to %s. " "Please %s", entity_id, f"from integration {domain} " if domain else "", state.state, + previous_fstate, state.last_updated.isoformat(), _suggest_report_issue(hass, entity_id), ) @@ -359,7 +362,7 @@ def reset_detected( return False if 0.9 * previous_fstate <= fstate < previous_fstate: - warn_dip(hass, entity_id, state) + warn_dip(hass, entity_id, state, previous_fstate) if fstate < 0: warn_negative(hass, entity_id, state) diff --git a/tests/components/sensor/test_recorder.py b/tests/components/sensor/test_recorder.py index 2da1a203dfd..faadb7140e2 100644 --- a/tests/components/sensor/test_recorder.py +++ b/tests/components/sensor/test_recorder.py @@ -1032,12 +1032,13 @@ def test_compile_hourly_sum_statistics_total_increasing_small_dip( recorder.do_adhoc_statistics(start=period2) wait_recording_done(hass) state = states["sensor.test1"][6].state + previous_state = float(states["sensor.test1"][5].state) last_updated = states["sensor.test1"][6].last_updated.isoformat() assert ( "Entity sensor.test1 has state class total_increasing, but its state is not " - f"strictly increasing. Triggered by state {state} with last_updated set to " - f"{last_updated}. Please create a bug report at https://github.com/home-assistant" - "/core/issues?q=is%3Aopen+is%3Aissue" + f"strictly increasing. Triggered by state {state} ({previous_state}) with " + f"last_updated set to {last_updated}. Please create a bug report at " + "https://github.com/home-assistant/core/issues?q=is%3Aopen+is%3Aissue" ) in caplog.text statistic_ids = list_statistic_ids(hass) assert statistic_ids == [