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

  1. YAML Basics
  2. Automation Structure Explained
  3. Full Motion → Light Automation
  4. Adding Time Conditions
  5. Adding Brightness and Color
  6. Testing and Debugging
  7. Troubleshooting
  8. FAQ

What You Need

YAML Basics

YAML (Yet Another Markup Language) is what Home Assistant uses for configuration and automations. Before writing your first automation, understand three rules:

  1. Indentation matters: YAML uses spaces (not tabs) for structure. Two spaces per level is the convention in HA.
  2. Key: value pairs: Everything is key: value. The colon + space is mandatory.
  3. Lists start with dashes: When a key can have multiple items, each item starts with -.
YAML — Basic structure example
# 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:

YAML — Automation skeleton
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:

automations.yaml — Motion to light, full version
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):

automations.yaml — With time condition
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:

YAML — Sun-based condition
    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

automations.yaml — Different brightness by time of day
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:

  1. Go to Settings → Automations & Scenes → Automations
  2. Find your automation — it should appear with its alias
  3. Click the three-dot menu → Run to test it manually without waiting for a trigger
  4. Check Settings → System → Logs for any YAML errors
  5. 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
Pro tip: The automation Trace is the most valuable debugging tool in HA. It shows exactly what happened at each step, including the values of entities at the time the automation ran. Always check it first when an automation isn't working.

Troubleshooting

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.