Template utility meter sourced from total_increasing sensors

Hello,
looking for some hints creating template sensors (utility meters?) sourcing from already total_increasing sensors.
Context: i have a three phase current system and a shelly3EM sensor (and PV and so on)
The problem is that the total IN and OUT energy of the Shelly do not count exactly the energy sourced from the provider and the energy returned to the provider, since it also counts the energy souced-returned from one phase to another.
So i basically need (at least i think :)) to create two utility meters (one for energy sourced and one for energy returned) startng from the difference between the two values returned by the shelly meter: if it’s positive, there is a template sensor rising, if it’s negative there is another template sensor rising.
Thanks for any hint or starting point…