Calculated difference between 2 power/energy sensors when they're not in sync

I have 2 Emporia Vue 2s, one on my main electrical panel and one on my sub panel. For those not familiar, these devices have CTs that monitor the power of a number of individual circuits and the overall power of the panel. In my main panel, I’m monitoring 16 circuits. In HA, each circuit has 2 entities: power (W), which is the instantaneous power drawn, and daily energy (Wh), which is a running total of the energy used that day and resets to 0 at midnight. There are also 2 entities for the remaining balance, or the total panel power/daily energy minus the sum of all 16. The sub panel is powered by a circuit from the main panel and since there is another Vue 2 in that, I’m not monitoring that circuit with a CT. As a result, its power/energy is included in the balance calculation even though I have an entity from the sub panel Vue 2 for the sub panel total power/daily energy. To solve this, I created an entity that calculated the difference between the main panel balance and the sub panel total to give me the actual main panel balance:

template:
  - sensor:
      - name: "vue2main Actual Balance Power"
        unit_of_measurement: "W"
        state: "{{ [0,( states('sensor.vue2main_balance_power')|float(0) - states('sensor.vue2sub_total_power')|float(0) )|round(0)]| max }}"
        state_class: "measurement"
      - name: "vue2main Actual Balance Daily Energy"
        unit_of_measurement: "Wh"
        state: "{{ [0,( states('sensor.vue2main_balance_daily_energy')|float(0) - states('sensor.vue2sub_total_daily_energy')|float(0) )|round(0)]| max }}"
        state_class: "measurement

You’d think all would be well, but there is a lag in the timing of the 2 devices. This results in the reported power difference spiking or going to zero when there are spikes on any of the sub panel circuits or main panel balance circuits from things turning on or off (e.g. AC, freezer, etc.). Spikes I can deal with, but what I’m struggling to solve is the Daily Energy reading right before it resets to zero. The sub panel resets to zero just before the main panel balance resets resulting in a final spike before the next day:

What can I do to work around this lag? Is there a way to use a rolling average in the calculation? Is there a solution that could work for both power and daily energy entities?