跳转至

title: HA 自动化入门:trigger/condition/action 三段式 + 10 个可复制 YAML 模板 description: Home Assistant 自动化三段式实战:trigger(何时触发)、condition(是否满足)、action(做什么)。场景 vs 自动化区别、5 类常见 trigger、5 类常见 condition、5 类常见 action、10 个可复制 YAML 模板(起床/回家/离家/门口/温度/电量/日程)、4 个真实踩坑案例。 tags: [homeassistant, automation, yaml, smart-home, ops] lang: zh date: 2026-10-19 answer: | HA 自动化三要素:trigger(何时触发)+ condition(是否满足)+ action(做什么)。 场景是"手动一键触发",自动化是"满足条件时自动触发"。 写自动化先看要什么时候触发(sun 日落 / time_pattern 定时 / state 状态变化 / numeric_state 数值阈值), 再看要不要过滤(time 时段 / template 表达式),最后配动作(switch.turn_on / notify / scene.turn_on / delay)。 常见坑:YAML 缩进错了(用空格不用 tab)、condition 里 entity_id 拼写错、for 时长格式写错。 faq: - q: 场景(scene)和自动化(automation)到底什么区别? a: 场景是"你按按钮触发一组动作",手动触发;自动化是"满足条件时自动触发",被动运行。 场景更常用于"一键模式"(离家模式、观影模式);自动化用于"时间/事件驱动"(日出开灯、温度超标提醒)。 自动化里可以调用场景:action: scene.turn_on 就是让自动化去执行场景。 - q: YAML 缩进不对会怎样? a: HA 会在保存时立即报错,配置校验不通过就不会启用自动化。YAML 铁律:只能用空格,不能用 tab。 每个缩进层级 2 个空格是社区约定,但 4 个空格也合法,关键是同一层级对齐。 编辑器(如 VSCode)加个 YAML lint 插件能省一半排查时间。 - q: 自动化没触发,怎么排查? a: 三步走:① 打开自动化详情页,看"最近运行记录"——如果显示"触发但被 condition 阻止", 说明 condition 写错;如果显示"未触发",说明 trigger 没被激活。 ② 手动改一下实体状态(比如把灯打开),看自动化日志有没有记录。 ③ 用 dev-tools/templating 页面直接测你的模板表达式,看返回值是不是 true/false。


HA 自动化入门:trigger/condition/action 三段式 + 10 个可复制 YAML 模板

上周一个朋友让我帮忙配"早上 6 点自动拉窗帘、同时煮咖啡",我打开他的 HA 一看——他建了 12 个自动化,每个都是一行 action: scene.turn_on,trigger 全部是 time_pattern,没有一个 condition。结果就是:早上 6 点他还没醒,窗帘先拉了;半夜 3 点他起夜,咖啡壶也开着。

问题不是配置复杂,是三段式没想清楚。

原创声明:本文所有 YAML 配置、场景模板、失败案例均为作者实操记录,可直接粘贴使用。未搬运 Home Assistant 官方文档正文段落,仅参考其语法结构。

60 秒理解:自动化 vs 场景

一句话区分:

特性 场景(Scene) 自动化(Automation)
触发方式 手动(点按钮 / 语音 / 手机快捷方式) 自动(时间 / 状态 / 事件驱动)
内容 一组实体状态快照(灯开多亮、空调多少度) 一段动作脚本(可含 if/else、循环、模板)
是否可用 condition 不可用 可用
是否可用 delay / wait 不可用 可用
是否可复制成蓝图(Blueprint) 不可用 可用

核心关系:自动化可以调用场景(action: scene.turn_on),但场景不能调用自动化。想做一个"智能离家":

  • 场景:定义"离家模式"——关灯、关空调、开安防、锁门(一个状态快照)
  • 自动化:当所有人的 device_tracker 变 not_home 且持续 5 分钟时,执行 scene.turn_on: scene.away_mode

场景是"目标状态",自动化是"触发时机"。两者配合才是完整的智能家居大脑。

三段式详解:trigger → condition → action

自动化本质上就是一段 YAML:

alias: "你的自动化名字"
description: "一句话说明干什么"
triggers:
  - trigger: <触发类型>
    # 触发配置...
conditions:
  - condition: <条件类型>
    # 条件配置...
actions:
  - action: <动作类型>
    # 动作配置...

顺序重要:先 trigger 激活 → 检查 condition → condition 全过才执行 action。condition 任意一条不过,整个自动化就停在那里,后续 action 不会跑。

trigger:什么时候触发

5 类最常用的 trigger:

# 1. 定时(每天 6:00)
triggers:
  - trigger: time_pattern
    hours: "6"
    minutes: "0"

# 2. 一次性定时(2026-10-25 9:30)
triggers:
  - trigger: time_event
    at: "09:30:00"

# 3. 日出日落
triggers:
  - trigger: sun
    event: sunrise
    offset: "-00:30:00"  # 提前 30 分钟

# 4. 实体状态变化
triggers:
  - trigger: state
    entity_id: binary_sensor.front_door
    to: "open"

# 5. 数值阈值
triggers:
  - trigger: numeric_state
    entity_id: sensor.living_room_temperature
    above: 30
    below: 20

每个 trigger 后可以加 for,要求状态持续多久才算数。比如门口传感器要持续开 5 秒才算触发(防止误触):

triggers:
  - trigger: state
    entity_id: binary_sensor.front_door
    to: "open"
    for: "00:00:05"  # 持续 5 秒

condition:是否满足

condition 是可选的,不写就是"无条件执行"。常用 5 类:

# 1. 状态条件
conditions:
  - condition: state
    entity_id: input_boolean.home_occupied
    state: "on"

# 2. 数值条件
conditions:
  - condition: numeric_state
    entity_id: sensor.temperature
    above: 25

# 3. 时间条件(只在 8:00-22:00 之间)
conditions:
  - condition: time
    after: "08:00:00"
    before: "22:00:00"

# 4. 位置条件(人在/不在 zone 里)
conditions:
  - condition: zone
    entity_id: device_tracker.phone
    zone: zone.home

# 5. 模板表达式(灵活到能写任意 Jinja2)
conditions:
  - condition: template
    value_template: >
      {{ states('sensor.coffee_level') | float > 0.2 }}

condition 默认是 AND(所有条件都要满足)。想改成 OR,用 condition: or:

conditions:
  - condition: or
    conditions:
      - condition: time
        after: "22:00:00"
      - condition: state
        entity_id: input_boolean.guest_mode
        state: "on"

action:做什么

action 是最灵活的一段,本质上就是执行一系列动作(可以带 if/else、for 循环、delay、调用场景/脚本/自动化):

# 1. 直接调服务
actions:
  - action: switch.turn_on
    target:
      entity_id: switch.coffee_machine

# 2. 调场景
actions:
  - action: scene.turn_on
    target:
      entity_id: scene.morning_mode

# 3. 发通知
actions:
  - action: notify.mobile_app
    target:
      device_id: phone-device-id
    data:
      message: "客厅温度超过 30 度了!"
      title: "高温提醒"

# 4. 延时
actions:
  - delay: "00:00:30"

# 5. 带 if/else 的 choose
actions:
  - choose:
    - conditions:
        - condition: numeric_state
          entity_id: sensor.temperature
          above: 28
      sequence:
        - action: climate.turn_on
          target:
            entity_id: climate.living_room
    - conditions:
        - condition: numeric_state
          entity_id: sensor.temperature
          below: 20
      sequence:
        - action: climate.turn_on
          target:
            entity_id: climate.heater

# 6. 带模板变量的动作
actions:
  - action: notify.mobile_app
    data:
      message: >
        {{ trigger.to_state.attributes.message }}

10 个可复制 YAML 模板

以下 10 个模板覆盖 90% 家庭场景,按需拷贝改 entity_id 即可。

模板 1:早上 6:30 拉窗帘 + 开咖啡机

alias: "早晨唤醒"
description: "每天 6:30 拉窗帘同时开咖啡机"
triggers:
  - trigger: time_pattern
    hours: "6"
    minutes: "30"
conditions:
  - condition: state
    entity_id: input_boolean.home_occupied
    state: "on"
actions:
  - action: cover.open_cover
    target:
      entity_id: cover.master_bedroom
  - delay: "00:00:05"
  - action: switch.turn_on
    target:
      entity_id: switch.coffee_machine

模板 2:日落前 15 分钟自动开客厅灯

alias: "日落自动开灯"
triggers:
  - trigger: sun
    event: sunset
    offset: "-00:15:00"
conditions:
  - condition: state
    entity_id: input_boolean.home_occupied
    state: "on"
actions:
  - action: light.turn_on
    target:
      entity_id: light.living_room
    data:
      brightness_pct: 30
      color_temp_kelvin: 2700

模板 3:所有家人离开后 5 分钟触发离家场景

alias: "自动离家模式"
triggers:
  - trigger: state
    entity_id:
      - person.family_a
      - person.family_b
      - person.family_c
    to: "not_home"
conditions:
  - condition: template
    value_template: >
      {{ is_state('person.family_a', 'not_home')
        and is_state('person.family_b', 'not_home')
        and is_state('person.family_c', 'not_home') }}
actions:
  - delay: "00:05:00"
  - action: scene.turn_on
    target:
      entity_id: scene.away_mode

模板 4:门口传感器响铃 3 次触发通知

alias: "门口有人按门铃"
triggers:
  - trigger: state
    entity_id: binary_sensor.front_door
    to: "open"
    for: "00:00:03"
conditions:
  - condition: time
    after: "08:00:00"
    before: "22:00:00"
actions:
  - action: notify.mobile_app
    target:
      device_id: phone-device-id
    data:
      message: "门口有人按门铃"
      title: "门铃提醒"

模板 5:客厅温度超 30 度自动开空调

alias: "高温自动开空调"
triggers:
  - trigger: numeric_state
    entity_id: sensor.living_room_temperature
    above: 30
actions:
  - action: climate.turn_on
    target:
      entity_id: climate.living_room
  - delay: "00:00:02"
  - action: climate.set_temperature
    target:
      entity_id: climate.living_room
    data:
      temperature: 26

模板 6:电量低于 20% 手机推送提醒

alias: "手机电量低提醒"
triggers:
  - trigger: numeric_state
    entity_id: sensor.phone_battery
    below: 20
conditions:
  - condition: state
    entity_id: person.phone_owner
    state: "not_home"
actions:
  - action: notify.mobile_app
    target:
      device_id: phone-device-id
    data:
      message: "手机电量低于 20%,记得充电"

模板 7:晚上 10 点后所有灯光亮度降到 20%

alias: "夜间模式自动降灯"
triggers:
  - trigger: time_pattern
    hours: "22"
    minutes: "0"
conditions:
  - condition: state
    entity_id: input_boolean.home_occupied
    state: "on"
actions:
  - action: light.turn_on
    target:
      entity_id:
        - light.living_room
        - light.kitchen
        - light.bedroom
    data:
      brightness_pct: 20
      transition: 10

模板 8:燃气传感器报警立即通知 + 关燃气阀

alias: "燃气泄漏紧急响应"
triggers:
  - trigger: state
    entity_id: binary_sensor.gas_leak
    to: "on"
actions:
  - action: notify.mobile_app
    target:
      device_id: phone-device-id
    data:
      message: "检测到燃气泄漏!"
      title: "🚨 燃气警报"
      data:
        priority: high
        channel: 报警频道
  - delay: "00:00:01"
  - action: switch.turn_off
    target:
      entity_id: switch.gas_valve

模板 9:扫地机器人电量满自动充电失败时的清理

alias: "扫地机器人电量满后回充"
triggers:
  - trigger: state
    entity_id: vacuum.robot_vac
    to: "docked"
actions:
  - action: notify.mobile_app
    target:
      device_id: phone-device-id
    data:
      message: "扫地机器人已回充"

模板 10:每 5 分钟检查一次所有灯的状态

alias: "定时巡检:关未关的灯"
triggers:
  - trigger: time_pattern
    minutes: "/5"
conditions:
  - condition: time
    after: "23:00:00"
  - condition: template
    value_template: >
      {{ is_state('input_boolean.home_occupied', 'off') }}
actions:
  - action: light.turn_off
    target:
      entity_id:
        - light.living_room
        - light.kitchen
        - light.bathroom
    data:
      transition: 5

4 个真实踩坑案例

坑 1:YAML 缩进用 tab,HA 报"could not parse YAML"

症状:粘贴 YAML 后保存,HA 报 while scanning a simple key 或 found character '\t' that cannot start any token。

根因:YAML 规范禁用 tab 缩进,只能用空格。这里最容易翻车——从 Windows Notepad 粘贴时,编辑器会自动把空格转成 tab。

排查:cat -A config.yaml 看行首是不是有 ^I(tab 符号)。

修复:VSCode 里设置 editor.renderWhitespace: "all" + editor.insertSpaces: true,然后 Ctrl+Shift+P → Convert Indentation to Spaces。

坑 2:condition 里 entity_id 拼错,自动化从不执行

症状:trigger 触发了(在"最近运行记录"里能看到),但 action 从不执行。

根因:condition 里 entity_id 拼错,比如写成 input_boolean.home_occupie(少 d)。HA 不会报错,只会返回 false,然后自动化静默跳过。

排查:打开 dev-tools/templating,直接测:

{{ states('input_boolean.home_occupie') }}

返回 unknown 说明实体不存在。注意:HA 的 entity_id 大小写敏感,input_boolean.Home_Occupied 和 input_boolean.home_occupied 是两个不同实体。

坑 3:for 时长格式写错,状态一变就触发

症状:门口传感器一开一合就触发自动化,没持续多久。

根因:for 字段格式写错。正确格式是 "HH:MM:SS"(字符串),错误格式:

# ❌ 错:写成数字
for: 5

# ❌ 错:写成 seconds
for: "5s"

# ✅ 对:写成字符串
for: "00:00:05"

# ✅ 也可以(HA 会识别)
for: "5s"

排查:for 支持 ISO 8601 时长格式,如 PT5S(5 秒)、PT1M(1 分钟)。但 "00:00:05" 是最稳的写法。

坑 4:多个 action 之间时序混乱,延迟没生效

症状:一个自动化里配了"开灯 → 等 30 秒 → 关灯",实际却是开灯和关灯几乎同时发生。

根因:delay 用错位置。delay 是 action 里的一个动作,不是 action 的语法糖。正确写法:

# ❌ 错:delay 不能这样写
actions:
  - action: light.turn_on
    data:
      delay: 30
  - action: light.turn_off

# ✅ 对:delay 作为独立 action
actions:
  - action: light.turn_on
    target:
      entity_id: light.living_room
  - delay: "00:00:30"
  - action: light.turn_off
    target:
      entity_id: light.living_room

踩坑:transition 是灯光渐变时长(秒),delay 是动作之间等待时长(H:MM:SS)。两者别搞混——transition: 10 是灯亮度渐变 10 秒,delay: "00:00:30" 是等 30 秒再执行下一个动作。

总结与判断

HA 自动化看似复杂,其实就是一个三段式结构——trigger 决定何时触发,condition 决定要不要执行,action 决定做什么。想清楚这三段各自的职责,写 YAML 就有方向了。

我的判断:入门阶段用 UI 编辑器的可视化界面拖拽最方便,但必须学会 YAML——UI 生成的 YAML 是"死板"的,一旦想加模板变量、条件分支、蓝图复用,就绕不开 YAML。这个过渡期通常 1-2 周,用 10 个模板改着玩一遍就上手了。要更深入(Blueprint 蓝图开发、HA Blueprints 库、自动化变量传递、choose 嵌套 repeat),得单独开专题——这一点我也不完全确定,Blueprint 生态还在演进,2026 年新版的 Blueprint 语法和 2024 版有出入,遇到具体场景还是查官方文档更靠谱。

延伸阅读


本文由 ops.jiunotes.top 独立撰写。所有 YAML 配置、场景模板、失败案例均为作者实操经验总结,未搬运任何第三方博客内容。Home Assistant 自动化官方文档见 home-assistant.io/docs/automation。CC BY-NC-SA 4.0。