Subtracting two "total increasing" sensors

Hi! I’m setting up energy monitoring in my house and running into a problem with subtracting sensors. I have an Emporia Vue 3 monitoring whole branch circuits, but also some high-energy devices on those branches are on their own power meters. I want to be able to subtract out the per-device readings from the branch circuit to get a “branch balance” sensor.

Following the advice of various other threads, I created a Utility Meter helper to get a strictly increasing kWh versions of the sensors (the Emporia Vue resets kWh at midnight). I then used a simple template sensor to subtract the two sensors:

{{states("sensor.garage_energy_total")|float - states("sensor.car_charger_energy_total")|float}}

Like the two input sensors, I set this up as a “total increasing” sensor.

This almost works, except that the two input sensors don’t necessarily update at the same moment. As a result, if car_charger_energy_total updates before garage_energy_total, the template sensor will briefly decrease until the next update of garage_energy_total reflects the energy used by the car charger. You can see this slight non-monotonicity in the graph:

I believe Home Assistant treats these brief dips as resets of the sensor value, leading to unpredictable results.

Are there any solutions to this? I could write a custom integration, but was hoping to not have to go down that route!

I meant to add, this leads to very wrong results on the energy dashboard.

For the template sensor I showed, you can see on the graph it’s used about 0.1 kWh today, but because of the resetting behavior of a “total increasing” sensor, the energy dashboard reports 0.53 kWh for this sensor.

Set your template sensor state_class correctly:

state_class: total 

not total_increasing

Thanks! I tried that, but saw no difference in the energy dashboard. It still reported 0.53 kWh instead of 0.1 kWh. I tried removing and re-adding it to the dashboard just in case. My next best guess is that the recorder still thinks this is a total_increasing sensor because it was originally, but I’m not sure how to check that.

Are there and FIX ISSUE buttons in Developer Tools → Statistics?

If so click them and fix them.

No FIX ISSUE buttons (at least, not related to this :sweat_smile:). But looking at the schema for the statistics tables, I’m pretty sure changing the state class to “total” wouldn’t cause it to recalculate its existing running sum, but I think it will do the right thing going forward. I’ll have to wait a little while until I can tell for sure.

Confirmed that the updates since I switched the template sensor to “total” have been accumulated correctly. Thanks!

1 Like