Set script to run every few seconds in certain circumstances

Actual Objective: Charge my car with spare solar power.
How I think it’ll work: In certain circumstances (e.g. either automatically based on sensor state, and also when I manually trigger it) I want Home Assistant to enter a “Variable Charge Mode”. When in this mode I want some code to run every few seconds which calculates my spare solar PV wattage (calculation will be based on several sensor numbers) and sets my Hypervolt car charger amperage to a value that would use the spare solar, but no more.

All applicable devices, sensors etc are all set up in HA.
I have scripting/coding experience, the logic should be easy enough to write.

What is the method used to do something like this in Home Assistant?
i.e. what should I create to set this mode to be toggled on/off, then how do I create the script and tell it to run at certain intervals? I’m looking for the terminology used in HA, e.g. “Automation”, “Template” etc