Solar: Subtract self-consumption from individual devices

I have a simple problem, but the solution seems more complex than necessary.
I can monitor the following energy consumption:

  • Total consumption (grid and solar)
  • Heat pump (grid and solar)
  • Sun (total generation)
  • Sun ( self-consumption)
    A solar log + two meters are used for this purpose.

I have created two utility meters so that I can see the share of the heat pump at a glance:

  • Heat pump
  • House (calculated by total consumption minus heat pump)
    image

The problem is that HA expects the grid consumption WITHOUT the solar component. Now I thought: No problem, I’ll just deduct the self-consumption proportionally from the two consumers. So dynamically e.g.

  • Heat pump grid: (heat pump grid and sun) - (60% self-consumption sun)
  • House grid: (house grid and sun) - (40% self-consumption sun)
template:
  - sensor:
    - name: "efh_verbrauch"
      unique_id: "efh_verbrauch"
      unit_of_measurement: kWh
      state_class: "total_increasing"
      device_class: "energy"
      state: >
           {% set total = (states('sensor.energy_consumption_daily_jp') | float(0)) %}
           {% set grid = total - (states('sensor.energy_self_consumption_daily_jp') | float(0)) %}
           {% set efh = total - (states('sensor.energy_consumption_heatpump_daily_jp') | float(0)) %}	 
           {{ grid * efh / total }}
    - name: "wp_verbrauch"
      unique_id: "wp_verbrauch"
      unit_of_measurement: kWh
      state_class: "total_increasing"
      device_class: "energy"
      state: >
          {% set total = (states('sensor.energy_consumption_daily_jp') | float(0)) %}
          {% set grid = total - (states('sensor.energy_self_consumption_daily_jp') | float(0)) %}	 
          {% set wp = (states('sensor.energy_consumption_heatpump_daily_jp') | float(0)) %}		 
          {{ grid * wp / total }}

Problem:
Deducting self-consumption means that the consumption of the two appliances can also fall (see screenshot). This is because I deduct the self-consumption proportionally based on the current daily total consumption. So the total is theoretically still correct, but as the utility-meter is total_increasing the calculation is wrong.
Actually, I would only have to split the last increment of self-consumption - but I fail to do this.