Calculation bug with HA or am I doing it wrong?

I created a simple calculation to give me mortality rate for this over-hyped coronavirus but the rounding doesn’t seem to work properly.

If I use round(2) I get a figure with only 1 decimal place, the same as if I use round(1), whereas round(3) gives me 3 decimal places… is this a bug in HA or am I doing something wrong? (it is my first time creating a calculation like this)

The following code:

value_template: "{{ ((states('sensor.australia_coronavirus_deaths') | float * 100) / (states('sensor.australia_coronavirus_confirmed') | float )) | round(2)}}"

Gives:

and:
value_template: "{{ ((states('sensor.australia_coronavirus_deaths') | float * 100) / (states('sensor.australia_coronavirus_confirmed') | float )) | round(3)}}"

gives:

I am trying to get 2.80 (2 decimal places…) WTF

I tried this with the worldwide figures to ensure this wasn’t a case of HA not showing the 0 but that provided the same issue.

It’s dropping the trailing zero

Something like this maybe…

{{ "%.2f"|format(variable) }}

I guess so… If I use the worldwide figure it rounds it to one decimal place instead of showing two


so is there a better way to do this so it simply shows two decimal places?

3.597 rounded to 2 decimals will be 3.6 most likely.
See my above suggestion re formatting the result. Pretty sure I use that somewhere.

legend!

this works:

value_template: "{{ "%.2f"|format(((states('sensor.worldwide_coronavirus_deaths') | float * 100) / (states('sensor.worldwide_coronavirus_confirmed') | float ))) }}"

EDIT: only works in the template dev tool… fails in practice due to the %

EDIT again: fixed it by swapping a set of quotes to singles
value_template: "{{ '%.2f'|format(((states('sensor.worldwide_coronavirus_deaths') | float * 100) / (states('sensor.worldwide_coronavirus_confirmed') | float ))) }}"

1 Like

Also rather than the number of infections, the infection rate is a far more important figure:

True, but I’m looking at how the mortality rate is dropping as more and more people recover safely. Once a huge number of people are infected and subsequently recover I think we will see that it isn’t as bad as the media is making out. So far unless you are really old, you should be ok… I may be proven wrong but at this stage I just want my overseas holiday to go ahead in 2 week’s time! :crossed_fingers:

1 Like