History Stats Sensor creates wrong data

Hi,
my history stats sensor is not working correctly. I have made a new test sensor and have the same Issue:

sensor:
  - platform: time_date
    display_options:
      - 'time'
  - platform: history_stats
    name: Test History
    entity_id: switch.stecker_1
    state: 'on'
    type: time
    start: '{{ 0 }}'
    end: '{{ now() }}'
  - platform: template
    sensors:
      test_laufzeit:
        friendly_name: 'Test Laufzeit'
        value_template: "{{ states('sensor.test_history') }}"

As soon as I activate the switch it creates this entry:

In the past, the values where like 0.49 and displayed in minutes on the Dashboard.
Any Ideas?

Anyone? :cry: :cry:

Hi Loader23,
I’ve a very similar problem here:

  - platform: history_stats
    name: Vita_Debatterizzatore_UV
    entity_id: switch.o3
    state: "on"
    type: time
    start: "{{ 0 }}"
    end: "{{ now() }}"

Here the Statistics query in home-assistant_V2.db

Delete the DB rows with wrong data don’t help.

My system:
Home Assistant 2022.9.6
Supervisor 2022.10.2
Operating System 7.6
Frontend 20220907.2 - latest

Anyone could help us? :pray: :pray:

Good to see that Iam not alone with that problem.
Don’t understand why no one can fix this :sob:

@McGagnor
I have found a workaround:
You can download the history_stats folder from this version:

Place it inside the custom_components and add a “version”: “2.0” to the manifest.json so it looks like that:

{
  "domain": "history_stats",
  "name": "History Stats",
  "documentation": "https://www.home-assistant.io/integrations/history_stats",
  "dependencies": ["recorder"],
  "codeowners": [],
  "quality_scale": "internal",
  "iot_class": "local_polling",
  "version": "2.0"
}

Restart HA and its working fine again :slight_smile:

+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.

FWIW the issue is reported at History Stats (type: time) problem when restarting Home Assistant · Issue #80871 · home-assistant/core · GitHub – add a thumbs up emoji to raise awareness from the home assistant dev.

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 :frowning:
Did someone find a Solution for this?

Anyone? Guess I have to stick with the last Release this year forever. :frowning:

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.

I have found some Issues about this problem, but all are closed.
Here is a new one:

That issue doesn’t say what’s wrong about the data. You also included 2 other sensors in that issue. Your history sensor is only:

  - platform: history_stats
    name: Test History
    entity_id: switch.stecker_1
    state: 'on'
    type: time
    start: '{{ 0 }}'
    end: '{{ now() }}'

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.^^

I have the very same problem. Now my working Hours of the Heatin are over 400.000 since beginning of November. Is there any solution for the Problem?

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):

start: "{{ now().replace(month=11, day=6, hour=0, minute=0, second=0, microsecond=0) }}"

This produces wrong behavior (stats start < birth date):

start: "{{ now().replace(month=1, day=1, hour=0, minute=0, second=0, microsecond=0) }}"

FYI midnight on 11/6

{{ today_at().replace(month=11, day=6) }}