This is close to impossible.
You have two entity sensors here: solar (power) and load (power).
For an accurate calculation, both entity state values must be from the exact same time event, otherwise they will be out of step - the solar power from say 10:12:34 and the load power from 10:13:15 where clearly they do not relate to the same instant in time.
You do not say where your sensor values come from, so I have to guess.
All entity state values come into Home Assistant from the outside world, via integrations that talk with devices. It is the responsibility of the integration to sample the device state, and therefore the entity state value seen in Home Assistant will change when the integration updates.
In the ‘worst’ case you will have two, independent integrations. The only way that the state values of both integrations will match is if you can orchestrate both integrations to update their entity state values at the same time. This may be possible if both integrations have a service call (now action) that permits a forced update (ie force the integration to ‘take a reading’ for you).
In my case, I have a solar inverter that provides me with register values for both solar power and load power, so in theory I have one integration (my Node-RED flow) that reads the inverter Modbus registers for solar power and load power, at the ‘same’ time.
The Modbus reads take time (3 seconds over the registers I want) and the inverter does not update all registers together. The major issue is that the load value is calculated. The inverter knows the grid import/export value (from a power meter CT clamp) which it samples every second. The inverter knows the solar power, and in my case the battery charge/discharge power from internal current sensors.
By the laws of physics
solar + import + discharge = load + export + charge
hence load = solar + import - export + discharge - charge
and in simple terms, the inverter itself cannot measure all of these at exactly the same time, so the load figure is a guestimate.
I calculate the unavoidable ‘error’ (much as the Architect does in The Matrix) by
imbalance = solar + import + discharge - load - export - charge
At quiet times, this is roughly the amount of energy the inverter is consuming itself. When turning large loads on or off, or for cloud cover, this value can be very high.
In your case, with a value regularly going to zero when you don’t expect it to, it may be the result of ‘unavailable’ rather than a precise reading, so you may want to explore the route by which your load sensor value arrives to see if you need to trap ‘unavailable’ and perhaps remove the zeros altogether. Availability templates and the like spring to mind.
An alternative would be to time-average both values over the same period, say 5 minutes. This is long enough to make the readings relate to the almost equal time period, yet short enough to provide a realistic view on what is going on. There is a great statistics integration in Home Assistant that I use for rolling-average values to try and smooth out fluctuations in solar production for much the same reason. Very much still a work in progress, since ‘excess solar’ will go from 3kW to 0 in the moment it takes to turn the vacuum cleaner on.