How to create a Sensor template for a Power (W) sensor from a existing kWh sensor

Hej community.
Im Trying to create a sensor that shows the current average power (w) per minute or 5 minutes from a sensor that shows accumulated KWh.

I tried this from chat GPTs suggestion:

{ states(‘sensor.your_kwh_sensor’) | float*1000 }}

But that did just change the unit. And i also tried the Integral - Reimann Sum. but that only seems to work “the other way”, converting a W sensor to kWh.

any pros out there on template coding and mathematics?

You want a derivative sensor to convert from Wh to W. There’s one in the helpers menu.

Remember…
Power = (v)(a)(pf) = kw. (pf should be close to 1 unless its a inductive load).

If you turn on a 1 kw load, and leave it on for an hour, that is 1kwh of energy (kwh is typical unit).

I compared this to my 1 minute window calculation on power from emporia meters and its close (in the ball park), to going to be perfect because a different length of time its looking at. You could go shorter than 5 minutes if your meter updates very fast to ha.

Create a derivative sensor (helpers, derivative sensor). 5 minute time, select hour at the bottom… (this will give you kwh/h) (hour divided by hour lol)… which is power. Or said another way, it is taking a 5 minute window sliding through time, and carrying out that use rate to an hour, which gets you back to kw (power). (I know it sounds weird.)

Then do a new template sensor (from the helpers)… All this does is present the other sensor with the correct units so kwh/h doesn’t annoy you. (may make you laugh every time you see it?)

The options screen shot is on the deritive sensor.

paste this in it… (name your entities correctly).

{% set kW = states(‘sensor.testdemand’) | float %}
{{ kW | float | round(2)}}


Woho. That’s amazing, they both seem to work. I set the derivative sensor to 5 minus. they give only slight difference in result. i created a additional sensor to convert the unit and greatness to Watt so that the output looked better. But for the template sensor i had to multiply with 10 to gett a value. no idea why. but it seams to do the trick.

thanks a lot for letting me borrow your brain power =)


Skärmavbild 2024-03-27 kl. 09.16.48

If you want to sanity check it, get a volt and amp meter and amp the load, and get voltage… watts should volts times amps (about)… less power factor. Should be close when its not changing… (Like the long period just under 1500 on your chart).

Glad it helped. I’m still learning ha, so may be a better way. :slight_smile: