Skip to main content
Lesson 5 of 5 5 min read

Writing Your First Automation

Automations in Home Assistant

Home Assistant's automation engine is where the platform truly shines. While other platforms offer basic "if this, then that" rules, Home Assistant provides a full automation system with multiple triggers, complex conditions, sequential and parallel actions, variables, templates, and the ability to call any service in the system. You can create automations through the visual editor (no coding required) or by writing YAML for maximum flexibility.

Every automation in Home Assistant has the same structure: one or more triggers that start the automation, optional conditions that determine whether it should run, and one or more actions that define what happens. This is the same trigger-condition-action pattern we discussed in the Advanced Automations course, but Home Assistant gives you more tools to work with at each step.

Creating an Automation with the Visual Editor

Let us build a practical automation: turning on the hallway light when motion is detected, but only after sunset. Here is the step-by-step process:

  1. Go to Settings, then Automations & Scenes, then click Create Automation.
  2. Choose Create new automation (as opposed to using a blueprint).
  3. Add a trigger: Click "Add Trigger," select "Device," then choose your motion sensor. Select "Motion detected" as the trigger event. If you are using an entity trigger instead, select the motion sensor entity and set the trigger to "from off to on."
  4. Add a condition: Click "Add Condition," select "Sun," then set it to "After sunset." You can add an offset here too, like -30 minutes to trigger the automation 30 minutes before actual sunset when it is already getting dark.
  5. Add an action: Click "Add Action," select "Device," choose your hallway light, and set it to "Turn on." Optionally, set the brightness to a specific level, like 80%.
  6. Give it a name: Something descriptive like "Hallway light on when motion detected after sunset."
  7. Click Save.

That is it. Your first automation is running. Walk in front of the motion sensor after sunset and the hallway light will turn on.

Adding the Turn-Off Companion Automation

An automation that turns on a light needs a companion that turns it off. Otherwise, the light stays on forever. Here is how to create the companion:

  1. Create a new automation.
  2. Trigger: Motion sensor, "Motion cleared" or "from on to off."
  3. Add a wait (optional but recommended): In the actions section, add a "Wait for time to pass" action set to 5 minutes. This prevents the light from turning off the instant motion stops, which would be annoying if you are just sitting still.
  4. Add a condition after the wait: Check that motion is still clear. This handles the case where motion was detected again during the 5-minute wait.
  5. Action: Turn off the hallway light.
  6. Name it "Hallway light off after no motion" and save.

Understanding Modes

Home Assistant automations have a "mode" setting that controls what happens when the automation is triggered while it is already running. This is important for automations with waits or delays:

  • Single (default): If the automation is already running, a new trigger is ignored. Good for most simple automations.
  • Restart: If the automation is already running, it stops and restarts from the beginning. This is perfect for the hallway light example: if motion is detected again during the 5-minute wait, the timer restarts. Set the turn-off automation to "restart" mode.
  • Queued: New triggers are queued and run after the current execution finishes. Useful for automations that should process every trigger in order.
  • Parallel: Multiple instances of the automation run simultaneously. Use this carefully, as it can lead to conflicting actions.

Using Blueprints: Community-Made Automations

Not every automation needs to be built from scratch. Home Assistant's blueprint system lets you import pre-made automation templates created by the community. Blueprints handle the logic while you just fill in the specific devices and settings for your home.

To use a blueprint, go to Settings, then Automations & Scenes, then Blueprints. Click "Import Blueprint" and paste a URL from the Home Assistant Blueprint Exchange. Popular blueprints include motion-activated lighting with configurable timeout and brightness, notification when a door has been left open for a specified duration, and HVAC control based on window open or closed state.

Blueprints are an excellent learning tool. After importing one, you can examine how it is built to understand techniques you can apply to your own automations.

Your Next Five Automations

Now that you know the basics, here are five practical automations to build as you get comfortable with the system:

1. Welcome home lights. When your phone connects to your home Wi-Fi (trigger) and it is after sunset (condition), turn on the entryway and living room lights (action).

2. Morning routine. At 6:30 AM on weekdays (trigger), gradually increase the bedroom light brightness from 0 to 50% over 10 minutes (action using a "repeat" with incremental brightness increases).

3. Door left open alert. When the front door opens (trigger), wait 5 minutes (action), check if the door is still open (condition), and send a push notification to your phone (action).

4. Thermostat away mode. When everyone leaves the geofence (trigger), wait 10 minutes (action), confirm nobody is home (condition), and set the thermostat to eco mode (action).

5. Goodnight button. When a specific button is pressed (trigger), turn off all lights, lock the doors, set the thermostat to sleep mode, and arm the security system (actions).

Build each one, live with it for a few days, and adjust as needed before moving on to the next. Each automation teaches you something new about how Home Assistant works, and within a few weeks you will be comfortable creating complex automations from scratch.

Lesson Complete