Mutex or resource lock in automations?

Is there any kind of mutex or resource lock that I can use in automations?

I have several automations that broadcast announcements via TTS to my google home devices. However if multiple announcements coincide, then one of them will be the last one to cast, and that’s the only one that gets heard. I want to have some reliable method of being able to tell when an announcement is playing, and have the other automations wait until the resources are free before starting their own announcements.

I’ve tried just setting and checking an input_boolean, and things are slightly better, but if two announcements are triggered at the same time, they can both turn on the input_boolean, and think that they are both the exclusive users of the playing devices.

Is there a real solution to this problem?

Same question here. As @SteveDinn alluded to, an input_boolean could be read by two or more automations in parallel and, when this happens, both read the value (zero/off say) that indicates that the needed resource (Google Home device/Alexa device/etc.) is available. Both then proceed to set the input_boolean value to one/on and execute the code that the addition of the input_boolean was intended to serialize.

The best way to handle this is with

mode: queued

Yes, you’re right, thanks Steve. So long as the resource (Google Home/Alexa/etc.) for which access needs to be serialized is only used by one script (or by one automation), using queued mode will mean that an invocation of the script that occurs while the script is already running will wait until that invocation finishes and then jump in and run. I updated my script a few days ago and now I do not lose audio audio announcements that previously got lost because they were on top of one another.

So, this thread is relatively old but I felt like I had to share my solution here… I indeed could not find a way to lock resources using Home Assistant alone, but I did achieve some sort of mutex by using the native flock command. If the platform of the automation that needs to lock is a command_line, then it’s relatively straight forward:

  platform: command_line
        friendly_name: "Water Heater ON/OFF"
        unique_id: switch_water_heater
        command_state: flock /dev/ttyUSB0 -c "echo 'PIN4' > /dev/ttyUSB0 ; sleep 0.5"
        command_on: flock /dev/ttyUSB0 -c "echo 'PIN4=ON' > /dev/ttyUSB0 ; sleep 0.5"
        command_off: flock /dev/ttyUSB0 -c "echo 'PIN4=4.5' > /dev/ttyUSB0 ; sleep 0.5"

The sleep 0.5 is probably overkill, and maybe useless for you, but my device reply was too fast for Home Assistant to process it in time…