Decimal points/rounding template values

I’m using a template to adjust some sensor values, it is working as expected with the catch that it rounds to the 15th decimal. Suggestions from searching don’t seem to do anything other than break the template, any way to round to two decimal points? Thank you!

sensor:
  - platform: template
    sensors:
      bathroom_humidity:
        friendly_name: "Bathroom Humidity"
        unit_of_measurement: '%'
        value_template: "{{ states('sensor.zooz_zse40_4_in_1_sensor_relative_humidity') | float - 6 }}"
        
      kids_room_humidity:
        friendly_name: "Kids Room Humidity"
        unit_of_measurement: '%'
        value_template: "{{ states('sensor.zooz_zse40_4_in_1_sensor_relative_humidity_3') | float - 10 }}"

Use the round filter.

{{ (states(...) | float - 6) | round(2) }}

Thank you! That seems to break the template, at least in the editor- this is what I’ve tried:

sensor:
  - platform: template
    sensors:
      bathroom_humidity:
        friendly_name: "Bathroom Humidity"
        unit_of_measurement: '%'
        value_template: "{{ states('sensor.zooz_zse40_4_in_1_sensor_relative_humidity') | float - 6) | round(2) }}"

You’re missing the opening parenthesis before “states”

sensor:
  - platform: template
    sensors:
      bathroom_humidity:
        friendly_name: "Bathroom Humidity"
        unit_of_measurement: '%'
        value_template: "{{ (states('sensor.zooz_zse40_4_in_1_sensor_relative_humidity') | float - 6) | round(2) }}"
1 Like

You’ve got an odd number of brackets in yours, you need the opening bracket at the start.

Edit - beaten to it

1 Like

Thanks everyone! Got it working!

Other answers already covered it. Just to add, the template editor is your friend, copy and paste templates there to test ahead of time.

round will not guarantee 2 decimal places though. If you want to force a 2 decimal output, you can use format().

{{ "%.2f" | format("34.56789" | float - 6) }}
{{ "%.2f" | format("7" | float - 6) }}
{{ ("7" | float - 6) | round(2) }}

Prints 28.57, 1.00 and 1.0. You can copy and paste that into the template editor to see yourself.
Replace the example strings with your states().

1 Like

That looks great, thank you so much for your time!

Hello I am trying to solve this same issue. I have another sensor template in my yaml file so it’s saying duplacated sensors. How do I add more than one sensor template using two diff. Entities? I’m sure it’s something to do with spacing ?

Hey guys,

Does anyone know whats wrong with my rounding?

“{{ (127775277056 - value | int * 4096) / 1073741824 | round(1) }}”

Still getting a long string of numbers

10.860652923583984

The pipe ( | ) operator is very low on the order of operations so your template as written is only rounding the divisor of your formula. Put another set of parentheses around the whole thing and you’ll get what you want.

{{ ((127775277056 - value | int * 4096) / 1073741824) | round(1) }}
3 Likes

Thanks mate :slight_smile:

Please help
I’ve converted MB to TB
and I get this 142735360 using this code.

template:
  - sensor:
      - name: "D Drive Available Space"
        unit_of_measurement: "TB"
        state:  "{{ states('sensor.kelvin_storage_d_available_space') | float * 1024  | round(1) }} "
The question is how do I round it down to 1 TB?

Seems like if you wanted to convert MB to TB you’d divide by 1024^2. It looks like you’re converting MB to KB here.

This might work.

{{ states('sensor.kelvin_storage_d_available_space') | float / 1024 / 1024 | round(1) }}

Thanks I will give it a try.

Another thing I didn’t notice the first time: The pipe (|) is a higher-order operator than the division (/) or multiply (*) operators, so the last bit of your template (and mine) “1024 | round(1)” would only round the integer 1024 to one decimal place, which of course it was already. If you want the result to be rounded, you’d have to put the whole thing in parentheses:

{{ (states('sensor.kelvin_storage_d_available_space') | float / 1024 / 1024) | round(1) }}```

Hi, thanks for your input, and looking at it for me, I have entered the template and the result I am getting is 0.1 it should be 1.0 as its a 1 TB hard drive.

I don’t know what exactly your sensors are intended to be, but “available space” seems to be the space that has not yet been used, not the total space. Could this be accurate? Could the drive have 0.1 Terabytes free? Judging from the value you included in the original question, I think it should have about 140 Gb free. Am I right?

Either this sensor doesn’t mean what you think it does, or the sensor is off by a factor of 10. The calculation itself is pretty straightforward.

I was reading it wrong its available space.
So what I have done is taken out one of the 1024 which give me 136.1
which is what I have left on that drive.

{{ (states('sensor.kelvin_storage_d_available_space') | float / 1024 ) | round(1)