Accumulating the difference from the last two values into two different new sensors

Hi all, I’ve been at it for two days now and I can’t figure out how to accurately collect and accumulate data from one sensor into two new sensors (import/export).

Situation:

I have this sensor: sensor.sunpower_consumption_lifetime_power

This sensor is the import/export value from my solar system. The current value is -6,960, this value keeps going down (negative) during the day (solar production and sending to the grid) and goes up in value (positive) during the night when we’re using energy.

The sensor updates roughly every 2 minutes.

The first thing I was able to find on the forum and utilize is the statistics platform to get the change (value from the newest and oldest measurement).

Here is my code:

- platform: statistics
  name: "Energy Changes"
  entity_id: sensor.sunpower_consumption_lifetime_power
  state_characteristic: change
  sampling_size: 2

Then I created two sensors that take this value and aggregate it depending on the change is a negative or positive value:

- platform: template
  sensors:
    home_importing:
      friendly_name: "Home Importing"
      unit_of_measurement: "kWh"
      value_template: >
        {% set importing_value = states('sensor.home_importing') | float (default=0) %}
        {% set delta_power = states('sensor.energy_changes') | float (default=0) %}

        {% if delta_power > 0 %}
          {{ (importing_value + (delta_power / 3))|round(2)| float }}
        {% else %}
          {{ importing_value }}
        {% endif %}
      device_class: energy

- platform: template
  sensors:
    home_exporting:
      friendly_name: "Home Exporting"
      unit_of_measurement: "kWh"
      value_template: >
        {% set exporting_value = states('sensor.home_exporting') | float (default=0) %}
        {% set delta_power = states('sensor.energy_changes') | float (default=0) %}

        {% if delta_power < 0 %}
          {{ (exporting_value - (delta_power / 3))|round(2)| float | abs }}
        {% else %}
          {{ exporting_value }}
        {% endif %}
      device_class: energy

This just puts the energy_changes into home_exporting or home_importing depending on if it’s a positive or negative value.

As I was trying to figure things out, I don’t know why, but I need to divide the statistics output (energy_changes) by 3 to get an accurate change.

The issue(s) that I’m having:

  • When sensor.sunpower_consumption_lifetime_power changes, if the change is the same as before, i.e. .04 then energy_changes doesn’t update, therefore, home_importing or home_exporting don’t recognize and don’t accumulate that change.
  • When energy_changes does update (to diff value), home_importing or home_exporting don’t always see this and don’t capture and accumulate.
  • When I restart home assistant or just reload the config file, the first 2 updates on energy_changes are very wonky, the first is always 0.01 and the second is always like 2.x, after that it’s accurate to what sensor.sunpower_consumption_lifetime_power is changing.

Am I overthinking or overdoing this? is there a simpler way to collect the changes of sensor.sunpower_consumption_lifetime_power over time and add them to home_importing and home_exporting?

My end goal is to have this populate my energy dashboard for consumption and return to the grid. Thanks in advance for any help, ideas, or directions.