AI Agent 智能家居自动化:Blueprint AI 2.0 自然语言编排
去年我花了三天手写 HA 自动化 YAML,结果"离家模式"总是漏掉两个灯。今年 HA 2025 上线了 Blueprint AI 2.0,我用一个 prompt 就搞定了一整套联动。但不是完全靠 AI,review 仍然是最重要的环节。
Blueprint AI 2.0 是什么
Blueprint AI 是 Home Assistant 2025.x 引入的功能,允许用户用自然语言描述自动化需求,AI 生成对应的 blueprint(可复用自动化模板)。
用户输入 prompt
↓
AI 分析意图(NLU)
↓
生成 YAML blueprint(带变量占位符)
↓
用户在 HA UI 中配置实体映射
↓
自动生成本地化 automation YAML
↓
进入测试阶段
关键区别:旧版 Blueprint 是"人工预写的 YAML 模板",新版 Blueprint AI 是"AI 从自然语言动态生成模板"。
上手:第一个 AI 自动化
步骤一:写 prompt
prompt 的质量决定 AI 生成的质量。遵循 SCE 框架:
- Scene(场景):什么情况下发生
- Constraints(约束):时间/条件/范围限制
- Expectation(期望):动作结果
场景:每天工作日早上 7:00-8:30,如果客厅无人且室外温度 > 28°C
约束:仅周一至周五,周末和节假日跳过;空调目标温度 26°C
动作:打开客厅空调制冷模式,设定 26°C,同时拉开卧室窗帘 50%
通知:手机推送"早安,客厅已降温准备中"
步骤二:AI 生成 blueprint
# 在 HA 里打开 Blueprint AI(Settings → Automations → Create Blueprint AI)
# 粘贴 prompt,AI 返回:
alias: "工作日晨间空调预冷"
description: "工作日早间根据室外温度自动启动客厅空调"
triggers:
- trigger: time_pattern
hours: "/1" # 每小时检查一次
conditions:
- condition: time
after: "07:00:00"
before: "08:30:00"
- condition: weekday
weekday:
- mon
- tue
- wed
- thu
- fri
- condition: state
entity_id: person.host
state: "home" # 家里有人才开
- condition: numeric_state
entity_id: sensor.outdoor_temperature
above: 28
actions:
- action: climate.set_temperature
target:
entity_id: climate.living_room
data:
temperature: 26
hvac_mode: cool
- action: cover.set_cover_position
target:
entity_id: cover.bedroom_curtain
data:
position: 50
- action: notify.mobile_app
data:
message: "早安,客厅空调已启动,目标 26°C"
步骤三:review 并部署
必须检查的点:
# ❌ AI 常见错误:condition 写反
conditions:
- condition: state
entity_id: person.host
state: "not_home" # 应该 home,不是 not_home
# ✅ 修正后
conditions:
- condition: state
entity_id: person.host
state: "home"
# 验证 condition 表达式
# HA → Developer Tools → Templating
{{ is_state('person.host', 'home') and is_state('climate.living_room', 'off') }}
提示词模板库
模板 1:离家安防模式
场景:当最后一个人离开家后
约束:延迟 10 分钟执行(防止误判),仅在 18:00-06:00 之间启用
动作:关闭所有灯光、关闭所有插座(除冰箱)、启动摄像头录制、设置安防模式
通知:门锁状态反馈给最后一个离开的人
模板 2:老人跌倒检测联动
场景:深夜 22:00-06:00,老人卧室有异常移动(基于雷达传感器)
约束:连续 5 分钟检测到非常规活动,且无语音回应(通过语音助手确认)
动作:立即通知子女手机、打开卧室灯、播放语音询问"需要帮助吗"
通知:紧急联系人收到位置 + 时间戳
模板 3:能耗优化
场景:电价高峰时段(10:00-14:00,18:00-22:00)
约束:仅当家里有人且空调温度设定 > 26°C
动作:自动调高空调温度至 27°C,关闭非必要电器(热水器、电暖器)
通知:推送当日节省电量估算
3 个真实翻车案例
案例一:AI 生成的 automation 每小时触发一次
现象:设置了"日出时开灯",结果每天凌晨 4:00、5:00、6:00 都触发一次。
根因:AI 用了 time_pattern hours: "/1" 当 trigger,没有加 condition 过滤时间窗口。
# ❌ AI 错误生成
triggers:
- trigger: time_pattern
hours: "/1"
conditions:
- condition: sun
event: sunrise # condition 不能替代 trigger!
修复:把 sun trigger 放进 triggers,不是 conditions:
# ✅ 正确
triggers:
- trigger: sun
event: sunrise
offset: "-00:15:00"
conditions: []
actions:
- action: light.turn_on
target:
entity_id: light.bedroom
判断:trigger 决定"何时激活",condition 决定"是否执行"。混淆两者是 AI 生成最常见的错误。
案例二:多条件 AND 写成了 OR
现象:"离家模式"在人不在家但有人在家时触发了——逻辑全反。
根因:prompt 里写"所有人都离开",AI 生成的是 or 而非 and。
# ❌ AI 错误:OR 逻辑
conditions:
- condition: or
conditions:
- condition: state
entity_id: person.person_a
state: "not_home"
- condition: state
entity_id: person.person_b
state: "not_home"
# ✅ 修正:AND 逻辑(所有人都离开)
conditions:
- condition: and
conditions:
- condition: state
entity_id: person.person_a
state: "not_home"
- condition: state
entity_id: person.person_b
state: "not_home"
排查技巧:在 HA dev-tools/templating 测试每个 condition 单独的值:
案例三:AI 用了不存在的 entity_id
现象:automation 创建了但从不执行,日志里报 Entity not found。
根因:prompt 里说"客厅灯",AI 生成了 light.living_room_light,但实际 entity 是 light.living_room_1。
修复:先用 HA UI 的"插入实体"功能找对 entity_id,再让 AI 用正确的 ID。
预防:prompt 里写实体 ID 而不是自然语言描述:
Blueprint AI 迁移路径
从旧版 YAML 自动化迁移到 AI 编排,分四步:
第 1 步:列出所有手动 YAML 自动化,按优先级排序
第 2 步:把简单自动化(单 trigger、单 action)用 AI 重写,验证通过后删除旧版
第 3 步:复杂自动化(多条件分支)保留 YAML,仅用 AI 生成 condition 片段
第 4 步:建立 prompt 库,重复场景直接复用
重要:不要一次性全换。每次只迁移 1-2 个,观察一周稳定后再迁移下一个。
总结
Blueprint AI 2.0 适合快速原型和模板生成,不适合最终交付。AI 生成的自动化必须经过人工 review,特别是 trigger/condition 逻辑和 entity_id 准确性。
我的判断:AI 在简单场景(开灯、关空调、推送通知)表现可靠,在复杂逻辑(多条件 AND/OR 嵌套、时间窗口、状态记忆)容易出错。建议把 AI 当成"打字员"而不是"架构师"——你给思路,它写代码,你负责审核。
延伸阅读
- HA 自动化入门:trigger/condition/action 三段式
- Node-RED + HA 复杂自动化流:可视化编排实战
- [AI Agent 智能家居:从 Blueprint 到 LangChain 本地部署(待发布)]