Template sensor calulation issues


I have an issue with my energy calculations. In my house I have a heatpump that reports is consumption and a sensor that reports the total consumption for the entire house. I also have some other devices that report consumption and with this I want to display them individually.

So I created a template that takes the total energy and subtracts all the other devices consumption, the issue is that my heatpump reports what looks like a step-curve. Not each kwh or each hour is reported its kind of random. This makes my template sensor report too high numbers and then be corrected each time the heatpump decides to deliver some measurement. I have checked the manufacture app for the heatpump and that looks the same, unfortunately. So increased polling would not have any affect. I wonder if there is a way to “smoothen” the sensor of the heatpump in a meaningful way?

Here is a screenshot of my sensor.
Orange is total power (reported with 2 decimals every change)
Brown is the heatpump (not reported every hour)
Blue is calculated template sensor (the drops are visible each time the heatpump reports)

My template:

    - name: "Other consumption"
      unique_id: "other_consumption" 
      unit_of_measurement: "kWh"
      device_class: energy
      state_class: total_increasing
      state: >
        {% set A = states("sensor.daily_accumulated_consumption") | float %}
        {% set B = states("sensor.daily_elbil") | float %}
        {% set C = states("sensor.daily_heatpump") | float %}
        {% set D = states("sensor.daily_heatpump_water") | float %}
        {% set E = states("sensor.daily_computer") | float %}
        {% set F = states("sensor.daily_tv") | float %}
        {{ (( 0 + A - (B + C + D + E + F) )) | round(2) }}

The daily_X means I put a daily cycle on each sensor before the calculation.

How about putting your own energy monitor on the heat pump. One that updates more frequently?

Also what is with the 0 + A in your template?

It seems to be a ‘parallax error’ - the heat pump is reporting its power infrequently and it’s possibly not representing the actual power consumed over the duration of time. For example if the HP reported 3kW as an instantaneous value but was switching on and off every minute (50% duty cycle), if this value was then used to calculate a energy over the duration of an hour time, then the consumption would be 3kWh and not 1.5kW.
As for the use of ‘theta’ (aka the “Voiceless dental fricative”) in the formula - I’m also curious!

Thanks for your answer,

Yes that would be a solution, but it runs on 3 phases and a power meter would probably set me back ~€100 which feels a bit sad when the pump actually reports its kwh usage.

The prefaced 0 is from the orignal author of the template, I thought it added some int-casting to the calculation…

The heatpump is reporting it’s “total-lifetime” energy usage in kwh directly so I’m not calculating the power myself.
The theta is actually just a “0” :slight_smile:

No, the 0 + adds nothing (pun!). You can remove it. You should also consider an availability template to prevent strange values if one or more sensor is unavailable. Use the |is_number filter to check all your sensors are available.

There are low pass filters but that will shift your data in time and screw things up as well.

It comes down to garbage data in → garbage data out.

Adding a better sensor is the way to go.

No way! That’s exactly what I was looking for (outlier) for another formula. Did not know about filters!