Write Home Assistant YAML Automations with AI in 15 Minutes

Use AI to create, debug, and optimize Home Assistant automations without memorizing YAML syntax or service calls.

Problem: YAML Syntax Kills Your Automation Flow

You know what you want your smart home to do, but you're stuck fighting YAML indentation errors and hunting through documentation for the right service call format.

You'll learn:

  • How to use AI to write valid Home Assistant automations
  • Debug YAML errors without trial-and-error
  • Convert natural language ideas into working automations

Time: 15 min | Level: Beginner


Why This Happens

Home Assistant's automation syntax is powerful but unforgiving—wrong indentation breaks everything, service names change between versions, and entity IDs must match exactly.

Common symptoms:

  • "Invalid config" errors with no line numbers
  • Automations that validate but don't trigger
  • Spending 20 minutes on documentation for a 2-minute task
  • Copy-pasting examples that don't match your setup

Solution

Step 1: Describe What You Want

Start with plain English. AI excels at translating intent into YAML structure.

Example prompts that work:

"When motion is detected in the hallway between 10pm and 6am,
turn on the hallway light at 20% brightness for 5 minutes"

"If the living room temperature goes above 75°F and someone is home,
send me a notification and turn on the AC"

"Every weekday at 7am, if it's a workday on my calendar,
start my morning routine (lights on, coffee maker on, read weather)"

Why this works: You focus on logic, AI handles YAML syntax, entity formats, and Home Assistant-specific service calls.


Step 2: Generate the YAML

Use Claude, ChatGPT, or any LLM with this specific prompt structure:

Create a Home Assistant automation in YAML format:

Goal: [your description from Step 1]

My entities:
- Motion sensor: binary_sensor.hallway_motion
- Light: light.hallway_ceiling
- Temperature: sensor.living_room_temperature

Requirements:
- Use valid Home Assistant 2024+ syntax
- Include mode: single to prevent overlapping runs
- Add comments explaining each section
- Use proper YAML indentation (2 spaces)

AI will generate:

alias: "Hallway Motion Light (Night)"
description: "Turn on hallway light at low brightness during nighttime motion"
mode: single

trigger:
  - platform: state
    entity_id: binary_sensor.hallway_motion
    to: "on"

condition:
  - condition: time
    after: "22:00:00"
    before: "06:00:00"

action:
  # Turn on light at 20% brightness
  - service: light.turn_on
    target:
      entity_id: light.hallway_ceiling
    data:
      brightness_pct: 20
  
  # Wait 5 minutes
  - delay:
      minutes: 5
  
  # Turn off light
  - service: light.turn_off
    target:
      entity_id: light.hallway_ceiling

Step 3: Test and Refine

Copy the YAML into Home Assistant:

  1. Settings → Automations & Scenes → Create Automation
  2. Click (three dots) → Edit in YAML
  3. Paste the generated code
  4. Click Save

Expected: Green checkmark = valid YAML

If it fails:

  • "Invalid config": Copy the full error back to AI with "fix this error in the YAML"
  • Wrong entity ID: Ask AI "update entity_id from X to Y throughout"
  • Doesn't trigger: Add - condition: template with debugging: value_template: "{{ true }}" temporarily

Step 4: Add Real-World Logic

AI can handle complex conditions you'd struggle to code manually:

"Only run this automation if:
- It's a weekday
- Someone has been home for at least 30 minutes
- The sun has set
- No one is watching TV (media_player.living_room is not playing)"

AI generates:

condition:
  - condition: time
    weekday:
      - mon
      - tue
      - wed
      - thu
      - fri
  
  - condition: state
    entity_id: zone.home
    state: "occupied"
    for:
      minutes: 30
  
  - condition: sun
    after: sunset
  
  - condition: not
    conditions:
      - condition: state
        entity_id: media_player.living_room
        state: "playing"

Verification

Test it:

  1. Manually trigger the automation from the UI
  2. Check Settings → System → Logs for errors
  3. Use Developer Tools → States to verify entity states

You should see: Automation runs successfully, actions execute in order.


Advanced: AI-Powered Debugging

When automations fail silently, ask AI to add debugging:

"Add trace logging to this automation so I can see what's happening at each step"

AI adds:

action:
  - service: system_log.write
    data:
      message: "Hallway automation triggered"
      level: info
  
  - service: light.turn_on
    target:
      entity_id: light.hallway_ceiling
    data:
      brightness_pct: 20
  
  - service: system_log.write
    data:
      message: "Light turned on, waiting 5 minutes"
      level: info

Now check logs to see exactly where it stops.


What You Learned

  • AI translates intent into valid Home Assistant YAML
  • Provide entity IDs and requirements for accurate output
  • Use AI for debugging error messages and complex conditions
  • Test in the UI before relying on automations

Limitations:

  • AI may use outdated service names (always verify against your HA version)
  • Entity IDs must be exact—AI can't guess your device names
  • Complex templating (Jinja2) may need manual review

Common Patterns to Automate

With AI, these go from 30 minutes to 3 minutes:

  1. "Good morning" routine:

    "At 6:30am on weekdays, if my phone alarm just went off,
    turn on bedroom lights gradually over 10 minutes,
    start coffee maker, and announce weather forecast"
    
  2. Security mode:

    "When the last person leaves home, lock all doors,
    turn off all lights except exterior ones,
    set thermostat to away mode, and enable security cameras"
    
  3. Energy saving:

    "If no motion detected in any room for 20 minutes,
    turn off all lights and pause HVAC. Resume when motion detected."
    
  4. Presence simulation:

    "When on vacation, randomly turn on/off living room and bedroom lights
    between 6pm-11pm to simulate occupancy"
    

Pro tip: Describe the complete scenario including edge cases. AI handles the complexity:

"Turn on porch light at sunset, but only if:
- Someone is home OR
- We're expecting a delivery today (check calendar) OR  
- It's a holiday
Keep light on until 11pm or until everyone goes to bed (bedroom lights off for 30min)"

AI will generate proper or conditions and state tracking you'd otherwise spend an hour debugging.


Troubleshooting Quick Reference

IssueAI Prompt
Wrong syntax"Convert this to Home Assistant 2024.2 format"
Entity not found"Replace entity_id X with Y throughout"
Automation doesn't trigger"Add debugging logs to track trigger conditions"
Action fails"Show me the correct service call for [device type]"
Complex timing"Add condition for [specific time logic]"

Tested on Home Assistant 2024.2, Core 2024.2.1, Supervisor 2024.01.1