Plant automation MQTT/Arduino - relative sensor data

Hi! I started a fun plant care automation project. Basically I have gotten 2x 5kg load cell weight sensors and 1x micro water pump motor connected to an Arduino Nano ESP32 and got that all running. The Arduino publishes weight of the two weight sensors on an interval (say once every 10s or something) and it subscribes to pump motor on with a payload to tell seconds on-time and perhaps motor speed. Over MQTT.

My intention is that the Arduino should just publish/subscribe to these simple values and not do any logic or smart stuff so that I can keep that in the HA world (easier and funnier to config).

One weight sensor is for the 5L water can (so I know when I need to refill it) and the other weight sensor is for the plant. Now the idea is that my rosemary plant might start at 300g when freshly manually watered for the first time ever.

Then I’d like a HA automation (as much done in UI as possible as I simply like it even though I’m not scared to add templates/helpers etc) to start with that value, say 300g, and trigger a refill of water onto the plant when those 300g has dropped to 80% of that value - in this case 240g. Then I want it to publish “run_pump 5s” until it measures 30% increase in weight - in this case 312g - and then save those 312g and repeat the process.

So my short question would be: how do I make an automation that regulates weight proportionally over time? It’s not clear what would help in this case, templates, input_number, statistics helpers?

What I want to accomplish if it was a simple python script is this:

if weight < 0.8 * newlyWateredWeight
  dryWeight = weight
  while weight < dryWeight * 1.3
    pump_motor = ON
  newlyWateredWeight = weight

So the result I expect is that I will relatively refill water and the plant to grow in weight over time. Maybe this is overworked and not reliable but I still wonder how to handle “variables” and relative data like this from within an automation.

Thanks for any input!