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!