Length of time a sensor has been in specific state

I know this has been asked a few times before but the resolution has always been to use a history stats sensor. The problem with which is history is not stored for very long.

I have a heat pump and can get its operation mode as a neat sensor:

image

I want to know how long it has been in HEAT state today, yesterday, this month, last month. History stats sensor will only look at the data recorded, which in my case is 10 days so current month, last month wont work.

image

The way I see it, I could increase the history time kept to 61 days but I’d rather ask if anyone has any other ideas? Can I not add some kind of time counter based on the state of this sensor and not rely on its history? The downsides being the counter would only start counting after its creation.

Take a look at this discussion - it looks like a similar issue to me:
Calculating days/hours per year below a certain temperature? - Energy - Home Assistant Community

And it provides one approach where you could draw on the existing history right away.

That’s a common request and the long-standing solution is to use your History Stats sensor with the Utility Meter integration.

That’s what I have used to record heating (and cooling) times for daily/weekly/monthly/yearly for three years now. No need to change Recorder’s purge duration or tinker with SQL.

Be advised that the Utility Meter integration will only show data going forward from the moment you implement it.

1 Like