Convert Wh to kWh and round by 2 decimals

What I’m trying to accomplish is basically what I’ve written in the title. I’ve been trying out the template tester, but I can’t get it to round up the number without breaking the whole syntax.

I’m currently stuck here, but it still has 3 decimals:

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

Thanks in advance!

That’s because you’re rounding “1000”, so you need more brackets. Try this instead:

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

However, keep in mind that this rounds to 2 decimals, it does not always produces a number with two decimals. For example, today I have produced 9896Wh, and your template would render this as 9.9kWh.

1 Like

You’re the best, thanks!

1 Like