Hi there,
Any help how to round the result of my adjusted Osram Smart+ motion sensor temperature. Now and then it shows too many figures after the comma - one would be enough …
Try not to use this format: states.sensor.varasto2_osram_temp.state use states('sensor.varasto2_osram_temp') instead. It will not generate errors if the state is unknown.
This usually works, but in some cases you can still get a lot of digits after the decimal point due to float precision. It’s generally better to do this:
But there is a difference between just taking the first two decimals after the point and rounding to two decimals.
I believe Tom’s solution ‘should’ work. The fact that it doesn’t always mean that round() is in need of a tinker.
How about if we use Tom’s and then force the format ?
You’re assuming, incorrectly, that floating point formatting truncates, but it does round, and guarantees the exact number of digits after the decimal point. No need to round first.
You’re confusing floating point resolution with print formatting. If you’re doing math, and you only want a certain number of decimal places, then that’s what round is for. If you’re outputting a floating point value and you want to control how many decimal digits are printed, that’s what format is for.
There’s nothing wrong with Python or the round function. This is just the nature of floating point numbers (in any language.)
Phil, your experience in these issues is far greater than mine but I can’t help but feel that this is far less than a satisfactory outcome for anyone.
Any Newbie would expect @tom_l 's solution to work in all cases (just as tom expected it to work here, and it ‘may’ have)
But if I have (say) 6.66666666 and I pipe it to round(2) I, and a LOT of other people ‘expect’ to get 6.67 not (and I’m guessing here but it illustates the point) 6.67000001 or similar.
When you use excel (as another case) formatting 6.6666666 to 2 dp gives you 6.66666666 it merely displays as 6.67 And if you use =round(6.6666666,2) it actually returns 6.67
This is what people expect, but clearly not what they are going to get.
So assuming that we HAVE to set a 2 dp float you are telling me that i would HAVE to use (for example) : -
I appreciate that this is not your fault and in no way do I want to rant at you merely because you showed up on this issue.
Unfortunately it’s just the case that you know a lot more about this stuff than almost anyother “member” who deigns to interact with us mortals (sorry, I know you hate that too )
I apologise if I’m wasting your time.
Are you arguing that a programming language like Python should work the same as a spreadsheet program like Excel? Hmm. Well, you can argue all you want, but it is what it is. If you want it to work some other way, then maybe join the Python development team (and expect to lose. ) Or you can accept the way it works and use it accordingly. And I can’t help that a LOT of people have incorrect expectations. I guess that’s where learning and experience comes into play.
Why would you add the float filter at the end? The result of a template is always, always, always a string. Why would you convert the string output from the format filter to a float, just so it can get converted back to a string? That would just introduce another opportunity for the float resolution to express the number with too many digits again.
I have actually seen round() fail to output the expected number of digits. Now I know why and thus have a bit of ‘find and replace’ editing to do in my config today.
This will actually play a part in my heating control far away in my summer cabin to take care of water equipment not to freeze, so I want to be extra sure