Mi Home (米家) Control
Control Xiaomi smart home devices and create automations via cloud API.
Setup
Prerequisites: INLINECODE0
Credentials: ~/.mijia_creds.json (chmod 600)
CODEBLOCK0
If login requires 2FA (device trust verification), obtain credentials via browser:
- 1. Open
https://account.xiaomi.com/pass/serviceLogin?sid=xiaomiio&_json=true in browser with existing cookies - POST to
serviceLoginAuth2 with hash password to get ssecurity + INLINECODE5 - Follow
location URL to get serviceToken from cookies
Device Control
List devices
CODEBLOCK1
Read property
CODEBLOCK2
Set property
CODEBLOCK3
Note: Mesh devices (BLE switches) return code:1 on set — this is normal (async confirmation). The command still executes. Verify by reading the property after a 1-2 second delay.
Batch control
Create a JSON file with commands:
[
{"did": "946635824", "siid": 2, "piid": 1, "value": true},
{"did": "946633803", "siid": 2, "piid": 1, "value": false}
]
CODEBLOCK5
MIoT Spec Reference
Each device has services (siid) → properties (piid). Look up specs at:
INLINECODE8
Common patterns:
- - Switch/Light: siid:2 piid:1 = on/off (bool)
- 3-way switch: siid:2/3/4 piid:1 = channel 1/2/3 (bool)
- AC: siid:2 piid:1 = on/off, piid:2 = mode, piid:3 = target temp
- Bath heater: siid:2 = light, siid:3 = PTC heater, siid:10 = environment (temp sensor)
- Water heater: siid:2 piid:1 = on/off, piid:2 = target temp, piid:3 = current temp
Create Automation Scenes
CODEBLOCK6
Scene JSON format (see references/scene_template.json):
CODEBLOCK7
Key fields:
- -
trigger.key: format event.<did>.<siid>.<eiid> for device events - INLINECODE12 : optional conditions (e.g., only if light is off)
- INLINECODE13 : JSON string with
props array - INLINECODE15 : 8 = user automation
- INLINECODE16 and
uid are auto-filled if omitted
Predefined Scenes
🚿 Bath Mode (洗澡模式)
When user says "我要洗澡":
- 1. Read bathroom temp: INLINECODE18
- If temp < 28°C → turn on bath heater:
set 927361805 3 1 1 (暖风), target 30°C - If temp ≥ 28°C → skip heater
- Turn on water heater:
set 965879649 2 1 1, set 43°C: INLINECODE21 - Turn on bathroom light: INLINECODE22
🐟 Auto Fish Feeding (出门自动喂鱼)
Feeds fish once per day when BOSS leaves home.
Architecture:
- 1. OpenClaw cron (every 5min): polls door lock event log via INLINECODE23
- If today has an auto-lock event (action=1) AND not fed yet → feed fish → record state
- After feeding, all subsequent checks skip immediately (daily dedup)
- State file
~/.fish_feed_state.json: tracks last feed date
Components:
- - Door lock events: 智能门锁2 Pro (did: 1175215651), event key
2.1020, action=1 = auto-lock (leaving home) - Fish feeder: 智能鱼缸 (did: 2026943875), action siid:2 aiid:1, in:[1] (1 portion)
- ⚠️ Action 参数格式:
"in": [1] 而不是
[{"piid":5,"value":1}]
Manual commands:
CODEBLOCK8
Trigger logic:
- - Polls lock event log for auto-lock (action=1) today → feed fish
- Once fed, skips all further checks until next day
- No米家自动化 needed, pure OpenClaw polling
小爱音箱 TTS
Make the XiaoAI speaker say something aloud.
CODEBLOCK9
Flow: unmute → play-text → wait → pause playback → re-mute.
This prevents the speaker from auto-playing music after TTS.
Important:
- - Speaker may be muted (default state at night) — script handles unmute/re-mute automatically
- After TTS, always pause playback to prevent auto-music
- Cloud API cannot control screen UI — returning to clock display requires physical tap or voice command
Token Refresh
Tokens expire periodically. If API returns decode errors or auth failures:
- 1. Re-login via browser (same flow as initial setup)
- Update
~/.mijia_creds.json with new serviceToken and INLINECODE31
The ssecurity changes each login. The serviceToken is session-bound.
米家控制
通过云API控制小米智能家居设备并创建自动化。
设置
前置条件: pip3 install micloud
凭据文件: ~/.mijia_creds.json(权限设为600)
json
{userId:..., serviceToken:..., ssecurity:..., cUserId:...}
如果登录需要双重验证(设备信任验证),可通过浏览器获取凭据:
- 1. 在已有登录Cookie的浏览器中打开 https://account.xiaomi.com/pass/serviceLogin?sid=xiaomiio&_json=true
- 使用哈希密码向 serviceLoginAuth2 发送POST请求,获取 ssecurity 和 location
- 访问 location 中的URL,从Cookie中获取 serviceToken
设备控制
列出设备
bash
python3 scripts/mijia.py devices
读取属性
bash
python3 scripts/mijia.py get
设置属性
bash
python3 scripts/mijia.py set
value:布尔值用true/false,uint8用整数,以此类推
注意: 网状网络设备(蓝牙开关)在设置时返回code:1——这是正常现象(异步确认)。命令仍会执行。延迟1-2秒后读取属性即可验证。
批量控制
创建一个包含命令的JSON文件:
json
[
{did: 946635824, siid: 2, piid: 1, value: true},
{did: 946633803, siid: 2, piid: 1, value: false}
]
bash
python3 scripts/mijia.py batch /tmp/commands.json
MIoT规格参考
每个设备都有服务(siid)→ 属性(piid)。在以下网址查询规格:
https://home.miot-spec.com/spec/
常见模式:
- - 开关/灯: siid:2 piid:1 = 开/关(布尔值)
- 三路开关: siid:2/3/4 piid:1 = 通道1/2/3(布尔值)
- 空调: siid:2 piid:1 = 开/关,piid:2 = 模式,piid:3 = 目标温度
- 浴霸: siid:2 = 照明,siid:3 = PTC加热器,siid:10 = 环境(温度传感器)
- 热水器: siid:2 piid:1 = 开/关,piid:2 = 目标温度,piid:3 = 当前温度
创建自动化场景
bash
python3 scripts/mijia.py scene_create scene.json
场景JSON格式(参见 references/scene_template.json):
json
{
name: 回家自动开灯,
identify: homelight_1234,
st_id: 8,
setting: {enable: 1, enable_push: 0},
trigger: {
key: event...,
did: devicedid>,
model: ,
extra: {\siid\:2,\eiid\:1}
},
action_list: [
{
did: devicedid>,
model: ,
extra: {\props\:[{\siid\:2,\piid\:1,\value\:true}]},
type: device_ctrl
}
]
}
关键字段:
- - trigger.key:设备事件格式为 event...
- launch.attrfilter:可选条件(例如仅在灯关闭时触发)
- actionlist[].extra:包含 props 数组的JSON字符串
- stid:8 = 用户自动化
- homeid 和 uid 如省略则自动填充
预设场景
🚿 洗澡模式
当用户说我要洗澡时:
- 1. 读取浴室温度:get 927361805 10 1
- 如果温度 < 28°C → 开启浴霸:set 927361805 3 1 1(暖风),目标30°C
- 如果温度 ≥ 28°C → 跳过加热器
- 开启热水器:set 965879649 2 1 1,设置43°C:set 965879649 2 2 43
- 开启浴室灯:set 946635824 2 1 true
🐟 出门自动喂鱼
当主人离家时,每天自动喂鱼一次。
架构:
- 1. OpenClaw定时任务(每5分钟):通过 /user/getuserdevicedata 轮询门锁事件日志
- 如果今天有自动上锁事件(action=1)且尚未喂鱼 → 执行喂鱼 → 记录状态
- 喂鱼后,后续所有检查立即跳过(每日去重)
- 状态文件 ~/.fishfeed_state.json:记录上次喂鱼日期
组件:
- - 门锁事件:智能门锁2 Pro(did: 1175215651),事件键 2.1020,action=1 = 自动上锁(离家)
- 喂鱼器:智能鱼缸(did: 2026943875),动作 siid:2 aiid:1,in:[1](1份)
- ⚠️ 动作参数格式:in: [1] 而非 [{piid:5,value:1}]
- - 脚本:scripts/fishautofeed.py
手动命令:
bash
python3 scripts/fishautofeed.py # 正常运行
python3 scripts/fishautofeed.py --dry-run # 检查但不执行喂鱼
python3 scripts/fishautofeed.py --force # 强制喂鱼
python3 scripts/fishautofeed.py --status # 显示状态
触发逻辑:
- - 轮询门锁事件日志,查找今天是否有自动上锁事件(action=1)→ 执行喂鱼
- 一旦喂鱼完成,跳过所有后续检查直到次日
- 无需米家自动化,纯OpenClaw轮询
小爱音箱TTS
让小爱音箱语音播报指定内容。
bash
python3 scripts/mijia.py tts
示例:python3 scripts/mijia.py tts 100783118 你好
流程:取消静音 → 播放文本 → 等待 → 暂停播放 → 重新静音。
这样可以防止音箱在TTS后自动播放音乐。
重要提示:
- - 音箱可能处于静音状态(夜间默认状态)——脚本会自动处理取消静音/重新静音
- TTS完成后务必暂停播放,防止自动播放音乐
- 云API无法控制屏幕界面——返回时钟显示需要物理点击或语音命令
Token刷新
Token会定期过期。如果API返回解码错误或认证失败:
- 1. 通过浏览器重新登录(与初始设置流程相同)
- 用新的 serviceToken 和 ssecurity 更新 ~/.mijia_creds.json
每次登录 ssecurity 都会变化。serviceToken 与当前会话绑定。