Edl21 Energy Dashboard Cascading electricity meters

System description see below.

My problem is this:

When installing my photovoltaic, a new system for the power connection was used: LEW (energy supplier in German Bavarian Swabia) Drives through the heat pump electricity consumption meter (DAY/Night) into the electricity consumption meter (light flux of the building) and only then through the photovoltaic measuring station.
(The advantage here is that the electricity you use yourself is deducted before the luminous flux is counted - this means significantly lower consumption figures).
Disadvantage: Electricity consumed from the network is counted twice (heat pump electricity [DAY/Night] plus luminous flux) - at least in Home Assistant - (not from the electricity provider - who balances the two!)

That’s why I always get wrong/incorrect information in my energy dashboard in Home Assistant.

I’ve been trying for months now to somehow compensate for this error - but I can’t do it.
My previous solution attempts fail again and again due to a lack of knowledge or problems with YAML…
My intended solution is the following:

1st step: Daily recording of the luminous flux counter reading at the beginning of night power: 21:59:59 in a variable (A) or a sensor value (A).
2nd step: Daily recording of the luminous flux meter reading at the beginning of the day: 5:59:59 in a 2nd variable (B) or a second sensor value (B).
3rd step: Calculation of the real power consumption of the heat pump (night) using a script which writes a new sensor value between 10:00:00 p.m and 6:00:00 a.m. according to the formula/schemes
[(Sensor.Heat pump night current) - (Sensor luminous flux - variable(or sensor value)A))]
4th step: Calculation of the real power consumption of the heat pump (day) using a script which writes a new sensor value if the meter reading for luminous flux changes between 6:00:00 a.m. and 10:00:00 p.m. according to the formula/schemes
[(Sensor.Heat pump daytime current) - (Sensor luminous flux - variable(or sensor value)B))]
6. Last but not least: transfer of the correct calculated sensor values to the energy dashboard
Heat pump consumption night / day

I am very grateful for all solution approaches!
Sorry for the endless text, but I always try to describe what I’m about as precisely as possible.
Unfortunately, my attempt to solve the problem with NodeRed also fails due to a lack of understanding.
If anyone has seen/read something similar, please share the link.
BTW: Please be patient with my bad englisch … got really rusty…

SYSTEM:
Home Assistant OS 9.4 Supervisor 2022.11.2 in Synology D216+II (On Virtual PC)
Integration of the Vissmann Vitocal 200S heat pump via ViCare (BJ 2011)
Integration of the photovoltaic FoxEss (KOPP BRANDING) with HACS in it FOXessCloud (BJ 2022) [to be changed to direct communication via MODBus]
Integration of the electricity meter I (luminous flux of the building) and electricity meter II (heat pump DAY/night) via IR opto heads USB to edl21
Integration of all “large consumers” (washing machine/dryer/dishwasher/cooker/iron/steamer/fridge/freezer…) via Miele@home
Integration of “small stuff” (printer/Fritzbox/repeater/smartphones [6 people]/smart TVs/media servers/network radios…)
Access to HA via VPN on the go (due to DSlite)
Access to Synlogy on the go via Quickconnect (due to DSlite)
[TO Do’s: Integration of water consumption meter/cistern consumption meter (washing machine/garden/dishwasher/toilets)
Entity for the bulk consumers to control the switch-on time/start consumption according to the power of the PV
Control of the heat pump in the event of overproduction of electricity - preheating of the hot water/building buffer tank to >55°C]
[If you have any questions about the implementation of the above integrations, please send a PM - there I can provide the links to the articles from which I copied it!]