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

  1. Go to Settings > Automations & scenes and open an automation. To edit a script, open the Scripts tab and open the script.
  2. In the Then do section of an automation, or the Sequence section of a script, select Add action.
  3. Select Blocks, then select If-then.
  4. Under If, select Add condition, and add the conditions to check.
  5. Under Then, select Add action, and add the actions to run if all conditions are met.
  6. Optional: Under Else, select Add action, and add the actions to run if the conditions are not met.
  7. Select Save.

Options in the UI

If (Required)

The conditions to check. All conditions must be met.

Then (Required)

The actions to run if all conditions are met.

Else (Optional)

The actions to run if the conditions are not met.

Using this building block in YAML

In YAML, use if, then, and optionally else:

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]
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

if list | template Required

The conditions to check. All conditions must be met. You can also use a template.

then list Required

The actions to run if all conditions are met.

else list (Optional)

The actions to run if the conditions are not met.

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.