Rounding of sensor values

Nope. States are strings. The + will concatenate the strings.

It is correct to cast each value. The problem is that it’s the integer 3 that gets rounded and not the whole result, due to the order of evaluation. The solution is to add brackets.

{{ ((float(states.sensor.1.state) + float(states.sensor.2.state) + float(states.sensor.3.state)) / 3) | round(2) }}

Did you find a solution? I am looking for a generic approach to get temp from sensors in xx.yy format but xx.y is sufficient. And I also don’t want to create another template sensor…

Well, you’ll have to do it… unfortunately, there’s no ‘one click’ solution.
I’m using this code in my configuration.yaml

  - platform: template
    sensors:
      bathroom_temperature:
        friendly_name: 'Bathroom Temperature'
        unit_of_measurement: '°C'
        value_template: "{{ states('sensor.atc_7aff_temperature')| float | round(1) }}"
      bathroom_humidity:
        friendly_name: 'Bathroom Humidity'
        unit_of_measurement: '%'
        value_template: "{{ states('sensor.atc_7aff_humidity')| float | round(1) }}"
1 Like

I’ll start by saying I’m new to the template topic, and have been playing with Home Assistant for a few months now with good success otherwise.

Can someone please help with this MQTT.yaml entry?
The default output is showing up as having 6 decimal places (i.e. 2.500000 C) and I’m trying to round it off to 1 decimal place, (i.e. 2.5 C)
The below code is showing me 0 C (when the temperature is really 2.5 C

Thanks in advance,

# MQTT Sensors
sensor:
    - name: "Backyard Temperature (433Mhz Probe)"
      state_topic: "rtl_433/13184/temperature_C"
      qos: 0
      state_class: measurement
      device_class: temperature
      unit_of_measurement: "°C"
      value_template: '{% set state = states("sensor.backyard_temperature_433mhz_probe") %}{{ state | round(1) if is_number(state)}}'

Why you check if it’s a number? Try without this check.

value_template: '{{states("sensor.backyard_temperature_433mhz_probe") | round(1) }}'

When I try with your code the result just says “Unknown”.

States are strings — even when they look like numbers (attributes retain their types). You need to make it a number first and you must specify a default in case it’s unavailable. I’m pretty sure your logs would’ve given some kind of warning or error.

value_template: '{{ states("sensor.backyard_temperature_433mhz_probe") | float(0) | round(1) }}'

Thank you, this one worked. I had tried similar but my “float” didn’t have the “(0)” included.

This feature is coming in 2023.2, it’s in the beta. Able to do it via GUI

1 Like

I spoke too soon. The value is showing up as 0.0 C now when the actual temperature is about -3.0 C.
Any tips on that?

No-one has read the question properly! You need:

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

This is an MQTT sensor, not a template sensor. You can’t look up its own state. The value_template allows you to convert whatever is in the MQTT topic to the format of your choosing.

I assume the topic contains e.g. 2.500000 — that is the value, which you then need to turn into a float and round it.

If that still doesn’t work, remove the value_template line and post a screenshot of the sensor state so we can see what’s actually coming from the sensor via the topic.

1 Like

I removed the line refreshed and here’s what I’m seeing.

Then my value_template above should work. What happens when you put that in?

Great, that seemed to have worked. Thank you so much.

1 Like

Now available via the UI: sensor display precision.

1 Like

Hey folks, I also have an issue with rounding my aqara sensor values. In deconz i receive zero decimals but in home assistant i suddenly receive two decimals (this started a while ago - before ther was only one decimal.)

The rounding accuracy is set to one decimal, too.

image

Do you have any idea how to fix it back to only one digit?

Thanks!

1 Like

I have the same issue

in multiple-entity-row card

What card is that ? the format looks different to your entity setting, ie your are using a comma for decimal place but the card is using a period

it´s not an issue of the card (it´s a button-card), I think.
Even in the states-list the sensor shows two decimals, even though the appearence-setting says one.

Yes, mine is the same as yours - the actual state is 2 decimals and the Display precision is set to 1 decimal.

If you display the entity in an entity card, does it display with 1 decimal ?