Understanding automations
All automationsAutomations in Home Assistant allow you to automatically respond to things that happen in and around your home. [Learn more] are made of at least a triggerA trigger is the part of an automation that makes the automation start when something specific happens, for example, when the sun sets, a door opens, or a button is pressed. [Learn more] and an actionAn action makes Home Assistant do something, for example, turn on a light, send a notification, or activate a scene. Automations and scripts run a list of actions. [Learn more]. Optionally combined with a conditionConditions check whether something is true at that moment, for example, whether a door is closed. You can use them in automations and scripts. If a condition is not met, the actions that come after it in the same sequence do not run. At the top level of an automation or script, this stops the automation or script. [Learn more]. Take for example the automation:
When Paulus enters home and it is after sunset, turn on the lights in the living room.
Parts of an automation
The previous automation example has the following three parts:
(trigger part) When Paulus enters home
(condition part) and it is after sunset
(action part) turn on the lights in the living room
Trigger part
The trigger belongs to the first part of the automation. When the trigger part is verified, the automation starts.
Condition part
The second part of an automation has the condition. If the condition is verified, the action part takes place. In the example of the automation above, the lights in the living room will turn on only if the sun has set. If Paulus comes home before sunset, nothing happens when the sun sets later. For why, and how to cover both cases, refer to how automations react to changes.
Action part
The last part of an automation has the action. The action part will be performed only if the trigger and condition parts are met. Some examples of actions are: turn on a light, set the temperature on a thermostat or activate a scene.
Creating automations
Now that you’ve got a sneak peek of what is possible, it’s time to get your feet wet and create your first automation.
By default, create automations with the visual editor, one of the available automation editors. You can create an automation in the visual editor of the UI by following the steps below.
- Go to Settings > Automations & scenes.
- In the lower right corner, select Create automation > Create new automation.
Adding a trigger
- In the When section, select Add trigger.
- Now you can do one of the following:
- Select a target and then a trigger.
- Under By target, select the entity, device or service to which you want to apply the trigger to. You can find the available entities, devices or services listed by:
- Area of your home, in the Home section.
- Entities, Devices and Services, in the Unassigned section.
- Labels that you have previously created, in the Labels section.
- For that target, you can see the available triggers on the right side. Select + on the desired one.
- Under By target, select the entity, device or service to which you want to apply the trigger to. You can find the available entities, devices or services listed by:
- Select a trigger.
- Select By type, and then select the trigger type from the list on the left.
- From the listed triggers, select + on the one you choose.
- Search for a trigger or target and then select a trigger.
- Enter the name of one of the following in the search box:
- A target: an entity, a device or a service.
- A group of targets: an area or label.
- A trigger.
- From the listed results, select + on the desired trigger.
- Enter the name of one of the following in the search box:
- Select a target and then a trigger.
- In the window on the right, you will have different options depending on the selected trigger. You might find the following ones, for example:
- Under Targets, you can select the first target or another one by selecting Add target.
- Do one of the following:
- Select an entity, device or service to monitor a specific one.
- Select entities, devices or services in an area, floor or with a certain label to monitor a group of them.
- You can add more targets by selecting Add target again.
- Do one of the following:
- Under Trigger when, if you monitor more than one target, you can decide when the automation starts:
- Each: Every time one of the targets makes the change that the trigger reacts to, for example, every time one of the lights turns on.
- First: The first time one of the targets makes that change. The trigger reacts again only after all targets have changed back.
- All: Once all targets have made that change, for example, when the last of the lights turns on.
- Under Targets, you can select the first target or another one by selecting Add target.
- Select Save.
Adding a condition
Note that you don´t need to add a condition to create an automation.
- In the And if section, select Add condition.
- Under Blocks, you have the following options:
- If you want to make sure that a condition is not verified for the automation to run, select + on the Not block.
- If you will add more than one condition, you can select:
- the And block to make sure all conditions are verified for the automation to run.
- the Or block to make sure at least one of the conditions is verified for the automation to run.
- Select Add condition again and now you can:
- Select a target and then a condition.
- Under By target, select the entity, device or service to which you want to apply the condition to. You can find the available entities, devices or services listed by:
- Area of your home, in the Home section.
- Entities, Devices and Services, in the Unassigned section.
- Labels that you have previously created, in the Labels section.
- For that target, you can see the available conditions on the right side. Select + on the desired one.
- Under By target, select the entity, device or service to which you want to apply the condition to. You can find the available entities, devices or services listed by:
- Select a condition.
- Select By type, and then select the condition type from the list on the left.
- From the listed conditions, select + on the one you choose.
- Search for a condition or target and then select a condition.
- Enter the name of one of the following in the search box:
- A target: an entity, a device or a service.
- A group of targets: an area or label.
- A condition.
- From the listed results, select + on the desired condition.
- Enter the name of one of the following in the search box:
- Select a target and then a condition.
- In the window on the right, you will have different options depending on the selected condition. Under Targets, you can add the first target or another one by selecting Add target.
- Do one of the following:
- Select an entity, device or service to monitor a specific one.
- Select entities, devices or services in an area, floor or with a certain label to monitor a group of them.
- You can add another target by selecting Add target again.
- Do one of the following:
- Select Save.
Adding an action
- In the Then do section, select Add action.
- Under Blocks, you have many options that allow you, for example, to:
- Set up conditions for specific actions.
- Define the way a sequence of actions is performed.
- Wait for a trigger or a template to run a sequence of actions.
- Now you can do one of the following:
- Select a target and then an action.
- Under By target, select the entity, device or service to which you want to apply the action to. You can find the available entities, devices or services listed by:
- Area of your home, in the Home section.
- Entities, Devices and Services, in the Unassigned section.
- Labels that you have previously created, in the Labels section.
- For that target, you can see the available actions on the right side. Select + on the desired one.
- Under By target, select the entity, device or service to which you want to apply the action to. You can find the available entities, devices or services listed by:
- Select an action.
- Select By type, and then select the action type from the list on the left.
- From the listed actions, select + on the one you choose.
- Search for an action or target and then select an action.
- Enter the name of one of the following in the search box:
- A target: an entity, a device or a service.
- A group of targets: an area or label.
- An action.
- From the listed results, select + on the desired action.
- Enter the name of one of the following in the search box:
- Select a target and then an action.
- In the window on the right, you will have different options depending on the selected action. Under Targets, you can add the first target or another one by selecting Add target.
- Do one of the following:
- Select an entity, device or service to monitor a specific one.
- Select entities, devices or services in an area, floor or with a certain label to monitor a group of them.
- You can add another target by selecting Add target again.
- Do one of the following:
- Select Save.
Automations as entities
Each automation is also an entityAn entity represents a sensor, actor, or function in Home Assistant. Entities are used to monitor physical properties or to control other entities. An entity is usually part of a device or a service. [Learn more], with an entity ID such as automation.turn_on_porch_light. Automation entities are listed with all other entities under Settings > Devices & services > Entities. Like other entities, you can show an automation entity on a dashboard, or use it in other automations and scriptsA script is a saved list of steps that runs when you start it, for example, from a dashboard, with Assist, or from an automation. Unlike an automation, a script has no triggers, so it doesn’t start by itself. [Learn more].
Automation states
The stateThe state holds the information of interest of an entity, for example, if a light is on or off. Each entity has exactly one state and the state only holds one value at a time. However, entities can store attributes related to that state such as brightness, color, or a unit of measurement. [Learn more] shows whether the automation is enabled. It doesn’t show whether the automation is running right now. Each item shows the label you see in the Home Assistant interface, followed by the state as Home Assistant stores it. If you write templatesA template is an automation definition that can include variables for the action or data from the trigger values. This allows automations to generate dynamic actions. [Learn more] or edit automations in YAMLYAML is a human-readable data serialization language. It is used to store and transmit data in a structured format. In Home Assistant, YAML is used for configuration, for example in the configuration.yaml or automations.yaml files. [Learn more], use the stored state.
-
On (
on): The automation is enabled. It reacts to its triggersA trigger is the part of an automation that makes the automation start when something specific happens, for example, when the sun sets, a door opens, or a button is pressed. [Learn more]. -
Off (
off): The automation is disabled. It ignores its triggers. -
Unavailable (
unavailable): The configuration of the automation isn’t valid, so Home Assistant can’t set it up. For details, refer to Automation is unavailable after an update.
When Home Assistant restarts, each automation keeps its on or off state, unless it has an initial state set in YAML.
Automation attributes
The attributes tell you more about an automation, such as when it last ran, or whether it’s running right now. You can use them to check an automation without opening it, or to make another automation depend on it.
You can see and use the attributes in different places:
- In the entities list or on a dashboard, select the automation entity, and then select Menu
> Details. - In an automation or a script, select an attribute in the Attribute field of a State trigger or condition.
- In templates, use
state_attr(). For example,state_attr('automation.turn_on_porch_light', 'last_triggered').
Automations have the following automation-specific attributes. Max running automations and ID are only present in some cases, as described in the list. If an automation is unavailable, ID is the only automation-specific attribute it can have.
-
Last triggered (
last_triggered): When the automation was last triggered after its conditionsConditions check whether something is true at that moment, for example, whether a door is closed. You can use them in automations and scripts. If a condition is not met, the actions that come after it in the same sequence do not run. At the top level of an automation or script, this stops the automation or script. [Learn more] were either met or skipped. If the conditions stopped the automation, or the run mode didn’t allow another run, this time doesn’t change.- Use it to check when an automation was last triggered, or to keep another automation from reacting again too soon.
-
Running automations (
current): How many runs of the automation there are right now. In Queued mode, this includes the runs that are waiting in the queue. Usually, this is0.- Use it to wait until an automation has finished, or to skip something while it’s still running.
-
Run mode (
mode): What happens when a trigger reacts while the automation is still running: Single, Restart, Queued, or Parallel. For details, refer to Automation modes. -
Max running automations (
max): How many runs there can be at the same time, including runs that are waiting. Only automations in Queued or Parallel mode have this attribute.- When Running automations reaches this number, the next time a trigger reacts, no new run is added. For details, refer to I see an “Already running” warning in the logs.
-
ID (
id): The unique ID of the automation. It connects the entity to the configuration of the automation. Automations that you create in the editor always have one. Automations in YAML only have one if you setid.
Actions for automations
To control an automation from another automation or a script, use these actionsAn action makes Home Assistant do something, for example, turn on a light, send a notification, or activate a scene. Automations and scripts run a list of actions. [Learn more]:
- Turn on automation: Sets the state to On.
- Turn off automation: Sets the state to Off. By default, it also stops the actions that are running.
- Toggle automation: Switches the state between On and Off.
- Trigger automation: Runs the actions right away, without waiting for a trigger. By default, it skips the conditions.