How to limt the numbers of characters?

Hi,

ic create a Template sensor which calculate the wind speed from m/s in km/h. The result ist ok- but how can I limit the numbers of characters of the result? Sometimes the result looks like 8,989898923 km/h but I only need eg. 8,98 km/h.

- platform: template
  sensors:
    wind_einheit:
      value_template:
        '{{ states.sensor.dark_sky_wind_speed.state | multiply(3.6) }}'
      unit_of_measurement: 'Km/h'

You can create a template sensor and limit the number of decimal places, however it won’t always work.

HA seems to have an issue with this. I believe there is a bug request in related to temperature readings. Not sure if it applies to other readings.

Maybe using round(2). Taked from https://home-assistant.io/topics/templating/#sensor-states

    - platform: template
      sensors:
        wind_einheit:
          value_template:
            '{{ states.sensor.dark_sky_wind_speed.state | multiply(3.6) |round(2)}}'
          unit_of_measurement: 'Km/h'

Thank you! Round work for me!

Hi

sorry to open such an old thread, but it was the only one I found.

I do have kind of the same issue but in my case, it does not work

I use this:

value_template: '{{ states.vacuum.eva.attributes.total_cleaning_time | float / 60 | round(2) }}'

But it still gives me a number like x.xxxxxxxx but I only need it like x.xx
Also, If the number changes, there is still that long string.
I thought maybe one it will happen, but it seems like it does this all the time.

Anyone know where it could be related to?

You need to use brackets, otherwise 60 will be rounded before the division happens:

value_template: '{{ (states.vacuum.eva.attributes.total_cleaning_time | float / 60) | round(2) }}'
1 Like

Thanks, that was the trick.
Simple math, did not thought about this.