Template to override resetting sensor

Hi

I am building out energy monitoring in HA: I’ve ended up with a few template sensors summing categories of individual devices, which I then include as grid consumption in the energy dashboard.

I have a new smart plug which has the annoying feature of having all its energy sensors reset on various cadences. If I add any of these plug’s sensors to my category sensor, I now have negative contributions at the reset (say midnight every day. Is there a good way to compensate for this?

Both the plug’s sensor and my template have state_class of total_increasing, but this doesn’t seem to help.

In the end I created a sensor which is triggered by the source sensor changes and totals up the difference between the current and previous state but only if it is positive. This works for total_increasing sensors.

Here it is if anyone else has a similar problem:

- trigger:
    - platform: state
      entity_id: sensor.plug_kitchen_oven_today_energy
      variables:
        from_state: "{{trigger.from_state.state | float('no value')}}"
        to_state: "{{trigger.to_state.state | float('no value')}}"
        counter: "{{states('sensor.plug_kitchen_oven_energy_cumulative') | float('no value yet')}}"
  sensor:
    - name: Plug Kitchen Oven Energy Cumulative
      state_class: total_increasing
      unique_id: "rjgherhtmegnvehgveg"
      unit_of_measurement: kWh
      icon: mdi:flash
      state: >
        {% if counter is number %}
          {% if to_state < from_state %}
            {{counter}}
          {% else %}
            {{counter + to_state - from_state}}
          {% endif %}
        {%else%}
          {{to_state}}
        {%endif%}
      availability: "{{counter is number or (from_state is number and to_state is number)}}"