Rounding of sensor values

I have a rains sensor configured with lot of decimals:

sensor.netatmo_regen_sum_rain_24 0.29831595829992

In the frontend, I want to show the value with only 1 digit after the comma.

Can anybody advise me, how to do this?

Thanks,
Albert

2 Likes

You can use a value template. Here is my config to round down a humidity sensor

platform: mqtt  
state_topic: "conservatory"  
name: "Conservatory Humidity"  
unit_of_measurement: "%"  
value_template: '{{ value_json.humidity | round(1) }}'
7 Likes

works perfect - thanky you very much!

1 Like

I have problem with rounding. For example in HA 0.63.2
{{ (float(states.sensor.1.state) + float(states.sensor.2.state) + float(states.sensor.3.state)) / 3 | round(2) }} is not rounded.
If I exchage valuses with numbers result is also nonrounded:
{{ (float(3) + float(3) + float(4)) / 3 | round(2) }} = 3.3333333333333335 on dev-template page.
What am I doing wrong?

1 Like

Try surrounding everything to the left of the pipe sign in parents. I think what you do now is rounding the 3 integer only.

5 Likes

that works in template assistant but not when I the value_template in config to that

I’m fairly new to HA so this might be a stupid question but I don’t manage to get the example of @BertrumUK working.

I tried multiple variation in the configuration and customisation file.

This is the code I’m trying to use, anyone any idea what’s wrong?

# MQTT sensors

  - platform: mqtt
    state_topic: "/sensor1/HUM"
    name: "humidity_slaapkamer"
    unit_of_measurement: "%"
    value_template: '{{ value_json.humidity_slaapkamer.value | round(1) }}'
  - platform: mqtt
    state_topic: "/sensor1/PRES"
    name: "Pressure slaapkamer"
  - platform: mqtt
    state_topic: "/sensor1/TEMP"
    name: "temperature_slaapkamer"
    value_template: '{{ value_json.temperature_slaapkamer.value | round(1) }}'

What does the yaml you have output as a sensor value ? Anything ?

Can you verify what is being sent by the sensor to the topic “/sensor1/HUM” ?

Sorry, figured it out in the mean time. Needed to be :
value_template: '{{value | round(1) }}'

4 Likes

Awesome !!

I guess your sensor output is not in JSON therefore my config won’t work for you. Glad you got it sorted :grinning:

I got some strange results from round() function, it gives me 11.520000000000001 value when I recalculate m/s to km/h and round it to 1 decimal place.

value_template: '{{ state_attr("sensor.redfish_pogoda", "wiatr_predkosc") | float * 3.6 | round(1) }}'

Any ideas?

value_template: '{{ state_attr("sensor.redfish_pogoda", "wiatr_predkosc") | (float * 3.6) | round(1) }}'
1 Like

Hello @BertrumUK and @Sonnstein,
Is there a way to round a state value without having to create a template sensor?
I need to do it for more than 30 sensors and I don’t want to type it down all over…

Another question is: is there a difference using mqtt and template integration for this purpose?

Thank you!

Can you give an example of your sensors ?

Hi @BertrumUK,
They are automatic sensors created after using the utility_meter integration.

That integration is using data from other sensors like your gas and electric sensors - you can round those values down before passing the data onto that integration.

Hi @BertrumUK,
Thanks for the answer. It seems to be working. However, one question, is it only affecting display or is it also affecting precision of the computation? What I mean is by rounding the values at the beginning of the computation (before Integration and Utility_meter), will I loose precision in the final value?

It uses the values of the sensors to create its own value so the original values are still the same.

Fair enough, I understand that but my question is something else. By using a round value for computation, after some time, the final result might differ from a computation using precise value then rounded.
In other words, I feel that:
Round(integration(sensor)) != Integration(round(sensor))