Rounding MQTT Sensors

Is it possible to directly round values from a MQTT sensor instead of making an additional temple sensor?

From my weather station I receive the values in 2 decimals and just as my another sensor I would like to show it just with 1 decimal.

What’s going wrong in this code:


- platform: mqtt  
  state_topic: sensor/weather_station/humidity
  value_template: '{{ states.sensor.weer_station_luchtvochtigheid | round(1) }}'
  name: "Luchtvochtigheid" 
  unit_of_measurement: "%"  
  qos: 0
  entity_namespace: weer_station  

Ok ok ok ok, I was thinking to difficult !

Got it!

value_template: “{{ value | round(1) }}”

6 Likes

Sorry for replying to such an old thread, but I found this on Google and was struggling to figure out why my sensor was returning “Unknown” after applying this. The problem was, that this forum automatically changes quotation marks if they are not inside a code block and directly copy-pasting the answer left me with “ and ” in the config instead of the normal quotation marks. The code below should work for anyone trying to directly copy-paste too.

value_template: "{{ value | round(1) }}"

That’s why many of us repeatedly bang on about formatting code correctly.

It’s also possible to change the display precision in the UI, rather than artificially constraining it in the sensor: