Skip to content

title: HomeAssistant 自动化 YAML 速查库:15 个即抄即用的自动化模板 description: 15 个拿来即用的 HA 自动化 YAML 模板,覆盖场景:人来灯亮/人走灯灭、温湿度自动调控、门窗传感器联动、漏水/烟雾告警、睡眠模式、离家模式、设备定时开关、电池低电量提醒。每个模板含注释和调参建议。 tags: [homeassistant, automation, yaml, smart-home, template] lang: zh date: 2026-10-22 original: true answer: | HA 自动化 YAML 最常用 15 个模板:人来灯亮(person + light)、温湿度自动调控(climate + sensor)、 门窗联动(door/window + lock/light)、漏水烟雾告警(sensor + notification)、 睡眠/离家模式切换、设备定时开关、电池低电量提醒。 核心原则:用 Trigger-Based 代替 Condition-Based,用 platform: state 代替 platform: time_pattern。 faq: - q: automation.yaml 和 dashboard 里的可视化编辑器写出来的自动化有什么区别? a: 本质一样,都是 YAML。可视化编辑器生成的代码有时会有冗余 condition,手动写的更简洁可控。 推荐复杂逻辑用 YAML 手写,简单逻辑用可视化编辑器快速创建。 - q: 自动化不触发怎么办? a: 三大排查步骤:① Settings → System → Logs 看有没有对应 automation 的错误日志; ② Developer Tools → States 确认触发实体(entity_id)的状态值是否真的变了; ③ 检查 enabled: false 或者 initial_state: off 导致自动化被禁用。 - q: 同一个触发器有多条自动化冲突怎么办? a: 用 mode: restart 让新触发取消正在执行的旧动作; 或用 mode: queued 排队执行;最安全是 mode: single 忽略重复触发。 人来灯亮的场景用 restart,定时开关用 single。 - q: 模板里的 entity_id 和 area 怎么找? a: Settings → Devices & Services → 点设备 → 复制 Entity ID; 区域用 Settings → Areas 创建并分配设备,然后用 area_id 代替 entity_id 可以批量触发。


HomeAssistant 自动化 YAML 速查库:15 个即抄即用的自动化模板

装了 HA 之后最头疼的不是"能做什么",而是"怎么写出能跑的 YAML"。官方文档讲语法,不讲场景;社区教程讲场景,不给你现成模板。这篇把 15 个最高频的自动化场景写成可直接粘贴的 YAML 模板,每个都带注释和调参建议。

使用指南

  1. 打开 /config/automations.yaml(或在 HA 界面「配置 → 自动化 & 场景 → 辅助功能 → 编辑器」粘贴)
  2. 把模板中的 your_entity_id 替换成你设备的真实 entity_id
  3. 点击「保存」→ 右上角三个点 → 「重新加载自动化」
  4. 用 Developer Tools → States 确认实体状态正常变化,再观察自动化是否触发

模板 1:人来灯亮(移动传感器 + 灯)

alias: "人来灯亮 - 走廊"
description: "走廊移动传感器检测到有人时打开灯,人走后 2 分钟关闭"
trigger:
  - platform: state
    entity_id: binary_sensor.corridor_motion
    from: "off"
    to: "on"
condition:
  - condition: state
    entity_id: light.corridor_light
    state: "off"        # 灯已亮则不再触发,避免重复
action:
  - service: light.turn_on
    target:
      entity_id: light.corridor_light
    data:
      brightness_pct: 80   # 亮度 80%,可根据喜好调整
      color_temp: 4000     # 暖白光 4000K
  - delay: seconds: 120    # 2 分钟无人后关灯
  - service: light.turn_off
    target:
      entity_id: light.corridor_light
mode: restart            # 新触发时取消之前的延时,人还在就一直亮着

调参建议:brightness_pct 白天可调高(100%),夜晚可调低(30%)。可用 condition 加时间段判断。


模板 2:人走灯灭(超时自动关灯)

alias: "人走灯灭 - 客厅"
description: "客厅没人超过 10 分钟自动关灯"
trigger:
  - platform: state
    entity_id: binary_sensor.livingroom_motion
    from: "on"
    to: "off"
condition:
  - condition: state
    entity_id: light.livingroom_light
    state: "on"
action:
  - delay: minutes: 10
  - service: light.turn_off
    target:
      entity_id: light.livingroom_light
mode: single             # 同一时间只允许一个延时任务,防止重复创建

模板 3:温湿度自动调控(空调 + 传感器)

alias: "恒温模式 - 卧室"
description: "温度超过 26°C 开空调制冷,低于 20°C 开制热"
trigger:
  - platform: state
    entity_id: sensor.bedroom_temperature
condition: []
action:
  - choose:
      - conditions:
          - condition: template
            value_template: "{{ states('sensor.bedroom_temperature') | float > 26 }}"
        sequence:
          - service: climate.set_hvac_mode
            target:
              entity_id: climate.bedroom_ac
            data:
              hvac_mode: cool
              temperature: 26
      - conditions:
          - condition: template
            value_template: "{{ states('sensor.bedroom_temperature') | float < 20 }}"
        sequence:
          - service: climate.set_hvac_mode
            target:
              entity_id: climate.bedroom_ac
            data:
              hvac_mode: heat
              temperature: 22
mode: single

调参建议:建议加 sensor.bedroom_humidity 条件,湿度 > 70% 时优先除湿模式(hvac_mode: dry)。


模板 4:门窗联动(开门开灯 + 提醒)

alias: "后门开门提醒"
description: "后门打开时开灯并推送手机通知"
trigger:
  - platform: state
    entity_id: binary_sensor.back_door
    from: "closed"
    to: "open"
condition:
  - condition: time
    after: "18:00"        # 只在傍晚后触发,白天不需要
    before: "23:00"
action:
  - service: light.turn_on
    target:
      entity_id: light.backyard_light
  - service: notify.mobile_app_your_phone
    data:
      title: "后门打开了"
      message: "后院灯已开启,请注意安全"
mode: single

模板 5:漏水传感器告警

alias: "厨房漏水告警"
description: "检测到漏水立即关闭水阀并推送通知"
trigger:
  - platform: state
    entity_id: binary_sensor.kitchen_water_leak
    to: "on"              # 干=off,漏水=on
condition: []
action:
  - service: valve.close
    target:
      entity_id: valve.kitchen_water_main
  - service: notify.mobile_app_your_phone
    data:
      title: "⚠️ 厨房漏水!"
      message: "已自动关闭总水阀,请尽快处理"
      data:
        push:
          sound: "alarm.ncb"   # 用尖锐提示音
          urgency: high
  - service: light.turn_on
    target:
      entity_id: light.kitchen_light
mode: single

重要:务必测试水阀是否能正常关闭,建议每月手动测试一次。


模板 6:烟雾/一氧化碳告警

alias: "烟雾告警"
description: "检测到烟雾或一氧化碳时大声报警并推送通知"
trigger:
  - platform: state
    entity_id:
      - binary_sensor.smoke_alarm
      - binary_sensor.co_alarm
    to: "on"
condition: []
action:
  - service: media_player.volume_set
    target:
      entity_id: media_player.speaker
    data:
      volume_level: 1.0
  - service: media_player.play_media
    target:
      entity_id: media_player.speaker
    data:
      media_content_id: "https://your-server.local/alert.mp3"
      media_content_type: "audio/mp3"
  - service: notify.mobile_app_your_phone
    data:
      title: "🚨 烟雾/CO 告警!"
      message: "请立即检查!"
      data:
        push:
          urgency: high
mode: restart

模板 7:睡眠模式(一键关闭全屋灯光)

alias: "睡眠模式"
description: "点击场景按钮关闭全屋灯光,打开卧室小夜灯"
trigger:
  - platform: state
    entity_id: button.sleep_mode    # 物理按钮或 HA 界面上的场景按钮
    from: "off"
    to: "on"
condition: []
action:
  - service: light.turn_off
    target:
      entity_id: all                # 关闭所有灯
      # 或指定:entity_id: [light.livingroom, light.kitchen, light.hallway]
  - service: light.turn_on
    target:
      entity_id: light.bedside_nightlight
    data:
      brightness_pct: 10
      rgb_color: [255, 200, 150]    # 暖橙色小夜灯
  - service: notify.mobile_app_your_phone
    data:
      title: "🌙 睡眠模式已开启"
      message: "全屋灯光已关闭,祝您晚安"
mode: single

模板 8:离家模式(一键布防)

alias: "离家模式"
description: "点击离家按钮:关所有灯、关空调、开启安防传感器"
trigger:
  - platform: state
    entity_id: button.away_mode
    from: "off"
    to: "on"
condition: []
action:
  # 关所有灯
  - service: light.turn_off
    target:
      area_id: living_room          # 按区域关闭
  - service: light.turn_off
    target:
      area_id: bedroom
  - service: light.turn_off
    target:
      area_id: kitchen
  # 关空调
  - service: climate.turn_off
    target:
      entity_id: climate.livingroom_ac
  - service: climate.turn_off
    target:
      entity_id: climate.bedroom_ac
  # 锁门(如果有的话)
  - service: lock.lock
    target:
      entity_id: lock.front_door
  # 通知
  - service: notify.mobile_app_your_phone
    data:
      title: "🏠 离家模式已开启"
      message: "灯光空调已关闭,安防已布防"
mode: single

模板 9:设备定时开关(台灯晚上自动关)

alias: "书房台灯定时关闭"
description: "台灯每天晚上 23:00 自动关闭"
trigger:
  - platform: time
    at: "23:00:00"
condition:
  - condition: state
    entity_id: light.desk_lamp
    state: "on"
action:
  - service: light.turn_off
    target:
      entity_id: light.desk_lamp
mode: single

扩展:可以用 time_pattern 做周期性定时:

trigger:
  - platform: time_pattern
    minutes: "/30"        # 每 30 分钟执行一次

模板 10:电池低电量提醒

alias: "低电量提醒 - 传感器"
description: "传感器电池低于 20% 时推送更换提醒"
trigger:
  - platform: state
    entity_id: sensor.front_door_battery
    above: 20             # 电量高于 20% 不提醒
condition:
  - condition: template
    value_template: "{{ trigger.to_state.state | float < 20 }}"
action:
  - service: notify.mobile_app_your_phone
    data:
      title: "🔋 电池低电量提醒"
      message: "大门传感器电池仅剩 {{ states('sensor.front_door_battery') }}%,请及时更换"
mode: single

模板 11:窗帘随日照自动开合

alias: "晨起窗帘自动打开"
description: "日出时自动打开卧室窗帘"
trigger:
  - platform: sun
    event: sunrise
    offset: "+00:15:00"   # 日出后 15 分钟
condition:
  - condition: state
    entity_id: cover.bedroom_shutter
    state: "closed"
action:
  - service: cover.open_cover
    target:
      entity_id: cover.bedroom_shutter
  - delay: minutes: 5
  - service: cover.set_cover_position
    target:
      entity_id: cover.bedroom_shutter
    data:
      position: 70          # 开到 70%,别全开
mode: single

模板 12:洗衣机完成提醒

alias: "洗衣机完成提醒"
description: "洗衣机运行完成后推送通知"
trigger:
  - platform: state
    entity_id: sensor.washing_machine_state
    to: "finished"          # 根据实际 entity 状态值调整
condition: []
action:
  - service: notify.mobile_app_your_phone
    data:
      title: "👔 洗衣完成"
      message: "洗衣机已完成,请及时晾晒"
  - service: media_player.play_media
    target:
      entity_id: media_player.kitchen_speaker
    data:
      media_content_id: "洗完了,该晾衣服了"
      media_content_type: "text/to_speech"
mode: single

模板 13:夜间起夜感应灯

alias: "夜间起夜感应灯"
description: "深夜移动传感器触发时,只开小夜灯不然大灯"
trigger:
  - platform: state
    entity_id: binary_sensor.hallway_motion
    from: "off"
    to: "on"
condition:
  - condition: time
    after: "22:00"
    before: "06:00"         # 只在夜间生效
  - condition: state
    entity_id: light.hallway_nightlight
    state: "off"
action:
  - service: light.turn_on
    target:
      entity_id: light.hallway_nightlight
    data:
      brightness_pct: 5     # 极低亮度,不打扰
mode: restart

模板 14:宠物喂食定时

alias: "宠物定时喂食"
description: "每天 7:00 和 19:00 自动投喂"
trigger:
  - platform: time
    at:
      - "07:00:00"
      - "19:00:00"
condition: []
action:
  - service: switch.turn_on
    target:
      entity_id: switch.pet_feeder
  - delay: seconds: 3
  - service: switch.turn_off
    target:
      entity_id: switch.pet_feeder
  - service: notify.mobile_app_your_phone
    data:
      title: "🐱 喂食完成"
      message: "宠物自动喂食器已工作,量:标准份"
mode: single

模板 15:全屋一键场景(观影模式)

alias: "观影模式"
description: "一键关闭主灯,打开氛围灯,降下投影幕布"
trigger:
  - platform: state
    entity_id: button.movie_mode
    from: "off"
    to: "on"
condition: []
action:
  - service: light.turn_off
    target:
      area_id: living_room    # 关主灯
  - service: light.turn_on
    target:
      entity_id: light.tv_background_led
    data:
      brightness_pct: 30
      rgb_color: [30, 60, 120]  # 深蓝色氛围
  - service: cover.close_cover
    target:
      entity_id: cover.projector_screen
  - service: media_player.media_play
    target:
      entity_id: media_player.projector
mode: single

调试技巧速查

问题 排查步骤
自动化不触发 Developer Tools → Events → 监听 state_changed 事件,确认触发实体状态变化
触发了但 action 没执行 Settings → System → Logs 搜索 automation 关键字
action 执行了但设备没响应 Developer Tools → States 确认 entity_id 存在且可控制
多个自动化互相冲突 检查 mode 设置,用 mode: single 避免重复执行
定时任务没按时执行 确认 HA 服务器时间正确,时区设置正确

写在最后

15 个模板覆盖了 80% 的家庭自动化场景。剩下的 20% 通常是特殊需求,可以在这些模板基础上修改。记住三个原则:

  1. 简单优先——能用一个自动化解决的,不要拆成两个
  2. 可测试——每个自动化写完后先手动触发测试一次再依赖它
  3. 留注释——三个月后你不会记得每个 entity_id 是干嘛的

祝你的智能家居越来越聪明。