Subtract one total_increasing energy state from another?

I am trying to subtract one energy monitor (an Athom switch) from another (an Iotawatt branch circuit sensor). The switch is on the branch and I want to distinguish the energy use in the dashboard. However, these are both total_increasing energy states that reset to zero at different times. Also, the switched portion uses more energy than the rest of the branch so the value often goes negative.

Whenever the value of the subtraction goes down, Home Assistant thinks that it’s a new reset point and calculates the new delta from zero. This usually makes the energy usage go negative, but sometimes it might go positive. It’s never correct.

How can I access the total_increasing aggregation from each sensor individually, and then do the subtraction?

template:
  - sensor:
    - name: "AV Rack without Networking Energy"
      unique_id: av_rack_without_networking_energy
      unit_of_measurement: 'kWh'
      device_class: energy
      state_class: total_increasing
      state: >
          {{ '%0.2f' | format(states('sensor.avrack_wh') | float / 1000 - 
                              states('sensor.networking_rack_power_strip_energy') | float ) }}

That can’t be possible. If the switch is on the branch then its energy consumption must be included in the branch energy consumption. Something is wrong here.

Than the rest of the branch, that is, the portion of the branch circuit not including the switch. What is wrong is that the totals reset to zero at different times. If the branch total energy for its period (day? unsure) resets to zero before the switch total energy has reset for its period (also unsure what this period actually is), it will create a negative value.

It would be less noticeable (but still wrong) if the switch only consumed a little energy, but it consumes the majority of the energy on the branch.

If HA, on detecting a decrease in a total_increasing value, reset to the new value instead of to zero, it would almost work, but a little energy would be lost between the reset and the first reading.

Yes I get that the totals reset at different times but you implied subtracting the switch power from the branch power went negative because of this:

That’s not possible. It may go negative because the branch resets before the switch but not because the switch uses more energy than the rest of the branch.

For the reset mismatch you could create separate utility meters for the branch and for the switch. Do not supply a cycle for either of the utility meters. That way they never rest and keep counting up (even when the source sensors reset).

Then subtract the utility meter sensors in your template.

Sorry I wasn’t clear. I will try the utility meter thing.

I don’t know what happens internally with a total_increasing value, but I was hoping I could directly reference the hourly aggregations and subtract those.

I am not sure if it makes a difference, but I saw now that my sensor.avrack_wh is shown as state_class: total with a last_reset attribute, not a total_increasing. I assume this comes from the Iotawatt integration.

If you are familiar with SQL queries you could extract the the hourly LTS (long term statistics) data using SQL sensors.

I am; I will try this.

1 Like

The SQL schema was confusing, but I got it working with your utility_meter solution. Thanks