Using round(2) sometimes results in one decimal when last number is 0

Hi,

I created a template to convert my current net energy to kW for my dashboard. I use round(2) to round off to two decimals, however this unfortunately does not always result in two decimals.

I’m using the following template:

  - platform: template
    sensors:
      p1_net_electricity_point_kw:
        friendly_name: "P1 Net electricity point kW"
        unit_of_measurement: "kW"
        value_template: "{{ (states('sensor.p1_net_electricity_point') | float * 0.001) | round(2)  }}"

This works well (img1), however when the last digit of this number ends with 0, I only get the one decimal (img2). This results in this sensor jumping slightly, which kind of bugs me (img3). Is there a way to force two digits? I don’t necessarily need is to round up to the last digit (i.e. it is also fine if the digits that come after the second decimal are simply removed), if that makes it easier.

2 dec
img1

1 dec
img2

IMG_1225
img3

Try this

  - platform: template
    sensors:
      p1_net_electricity_point_kw:
        friendly_name: "P1 Net electricity point kW"
        unit_of_measurement: "kW"
        value_template: "{{ '{:.2f}'.format((states('sensor.p1_net_electricity_point') | float * 0.001) | round(2)) }}"

Thanks for your reply. Unfortunately it shows the same result…

Then it is the home assistant dashboard trimming the trailing zero and there is nothing you can do about it.

Maybe I am misinterpreting the word “dashboard” in your response, but it also shows this behaviour in the entity info view of ‘sensor.p1_net_electricity_point_kw’.

Home Assistant frontend in general.

I was afraid of that. Oh well, I guess the best solution I have is using a fixed size for the chip in my dashboard and accepting that sometimes I will be missing the trailing zero.

Thank you for your help!

Did you use tom_l’s suggest with the round(2) still in effect?

Round(2) will round the number off and store it in a HA variable which is always a string.
You then might need to convert it to a float before displaying it or better yet, not use round(2) at all, but instead just use the format command tom_l suggested to control the viewing.

I am not exactly sure what you mean, but I copy - pasted the code that tom_I suggested. I also tried a template where I removed round(2), but it had the same result.

{{
'{:.2f}'.format((states('sensor.p1_net_electricity_point') | float * 0.001))
}}

Yeah, I’m pretty sure the fronted drops trailing zeros no matter what formatting is applied to the state.

ok, and what happens if the value happens to hit a number without any decimals?
Does it then drop the decimal indicator and the decimals or do it atleast have 1 decimal?

You might be able to do something like this:

{%- if (states('sensor.p1_net_electricity_point')%0.1)==0 -%}
  {{states('sensor.p1_net_electricity_point')}}0
{%- else -%}
  {{states('sensor.p1_net_electricity_point')}}
{%- endif -%}

Beware that this is not tested, since I have no access to my HA from here, so some tinkering to get it to work might be required. :slight_smile:

Heh, good question. It actually still shows 1.0kW, which doesn’t make too much sense to me

A quick look through the values in my dashboard shows a minimum of one decimal place.

1 Like

Thanks for the pragmatic approach! I will try that one out next…

Welp… I guess sometimes we make things more complicated than they are.

As @tom_l mentioned HA is very hardcore in removing trailing zeros from your sensors. Adding any other character at the end of the sensor is not a problem, but as soon as it ends with zero it is removed.

Even if you purposely add the zero using a template, like the one @WallyR suggested. If you change the 0 to any other digit, there’s no problem.

So I figured “Why not just add the kW to the end of the sensor?”. And there we go. I use these sensors only for my dashboard so it’s a fine solution for my particular situation.

  - platform: template
    sensors:
      p1_net_electricity_point_kw:
        friendly_name: "P1 Net electricity point kW"
        value_template: "{{ '{:.2f}'.format((states('sensor.p1_net_electricity_point') | float * 0.001)) }}kW"

Dashboard

Thanks for all the help!

My suggestion was not for a template, but for the lovelace card.

Ah I misunderstood then. That should work as well!

Still good that you made that mistake, because I never thought about it being used in the template sensor.
I learned something new to take into account there. :slight_smile:

I guess the ‘precision’ feature makes this obsolete now. All this work :’)