Create accumulating/increasing sensor for grid export based on consumption and solar generation

Hello,

I currently have total increasing sensors for solar generation and mains consumption, but not for mains export. But this should be easy to determine from the solar generation minus the mains consumption, as long as that is positive. However, what I have come up with for a template sensor seems overly complicated, and I wondered if I’m missing a simpler way to do it. It would be far easier if both were measured in IotaWatt and I could use its (mathematic) integration feature to accumulate the positive/negative values separately, but unfortunately my solar figures come from the inverter itself.

What I have come up with is this monstrosity:

    - name: mainsexport_kwh_accumulated
      state: "{% if this.attributes.last_state is defined %}
        {% set consumed = ((states('sensor.mainsconsumption_wh_accumulated') | float / 1000) - (this.attributes.last_consumed)) %}
        {% set produced = ((states('sensor.solaredge_i1_ac_energy_kwh') | float) - (this.attributes.last_produced)) %}
        {% set exported = produced - consumed %}
        {% if exported > 0 %}
        {{ (this.attributes.last_state | default(0)) + exported }}
        {% else %}
        {{ (this.attributes.last_state | default(0)) }}
        {% endif %}
        {% else %}
        0.0
        {% endif %}"
      attributes:
        last_produced: "{{ states('sensor.solaredge_i1_ac_energy_kwh') | float }}"
        last_consumed: "{{ states('sensor.mainsconsumption_wh_accumulated') | float / 1000 }}"
        last_state: "{% if this.attributes.last_state is defined %}
        {% set consumed = ((states('sensor.mainsconsumption_wh_accumulated') | float / 1000) - (this.attributes.last_consumed)) %}
        {% set produced = ((states('sensor.solaredge_i1_ac_energy_kwh') | float) - (this.attributes.last_produced)) %}
        {% set exported = produced - consumed %}
        {% if exported > 0 %}
        {{ (this.attributes.last_state | default(0)) + exported }}
        {% else %}
        {{ (this.attributes.last_state | default(0)) }}
        {% endif %}
        {% else %}
        0.0
        {% endif %}"
      unit_of_measurement: "kWh"
      state_class: total_increasing
      device_class: energy

Basically, I try to keep track of the last values in attributes, and compute the new value based on those. I have no idea if this works yet as its currently dark and there’s no solar production. But does anyone know a better way to compute this other than writing my own HA integration? Or at the very least, can I avoid duplicating all the code for the ‘last_state’ attribute? Is there nothing built into HA to get the last state when updating the state?

Maybe my last post was a little too technical and I should ask the higher level question.

Home assistant energy dashboard requires: Solar output, consumed from grid, exported to grid. It computes itself what you consumed from solar (the difference)

I instead have: Solar output, total power consumed. It’s easy to compute if the solar was higher than consumed and I exported, or vice versa and I consumed from the grid. So can I have home assistant compute what came from the grid and what was exported to the grid based on the sensors I have?

(That’s what I’m trying to achieve with template sensors like the one above)