Trying to smooth out a power sensor

Hello

Hope this is the right area. I have a house with power that goes to a granny flat then from the granny flat to the shed. At the moment i have sensors on the shed and the house. I was able to create a sensor for the granny flat but i have noticed when 3d printing in the shed it sends the granny flat way out in positive and negative readings. Is there a way to smooth or arverage this sensor? .like make it an avererage over 2 seconds?

    - name: "Granny Flat Power"
      unique_id: 123456789
      unit_of_measurement: "W"
      device_class: power
      state: > 
        {% set mainsin = states('sensor.mains_in_out_channel_2_power') | float %}
        {% set shed = (states('sensor.shellypro4pm_shed_switch_0_power')|float + states('sensor.shellypro4pm_shed_switch_1_power')|float + states('sensor.shellypro4pm_shed_switch_2_power')|float + states('sensor.shellypro4pm_shed_switch_3_power')|float)%}
        {{ ((mainsin - shed)) | round(2, default=0) }}  

Have a look here

Thank you. I will look into this.

I think you should not use the sensors supplying the actual power usage, because they seem to update at an uncontrolled interval. Using the total usage over the last minute or 30 seconds seems to be more stable.