Running Scripts
Sync vs Async
When an automation calls a script in Home Assistant, it runs synchronously by default. That means the automation waits for the script to finish before completing itself. This has big implications for how long automations appear to be “running”, and how mode handling behaves.
What “Synchronous” Really Means
action:
- service: script.do_something_long
This is the same as if you had copy-pasted all of script.do_something_long directly into your automation. It becomes part of the automation’s own execution path.
What actually happens under the hood
- The automation triggers
- It calls the script
- It waits for the script to finish (including delays, waits, etc.)
- Only then does the automation return to “idle”
This is why long-running scripts make your automation appear “running” for longer.
Benefits of running scripts synchronously
-
Mode handling works properly
Example: if the automation uses mode: single, it won’t trigger again while the script is still running. -
Traceability
The full execution time shows in the automation trace, even when the logic is in the script.
Want to Run a Script Asynchronously?
You can bypass the sync behavior using script.turn_on. This tells Home Assistant: “Run this script, but don’t wait for it to finish.”
action:
- service: script.turn_on
target:
entity_id: script.do_something_long
What this changes
- The automation exits immediately
- The script runs independently in the background
- mode: handling in the script is ignored — so be careful with race conditions
Rule of Thumb
Use this | When you want |
---|---|
script.do_something | Full control over execution order and mode handling |
script.turn_on | Fire-and-forget behavior; let the automation move on |
If you’re working on modularizing your automations, this is a key piece of how execution flow works in Home Assistant.
- Mentionné dans
- Home Assistant