Rounding in template has wrong number of decimals

Hi,
i have the following template sensor:

  feinstaub_luftdruck_hpa:
    value_template: '{{ states("sensor.feinstaub_luftdruck") | float / 100 | round(2) }}'
    friendly_name: "Luftdruck"
    unit_of_measurement: "hPa"

As you can see, i’d like to round the result of the division with 100 to a value of two decimals. However, i seem to be unable to do this. Currently, the value of this template sensor is: 1007.0975999999999 while the raw value of the sensor input for the template is 100709.76. I do not understand where the suffix of 9 comes from. It looks like a rounding error. Sometimes the value is displayed correctly, but with four decimals, and not with two decimals, like desired. Where is my error?

The way you wrote it, the rounding is applying just to 100, not the entire equation before it. So you have to use parentheses:

'{{ (states("sensor.feinstaub_luftdruck") | float / 100) | round(2) }}'

I’ve gotten bitten by this myself.

4 Likes

OK, thanks. I’ve got it now :slight_smile:

Thanks for posting that, was having the same issue.