Issues with History Sensor adding or inflating values

I have set up history_sensors to track how much different users watch Plex.

These are based on sensors brought in by the tautulli integration, as the plex ones were unreliable.

The code for the history_sensor is fairly reliable and is below:

  - platform: history_stats
    name: Joseph Plex History
    entity_id: sensor.plex_joseph_state
    state: "playing"
    type: time
    start: "{{ now().replace(day=1, hour=0, minute=0, second=0, microsecond=0 ) }}"
    end: "{{ now() }}"   

It will work well enough for the first few days of the month but then mid-way through the month I’ll look and suddenly Joseph’s plex history shows he has watched a ridiculous amount of hours. In fact, this month, this jumped from 6.71 hours yesterday at 9:40pm to 102 hours at 9:45pm

This happened for a number of user at around the same time. One user went from 4 hours to 41, another from 9.44 to 43 hours, whereas other users are completely unaffected.

I can’t see from the code above what might be wrong. Upon a closer inspection of my code, it looks like I restarted HA at this point. It doesn’t appear that any of the users affected were watching Plex at the time and I can’t see a pattern that would you show why they in particular are the onlys affected. From memory, I think actually two of the users affected this time are also the ones who have been affected in the past by this bug.

Has anyone experienced an issue like this before with the history_sensor? Is it something unique to the tautulli sensor and something I should find a way around? Any suggestions would be really helpful before I abandon this idea completely.

The history stats sensor inspects the recorder database to determine its value. By default the recorder only keeps 10 days of data, you appear to be wanting a monthly time total. You will have to increase your recorder purge_keep_days option to 31 days for this sensor to work.

The other option is to only track a weekly total and leave the recorder settings as default (10 days).

Thanks @tom_l but unfortunately I have recorder set at 32 days and it still seems to be an issue

I have the following in my config yaml

recorder:
  purge_keep_days: 32

Even if it were the case that I only had 10 days of history, doesn’t that mean that the value would just reduce rather than randomly inflate each month?

Ok if that is the case (and has been since you created the sensors) then this may mean you have some corrupt data in your recorder database.

What is the best way to uncorrupt it? Can I manually purge it? Do you know if there is a way to stop this file getting corrupted as this seems to be a recurring issue?

Search the forum. There are some topics on database repair.

I imagine the side effect is that the database’s size is rather large.

An alternative is to use History Stats sensor to compute the total of a single day’s worth of data (which eliminates the need to set recorder for more than the default 10 days) and then using the Utility Meter integration, with the History Stats sensor, to report daily/weekly/monthly/yearly totals.

The Utility Meter integration stores its data in a database table that’s not subjected to periodic purges.

Example