Skip to content

Commit

Permalink
Fix history stats count update immediately after change
Browse files Browse the repository at this point in the history
  • Loading branch information
karwosts committed Nov 28, 2024
1 parent 8b46726 commit 5ce3354
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion homeassistant/components/history_stats/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

from dataclasses import dataclass
import datetime
import logging
import math

from homeassistant.components.recorder import get_instance, history
from homeassistant.core import Event, EventStateChangedData, HomeAssistant, State
Expand All @@ -14,6 +16,8 @@

MIN_TIME_UTC = datetime.datetime.min.replace(tzinfo=dt_util.UTC)

_LOGGER = logging.getLogger(__name__)


@dataclass
class HistoryStatsState:
Expand Down Expand Up @@ -186,8 +190,13 @@ def _async_compute_seconds_and_changes(
current_state_matches = history_state.state in self._entity_states
state_change_timestamp = history_state.last_changed

if state_change_timestamp > now_timestamp:
if math.floor(state_change_timestamp) > now_timestamp:
# Shouldn't count states that are in the future
_LOGGER.debug(
"Skipping future timestamp %s (now %s)",
state_change_timestamp,
now_timestamp,
)
continue

if previous_state_matches:
Expand Down

0 comments on commit 5ce3354

Please sign in to comment.