+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!
Since the HA logs complains that a part of the old history stats integration will be removed with HA Core 2025.1 I just tested it again but the Issue is still the same
Did someone find a Solution for this?
The GitHub issue linked above seems to have been closed. Is there another equivalent we can follow? If not another issue should be opened as otherwise it won’t be on the devs radar.
Thats why I have added a Screenshot. But you are right, a proper description was missing. I have also removed the other Sensors. Did just copy from first post.^^
If the start date is before the “birth date” of the source sensor, the time between start date and birth date is added to the time.
In the examples above, the 480’000 h correspond to the time from 0 (1/1/1970) to the birth date of your source sensor.
Workaround:
Add a start date later than the birth date of your source sensor.
Example for workaround:
In my case, I added the source sensor on 11-05.
This produces correct behavior (stats start > birth date):