Home Assistant YAML Automations
YAML Style Rules
- 2-space indentation (never tabs)
- Lowercase booleans:
true/false - Entity IDs:
domain.entity_name(lowercase, underscores)
Automation Template
automation:
- id: 'unique_automation_id'
alias: 'Descriptive Name'
description: 'What this does'
mode: single # see Run Modes below; defaults to single if omitted
triggers:
- trigger: state
entity_id: binary_sensor.front_door
to: 'on'
conditions:
- condition: time
after: '08:00:00'
before: '22:00:00'
actions:
- action: light.turn_on
target:
entity_id: light.hallway
data:
brightness_pct: 100
Run Modes
mode: controls what happens when the automation is triggered again while a previous run is still going. Omitting it defaults to single.
- single — ignore the new trigger; the in-progress run continues uninterrupted (default).
- restart — cancel the running run and start over from the top.
- queued — run sequentially; each new trigger waits in line and runs after the previous one finishes.
- parallel — run concurrently; each trigger starts its own run immediately.
Common Triggers
# State
- trigger: state
entity_id: sensor.temperature
above: 25
# Time
- trigger: time
at: '07:30:00'
# Sun
- trigger: sun
event: sunset
offset: '-00:30:00'
# Template
- trigger: template
value_template: "{{ states('sensor.power') | float > 1000 }}"
# Webhook
- trigger: webhook
webhook_id: 'my_unique_webhook_id'
allowed_methods: [POST]
Common Conditions
# State
- condition: state
entity_id: alarm_control_panel.home
state: 'armed_away'
# Numeric
- condition: numeric_state
entity_id: sensor.temperature
above: 20
below: 30
# Time
- condition: time
after: '08:00:00'
before: '23:00:00'
weekday: [mon, tue, wed, thu, fri]
# Template
- condition: template
value_template: "{{ is_state('person.john', 'home') }}"
# AND/OR
- condition: and
conditions:
- condition: state
entity_id: input_boolean.guest_mode
state: 'off'
- condition: state
entity_id: binary_sensor.motion
state: 'on'
Common Actions
# Service call
- action: light.turn_on
target:
entity_id: light.living_room
data:
brightness_pct: 80
# Delay
- delay:
seconds: 30
# Wait for trigger
- wait_for_trigger:
- trigger: state
entity_id: binary_sensor.motion
to: 'off'
timeout:
minutes: 5
continue_on_timeout: true
# Choose (if/else)
- choose:
- conditions:
- condition: state
entity_id: input_select.mode
state: 'away'
sequence:
- action: climate.set_temperature
target:
entity_id: climate.thermostat
data:
temperature: 18
default:
- action: notify.mobile_app
data:
message: 'Unknown mode'
# Repeat
- repeat:
count: 3
sequence:
- action: light.toggle
target:
entity_id: light.alert
- delay:
seconds: 1
Key Rules
- Use the plural block keys
triggers:/conditions:/actions:(HA 2024.10+; the singular forms still work as aliases) - Within them use
- trigger:/- action:item keys (not- platform:/- service:) - Always include
aliasanddescription - Use
target:for entity/area/device targeting - Templates use Jinja2:
"{{ states('sensor.x') }}"
Validate / Troubleshoot
Before deploying, validate the config via Developer Tools → YAML → Check Configuration in the UI, or hass --script check_config on the CLI. Most common YAML failures:
- Indentation — a tab anywhere (HA requires 2-space indent) or a misaligned list item breaks parsing.
- Unquoted time strings —
at: 07:30:00parses as a sexagesimal number; always quote them:at: '07:30:00'. - Misplaced
!input—!inputis only valid inside a blueprint; a referenced entity or input that does not exist fails the run at trigger time, not at parse time.
Related Skills
- Scripts →
ha-scripts - Blueprints →
ha-blueprints - Device triggers →
ha-device-triggers
微信扫一扫