跳转至

title: HomeAssistant 自动化触发器 vs 条件详解:为什么你的自动化不触发或误触发 description: HA 自动化最常见的两个坑:trigger 和 condition 搞混、重启后一堆自动化同时触发。详解 state/sun/time 三种触发器、condition 判断时机、mode 模式选择,附 10 个真实翻车案例和修复方案。 tags: [homeassistant, automation, trigger, condition, troubleshooting] lang: zh date: 2026-10-22 original: true answer: | HA 自动化核心区别:trigger 是"什么时候开始"(事件),condition 是"现在符不符合"(状态检查)。 trigger 触发后 condition 只检查一次,之后不管条件变不变都会执行 action。 最常见的翻车:trigger 和 condition 写重了(冗余)、忘记配 mode 导致重复执行、重启后所有 state 触发器同时 firing。 faq: - q: trigger 和 condition 有什么区别? a: trigger 是"事件",condition 是"状态检查"。比如"门打开了"(trigger)之后检查"现在是晚上吗"(condition)。 关键区别:trigger 触发后 condition 只检查一次,即使 condition 后来变成 false,action 也会照常执行。 - q: 为什么 HA 重启后所有自动化一起触发? a: state 触发器在重启时会重新评估所有实体状态。如果条件符合,就会 firing。 修复:给 trigger 加 for: 延迟(如 for: seconds: 5),重启时的瞬时状态变化会被忽略。 或者用 initial_state: off 配合手动触发。 - q: mode: single / restart / queued 怎么选? a: single = 忽略重复触发(安全但可能漏);restart = 新触发取消正在执行的(适合延时关灯); queued = 排队执行(保证每条都跑但可能积压)。人来灯亮用 restart,定时开关用 single。 - q: 同一个实体既在 trigger 又在 condition 里,会重复吗? a: 会冗余。trigger 已经检测了状态变化,condition 再检查一遍是浪费。 正确做法:trigger 负责"发生了什么",condition 负责"额外的限制条件"。


HomeAssistant 自动化触发器 vs 条件详解:为什么你的自动化不触发或误触发

装了 HA 三个月,我发现 90% 的自动化问题都出在触发器和条件搞混上。官方文档讲语法不讲场景,社区教程讲场景不讲原理。这篇把 trigger / condition / mode 三个核心概念的底层逻辑拆清楚,附 10 个真实翻车案例。

核心概念:trigger ≠ condition

很多初学者把 trigger 和 condition 混着用,导致自动化行为不可预测。搞清楚三句话:

  1. trigger 是事件,condition 是状态检查
  2. trigger 触发后 condition 只检查一次
  3. condition 不会"等待",它只"检查"
alias: "人来灯亮"
trigger:
  - platform: state          # 事件:状态变化
    entity_id: binary_sensor.hallway_motion
    to: "on"                 # 当检测到移动时触发
condition:
  - condition: time          # 检查:现在是晚上吗?
    after: "18:00"
    before: "06:00"
action:
  - service: light.turn_on   # 执行:开灯

执行顺序:

1. trigger 检测:运动传感器从 off → on?
   ├─ 否 → 等待下一个事件
   └─ 是 → 进入 condition 检查

2. condition 检查:现在时间在 18:00-06:00 之间?
   ├─ 否 → 不执行 action(即使 trigger 已触发)
   └─ 是 → 执行 action

3. action 执行:开灯
   (注意:condition 只在步骤 2 检查一次,之后不管时间怎么变都会执行)

三种触发器详解

1. state 触发器(最常用)

trigger:
  - platform: state
    entity_id: binary_sensor.front_door
    from: "closed"
    to: "open"

关键点:

  • from / to 是可选的,不写就监听所有状态变化
  • 只监听值的变化,不监听属性的变化
  • 实体从 unavailable 恢复到 on 也会触发
# ❌ 错误:想用 brightness 变化触发
trigger:
  - platform: state
    entity_id: light.livingroom
    attribute: brightness      # state 触发器不支持 attribute!

# ✅ 正确:用 event 触发器
trigger:
  - platform: event
    event_type: light_bright
    event_data:
      entity_id: light.livingroom

2. time 触发器(定时任务)

trigger:
  - platform: time
    at: "07:00:00"             # 每天 7:00

  - platform: time_pattern
    minutes: "/30"             # 每 30 分钟
    seconds: "0"               # 整点触发

关键点:

  • at 是精确时间点,pattern 是周期
  • 时区用 HA 全局设置,不在触发器里配置
  • 服务器重启后会从下一个整点开始计时,不会补跑

3. sun 触发器(日出日落)

trigger:
  - platform: sun
    event: sunrise
    offset: "+00:15:00"        # 日出后 15 分钟

  - platform: sun
    event: sunset
    offset: "-00:30:00"        # 日落前 30 分钟

关键点:

  • event 只能是 sunrise 或 sunset
  • offset 可以是正(延迟)或负(提前)
  • 时区自动用 HA 全局设置

Condition 的执行时机

这是最容易误解的地方:condition 只在 trigger 触发后检查一次,之后不管状态怎么变都不会再检查。

alias: "温度过高自动开窗"
trigger:
  - platform: state
    entity_id: sensor.bedroom_temperature
    above: 28
condition:
  - condition: state
    entity_id: switch.window_open
    state: "off"               # 检查窗户是否关闭
action:
  - service: switch.turn_on
    target:
      entity_id: switch.window_open

执行流程:

温度升到 28°C → trigger 触发
    ↓
检查 condition:窗户是关的吗?
    ├─ 是 → 开窗
    └─ 否 → 不执行(即使温度继续升高也不管)

如果温度降回 27°C 又升到 28°C,会再次检查 condition。所以 condition 的语义是"触发时检查当前状态",不是"持续监控"。

Mode 模式选择

mode 控制重复触发时的行为,选错了要么漏执行要么重复执行。

mode 行为 适用场景
single 忽略正在执行的新触发 定时任务、一次性操作
restart 新触发取消正在执行的 延时关灯、防抖场景
queued 排队等待执行 需要保证每条都执行的场景
parallel 同时执行多条 几乎不用,容易混乱
# ❌ 错误:人来灯亮用 single,人走后又回来灯灭了
alias: "人来灯亮"
mode: single           # 第二次触发被忽略,灯一直亮着
trigger:
  - platform: state
    entity_id: binary_sensor.hallway_motion
    to: "on"
action:
  - service: light.turn_on
    target:
      entity_id: light.hallway
  - delay: seconds: 120
  - service: light.turn_off

# ✅ 正确:用 restart,人还在就一直亮,人走了才延时关
alias: "人来灯亮"
mode: restart          # 新触发取消之前的延时
trigger:
  - platform: state
    entity_id: binary_sensor.hallway_motion
    to: "on"
action:
  - service: light.turn_on
    target:
      entity_id: light.hallway
  - delay: seconds: 120
  - service: light.turn_off

10 个真实翻车案例

案例 1:trigger 和 condition 写重了

# ❌ 错误:trigger 和 condition 检查同一个实体
trigger:
  - platform: state
    entity_id: light.livingroom
    to: "on"
condition:
  - condition: state
    entity_id: light.livingroom
    state: "on"        # 冗余!trigger 已经保证了 this is "on"

修复:删除 condition,或者把 condition 改成检查其他实体。

案例 2:忘记配 mode,延时关灯反复触发

# ❌ 错误:没有 mode,每次触发都创建新的延时任务
alias: "延时关灯"
trigger:
  - platform: state
    entity_id: binary_sensor.bedroom_motion
    to: "off"
action:
  - delay: minutes: 10
  - service: light.turn_off
    target:
      entity_id: light.bedroom

现象:人在卧室来回走动,每次触发都创建一个新的 10 分钟后关灯任务。最后灯关了又开、开了又关。

修复:加 mode: restart。

案例 3:重启后所有自动化同时 firing

# ❌ 错误:state 触发器在重启时重新评估所有实体
trigger:
  - platform: state
    entity_id: binary_sensor.front_door
    to: "open"

现象:HA 重启后,如果前门恰好是 open 状态,所有监听 front_door 的自动化同时触发。

修复:加 for: 延迟。

trigger:
  - platform: state
    entity_id: binary_sensor.front_door
    to: "open"
    for:
      seconds: 5       # 状态持续 5 秒才算有效触发

案例 4:用 condition 代替 trigger

# ❌ 错误:condition 不会"等待",它只检查当前状态
trigger:
  - platform: time
    at: "07:00:00"
condition:
  - condition: state
    entity_id: person.zhangsan
    state: "home"      # 如果 7:00 张三不在家,这个自动化永远不会执行!

现象:设定 7:00 叫张三起床,但如果 7:00 他还没回家,这一天就叫不醒了。

修复:用 choose 或者改成 trigger 检查时间 + condition 检查状态。

案例 5:属性变化不触发

# ❌ 错误:brightness 属性变化不会触发 state 触发器
trigger:
  - platform: state
    entity_id: light.livingroom
    to: "on"
    # 想监听 brightness 变化?state 触发器做不到

修复:用 template 触发器。

trigger:
  - platform: template
    value_template: "{{ state_attr('light.livingroom', 'brightness') | int > 200 }}"

案例 6:from/to 写反了

# ❌ 错误:逻辑反了,门打开时不触发,关门时才触发
trigger:
  - platform: state
    entity_id: binary_sensor.front_door
    from: "open"       # 从 open 变成...
    to: "open"         # open?这永远不会匹配

# ✅ 正确
trigger:
  - platform: state
    entity_id: binary_sensor.front_door
    from: "closed"
    to: "open"

案例 7:时间条件时区错误

# ❌ 错误:没有指定时区,用 UTC 时间
condition:
  - condition: time
    after: "18:00"
    before: "23:00"

现象:在中国(UTC+8),自动化在早上 10 点到下午 3 点执行。

修复:在 HA 全局设置里配置时区,或者用 timezone condition。

condition:
  - condition: time
    after: "18:00"
    before: "23:00"
    timezone: "Asia/Shanghai"

案例 8:重复的 condition

# ❌ 错误:两个 condition 逻辑冲突
condition:
  - condition: state
    entity_id: light.livingroom
    state: "on"
  - condition: state
    entity_id: light.livingroom
    state: "off"       # 矛盾!不可能同时 on 和 off

修复:删掉矛盾的 condition。

案例 9:mode: queued 导致积压

# ❌ 错误:排队模式导致大量积压
alias: "每分钟记录一次温度"
mode: queued           # 每条都排队,队列无限增长
trigger:
  - platform: time_pattern
    minutes: "/1"
action:
  - service: script.record_temperature
    metadata: {}

现象:HA 重启或卡顿后,队列里有几百条待执行,一口气全跑完。

修复:改用 mode: single 或 mode: restart。

案例 10:忘记加 id,无法调试

# ❌ 错误:没有 id,trace 里看不出是哪条自动化
alias: "人来灯亮"
trigger:
  - platform: state
    entity_id: binary_sensor.hallway_motion
    to: "on"

修复:永远加 id。

alias: "人来灯亮"
id: "hallway_motion_light"    # 唯一标识,调试必备
trigger:
  - platform: state
    entity_id: binary_sensor.hallway_motion
    to: "on"

调试技巧

1. 用 Developer Tools 测试 trigger

Settings → System → Logs → 搜索 automation

2. 用 Trace 看执行路径

Automations → 点某条自动化 → Trace → 展开看每一步

3. 临时禁用 condition 调试

# 调试时注释掉 condition,确认 trigger 本身没问题
# condition:
#   - condition: time
#     after: "18:00"

4. 用 trigger.id 区分多条 trigger

trigger:
  - platform: state
    id: "motion_on"
    entity_id: binary_sensor.hallway_motion
    to: "on"
  - platform: state
    id: "motion_off"
    entity_id: binary_sensor.hallway_motion
    to: "off"

action:
  - service: light.toggle
    target:
      entity_id: light.hallway
  # 用 trigger.id 可以做不同处理
  - if:
      - condition: template
        value_template: "{{ trigger.id == 'motion_on' }}"
    then:
      - service: light.turn_on
        target:
          entity_id: light.hallway
    else:
      - delay: seconds: 120
      - service: light.turn_off

最佳实践清单

检查项 正确做法
trigger 和 condition 是否重复? trigger 负责事件,condition 负责额外限制,不重叠
是否配了 mode? 延时类自动化用 restart,定时类用 single
是否加了 id? 所有自动化都必须有唯一 id
重启后会不会 firing? state 触发器加 for: 延迟
时区是否正确? 全局设置或 condition 里指定 timezone
实体 ID 是否存在? 用 Developer Tools → States 验证

写在最后

HA 自动化的核心是理解触发器和条件的执行模型。trigger 是"点火",condition 是"安全检查",action 是"执行"。三者各司其职,不要混用。

记住三句话:

  1. trigger 触发后 condition 只检查一次
  2. mode 选错会导致重复执行或漏执行
  3. 永远加 id,方便调试

把这三条刻进 DNA 里,80% 的自动化问题不会再犯。