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.
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.
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’.
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.
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.
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?
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"
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.