Utility meter – returning higher values than source sensor (part of the time)

I have recently installed solar panels and want to calculate the value of the energy produced by the system.
This is found by adding the value of self-consumption and the value of the exported energy. In Norway we have separate tariffs for import and export, and they change every hour.
I have no issues with the calculations of the value of exported energy, it is the calculation of the value of self-consumption that fails.

To find the amount of solar energy that is self-consumed I have defined a template sensor that subtracts the energy exported pr hour from the total energy produced by the solar inverter per hour:

      - name: "Energy hourly consumption from solar calculated"
        unit_of_measurement: "kWh"
        state_class: total_increasing
        state: >
          {% set from_solar = states('sensor.solar_production_hourly') | float(0) %}
          {% set to_grid = states('sensor.energy_export_hourly') | float(0) %}
          {{ (from_solar - to_grid) | round(3, default=0) }}  

When inspecting this sensor during daytime when energy is produced, it returns correct values,
including when we are actively exporting.

What is missing then, is to multiply the value from the template sensor above with the import price pr kWh for the current hou,r to get the value of self-consumption this hour. For this I have created the template sensor below:

      - name: "Base Value Solar Consumption"  
        unit_of_measurement: "kr"
        state: >
          {% set current_price = states('sensor.nordpool_kwh_oslo_nok_5_08_025') | float(0) %}
          {% set solar_consumption = states('sensor.energy_hourly_consumption_from_solar_calculated') | float(0) %}
          {{ (current_price * solar_consumption) | round(6, default=0) }}          

When inspecting this sensor during solar production, and even when exporting energy, everything looks correct.

To keep track of the value of self-consumption over time, I’ve created utility meters for hour, day, week, month and year.
The utility meter for hours looks like this:

  value_hourly_solar_consumption:
    source: sensor.base_value_solar_consumption
    name: Value hourly solar consumption
    cycle: hourly

I assumed that the resulting sensor.value_hourly_solar_consumption would have the exact same value as its source sensor sensor.base_value_solar_consumption. And it does, if there is no export. But for some reason, when exporting, the utility meter ends up with a value that is higher than its source sensor. In fact, the difference equals the exported energy multiplied by the import price.

Here is a picture visualizing the problem:


From 11:00 to 12:00 there is no export and the ‘Base Value Solar Consumption’ is equal to ‘Value hourly solar consumption’.
Both sensors reset correctly to 0 at 12:00 and are equal until 12:15. Then the system starts exporting and the base sensor calculates this correctly, while the utility meter sensor ‘Value hourly solar consumption’ does not.

I’ve tried searching for similar issues, I’ve tried renaming the sensors and also rebuilding them all from scratch, but I end up with the same result.
I would really appreciate it if someone could help me understand what I have done wrong here?

I know this post is well over a year old, but did you ever find a fix for this? I am having the same exact issue where the power meter starts showing higher values as soon as solar starts exporting.