+1, this screwed my energy history. The bump (462934h) is equal to the time since the Unix epoch (1970), so it’s a software bug somewhere calculating the timestamps.
Thanks for this, but it’s only part of the solution. The problem is if the first recorded data in the period matches the criteria. Which is not a problem if you have more data than the period you want to look up in the history_stats sensor, but messes up if you want “all-time-stats” or if data collection/retention period is smaller than your stats period. For example: you just set up a new switch and want to measure “on time for the last 365 days”, it will use start_timestamp of the query (365 days ago) as the start of the first period.
I fixed it by editing data.py in the downloaded folder under custom_components as follows (around line 149):
previous_state_matches = (
self._history_current_period
and self._history_current_period[0].state in self._entity_states
and start_timestamp > self._history_current_period[0].last_changed.timestamp() # fix first period
)
this ensures the first period is not calculated from the beginning of time if it started after the desired period of the sensor. Good luck to anyone else having this problem!