How to create a function shared between different automations?

Thanks Phil.

This sounds like it will do what I want. I will look out for your release.

To clarify, if I wrote a script and used “mode: parallel”, then called it several times with different arguments would they run in parallel because each script is uniquifed by its arguments?
(I guess the answer is yes otherwise your automation example would be limited and not handle multiple re-triggers under the script execution time).

If they could not run together, a work around would be to queue. But it would not be quite as nice because all the shades would come down in sequence rather than together.

There’s actually another dimension to this that I didn’t mention, and that’s how the “sequence” (i.e., script or automation action) is “called.” That can either be blocking or background.

Automations will always call their action sequence non-blocking (i.e., “background.”) So in that case, if you configure the action sequence to be parallel, then yes, all runs, from all trigger firing events, will run in parallel, so there could be many, many runs happening concurrently, each with its own unique trigger variable.

For scripts, you will be able to control how it calls other scripts via a new parameter to the script.turn_on service. The new parameter will be background, and its default will be false (which also applies if you call the script directly by its name, such as script.abc instead of via the script.turn_on service.) So, by default, when one script calls another, it will wait for it to finish before moving on to the next step. So, even if the called script is configured in parallel mode, it won’t actually run “in parallel” if called twice from another script using the default blocking (i.e., background false) call mode. But, if one script calls another script with “background: true”, then it is possible for one script to call another script multiple times where the called script will run multiple times concurrently. Or it’s also possible for a script to get called from multiple places, and that, too, can lead to multiple parallel runs.

So, in summary, there are two dimensions.

The first is, when one script calls another script, you can decide whether it should do that call in a blocking fashion, or if the called script should be run in the background (and hence the calling script will almost immediately go onto its next step without waiting for the called script to finish.) And for automations, there is only background call mode for its action sequence.

The second is the sequence’s mode, which controls what it should do if invoked when a previous invocation is still running. As I mentioned before there will be several options. The complete list will be: ignore, error, restart, parallel, queue, oh, and legacy to mimic the current behavior.

BTW, don’t hold your breath. Getting this working has been more involved than originally envisioned. There’s quite a bit of complexity to how scripts used to work and how they interacted with the rest of the system. I’m running into the classic case of pull a thread here and something over there comes unraveled. I would not be surprised if it still takes a few more weeks, at least, to work out all the details.

OK, I’ll keep breathing in the meantime :slight_smile:
I’d love to help test it but in a couple of weeks I’ll be moving to Ireland and running the home automation “by remote” for tenants. So I’ll only be doing very conservative upgrades.
So for now I’ll roll things out and make 5 almost identical scripts.

Thanks for all your effort making HA so awesome.

1 Like