返回

18. OpenClaw 实战:构建智能家居控制中心

本文演示如何使用 OpenClaw 构建智能家居控制中心,集成 Home Assistant、智能设备控制、语音指令、自动化场景等功能,打造真正的智能生活体验。

适用于 OpenClaw v2026.2 | 需要 Home Assistant 实例或智能设备,安装 home-assistant 技能。

TL;DR: 安装技能 openclaw skill install home-assistant。配置连接:"url": "http://homeassistant.local:8123", "token": "..."。控制设备:"开灯" → light.living_room.on。自动化场景:"scenes": [{"id": "morning", "schedule": "0 7 * * *"}]。能耗监控:查询用电统计。安全监控:门窗传感器 + 移动检测 + 异常告警。

实战目标

构建一个能够:

  1. 控制智能家居设备
  2. 创建自动化场景
  3. 语音控制家电
  4. 能耗监控与优化
  5. 安全监控提醒

的智能家居控制中心。

安装必要技能

# Home Assistant 技能
openclaw skill install home-assistant

# 智能家居技能
openclaw skill install smart-home-io

# 天气技能
openclaw skill install weather-api

Home Assistant 集成

配置连接

{
  "skills": {
    "home-assistant": {
      "url": "http://homeassistant.local:8123",
      "token": "${HOME_ASSISTANT_TOKEN}",
      "entities": {
        "lights": ["light.living_room", "light.bedroom"],
        "climate": ["climate.living_room"],
        "sensors": ["sensor.temperature", "sensor.humidity"],
        "switches": ["switch.tv", "switch.air_purifier"]
      }
    }
  }
}

设备控制场景

用户:打开客厅的灯

Agent:好的,正在打开客厅的灯...

[调用 Home Assistant API]
light.living_room → on

客厅的灯已打开。当前亮度 80%。

需要调整亮度吗?
用户:把空调调到 25 度

Agent:正在设置空调温度...

[调用 climate API]
climate.living_room → 25°C

空调已设置为 25 度,模式:制冷。

当前室温:28°C,预计 20 分钟后达到目标温度。

批量控制

用户:我要睡觉了,把所有灯关掉

Agent:好的,晚安!

[批量控制设备]
light.living_room → off
light.bedroom → off
light.kitchen → off
switch.tv → off

已关闭所有灯和电视。空调已调至睡眠模式(26°C)。

需要我设置明早的闹钟吗?

自动化场景

场景配置

{
  "scenes": [
    {
      "id": "morning",
      "name": "早安模式",
      "trigger": {
        "type": "time",
        "time": "07:00"
      },
      "actions": [
        { "entity": "light.bedroom", "action": "turn_on", "brightness": 30 },
        { "entity": "switch.coffee_machine", "action": "turn_on" },
        { "entity": "climate.living_room", "action": "set_temperature", "value": 22 }
      ]
    },
    {
      "id": "leave_home",
      "name": "离家模式",
      "trigger": {
        "type": "location",
        "condition": "leave_home"
      },
      "actions": [
        { "entity": "group.all_lights", "action": "turn_off" },
        { "entity": "switch.tv", "action": "turn_off" },
        { "entity": "climate.home", "action": "turn_off" }
      ]
    }
  ]
}

场景触发

用户:我出门了

Agent:启动离家模式...

[触发场景:leave_home]

离家模式已启动:
- ✅ 关闭所有灯
- ✅ 关闭电视
- ✅ 关闭空调
- ✅ 启动安防监控

预计回家时间?我可以帮你准备回家模式。

智能场景

用户:设置一个晚上回家的场景

Agent:好的,我来创建回家场景...

场景「回家模式」已创建:
- 触发条件:位置到家 + 时间 18:00-23:00
- 动作:
  1. 打开客厅灯(亮度 50%)
  2. 打开空调(根据当前温度自动选择模式)
  3. 播放欢迎语音
  4. 启动热水器

还需要添加其他设备吗?

语音控制

语音指令映射

{
  "voiceCommands": {
    "开灯": { "action": "light.turn_on" },
    "关灯": { "action": "light.turn_off" },
    "调亮": { "action": "light.brightness_up" },
    "调暗": { "action": "light.brightness_down" },
    "打开空调": { "action": "climate.turn_on" },
    "关闭空调": { "action": "climate.turn_off" }
  }
}

自然语言控制

用户:有点冷,把温度调高一点

Agent:当前室温 22°C,空调已关闭。

要打开空调并设置到多少度?
- 建议:24-25°C
- 舒适范围:23-26°C

用户:打开空调,设为 25 度

Agent:好的...

[climate.living_room → on, temperature: 25]

空调已打开,设置为 25°C 制热模式。
预计 15 分钟后达到目标温度。

能耗监控

能耗统计

用户:今天的用电情况怎么样?

Agent:让我查询今天的能耗数据...

[获取能耗数据]
[分析使用情况]

## 今日能耗报告

### 总用电量
- 今日:12.5 kWh
- 昨日:14.2 kWh
- 节省:1.7 kWh (-12%)

### 设备用电排行
1. 空调:5.2 kWh (42%)
2. 热水器:3.1 kWh (25%)
3. 照明:1.8 kWh (14%)
4. 电视:1.2 kWh (10%)
5. 其他:1.2 kWh (9%)

### 用电建议
- 💡 空调温度可调至 26°C,可节省约 20%
- 💡 热水器可设置定时关闭
- 💡 使用 LED 灯替代传统灯泡

### 高峰时段
- 18:00-21:00 用电最高,建议错峰使用大功率电器

需要我设置自动化节能场景吗?

节能场景

{
  "scenes": [
    {
      "id": "energy_saving",
      "name": "节能模式",
      "actions": [
        { "entity": "climate.home", "action": "set_temperature", "value": 26 },
        { "entity": "light.*", "action": "set_brightness", "value": 50 },
        { "entity": "switch.standby_devices", "action": "turn_off" }
      ]
    }
  ]
}

安全监控

安全配置

{
  "security": {
    "sensors": {
      "motion": ["binary_sensor.motion_living_room", "binary_sensor.motion_bedroom"],
      "door": ["binary_sensor.door_main", "binary_sensor.door_back"],
      "window": ["binary_sensor.window_bedroom", "binary_sensor.window_living"],
      "camera": ["camera.living_room", "camera.door"]
    },
    "alerts": {
      "motion_detected": {
        "enabled": true,
        "notify": true,
        "record": true
      },
      "door_opened": {
        "enabled": true,
        "notify": true
      }
    }
  }
}

安全提醒

用户:我出门了,开启安防模式

Agent:启动安防模式...

[安防模式已启动]

安防状态:
- ✅ 门窗传感器:已启用
- ✅ 移动侦测:已启用
- ✅ 摄像头录像:已启用
- ✅ 异常通知:已启用

我将监控以下内容:
- 门窗开关状态
- 室内移动检测
- 异常声音检测

如果检测到异常,我会立即通知你。

异常处理

[告警触发]

Agent:🚨 安全警报!

检测到异常:
- 时间:2026-02-26 14:30:25
- 类型:门口移动检测
- 设备:camera.door

[附上门口摄像头截图]

是否需要我:
1. 查看实时画面
2. 报警
3. 通知家人
4. 开启警示灯

请立即回复!

自动化规则

规则配置

{
  "automations": [
    {
      "id": "auto_lights_sunset",
      "name": "日落自动开灯",
      "trigger": {
        "type": "sun",
        "event": "sunset"
      },
      "actions": [
        { "entity": "light.living_room", "action": "turn_on", "brightness": 70 },
        { "entity": "light.hallway", "action": "turn_on", "brightness": 50 }
      ]
    },
    {
      "id": "auto_climate_temp",
      "name": "自动温控",
      "trigger": {
        "type": "sensor",
        "entity": "sensor.temperature",
        "above": 28
      },
      "actions": [
        { "entity": "climate.living_room", "action": "turn_on" },
        { "entity": "climate.living_room", "action": "set_temperature", "value": 25 }
      ]
    }
  ]
}

规则管理

用户:创建一个规则:温度超过 28 度自动开空调

Agent:好的,我来创建这个规则...

[创建自动化规则]

规则已创建:
- 名称:自动温控
- 触发:温度传感器 > 28°C
- 动作:打开空调,设置 25°C

是否还需要添加其他条件?
例如:
- 只在有人在时触发
- 只在特定时间段生效
- 发送通知提醒

整合体验

一天智能生活

{
  "dailyAutomations": [
    {
      "time": "07:00",
      "name": "早安",
      "actions": ["开灯 30%", "开空调 22°C", "开咖啡机"]
    },
    {
      "time": "08:30",
      "name": "离家",
      "condition": "位置离家",
      "actions": ["关所有灯", "关空调", "开启安防"]
    },
    {
      "time": "18:00",
      "name": "回家准备",
      "condition": "位置到家前 15 分钟",
      "actions": ["开客厅灯", "开空调", "启动热水器"]
    },
    {
      "time": "22:00",
      "name": "睡眠准备",
      "actions": ["关客厅灯", "卧室灯 30%", "空调 26°C"]
    },
    {
      "time": "23:00",
      "name": "晚安",
      "actions": ["关所有灯", "空调睡眠模式", "开启安防"]
    }
  ]
}

一键场景

用户:我要看电影

Agent:启动电影模式...

[电影模式已启动]
- ✅ 客厅灯:调暗至 20%
- ✅ 窗帘:关闭
- ✅ 电视:打开
- ✅ 音响:切换到电影模式
- ✅ 空调:舒适温度 24°C

需要我推荐电影吗?

小结

通过 OpenClaw 构建智能家居控制中心:

  • 设备控制:灯光、空调、家电
  • 自动化场景:早安、离家、回家、睡眠
  • 语音控制:自然语言指令
  • 能耗监控:用电统计、节能建议
  • 安全监控:门窗传感、移动检测、异常告警

更新记录

  • 2026-02-26:初版发布

系列导航