Can two automations call into the same script? Is re-entrancy or even recursion allowed?

I have hit an issue with a utility script that I built that handles tts notifications for a Sonos. The code itself is boring - I have several sonos_say scripts that call a central script that sends the tts notification.

While my system is working, I noticed that some speech notifications are happening (I know the Sonos is screwy for this.) I see “script foo already running” warnings in my log.

From doing a little research it seems that recursion has been a problem for Homeassistant and it is actively prevented. Is that correct? I haven’t seen this limitation in the documentation and it seems bizarre/probably a hack to me.

Is it possible to have two automations invoke the same script at the same time and not fail? Recursion has many uses re-entrant scripts are incredibly useful for all sorts of reasons. Am I misunderstanding something?

A script can only be run one instance at a time.

Thanks,

Is there a reason for this? Some documentation that explains it? It seems to me that any kind of utility script would be useless because of this.

What am I missing?

Am I misunderstanding the use of the word “instance” - I assume that the script is common in memory code that other scripts call into. Is that not the case. Are scripts embedded only in the code for a specific automation and this only truly affects recursion?

Did a search for “script already running” and found this (fragile) workaround:

It’s for a looping script so the kluge might not be useful for your case.

FWIW, I use another home automation system and it has a (user-adjustable) recursion limit (default is something like 60). When it hits the ceiling, it halts execution and logs an error. This functionality requires additional overhead so I imagine, for simplicity’s sake, Home Assistant’s designers disallowed it.

Interesting. Thanks for the information. I guess this is why things like Node-Red and AppDaemon exist.

/Dedd

1 Like