Data fluctuation for state-based template sensor

Hi,

I have a template sensor to calculate how much solar-produced power is consumed locally.
Envoy monitors solar production while Eagle-200 monitors power meter.
I assume the difference between them will be the consumed power.

However, two sensors are updated in different time and hence sometimes the calculated value is incorrect (e.g. consumed “negative” solar power for blue line in the lower graph)

Is there a good way to prevent this issue? Not sure if the state update logic could be changed.
Not sure if trigger-based will help but looks like it cannot wait for both two sensors to trigger?

Thanks!

I have resolved the issue with the following approach.

Let Entity H be the one has higher update frequency.
Let Entity L be the one has lower update frequency.

  1. Create an automation to force update Entity L whenever Entity H is changed

    • Entity L will have the same update frequency as Entity H but will have some delay (say 1 second)
  2. Add a trigger to the template sensor to update the sensor only when Entity L is updated

    • It could prevent a short fluctuation due to the update delay of Entity L
    • However, the template sensor will not be updated if Entity L value is unchanged

Here is the resulting graph and I am pretty happy with the outcome!