Rounding of sensor values

in an entity card everything is fine. that´s weired:
image

My guess is the button card uses the entity state ?

I am having a rounding up issue:

My code for a template sensor which is W to convert to KW:

- platform: template
  sensors:
    convert_charge_power:
      unique_id: convert_charge_power
      friendly_name: "Charge Power KW"
      value_template: "{{ states('sensor.wattpilot_charging_energy') | float / 1000 | round(2) }}"

However the result is not rounded at all:
image

What am I doing wrong?

Will this work ? I think you are rounding the 1000 only

value_template: "{{ (states('sensor.wattpilot_charging_energy') | float / 1000) | round(2) }}"

Have you tried just changing the units in the entity info dialog instead ?

Try this:

- platform: template
  sensors:
    convert_charge_power:
      unique_id: convert_charge_power
      friendly_name: "Charge Power KW"
      value_template: "{{ (states('sensor.wattpilot_charging_energy') | float / 1000) | round(2) }}"

The order is to do conversions before math. It was rounding the 1000 to two digits not the result

Isn’t that exactly what I said above ?

Yes, I just missed it.

@AllHailJ & @Holdestmade,

Thanks for the suggestion. It worked!

Same for me. I’ve had this happen today out of the blue, but otherwise it has worked ok for me until now


 {{ state_attr('sensor.my_sensor')|float|round(4)*100 }}

But today sensor.my_sensor = 0.036855 and instead of getting 3.69 the above code returned 3.6900000000000004

What is going on!? Where is that 0.0000000000000004 coming from?

  1. You got a typo - an attribute’s name is missing.
  2. Try to multiply before rounding.
1 Like

Yes sorry, I simplified the code for easier reading.
The round() and *100 is the problem but only today or for this value, which is strange.

What works right now is this:

(state_attr('sensor.my_sensor','attribute') | float(0) | round(4) * 100 ) | round(2)
}}

But having to round twice is crazy.
I’m struggling to see what’s causing this.
Am I missing something?

Sorry, I missed the fact that

*100|round

is treated as

* (100|round)
1 Like

Indeed, I just realised the mistake and this fixed it:

((state_attr('sensor.my_sensor','attribute')  | float(0) ) *100) | round(2)
}}p/kWh

But I am still confused about the first result

Just a speculation:
rounded value “1.234” * 100 = float value with some epsilon

Many numbers cannot be represented exactly in binary with a fixed number of digits.

I’ve never come across something like this

{{ float("0.036855") }} = 0.036855
{{ float("0.036855")|round(4) }} = 0.0369
{{ float("0.036855")|round(4) * 10 }} = 0.369
Yet somehow
{{ float("0.036855")|round(4) * 100 }} = 3.6900000000000004
or
{{ float("0.036855")|round(4) * 1000 }} = 36.900000000000006
but
{{ float("0.036855")|round(4) * 10000 }} = 369.0

Does anyone know what is going on here?

Do you know how binary works? If so, convert the numbers to binary and see what happens.

I gave you the answer above: When you cast from string to float, it becomes an actual number, but you have a fixed number of bits to represent the number. What might look like a finite decimal expansion in isn’t necessarily possible in binary for the same number.

Some references with illustrative examples:

https://www.uvm.edu/~cbcafier/cs1210/book/03_types_and_literals/representation.html

1 Like