Skip to content

HomeAssistant Blueprint 实战模板库:8 个开箱即用的智能场景自动化

装了 HA 之后,最麻烦的不是"怎么写自动化",而是"同样的逻辑要写 N 遍"。比如每个房间都要人来灯亮,难道要写 10 个相似的自动化?Blueprint 就是解决这个问题的——写好一次模板,导入后选实体就能生成自动化。

这篇分享 8 个生产环境验证过的 Blueprint 模板,每个都带完整配置和调参建议。

什么是 Blueprint

Blueprint 是 Home Assistant 的可复用自动化模板。它定义了自动化的逻辑框架,但不绑定具体设备。导入后选择实体,HA 自动生成完整的 YAML 自动化。

Blueprint 模板              生成的自动化
┌─────────────────┐       ┌─────────────────┐
│ Trigger:        │       │ Trigger:        │
│   motion_sensor │       │   binary_sensor. │
│                 │  →    │   livingroom_   │
│ Condition:      │       │   motion        │
│   after sunset  │       │                 │
│ Action:         │       │ Action:         │
│   turn_on light │       │   light.turn_on │
└─────────────────┘       │   light.living  │
                          └─────────────────┘

优势: - 一次编写,多处复用 - 统一逻辑,降低维护成本 - 非技术人员也能通过 UI 配置

模板 1:欢迎回家模式

# blueprints/automation/welcome_home.yaml
blueprint:
  name: 欢迎回家模式
  description: 当最后一个人回家时,自动开灯、开空调、播放音乐
  domain: automation
  input:
    people_sensor:
      name: 在家传感器
      selector:
        entity:
          multiple: true
          domain: person
    lights:
      name: 灯光
      selector:
        target:
          entity:
            domain: light
    climate:
      name: 空调
      selector:
        entity:
          domain: climate
    media_player:
      name: 音箱
      selector:
        entity:
          domain: media_player
    playlist:
      name: 播放列表
      selector:
        text:
          default: "轻松音乐"

trigger:
  - platform: state
    entity_id: !input people_sensor
    to: "home"
condition:
  - condition: template
    value_template: "{{ trigger.to_state.state == 'home' }}"
action:
  - service: light.turn_on
    target: !input lights
    data:
      brightness_pct: 80
      color_temp: 4000
  - service: climate.set_hvac_mode
    target: !input climate
    data:
      hvac_mode: cool
      temperature: 24
  - service: media_player.play_media
    target: !input media_player
    data:
      media_content_id: !input playlist
      media_content_type: "playlist"
  - service: notify.mobile_app_your_phone
    data:
      title: "🏠 欢迎回家"
      message: "灯光空调已开启,音乐正在播放"
mode: single

调参建议: - brightness_pct: 80——白天可调高,夜晚可调低(30%) - temperature: 24——根据季节调整,夏天 24-26°C,冬天 20-22°C - 可加时间条件:只在 18:00-23:00 触发

模板 2:离家安防模式

# blueprints/automation/away_mode.yaml
blueprint:
  name: 离家安防模式
  description: 当最后一个人离开时,自动关灯、关空调、锁门、开启安防
  domain: automation
  input:
    people_sensor:
      name: 在家传感器
      selector:
        entity:
          multiple: true
          domain: person
    lights:
      name: 要关闭的灯
      selector:
        target:
          entity:
            domain: light
    climate:
      name: 要关闭的空调
      selector:
        entity:
          domain: climate
    locks:
      name: 门锁
      selector:
        target:
          entity:
            domain: lock
    security_camera:
      name: 安防摄像头
      selector:
        entity:
          domain: camera
    notification_phone:
      name: 手机通知
      selector:
        entity:
          domain: notify

trigger:
  - platform: state
    entity_id: !input people_sensor
    from: "home"
    to: "not_home"
condition:
  - condition: template
    value_template: >
      {{ states('person.zhangsan') == 'not_home' and
         states('person.li si') == 'not_home' }}
action:
  - service: light.turn_off
    target: !input lights
  - service: climate.turn_off
    target: !input climate
  - service: lock.lock
    target: !input locks
  - service: camera.record
    target: !input security_camera
    data:
      duration: 300
  - service: notify.mobile_app_your_phone
    data:
      title: "🏠 离家模式已开启"
      message: "灯光空调已关闭,门锁已锁定,安防已布防"
mode: single

调参建议: - 加延迟:人刚出门可能又回来,加 delay: minutes: 5 避免误触发 - 可加例外:宠物在家时不触发(检测 pet_sensor)

模板 3:睡眠场景

# blueprints/automation/bedtime.yaml
blueprint:
  name: 睡眠场景
  description: 一键开启睡眠模式:关灯、锁门、调空调、开小夜灯
  domain: automation
  input:
    lights_to_off:
      name: 要关闭的灯
      selector:
        target:
          entity:
            domain: light
    nightlight:
      name: 小夜灯
      selector:
        target:
          entity:
            domain: light
    locks:
      name: 要锁的门
      selector:
        target:
          entity:
            domain: lock
    climate:
      name: 卧室空调
      selector:
        entity:
          domain: climate
    notification:
      name: 通知方式
      selector:
        target:
          entity:
            domain: notify

trigger:
  - platform: state
    entity_id: !input notification
    from: "off"
    to: "on"
condition: []
action:
  - service: light.turn_off
    target: !input lights_to_off
  - service: light.turn_on
    target: !input nightlight
    data:
      brightness_pct: 10
      rgb_color: [255, 200, 150]
  - service: lock.lock
    target: !input locks
  - service: climate.set_hvac_mode
    target: !input climate
    data:
      hvac_mode: heat
      temperature: 22
  - service: notify.mobile_app_your_phone
    data:
      title: "🌙 睡眠模式已开启"
      message: "全屋灯光已关闭,祝您晚安"
mode: single

模板 4:能源优化 EV 充电

# blueprints/automation/ev_surplus_charging.yaml
blueprint:
  name: 太阳能余电充电
  description: 当光伏电量充足时,自动为电动车充电
  domain: automation
  input:
    pv_power_sensor:
      name: 光伏功率
      selector:
        entity:
          domain: sensor
    ev_charger:
      name: 充电桩
      selector:
        entity:
          domain: switch
    min_battery:
      name: 最低电量阈值
      selector:
        number:
          min: 0
          max: 100
          step: 5
          unit: "%"
      default: 20
    target_battery:
      name: 目标电量
      selector:
        number:
          min: 50
          max: 100
          step: 5
          unit: "%"
      default: 80

trigger:
  - platform: state
    entity_id: !input pv_power_sensor
condition:
  - condition: template
    value_template: >
      {{ states(!input pv_power_sensor) | float > 2000 }}
  - condition: template
    value_template: >
      {{ states('sensor.ev_battery') | float < !input target_battery }}
action:
  - service: switch.turn_on
    target: !input ev_charger
  - service: notify.mobile_app_your_phone
    data:
      title: "🔋 EV 充电中"
      message: >
        光伏功率 {{ states(!input pv_power_sensor) }}W,
        当前电量 {{ states('sensor.ev_battery') }}%,目标 {{ !input target_battery }}%
mode: restart

调参建议: - min_battery: 20——低于此值不充电,保护电池 - 可加时间窗口:只在 10:00-16:00 充电(午休时段光伏高峰) - 可接入电价:谷电时段优先充电

模板 5:窗户联动空调

# blueprints/automation/window_ac_control.yaml
blueprint:
  name: 开窗关空调
  description: 当窗户打开时自动关闭空调,开窗时恢复
  domain: automation
  input:
    windows:
      name: 窗户传感器
      selector:
        entity:
          multiple: true
          domain: binary_sensor
    ac:
      name: 空调
      selector:
        entity:
          domain: climate
    delay:
      name: 延迟时间(秒)
      selector:
        number:
          min: 0
          max: 300
          step: 10
          unit: "秒"
      default: 30

trigger:
  - platform: state
    entity_id: !input windows
    to: "on"
  - platform: state
    entity_id: !input windows
    to: "off"
condition: []
action:
  - choose:
      - conditions:
          - condition: state
            entity_id: !input windows
            state: "on"
        sequence:
          - delay: seconds: !input delay
          - condition: state
            entity_id: !input windows
            state: "on"
          - service: climate.turn_off
            target: !input ac
      - conditions:
          - condition: state
            entity_id: !input windows
            state: "off"
        sequence:
          - service: climate.set_hvac_mode
            target: !input ac
            data:
              hvac_mode: cool
              temperature: 24
mode: restart

调参建议: - delay: 30——避免开窗通风瞬间误触发,等 30 秒确认 - 可加温度条件:室外温度低于 26°C 才开窗关空调

模板 6:智能照明日落调节

# blueprints/automation/sunset_light_adjust.yaml
blueprint:
  name: 日落智能调光
  description: 日落后 gradualy 调暗灯光,模拟自然光变化
  domain: automation
  input:
    lights:
      name: 要调节的灯
      selector:
        target:
          entity:
            domain: light
    start_brightness:
      name: 初始亮度
      selector:
        number:
          min: 10
          max: 100
          step: 10
          unit: "%"
      default: 100
    end_brightness:
      name: 最终亮度
      selector:
        number:
          min: 5
          max: 30
          step: 5
          unit: "%"
      default: 20
    duration:
      name: 调节时长(分钟)
      selector:
        number:
          min: 10
          max: 120
          step: 10
          unit: "分钟"
      default: 30

trigger:
  - platform: sun
    event: sunset
condition: []
action:
  - variables:
      steps: 6
      delay_seconds: >
        {{ (duration * 60) / steps | int }}
  - repeat:
      count: !input steps
      sequence:
        - service: light.turn_on
          target: !input lights
          data:
            brightness_pct: >
              {{ start_brightness - (start_brightness - end_brightness) * repeat.index / steps | int }}
            color_temp: >
              {{ 4000 + (6500 - 4000) * repeat.index / steps | int }}
        - delay: seconds: !input delay_seconds
mode: single

模板 7:管道漏水保护

# blueprints/automation/leak_protection.yaml
blueprint:
  name: 漏水自动保护
  description: 检测到漏水立即关闭水阀并推送告警
  domain: automation
  input:
    leak_sensors:
      name: 漏水传感器
      selector:
        entity:
          multiple: true
          domain: binary_sensor
    water_valve:
      name: 水阀
      selector:
        entity:
          domain: valve
    notification:
      name: 通知方式
      selector:
        target:
          entity:
            domain: notify
    alarm_sound:
      name: 警报音
      selector:
        entity:
          domain: media_player

trigger:
  - platform: state
    entity_id: !input leak_sensors
    to: "on"
condition: []
action:
  - service: valve.close
    target: !input water_valve
  - service: media_player.volume_set
    target: !input alarm_sound
    data:
      volume_level: 1.0
  - service: media_player.play_media
    target: !input alarm_sound
    data:
      media_content_id: "https://your-server.local/alarm.mp3"
      media_content_type: "audio/mp3"
  - service: notify.mobile_app_your_phone
    data:
      title: "⚠️ 漏水告警!"
      message: "已自动关闭总水阀,请立即检查!"
      data:
        push:
          urgency: high
mode: single

重要: - 每月手动测试一次水阀关闭功能 - 考虑加 "Test Mode" 开关,测试时不触发 - 备用方案:手动关闭水阀的位置要标识清楚

模板 8:每日设备报告

# blueprints/automation/daily_device_report.yaml
blueprint:
  name: 每日设备状态报告
  description: 每天早上发送当日设备状态摘要
  domain: automation
  input:
    devices:
      name: 要监控的设备
      selector:
        entity:
          multiple: true
    notification:
      name: 通知方式
      selector:
        target:
          entity:
            domain: notify
    report_time:
      name: 报告时间
      selector:
        time:
          default: "08:00"

trigger:
  - platform: time
    at: !input report_time
condition: []
action:
  - variables:
      report: "📊 每日设备状态报告\n\n"
  - repeat:
      for_each: !input devices
      sequence:
        - variables:
            device_state: "{{ states(repeat.item) }}"
            device_name: "{{ state_attr(repeat.item, 'friendly_name') | default(repeat.item) }}"
        - variables:
            report: "{{ report + '- ' + device_name + ': ' + device_state + '\n' }}"
  - service: notify.mobile_app_your_phone
    data:
      title: "📱 每日设备报告"
      message: !input report
mode: single

使用最佳实践

实践 说明
做好注释 Blueprint 要写明每个输入的用途和推荐值
版本管理 用 Git 管理 Blueprint 文件,记录变更历史
测试优先 先在测试环境验证,确认无误再应用到生产
备份配置 导出 Blueprint YAML 备份,防止丢失
统一命名 实体命名遵循统一规范(如 area_device_type)

写在最后

Blueprint 是 HA 自动化的"积木"——写好一次,随处复用。八个模板覆盖了智能家居最常见的场景:

  1. 欢迎回家 —— 多设备联动
  2. 离家安防 —— 一键布防
  3. 睡眠场景 —— 一键关灯锁门
  4. 能源优化 —— EV 智能充电
  5. 窗户联动 —— 开窗关空调
  6. 智能照明 —— 日落调光
  7. 漏水保护 —— 安全兜底
  8. 每日报告 —— 设备状态汇总

用好 Blueprint,让你的智能家居真正"智能"起来,而不是写一堆重复的自动化。