diff --git a/homeassistant/components/history_stats/data.py b/homeassistant/components/history_stats/data.py index 8153557422d..5466498fc32 100644 --- a/homeassistant/components/history_stats/data.py +++ b/homeassistant/components/history_stats/data.py @@ -73,7 +73,8 @@ class HistoryStats: # History cannot tell the future self._history_current_period = [] self._previous_run_before_start = True - + self._state = HistoryStatsState(None, None, self._period) + return self._state # # We avoid querying the database if the below did NOT happen: # @@ -82,7 +83,7 @@ class HistoryStats: # - The period shrank in size # - The previous period ended before now # - elif ( + if ( not self._previous_run_before_start and current_period_start_timestamp == previous_period_start_timestamp and ( @@ -117,10 +118,6 @@ class HistoryStats: ) self._previous_run_before_start = False - if not self._history_current_period: - self._state = HistoryStatsState(None, None, self._period) - return self._state - hours_matched, match_count = self._async_compute_hours_and_changes( now_timestamp, current_period_start_timestamp, diff --git a/tests/components/history_stats/test_sensor.py b/tests/components/history_stats/test_sensor.py index b375a8f63c4..f824ee552ca 100644 --- a/tests/components/history_stats/test_sensor.py +++ b/tests/components/history_stats/test_sensor.py @@ -357,7 +357,7 @@ async def test_measure_multiple(hass, recorder_mock): await hass.async_block_till_done() assert hass.states.get("sensor.sensor1").state == "0.5" - assert hass.states.get("sensor.sensor2").state == STATE_UNKNOWN + assert hass.states.get("sensor.sensor2").state == "0.0" assert hass.states.get("sensor.sensor3").state == "2" assert hass.states.get("sensor.sensor4").state == "50.0"