core icon indicating copy to clipboard operation
core copied to clipboard

Sensor history sometimes doesn't honor Display precision (or incorrectly rounds)

Open oliv3r opened this issue 1 year ago • 4 comments

The problem

I have a few graph sensors, where due to one reason or another, the graphs don't properly honor the (default) precision setting, or some weird rounding happens that causes weird fluctuations.

The data is stored via recorder in postgresql instead of sqlite.

I see it with various different integrations, so not sure if that's related.

Could it be that internally something is using floats, and due to the lack of precision with floats something sometimes goes wrong, and thus when floats being used they should be rounded?

Two random examples image Here we're talking KM, which is reported in whole KM by the integration (renault)/API

image The strange thing is here, that this integration is supposedly running only once every 6 hours to update its values.

What version of Home Assistant Core has the issue?

core-2024.5.5

What was the last working version of Home Assistant Core?

No response

What type of installation are you running?

Home Assistant Container

Integration causing the issue

No response

Link to integration documentation on our website

No response

Diagnostics information

No response

Example YAML snippet

No response

Anything in the logs that might be useful for us?

No response

Additional information

No response

oliv3r avatar May 30 '24 18:05 oliv3r

This is floating point rounding error in calculating the statistics min/max/mean.

There's a related issue in frontend, but I don't know what the right answer is for a fix, if it should be in core, or frontend, or chart.js, or maybe this is not a bug at all.

https://github.com/home-assistant/frontend/issues/18982

Also display precision settings are only used for choosing the number of decimals when writing the state. It does not affect how the sensor values are plotted on charts.

karwosts avatar May 30 '24 20:05 karwosts

Looking at the database, I see indeed the same thing.

 9123 |         | 1718082010.4386117 |          22 |       | 1718078400 | 83.03999999999999 | 83.04 | 83.04 |            |               |       |    
 9149 |         | 1718085610.4388337 |          22 |       | 1718082000 | 83.03999999999999 | 83.04 | 83.04 |            |               |       |    
 9175 |         |  1718089210.417186 |          22 |       | 1718085600 | 83.03999999999999 | 83.04 | 83.04 |            |               |       |    
 9201 |         |  1718092810.440033 |          22 |       | 1718089200 | 83.03999999999999 | 83.04 | 83.04 |            |               |       |    
 9228 |         | 1718096410.4281015 |          22 |       | 1718092800 | 83.03999999999999 | 83.04 | 83.04 |            |               |       |    
 9255 |         | 1718100010.4390285 |          22 |       | 1718096400 | 83.26130507151669 | 83.04 | 83.46 |            |               |       |    
 9282 |         |    1718103610.4422 |          22 |       | 1718100000 | 83.46000000000001 | 83.46 | 83.46 |            |               |       |    
 9309 |         |  1718107210.421433 |          22 |       | 1718103600 | 83.46000000000001 | 83.46 | 83.46 |            |               |       |    
 9336 |         | 1718110810.4408443 |          22 |       | 1718107200 | 83.46000000000001 | 83.46 | 83.46 |            |               |       |    
 9363 |         | 1718114410.4144876 |          22 |       | 1718110800 | 83.46000000000001 | 83.46 | 83.46 |            |               |       |    
 9390 |         | 1718118010.4440346 |          22 |       | 1718114400 | 83.46000000000001 | 83.46 | 83.46 |            |               |       |    
 9417 |         | 1718121610.4402826 |          22 |       | 1718118000 | 83.46000000000001 | 83.46 | 83.46 |            |               |       |    

oliv3r avatar Jun 14 '24 05:06 oliv3r

I notice this with a sensor that has the same value, but at the time of a Home Assistant restart, it got the rounding error. Maybe this is a race condition where a value got in during startup and the precision wasn't loaded yet or something?

Screenshot_20240624-063359

michaelarnauts avatar Jun 24 '24 04:06 michaelarnauts

There hasn't been any activity on this issue recently. Due to the high number of incoming GitHub notifications, we have to clean some of the old issues, as many of them have already been resolved with the latest updates. Please make sure to update to the latest Home Assistant version and check if that solves the issue. Let us know if that works for you by adding a comment 👍 This issue has now been marked as stale and will be closed if no further activity occurs. Thank you for your contributions.