If-then
The If-then building block runs actions only if its conditions are met. Optionally, it runs other actions if they are not met. Use it when the automation or script should do something different depending on the situation, for example, start the vacuum only when no one is home.
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 If-then.
- Under If, select Add condition, and add the conditions to check.
- Under Then, select Add action, and add the actions to run if all conditions are met.
- Optional: Under Else, select Add action, and add the actions to run if the conditions are not met.
- Select Save.
Options in the UI
The conditions to check. All conditions must be met.
Using this building block in YAML
In YAML, use if, then, and optionally else:
if:
- alias: "If no one is home"
condition: state
entity_id: zone.home
state: "0"
then:
- alias: "Then start cleaning already!"
action: vacuum.start
target:
area_id: living_room
else:
- action: notify.notify
data:
message: "Skipped cleaning, someone is home!"
Options in YAML
The conditions to check. All conditions must be met. You can also use a template.
Good to know
- All conditions under If must be met. To run the actions if any of them is met, put them in an Or condition.
- You can put an If-then inside another If-then. If you find yourself adding more If-then building blocks under Else, use Choose instead.