Learning template engine

Hello fellow mates,
I am learning HA and found the following problem.

I have entity as seen in blow picture, that has some history data (top half of picture)

The thing is this data is in Wh, and I need it in kWh.

I have learned I need to create new entity configuration in configuration.yaml using template engine.
So I have created this:

sensor:
  - platform: template
    sensors:
      qm_tpl_pvproduction:
        friendly_name: "PV production (kWh)"
        unit_of_measurement: "kWh"
        value_template: "{{ states('sensor.fotowoltaika_production_power') | multiply(0.001) | int }}"
        device_class: energy

But then, when I check the history panel, I get this (bottom half of picture)

Developer tools panel, does not see any problem with my template.
I am missing something, but I don’t know what.
Cheers,
B.

Your template sensor looks good.

It will not convert historical data (from the past) it will update the next time sensor.fotowoltaika_production_power updates. At the moment this is 0.

So 0.001 * 0 = 0

All entity state values are strings. If you want to use them in a mathematical equation, you must convert the string to either a float or an int.

        value_template: "{{ (states('sensor.fotowoltaika_production_power') | int * 0.001) | int }}"
1 Like

Oops. I should have seen that.

Thank you coleauges. I understand and have applied changes. Will wait than few days to see how my custom sensor behaves when PV panels start producing again (we have heavy snow for last week in Poland = no kW produced :wink: )

1 Like