Automations in Home Assistant
Automations are the cornerstone of Home Assistant, allowing you to connect individual devices and define actions that should be performed based on specific conditions and triggers.
Basic Building Blocks of Automation
-
When - This part defines the trigger that activates the automation. It is possible to define one or more triggers. Each trigger can optionally have a unique ID defined, which can then be used later in the "what to do" section.
-
And if (optional) - Here you can define conditions that must be met for the automation to be triggered. In the case of multiple conditions, all must usually be met, which can be compared to a logical "and". However, we can use so-called building blocks to create more complex logical structures, such as a logical "or", which allows the automation to be triggered if at least one condition is met.
Global Automation ConditionsThese conditions can be thought of as global conditions, i.e., they will apply to all actions listed below.
-
Then do - This is a list of actions that will be performed if the automation is triggered and the conditions are met.
Simple Automation Example
Imagine two physical devices - e.g., a Zigbee button and a WiFi bulb. Thanks to Home Assistant, we can connect these seemingly different technologies.
Task
We want to turn on the bulb using the button, but only if the sun has already set.
Solution
In our simple example, we will only use the basic building blocks of automation, which we will set up as follows:
-
When - the trigger will be pressing the Zigbee button.
-
And if (optional) - the limiting condition will be the state of the sun after sunset.
-
Then do - and finally, the action will be to turn on the WiFi light.
Automation Scheme
For a better understanding, we can use an automation scheme, where we also see the state that we do not need to define - what happens if the condition is not met.
Types of Conditions
Numeric or Text Conditions
Another option is conditions where the state corresponds to a certain numeric or text value.
-
Numeric State - tests whether the numeric state of a certain entity is greater/less than the specified value. For example, whether the temperature is higher than 20 degrees.
-
Trigger ID - tests whether the automation was triggered by one of the listed triggers defined by ID.
-
State or Device - this condition checks the current state of the entity or device at the time the automation is triggered. We distinguish between two types:
- Entity State: Tests a specific attribute or value of an individual entity (e.g., whether the light is on).
- Device State: Checks the overall state of the device, which may have multiple entities (e.g., whether the thermostat is in heating mode).
Time and Place
-
Time - tests whether the automation is triggered before/after a certain time, which can be defined in the format HH:MM:SS (hour:minute:second) or using a helper.
-
Sun - tests whether the automation is triggered before/after sunrise/sunset. It is also possible to define a time offset from this moment.
-
Zone - tests whether an entity with a location (typically a person) has entered a certain area - zone.
Logical Conditions
Building blocks for more complex conditions when we want to combine several together.
-
And (intersection or logical conjunction) - tests whether all conditions are met
-
Or (union or logical disjunction) - tests whether at least one condition is true
-
Not (negation) - tests whether the condition is not true
Editing Automations in Home Assistant
Home Assistant offers two ways to edit automations, allowing users to customize their smart homes according to their needs.
Visual Editor
The visual editor is the default and user-friendly option for editing automations. This approach is ideal especially for beginners and for creating basic automations. The main advantages of the visual editor are:
- Intuitive Interface: Allows you to "click" all settings without the need for code knowledge.
- Guidance: Provides guidance when creating functional automations.
- Minimizing Errors: Reduces the risk of syntax errors that can occur when manually writing code.
YAML Code
Editing using YAML code offers advanced users greater flexibility and control over automations. This approach is suitable for:
- Complex Automations: Allows creating more complex logic and conditions.
- Transferring Automations: Easy copying and transferring of automations between different instances of Home Assistant.
- Backup: The ability to easily back up specific automations in text form. For general backups, however, I recommend using classic backups.
When editing YAML code, extra caution is required, as incorrect edits can lead to automation malfunction.
Switching Between Modes
Home Assistant allows flexible switching between the visual editor and YAML code:
- Open the automation edit.
- Click on the three-dot menu in the top right corner.
- Choose the desired edit mode.
This feature allows you to take advantage of both approaches according to the current need.
Example Automation - Visual Mode vs. YAML Code
alias: System - Weekly Backup (Auto backup)
description: ""
mode: single
triggers:
- at: "22:10:00"
trigger: time
conditions:
- condition: time
weekday:
- sun
actions:
- data:
exclude:
addons:
- Studio Code Server
folders:
- Local add-ons
- share
compressed: true
keep_days: 28
action: auto_backup.backup_full
Comments