Stop
The Stop building block stops the automation or script. The next steps don’t run. Use it to end a run early, for example, inside an If-then when something is not as expected. A script can also use it to return a response.
Using this building block from the user interface
- Go to Settings > Automations & scenes and open an automation. To edit a script, open the Scripts tab and open the script.
- In the Then do section of an automation, or the Sequence section of a script, select Add action.
- Select Blocks, then select Stop.
- Optional: In Reason for stopping, enter why the run stops. The reason is shown in the logs and in the trace.
- Optional: To return a response from a script, in The name of the variable to use as response, enter the name of the variable that holds the response.
- Optional: To mark the run as failed, turn on Stop because of an unexpected error.
- Select Save.
Options in the UI
The variable that holds the response of the script. The variable must contain a mapping of keys and values. You can’t use it together with Stop because of an unexpected error.
Using this building block in YAML
In YAML, use stop with the reason:
stop: "Stop running the rest of the sequence"
To return a response from a script, add response_variable with the name of the variable that holds the response:
stop: "Stop running the rest of the sequence"
response_variable: "my_response_variable"
To mark the run as failed, add error: true:
stop: "Well, that was unexpected!"
error: true
Options in YAML
Why the run stops. Shown in the logs and in the trace. You must add stop, but you can leave the reason empty, for example, stop: "".
The name of the variable that holds the response of the script. The variable must contain a mapping of keys and values. You can’t combine it with error.
Good to know
- Stop ends the whole run, not only the current building block. Inside Run in parallel, the other actions that already started still finish.
- To only stop the steps that follow in the current sequence when something is not true, use a Condition building block instead.
- If the variable in The name of the variable to use as response doesn’t exist, the run stops with an error.
Examples
Script and automation: Return the outdoor temperature from a script
The script get_outdoor_temperature puts the outdoor temperature in a variable, and returns it with Stop. The automation calls the script every morning, receives the response in its own variable, and sends it in a notification.
To receive a response, the automation must call the script directly, as script.get_outdoor_temperature. The script.turn_on action doesn’t wait for the script and doesn’t return a response. For details, refer to Waiting for a script to complete.
-
Script: Get outdoor temperature
-
Action: Define variables
-
result: the outdoor temperature
-
-
Action: Stop
-
Reason for stopping:
Return the outdoor temperature -
The name of the variable to use as response:
result
-
Reason for stopping:
-
Action: Define variables
-
Automation: Morning temperature
-
Trigger: Time
-
At time:
07:00
-
At time:
-
Action: Get outdoor temperature (the script)
-
Response variable:
weather
-
Response variable:
-
Action: Send a notification message
-
Target: My Device (
notify.my_device) -
Message:
It's {{ weather.temperature }} °C outside.
-
Target: My Device (
-
Trigger: Time
YAML example
get_outdoor_temperature:
alias: "Get outdoor temperature"
sequence:
- variables:
result:
temperature: "{{ states('sensor.outdoor_temperature') }}"
- stop: "Return the outdoor temperature"
response_variable: result
alias: "Morning temperature"
triggers:
- trigger: time
at: "07:00:00"
actions:
- action: script.get_outdoor_temperature
response_variable: weather
- action: notify.send_message
target:
entity_id: notify.my_device
data:
message: "It's {{ weather.temperature }} °C outside."