Formatting a number with 2 decimals in value template

For a sensor that is measuring how much watt a device is using I would like to calclulate the energy costs per day in euro’s.

It seems that I am able to do this with this value template sensor:

  • platform: template
    sensors:
    PC enery costs:
    friendly_name: “Energy costs per day”
    unit_of_measurement: ‘Euro’
    value_template: “{{ states(‘sensor.tussenstekkercomputer_power’)|float * 24 / 1000|float * 0.22 | round(2)}}”

I do now however get a lot of decimals (Like 2.122296 Euro). I would like to have just 2 decimals. I thought I would be able to do that using round(2) like this:

  • platform: template
    sensors:
    PC enery costs:
    friendly_name: “Energy costs per day”
    unit_of_measurement: ‘Euro’
    value_template: “{{ states(‘sensor.tussenstekkercomputer_power’)|float * 24 / 1000|float * 0.22 | round(2)}}”

But is still get much more decimals. Any idea where I’m going wrong?

2 Likes

Try adding parenthesis

value_template: “{{ ( states(‘sensor.tussenstekkercomputer_power’)|float * 24 / 1000|float * 0.22 ) | round(2)}}”

You can experiment with templates in the template page of HA (http://hassio:8123/dev-template)

2 Likes

Thanks! That did the trick!