Final solution:
- Acquiring a lock is the only action that is serialized via a script
- Releasing the lock is done by writing an empty value to the input_text helper
The script looks like this
alias: lock1
sequence:
- wait_template: "{{ is_state('input_text.lock1_owner', '') }}"
continue_on_timeout: true
alias: Wait for lock to be available
- service: input_text.set_value
data:
value: "{{ owner }}"
target:
entity_id: input_text.lock1_owner
alias: Set lock owner
mode: queued
fields:
owner:
selector:
text: null
name: owner
description: Name of the owner
required: true
You will need 1 script and 1 variable per mutex.
Example automations that use the mutex:
alias: Exclusive 1
description: ""
trigger:
- platform: event
event_type: ""
condition: []
action:
- variables:
lock_name: exclusive1
- service: script.lock1
data:
owner: "{{ lock_name }}"
alias: Request lock
- delay:
hours: 0
minutes: 0
seconds: 10
milliseconds: 0
- alias: Release lock
service: input_text.set_value
data:
value: ""
target:
entity_id: input_text.lock1_owner
mode: single
And another one that contests the same lock
alias: Exclusive 2
description: ""
trigger:
- platform: event
event_type: ""
condition: []
action:
- variables:
lock_name: exclusive2
- service: script.lock1
data:
owner: "{{ lock_name }}"
alias: Request lock
- delay:
hours: 0
minutes: 0
seconds: 10
milliseconds: 0
- alias: Release lock
service: input_text.set_value
data:
value: ""
target:
entity_id: input_text.lock1_owner
mode: single