Brainstorming: Electric Load Control Concept

My fellow Home Assistant Nerds,

I would like to brainstorm with you, what the best conceptual way is to automaticaly control electrical loads in Home Assistant.
.

Let me elaborate on what I currently have:

I have multiple consumers controlled by Shelly devices.
Depending on the Consumer they should be switched on or of under certain conditions.
i.e. the Air Dryer in the sleeping room should not run while Night Mode is on, the battery charger should run only when the UPS input is on and when it runs it should remain on for at least 5 minutes, etc. etc.
So far nothing special and all of that logic can be build into HA Automations or Scripts.
Think of it as a complex set of dependancies and conditions for a device to be allowed to run.

On the other hand there are multiple instances that want to turn a device on or off.
These inlcude my Script governing my Load Priority System depending on how much excess solar power is currently available, my QuickLoadControl Phyton Script governing the Buffer Loads, several automations that start / stop devices based on certain triggers.
Some device can therefore be started / stopped by multiple “triggers”.
.

There are quite a few problems with this approach:

a) currently I have the before mentioned complex dependencies in each of the “triggers” so in each of the Load Control Scripts and each automation seperately.
This makes them very convoluted in the UI (and the yaml), hard to troubleshoot, and if you make a change to a dependency/condition in one Script you have to replicate that change over to all the other places where this devices gets triggered. So pretty much an admin nightmare.

b) In part due to that redundant logic that needs to get evaluated over and over again, my HA is at times running pretty hot and I suspect my Memory Leak is in part also related to this. (I have some evidence hinting in that direction, but that is besides the point of this discussion here)

c) Switch Turn on and Turn Off Actions are sometimes called multiple times while the previous action is still executing. This produces Warnings in the Logs and is unneccessary.

d) Aparently sometimes a device is missing a command to turn on or turn off. (might be lost in the network, never troubleshooted it as its rare but happens). In that case it stays in the “wrong” state unless the trigger fires again. This made me put time pattern triggers in each automation as a safeguard against a lost command. Instead something checking for a “desired State” and enforcing that State of the Device sounds prudent.

In essence I am looking for a better way to do this.
.

My current idea is vaguely along the following line:

Have a Script / Automation / Service (which?) that governs the dependancies for the specific device in one place, and then have the trigger scripts just fire that Script / Automation / Service.
.

But I see the following problems with that:

If I use Automations (two per device, one for ON and one for OFF) I end up with a pretty long list of automations, some of which can and will be triggered every few seconds.
With the “Last triggered” Column in the UI constantly refreshing it becomes impossible to administer any of the Automations as the 3-dot menue keeps closing with every refresh. Plus it would still trigger automations multiple times while they are still running.

If I use Scripts instead, the “Last Triggered” issue would also apply, but I could at least check if the Script is still running before triggering it. Also I dont think that scripts are intended to be exectuted every couple of seconds and run for just some miliseconds before ending again (can anyone comment on that?). Also what the intended use case for Scripts vs. Automations is, still eludes me.

Using Services seems to be apealing, but I have not yet figured out if and how I can definde Custom Services in a templates.yaml
.

So how are you handling this?
What are your ideas on how to avoid the current problems?
Lets have a nice productive discussion, everybody can take something home from.

o/ Zarox