Creating energy sensor from switch/fan/climate/binary sensor - almost works

So, want to monitor the energy usage of certain sensors I have that do not report the power usage directly. The way I try to do this is: Switch → Power sensor → Riemann sum (Integral) → Energy.
However, it currently doesn’t give the correct result in the Energy dashboard.

Based on info in other threads, I have managed to create wattage (power, kW) sensors for a switch, fan and climate sensor and that works. For instance, I have a hottub with a heater and a pump that both draw 2.8 kW:

# configuration.yaml
  - platform: template
        friendly_name: "Hottub heater power consumption"
        device_class: power
        unit_of_measurement: "kW"
        value_template: >
          {% set is_heating = is_state_attr('climate.hottub_heater', 'hvac_action', 'heating') %}
          {% set kilowatts_when_on = 2.8 %}                                                                                                                                                                                                 
          {{ is_heating * kilowatts_when_on }}                                                                                                                                                                                              

  - platform: template
        friendly_name: "Hottub pump 1 power consumption"
        device_class: power
        unit_of_measurement: "kW"
        value_template: >
          {% set kilowatts_when_on = 2.800 %}
          {{ is_state_attr('fan.hottub_pump_1', 'preset_mode', 'HI')  * kilowatts_when_on }}                                                                                                                                                

This works, and I can see that when I turn on/off the pump the sensor sensor.hottub_pump1_power_consumption show the correct value when I look at the history dashboard.

Now, to get the energy (kWh), I use the “Integral - Riemann sum integral sensor” helper in /config/helpers with the power sensor as input (trapezoidal, no prefix, time unit: hours) to create eg. sensor.hottub_pump_1_energy_consumed. Now, here’s the problem - this sensor immediately jumps up 2.8 kWh when I start the pump, even if I let it run only for a few seconds.

How can I get my kWh measurements (which the Energy dashboards requires) based on “sub hour sampling” (not sure how to put it)?

I should mention that I also have tried with following in my customize.yaml:

  state_class: total_increasing

But am not sure anymore why I put it there of if it is needed - if it should be total_increasing or total.

In any case, the problem I have is that the Integral (Riemann sum) helper doesn’t work as I expected. Do I need to create a sensor with time resolution of seconds (kWs) and then another template sensor that re-scales it to kWh?

I’d highly suggest using the Powercalc integration:

It has this functionality as well as a ton more.


Thank you Ben! I did look briefly into the powercalc integration earlier, but thought is was more aimed at pre-defined lamps and stuff, but hey, it was quite easy to define virtual power sensors manually, and the integration provides Energy sensors automatically, great!

I’ll mark this as solved. For anyone else coming here - dropt the template stuff and install the Powercalc integration. Read the instructions carefully - it requires two restarts of HA (install via HACS, restart, put powercalc: in configuration.yaml, restart).

1 Like