前言
OpenClaw 的强大之处在于它的 可扩展性。通过 SKILL.md,我们可以把任何 API、脚本或者系统工具封装成 Agent 可以理解的“技能”。
今天,我们不玩虚的。我们要让 OpenClaw 变成你的智能家居管家。
准备工作
你需要:
- 一个运行中的 Home Assistant (HA) 实例。
- 一个 HA 的 Long-Lived Access Token。
- OpenClaw 环境。
步骤一:理解 Skill 协议
在 OpenClaw 中,一个 Skill 本质上是一个目录,里面至少包含一个 SKILL.md。这个 Markdown 文件不仅是给人看的文档,更是给 LLM 看的 Prompt Context。
当 Agent 决定调用这个 Skill 时,它会读取 SKILL.md 中的指令,并根据定义的 Interface 去执行操作。
步骤二:创建目录结构
在你的 workspace 中创建如下结构:
skills/└── home-assistant/ ├── SKILL.md └── scripts/ └── control.sh步骤三:编写执行脚本
为了安全和解耦,我们不建议直接在 LLM 中拼接 curl 命令。我们写一个封装好的 Shell 脚本。
skills/home-assistant/scripts/control.sh:
#!/bin/bash
# 使用环境变量传递 Token,避免硬编码HA_URL="http://192.168.1.100:8123"TOKEN="$HA_TOKEN"
ACTION=$1 # on, off, toggleENTITY=$2 # light.living_room
if [ -z "$ACTION" ] || [ -z "$ENTITY" ]; then echo "Usage: $0 <action> <entity_id>" exit 1fi
curl -X POST \ -H "Authorization: Bearer $TOKEN" \ -H "Content-Type: application/json" \ -d "{\"entity_id\": \"$ENTITY\"}" \ "$HA_URL/api/services/light/turn_$ACTION"记得给脚本加执行权限:chmod +x scripts/control.sh。
步骤四:编写 SKILL.md (核心)
这是 Agent 的大脑部分。我们需要清晰地告诉它:我是谁,我能干什么,我该怎么干。
skills/home-assistant/SKILL.md:
---name: home_assistantdescription: Control smart home devices via Home Assistant.tools: - name: ha_light_control description: Turn lights on or off. parameters: type: object properties: action: type: string enum: [on, off] description: The action to perform. entity_id: type: string description: The entity ID of the light (e.g., light.living_room). required: [action, entity_id]---
# Home Assistant Skill
You are a smart home controller. You have access to the user's Home Assistant instance to control lights and switches.
## Capabilities
- Turn lights on/off using the `ha_light_control` tool.- If the user says "Turn on the living room lights", infer the entity ID `light.living_room` (or ask if unsure).
## Tools Implementation
### ha_light_control
This tool executes a local script to call the Home Assistant API.
- **Command**: `bash`- **Script Path**: `./scripts/control.sh`- **Execution**: `{{script_path}} {{action}} {{entity_id}}`
## Safety Rules
1. Never control security devices (locks, alarms) unless explicitly authorized with a confirmation code.2. If multiple devices match (e.g., "turn on the light"), ask for clarification: "Which light? Living room or Bedroom?"步骤五:注入配置
在 openclaw.json 的 env 中添加你的 HA_TOKEN,或者在启动 Gateway 时 export 环境变量。
实战效果
User: “把客厅的灯关掉。”
OpenClaw (Thinking):
- 用户意图:控制设备。
- 匹配 Skill:
home_assistant。 - 提取参数:Action=
off, Entity=light.living_room(推断)。 - 调用工具:
ha_light_control(action="off", entity_id="light.living_room")。 - 底层执行:
./scripts/control.sh off light.living_room。
Agent: “没问题,客厅的灯已关闭。💡“
总结
看,这就是 OpenClaw 的魅力。我们没有写一行复杂的 Python 插件代码,仅仅通过 Prompt Engineering + Shell Script,就赋予了 Agent 物理世界的控制能力。
这就是 Agentic Workflow 的未来:用自然语言编程,用脚本执行。