Quick Answer
A Home Assistant automation has three parts: trigger (what starts it), condition (optional check), and action (what happens). Write it in YAML, paste it into automations.yaml, reload automations, and it's live. The full motion→light automation with time conditions is in this guide — copy-paste ready.
Table of Contents
What You Need
- A running Home Assistant instance
- At least one motion sensor (any brand — Aqara, IKEA, Sonoff SNZB)
- At least one controllable light entity in HA
YAML Basics
YAML (Yet Another Markup Language) is what Home Assistant uses for configuration and automations. Before writing your first automation, understand three rules:
- Indentation matters: YAML uses spaces (not tabs) for structure. Two spaces per level is the convention in HA.
- Key: value pairs: Everything is
key: value. The colon + space is mandatory. - Lists start with dashes: When a key can have multiple items, each item starts with
-.
# This is a comment parent_key: child_key: value another_child: other_value list_key: - first_item - second_item - third_item
If your automation doesn't work, indentation errors are the most common cause. Use the HA YAML editor (which has built-in validation) rather than a plain text editor.
Automation Structure Explained
Every HA automation has this structure:
automation:
- alias: "Human-readable name for the automation"
description: "Optional description"
trigger: # What causes this automation to run?
- platform: ...
# trigger-specific settings
condition: # Optional: must be true for action to run
- condition: ...
action: # What happens when triggered (and conditions pass)?
- service: ...
target:
entity_id: ...
Trigger: When something changes — a sensor state, a time, an MQTT message, the sun's position. The automation fires whenever any listed trigger fires.
Condition: Optional check that must be true for the action to proceed. If conditions fail, the automation stops silently.
Action: What HA does — turn a light on, send a notification, call a script, change an input value.
Full Motion → Light Automation
Copy this into your automations.yaml file (or paste it using the HA automation editor's YAML mode). Replace entity IDs with your actual sensor and light:
automation:
- alias: "Hallway light on motion"
description: "Turn on hallway light when motion, off when clear for 5 min"
mode: restart
trigger:
- platform: state
entity_id: binary_sensor.hallway_motion_occupancy
to: "on"
action:
- service: light.turn_on
target:
entity_id: light.hallway
- wait_for_trigger:
- platform: state
entity_id: binary_sensor.hallway_motion_occupancy
to: "off"
for:
minutes: 5
- service: light.turn_off
target:
entity_id: light.hallway
How this works: When motion is detected, the light turns on. The automation then waits for motion to clear for 5 continuous minutes. Once clear, the light turns off. The mode: restart means if motion is detected again while waiting, the timer resets.
Adding Time Conditions
Only run the automation between 6am and midnight (don't wake people up at 3am for a bathroom visit):
automation:
- alias: "Hallway light on motion (daytime only)"
mode: restart
trigger:
- platform: state
entity_id: binary_sensor.hallway_motion_occupancy
to: "on"
condition:
- condition: time
after: "06:00:00"
before: "00:00:00"
action:
- service: light.turn_on
target:
entity_id: light.hallway
- wait_for_trigger:
- platform: state
entity_id: binary_sensor.hallway_motion_occupancy
to: "off"
for:
minutes: 5
- service: light.turn_off
target:
entity_id: light.hallway
You can also use the sun's position instead of hardcoded times — replace the time condition with:
condition:
- condition: sun
after: sunrise
after_offset: "-00:30:00" # 30 minutes before sunrise
before: sunset
before_offset: "01:00:00" # 1 hour after sunset
Adding Brightness and Color Temperature
automation:
- alias: "Hallway light - adaptive brightness"
mode: restart
trigger:
- platform: state
entity_id: binary_sensor.hallway_motion_occupancy
to: "on"
action:
- choose:
- conditions:
- condition: time
after: "06:00:00"
before: "20:00:00"
sequence:
- service: light.turn_on
target:
entity_id: light.hallway
data:
brightness_pct: 100
color_temp: 6500 # cool white (Kelvin)
- conditions:
- condition: time
after: "20:00:00"
before: "23:59:59"
sequence:
- service: light.turn_on
target:
entity_id: light.hallway
data:
brightness_pct: 30
color_temp: 2700 # warm white
default:
- service: light.turn_on
target:
entity_id: light.hallway
data:
brightness_pct: 10
color_temp: 2200 # very warm for night
- wait_for_trigger:
- platform: state
entity_id: binary_sensor.hallway_motion_occupancy
to: "off"
for:
minutes: 5
- service: light.turn_off
target:
entity_id: light.hallway
Testing and Debugging
After saving your automation:
- Go to Settings → Automations & Scenes → Automations
- Find your automation — it should appear with its alias
- Click the three-dot menu → Run to test it manually without waiting for a trigger
- Check Settings → System → Logs for any YAML errors
- Click on your automation to see the Trace — a visual representation of the last execution, showing which trigger fired, which conditions passed, and what actions ran
Troubleshooting
- Entities don’t appear in Home Assistant: Confirm the integration is loaded correctly and double-check that the device is supported by the platform you chose.
- Commands feel delayed: This is usually caused by cloud polling, weak Wi-Fi, or a slow Zigbee mesh rather than Home Assistant itself.
- Things work in the vendor app but not in HA: Re-check credentials, local API permissions, and whether the device is using the correct network path.
- Automations misfire: Open the automation trace in Home Assistant and verify the exact entity IDs and state changes being used.
FAQ
Should I write automations in YAML or the UI editor?
Both produce the same result — the UI editor generates YAML behind the scenes. YAML is better for complex logic, version control (git), and copying between setups. The UI editor is easier for beginners and simple automations.
Where do I put my automations YAML?
In /config/automations.yaml (the default location). Make sure your configuration.yaml includes automation: !include automations.yaml. After adding automations, go to Developer Tools → YAML → Reload Automations.
What's the difference between trigger and condition?
The trigger starts the automation (when this happens). The condition is checked after the trigger fires (is this true right now?). If you put time constraints in the trigger, the automation only fires at that time. If you put them in conditions, the automation fires at any time but only runs the action if it's the right time.
SmartWired uses affiliate links. If you buy through our links, we may earn a commission at no extra cost to you. See our Affiliate Disclosure.