History Statistics sensor - change unit of measurement to seconds

What more can I add?

Seconds is the standard unit of measurement for time, not hours as the History Sensor currently measures returns.

As it stands now we get anomalies like this where my switch was on for 10 minutes today (controlled by an HA timer) but the sensor rounds this up to 0.17 of an hour which is 10’ 12".

And bizarrely…

gives an attribute value: 9m?

All this could be avoided by measuring in seconds. Or more accurately not converting to hours:

See Line 260 here

        # Save value in hours
        self.value = elapsed / 3600

I’m not sure why this was ever thought a good idea but then this sensor has been around a long time (since 0.39) so maybe it was an oversight?

Ideally it should be a configuration variable named unit_of_measurement. And I would like also a precision configuration variable to round the state value.

1 Like

Is it possible to change this manually in your home assistant installation? (Hopefully this indeed becomes the standard in some update)

Anyone ever work out how to do this?

This is a pretty ridiculous oversight and should have been rectified ages ago.

My pull request Stop rounding history_stats sensor by amosyuen · Pull Request #97195 · home-assistant/core · GitHub just got merged which should stop rounding the sensor. The default precision is still 0.01, but you can fix that by changing the entity setting. Either set the time unit to seconds or change the precision to 0.0001.

2 Likes

Anyway I still see rounding in time sensors.
This PR did not fix the problem.

I don’t remember who it was (sincere apologies if you are reading this) but someone soon after I originally posted about this (ridiculous) state of affairs attempted to deal with all these rounding issues with History Stats, and a lot more. It looked to me like a very good implementation which not only solved the problem but made the whole sensor much more useful.

His pull request got rejected for I don’t know what reason. It was around the time when when the long term recorder/history stats came in so I think he (we) assumed that it was rejected because ‘they’ had plans for this sensor.

:man_shrugging:

Did you change the precision mentioned in my post above? Also are you on a version that includes the change e.g. 2023.8?

Using 2023.8.

!!!
This is what I completely missed.
Added “unique_id” for my sensor, reloaded history-stats sensors, opened more-info, set precision to smth like “56.12345” - now I see small values!

Seems that default precision settings do not allow to show precise data. Is it possible to change this?

Looked at your post, looks like your HA version is 2023.6 so you won’t have the PR yet.

That issue was made in June, now I am testing with 2023.8.

Ah ok that makes sense.

I wanted to set the default precision higher, but maintainers asked me to set it to match old behavior.

I see. Well, it is not a big deal to manually increase a precision. Thanks a LOT !!!