Compare the last result to the new result in Automation

Hey Folks.
Iam controlling my heating coil power with a restfull command.
Everytime the house consumption goes below 0, the power is meant to go into hot water production. Also i want it to have maximum 75 percent power usage. I got it working, but i want to make it a little bit more smooth and also accurate. so when the last value of the calculation is only 2-3 percent different to the new calculation, i want it to do nothing.

So is it possible to compare the last value to the new incoming value or is there even a better idea for controlling?

Here’s the code from the automation:

service: rest_command.set_power_heizstab
data:
  power: >-
    {% set p = states('sensor.hausverbrauchkomplett') | float  %}
    {% set u = states('sensor.hausverbrauch_channel_b_power') | float / 2000 * 100  %}
    {% set i = (( p / 2000 * 100)) * 0.99 | float  | round(0) | abs %}
    {% if p >= 0 %}
       0
    {% elif p <= -1500 %}
       75
    {% else %}  
      {{ ((i - u) | abs -2) | float | round(0) }} 
    {% endif %}

to better visualize what its doing:

I would do it outside of HA with something like this DIY approach. https://mk2pvrouter.co.uk/
Any surplus solar energy is sent to the hot water
I use a similar commercial system from paladin in .AU

Today has been a supurb solar day and its not over!


Red = solar output clipped by inverter capacity, Grey = house load, Green = Hot water diversion
HA monitors water temp and hot water power draw so I can use more surplus energy…
Here is todays water temp showing the heat loss overnight, then the heating…