Energy template sensor not resetting at midnight

I have a number of template sensors that I use to add multiple sensors together

example.

  - sensor:
    - name: "ma9: BatteryALL"
      unique_id: "ma9: BatteryALL"
      unit_of_measurement: kWh  
      device_class: energy
      state_class: total_increasing
      state: >
        {% set sensor1 = states('sensor.ma9_um_battery2grid') | float %}
        {% set sensor2 = states('sensor.ma9_um_battery2house') | float %}
      
        {{ (sensor1 + sensor2) | round(3, default=0) }}

and everything use to work fine with them. But in the past few weeks the statistics being recordered for them no longer resets to 0 at midmight, so i have to manually go to the Developers Tools > Statistics and change enteries.

The actual sensor history looks fine:
image

but if you look at the statistics you see at midnight the last days total is added in

image

has something changed with the way the “device_class: energy” works?

in the short term i’ve created a utility meter sensor to sit ontop of the template sensors, this does work but means i’ve lost all the history.

No. But something might have changed with whatever integration was used to create these two sensors:

sensor.ma9_battery2grid
sensor.ma9_battery2house

How were they created?

The only reason your template sensor reset at midnight is because these two sensors reset at midnight. Nothing to do with the device class. Please share the state history for these two sensors.

Also you really should use an availability template to prevent glitches if one or more of the source sensors becomes unavailable.

  - sensor:
    - name: "ma9: BatteryALL"
      unique_id: "ma9: BatteryALL"
      unit_of_measurement: kWh  
      device_class: energy
      state_class: total_increasing
      state: >
        {% set sensor1 = states('sensor.ma9_battery2grid') | float %}
        {% set sensor2 = states('sensor.ma9_battery2house') | float %}
        {{ (sensor1 + sensor2) | round(3) }}
      availability: >
        {{ has_value('sensor.ma9_battery2grid') and
        has_value('sensor.ma9_battery2house') }}

thanks @tom_l

Sorry, it’s been awhile since I set this all up, so just going through how everything comes together.

The two sensors in the template that are being added together are two Utility Meter Helper sensors. These in turn come from two Integration - Riemann sum integral Helper sensors, which come from the raw power data I get from the GivTCP integration.

I needed to sum them for the Sankey chart I use.

state history of the two:
image
image