Script Syntax
Scripts are a sequence of actionsActions are used in several places in Home Assistant. As part of a script or automation, actions define what is going to happen once a trigger is activated. In scripts, an action is called sequence. [Learn more] that Home Assistant will execute. Scripts are available as an entity through the standalone Script integration but can also be embedded in automationsAutomations in Home Assistant allow you to automatically respond to things that happen in and around your home. [Learn more] and Alexa/Amazon Echo configurations.
When the script is executed within an automationAutomations in Home Assistant allow you to automatically respond to things that happen in and around your home. [Learn more], the trigger
variable is available. See Available-Trigger-Data.
Script syntax
The script syntax basic structure is a list of key/value maps that contain actionsActions are used in several places in Home Assistant. As part of a script or automation, actions define what is going to happen once a trigger is activated. In scripts, an action is called sequence. [Learn more]. If a script contains only 1 actionActions are used in several places in Home Assistant. As part of a script or automation, actions define what is going to happen once a trigger is activated. In scripts, an action is called sequence. [Learn more], the wrapping list can be omitted.
All actionsActions are used in several places in Home Assistant. As part of a script or automation, actions define what is going to happen once a trigger is activated. In scripts, an action is called sequence. [Learn more] support an optional alias
.
- Script syntax
- Perform an action
- Variables
- Test a condition
- Wait for time to pass (delay)
- Wait
- Fire an event
- Repeat a group of actions
- If-then
- Choose a group of actions
- Grouping actions
- Parallelizing actions
- Stopping a script sequence
- Continuing on error
- Disabling an action
- Respond to a conversation
Perform an action
Performing an action can be done in various ways. For all the different possibilities, have a look at the actions page.
Activate a scene
Scripts may also use a shortcut syntax for activating scenesScenes capture the states you want certain entities to be. For example, a scene can specify that light A should be turned on and light B should be bright red. [Learn more] instead of calling the scene.turn_on
action.
Variables
The variables actionActions are used in several places in Home Assistant. As part of a script or automation, actions define what is going to happen once a trigger is activated. In scripts, an action is called sequence. [Learn more] allows you to set/override variables that will be accessible by templates in actionActions are used in several places in Home Assistant. As part of a script or automation, actions define what is going to happen once a trigger is activated. In scripts, an action is called sequence. [Learn more] after it. See also script variables for how to define variables accessible in the entire script.
Variables can be templated.
Scope of variables
Variables defined by the variables
actionActions are used in several places in Home Assistant. As part of a script or automation, actions define what is going to happen once a trigger is activated. In scripts, an action is called sequence. [Learn more] have local scope. This means that if a variable is changed in a nested sequence block, that change will not be visible in an outer sequence block.
Inside the if
sequence the variables
actionActions are used in several places in Home Assistant. As part of a script or automation, actions define what is going to happen once a trigger is activated. In scripts, an action is called sequence. [Learn more] will only alter the people
variable for that sequence.
Test a condition
While executing a script you can add a condition in the main sequence to stop further execution. When a condition does not return true
, the script will stop executing. For documentation on the many different conditions refer to the conditions page.
The condition
actionActions are used in several places in Home Assistant. As part of a script or automation, actions define what is going to happen once a trigger is activated. In scripts, an action is called sequence. [Learn more] only stops executing the current sequence block. When it is used inside a repeat action, only the current iteration of the repeat
loop will stop. When it is used inside a choose action, only the actionsActions are used in several places in Home Assistant. As part of a script or automation, actions define what is going to happen once a trigger is activated. In scripts, an action is called sequence. [Learn more] within that choose
will stop.
condition
can also be a list of conditions and execution will then only continue if ALL conditions return true
.
Wait for time to pass (delay)
Delays are useful for temporarily suspending your script and start it at a later moment. We support different syntaxes for a delay as shown below.
All forms accept templates.
Wait
These actionsActions are used in several places in Home Assistant. As part of a script or automation, actions define what is going to happen once a trigger is activated. In scripts, an action is called sequence. [Learn more] allow a script to wait for entities in the system to be in a certain state as specified by a template, or some event to happen as expressed by one or more triggers.
Wait for a template
This actionActions are used in several places in Home Assistant. As part of a script or automation, actions define what is going to happen once a trigger is activated. In scripts, an action is called sequence. [Learn more] evaluates the template, and if true, the script will continue. If not, then it will wait until it is true.
The template is re-evaluated whenever an entity ID that it references changes state. If you use non-deterministic functions like now()
in the template it will not be continuously re-evaluated, but only when an entity ID that is referenced is changed. If you need to periodically re-evaluate the template, reference a sensor from the Time and Date integration that will update minutely or daily.
Wait for a trigger
This actionActions are used in several places in Home Assistant. As part of a script or automation, actions define what is going to happen once a trigger is activated. In scripts, an action is called sequence. [Learn more] can use the same triggers that are available in an automation’s trigger
section. See Automation Trigger. The script will continue whenever any of the triggers fires. All previously defined trigger variables, variables and script variables are passed to the trigger.
Wait timeout
With both types of waits it is possible to set a timeout after which the script will continue its execution if the condition/event is not satisfied. Timeout has the same syntax as delay
, and like delay
, also accepts templates.
You can also get the script to abort after the timeout by using optional continue_on_timeout: false
.
Without continue_on_timeout: false
the script will always continue since the default for continue_on_timeout
is true
.
Wait variable
After each time a wait completes, either because the condition was met, the event happened, or the timeout expired, the variable wait
will be created/updated to indicate the result.
Variable | Description |
---|---|
wait.completed |
true if the condition was met, false otherwise |
wait.remaining |
Timeout remaining, or none if a timeout was not specified |
wait.trigger |
Exists only after wait_for_trigger . Contains information about which trigger fired. (See Available-Trigger-Data.) Will be none if no trigger happened before timeout expired |
This can be used to take different actions based on whether or not the condition was met, or to use more than one wait sequentially while implementing a single timeout overall.
Fire an event
This actionActions are used in several places in Home Assistant. As part of a script or automation, actions define what is going to happen once a trigger is activated. In scripts, an action is called sequence. [Learn more] allows you to fire an event. Events can be used for many things. It could trigger an automationAutomations in Home Assistant allow you to automatically respond to things that happen in and around your home. [Learn more] or indicate to another integration that something is happening. For instance, in the below example it is used to create an entry in the logbook.
You can also use event_data to fire an event with custom data. This could be used to pass data to another script awaiting an event trigger.
The event_data
accepts templates.
Raise and Consume Custom Events
The following automationAutomations in Home Assistant allow you to automatically respond to things that happen in and around your home. [Learn more] example shows how to raise a custom event called event_light_state_changed
with entity_id
as the event data. The actionActions are used in several places in Home Assistant. As part of a script or automation, actions define what is going to happen once a trigger is activated. In scripts, an action is called sequence. [Learn more] part could be inside a script or an automationAutomations in Home Assistant allow you to automatically respond to things that happen in and around your home. [Learn more].
The following automationAutomations in Home Assistant allow you to automatically respond to things that happen in and around your home. [Learn more] example shows how to capture the custom event event_light_state_changed
with an Event Automation Trigger, and retrieve corresponding entity_id
that was passed as the event trigger data, see Available-Trigger-Data for more details.
Repeat a group of actions
This actionActions are used in several places in Home Assistant. As part of a script or automation, actions define what is going to happen once a trigger is activated. In scripts, an action is called sequence. [Learn more] allows you to repeat a sequence of other actionsActions are used in several places in Home Assistant. As part of a script or automation, actions define what is going to happen once a trigger is activated. In scripts, an action is called sequence. [Learn more]. Nesting is fully supported. There are three ways to control how many times the sequence will be run.
Counted repeat
This form accepts a count value. The value may be specified by a template, in which case the template is rendered when the repeat step is reached.
For each
This repeat form accepts a list of items to iterate over. The list of items can be a pre-defined list, or a list created by a template.
The sequence is ran for each item in the list, and current item in the
iteration is available as repeat.item
.
The following example will turn a list of lights:
Other types are accepted as list items, for example, each item can be a template, or even an mapping of key/value pairs.
While loop
This form accepts a list of conditions (see conditions page for available options) that are evaluated before each time the sequence is run. The sequence will be run as long as the condition(s) evaluate to true.
The while
also accepts a shorthand notation of a template condition.
For example:
Repeat until
This form accepts a list of conditions that are evaluated after each time the sequence is run. Therefore the sequence will always run at least once. The sequence will be run until the condition(s) evaluate to true.
until
also accepts a shorthand notation of a template condition.
For example:
Repeat loop variable
A variable named repeat
is defined within the repeat actionActions are used in several places in Home Assistant. As part of a script or automation, actions define what is going to happen once a trigger is activated. In scripts, an action is called sequence. [Learn more] (i.e., it is available inside sequence
, while
& until
.)
It contains the following fields:
field | description |
---|---|
first |
True during the first iteration of the repeat sequence |
index |
The iteration number of the loop: 1, 2, 3, … |
last |
True during the last iteration of the repeat sequence, which is only valid for counted loops |
If-then
This actionActions are used in several places in Home Assistant. As part of a script or automation, actions define what is going to happen once a trigger is activated. In scripts, an action is called sequence. [Learn more] allows you to conditionally (if
), based on or more conditions (which are and
combined),
run a sequence of actions (then
) and optionally supports running other sequence when the condition didn’t pass (else
).
This actionActions are used in several places in Home Assistant. As part of a script or automation, actions define what is going to happen once a trigger is activated. In scripts, an action is called sequence. [Learn more] supports nesting, however, if you find yourself using nested if-then
actions in the else
part, you may want to consider using
choose instead.
Choose a group of actions
This actionActions are used in several places in Home Assistant. As part of a script or automation, actions define what is going to happen once a trigger is activated. In scripts, an action is called sequence. [Learn more] allows you to select a sequence of other actionsActions are used in several places in Home Assistant. As part of a script or automation, actions define what is going to happen once a trigger is activated. In scripts, an action is called sequence. [Learn more] from a list of sequences. Nesting is fully supported.
Each sequence is paired with a list of conditions. (See the conditions page for available options and how multiple conditions are handled.) The first sequence whose conditions are all true will be run.
An optional default
sequence can be included which will be run only if none of the sequences from the list are run.
An optional alias
can be added to each of the sequences, excluding the default
sequence.
The choose
actionActions are used in several places in Home Assistant. As part of a script or automation, actions define what is going to happen once a trigger is activated. In scripts, an action is called sequence. [Learn more] can be used like an “if/then/elseif/then…/else” statement. The first conditions
/sequence
pair is like the “if/then”, and can be used just by itself. Or additional pairs can be added, each of which is like an “elif/then”. And lastly, a default
can be added, which would be like the “else.”
conditions
also accepts a shorthand notation of a template condition.
For example:
More choose
can be used together. This is the case of an IF-IF.
The following example shows how a single automationAutomations in Home Assistant allow you to automatically respond to things that happen in and around your home. [Learn more] can control entities that aren’t related to each other but have in common the same trigger.
When the sun goes below the horizon, the porch
and garden
lights must turn on. If someone is watching the TV in the living room, there is a high chance that someone is in that room, therefore the living room lights have to turn on too. The same concept applies to the studio
room.
Grouping actions
The sequence
actionActions are used in several places in Home Assistant. As part of a script or automation, actions define what is going to happen once a trigger is activated. In scripts, an action is called sequence. [Learn more] allows you to group multiple actionsActions are used in several places in Home Assistant. As part of a script or automation, actions define what is going to happen once a trigger is activated. In scripts, an action is called sequence. [Learn more]
together. Each action will be executed in order, meaning the next action will
only be executed after the previous action has been completed.
Grouping actions in a sequence can be useful when you want to be able to collapse related groups in the user interface for organizational purposes.
Combined with the parallel
action, it can also be
used to run multiple groups of actions in a sequence in parallel.
In the example below, two separate groups of actions are executed in sequence, one for turning on devices, the other for sending notifications. Each group of actions is executed in order, this includes the actions in each group and the groups themselves. In total, four actions are executed, one after the other.
Parallelizing actions
By default, all sequences of actionsActions are used in several places in Home Assistant. As part of a script or automation, actions define what is going to happen once a trigger is activated. In scripts, an action is called sequence. [Learn more] in Home Assistant run sequentially. This means the next actionActions are used in several places in Home Assistant. As part of a script or automation, actions define what is going to happen once a trigger is activated. In scripts, an action is called sequence. [Learn more] is started after the current action has been completed.
This is not always needed, for example, if the sequence of actions doesn’t rely
on each other and order doesn’t matter. For those cases, the parallel
action
can be used to run the actionsActions are used in several places in Home Assistant. As part of a script or automation, actions define what is going to happen once a trigger is activated. In scripts, an action is called sequence. [Learn more] in the sequence in parallel, meaning all
the actionsActions are used in several places in Home Assistant. As part of a script or automation, actions define what is going to happen once a trigger is activated. In scripts, an action is called sequence. [Learn more] are started at the same time.
The following example shows sending messages out at the same time (in parallel):
It is also possible to run a group of actions sequentially inside the parallel actions. The example below demonstrates that:
Running actionsActions are used in several places in Home Assistant. As part of a script or automation, actions define what is going to happen once a trigger is activated. In scripts, an action is called sequence. [Learn more] in parallel can be helpful in many cases, but use it with caution and only if you need it.
There are some caveats (see below) when using parallel actions.
While it sounds attractive to parallelize, most of the time, just the regular sequential actionsActions are used in several places in Home Assistant. As part of a script or automation, actions define what is going to happen once a trigger is activated. In scripts, an action is called sequence. [Learn more] will work just fine.
Some of the caveats of running actionsActions are used in several places in Home Assistant. As part of a script or automation, actions define what is going to happen once a trigger is activated. In scripts, an action is called sequence. [Learn more] in parallel:
- There is no order guarantee. The actionsActions are used in several places in Home Assistant. As part of a script or automation, actions define what is going to happen once a trigger is activated. In scripts, an action is called sequence. [Learn more] will be started in parallel, but there is no guarantee that they will be completed in the same order.
- If one actionActions are used in several places in Home Assistant. As part of a script or automation, actions define what is going to happen once a trigger is activated. In scripts, an action is called sequence. [Learn more] fails or errors, the other actionsActions are used in several places in Home Assistant. As part of a script or automation, actions define what is going to happen once a trigger is activated. In scripts, an action is called sequence. [Learn more] will keep running until they too have finished or errored.
- Variables created/modified in one parallelized actionActions are used in several places in Home Assistant. As part of a script or automation, actions define what is going to happen once a trigger is activated. In scripts, an action is called sequence. [Learn more] are not available in another parallelized actionActions are used in several places in Home Assistant. As part of a script or automation, actions define what is going to happen once a trigger is activated. In scripts, an action is called sequence. [Learn more]. Each step in a parallelized has its own scope.
- The response data of a parallelized actionActions are used in several places in Home Assistant. As part of a script or automation, actions define what is going to happen once a trigger is activated. In scripts, an action is called sequence. [Learn more] is however also available outside of its own scope. This is especially useful for parallelizing execution of long-running actionsActions are used in several places in Home Assistant. As part of a script or automation, actions define what is going to happen once a trigger is activated. In scripts, an action is called sequence. [Learn more].
Stopping a script sequence
It is possible to halt a script sequence at any point and return script responses
using the stop
actionActions are used in several places in Home Assistant. As part of a script or automation, actions define what is going to happen once a trigger is activated. In scripts, an action is called sequence. [Learn more].
The stop
actionActions are used in several places in Home Assistant. As part of a script or automation, actions define what is going to happen once a trigger is activated. In scripts, an action is called sequence. [Learn more] takes a text as input explaining the reason for halting the
sequence. This text will be logged and shows up in the automationsAutomations in Home Assistant allow you to automatically respond to things that happen in and around your home. [Learn more] and
script traces.
stop
can be useful to halt a script halfway through a sequence when,
for example, a condition is not met.
To return a response from a script, use the response_variable
option. This
option expects the name of the variable that contains the data to return. The
response data must contains a mapping of key/value pairs.
There is also an error
option, to indicate we are stopping because of
an unexpected error. It stops the sequence as well, but marks the automationAutomations in Home Assistant allow you to automatically respond to things that happen in and around your home. [Learn more]
or script as failed to run.
Continuing on error
By default, a sequence of actionsActions are used in several places in Home Assistant. As part of a script or automation, actions define what is going to happen once a trigger is activated. In scripts, an action is called sequence. [Learn more] will be halted when one of the actionsActions are used in several places in Home Assistant. As part of a script or automation, actions define what is going to happen once a trigger is activated. In scripts, an action is called sequence. [Learn more] in that sequence encounters an error. The automationAutomations in Home Assistant allow you to automatically respond to things that happen in and around your home. [Learn more] or script will be halted, an error is logged, and the automationAutomations in Home Assistant allow you to automatically respond to things that happen in and around your home. [Learn more] or script run is marked as errored.
Sometimes these errors are expected, for example, because you know the action
you perform can be problematic at times, and it doesn’t matter if it fails.
You can set continue_on_error
for those cases on such an actionActions are used in several places in Home Assistant. As part of a script or automation, actions define what is going to happen once a trigger is activated. In scripts, an action is called sequence. [Learn more].
The continue_on_error
is available on all actionsActions are used in several places in Home Assistant. As part of a script or automation, actions define what is going to happen once a trigger is activated. In scripts, an action is called sequence. [Learn more] and is set to
false
. You can set it to true
if you’d like to continue the actionActions are used in several places in Home Assistant. As part of a script or automation, actions define what is going to happen once a trigger is activated. In scripts, an action is called sequence. [Learn more]
sequence, regardless of whether that actionActions are used in several places in Home Assistant. As part of a script or automation, actions define what is going to happen once a trigger is activated. In scripts, an action is called sequence. [Learn more] encounters an error.
The example below shows the continue_on_error
set on the first actionActions are used in several places in Home Assistant. As part of a script or automation, actions define what is going to happen once a trigger is activated. In scripts, an action is called sequence. [Learn more]. If
it encounters an error; it will continue to the next actionActions are used in several places in Home Assistant. As part of a script or automation, actions define what is going to happen once a trigger is activated. In scripts, an action is called sequence. [Learn more].
Please note that continue_on_error
will not suppress/ignore misconfiguration
or errors that Home Assistant does not handle.
Disabling an action
Every individual actionActions are used in several places in Home Assistant. As part of a script or automation, actions define what is going to happen once a trigger is activated. In scripts, an action is called sequence. [Learn more] in a sequence can be disabled, without removing it.
To do so, add enabled: false
to the actionActions are used in several places in Home Assistant. As part of a script or automation, actions define what is going to happen once a trigger is activated. In scripts, an action is called sequence. [Learn more]. For example:
Actions can also be disabled based on limited templates or blueprint inputs.
Respond to a conversation
The set_conversation_response
script actionActions are used in several places in Home Assistant. As part of a script or automation, actions define what is going to happen once a trigger is activated. In scripts, an action is called sequence. [Learn more] allows returning a custom response
when an automationAutomations in Home Assistant allow you to automatically respond to things that happen in and around your home. [Learn more] is triggered by a conversation engine, for example a voice
assistant. The conversation response can be templated.
The response is handed to the conversation engine when the automationAutomations in Home Assistant allow you to automatically respond to things that happen in and around your home. [Learn more] finishes. If
the set_conversation_response
is executed multiple times, the most recent
response will be handed to the conversation engine. To clear the response, set it
to None
:
If the automationAutomations in Home Assistant allow you to automatically respond to things that happen in and around your home. [Learn more] was not triggered by a conversation engine, the response will not be used by anything.