How to round the result of adjusted temperature

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 …

image

      osram_varasto_temppi2:
        friendly_name: "OsramVarastoTemp2"
        unit_of_measurement: '°C'
        value_template: >-
          {{ states.sensor.varasto2_osram_temp.state | float + 0.7 }}  
       value_template: >-
          {{ ( states('sensor.varasto2_osram_temp')|float + 0.7 )|round(2) }}  

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.

1 Like

Thanks! That was quick …

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:

{{ '%0.2f'|format(states('sensor.varasto2_osram_temp')|float + 0.7) }}
4 Likes

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.

Okay, I stand corrected on that front but you must agree that there is an issue with round() given both your statement : -

AND that I agree, as I’ve seen it happen. (I have used your "‘%0.2f’| " (copyright pnbruckner 2018 :rofl: ) a couple of times myself)

So is this a fault in python (like the famous one in windows 3.1 calculator) or is it in Hassio ?

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.)

1 Like

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) : -

{{ '%0.2f'| format(states('sensor.varasto2_osram_temp') | float + 0.7) | float }} {# ??? #}

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 :rofl: (sorry, I know you hate that too :smiley: )
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. :stuck_out_tongue_winking_eye:) 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. :slightly_smiling_face:

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.

Where is there fault? I see nothing faulty here.

This makes sense. Thanks Phil.

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.

1 Like
{{ '%0.2f'|format(states('sensor.varasto2_osram_temp')|float + 0.7) }}

Not a big deal, but this gave me 2 digits after the comma, should I use this instead:

{{ '%0.1f'|format(states('sensor.varasto2_osram_temp')|float + 0.7) }}

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 :wink:

1 Like

Yes, that will result in a number with just one digit to the right of the decimal point.

As another possibility, you could use the g format instead of the f format. The g format will print (up to) N significant digits. So, for example:

{{ '%0.3g'|format(states('sensor.varasto2_osram_temp')|float + 0.7) }}

would print up to 3 significant digits (0.123, 1.23, 12.3, etc.)

See the Jinja2 format filter for more details.

3 Likes

Many Thanks!

1 Like