Mijia Control
by @jasonzhang2015
Control Xiaomi Mi Home (米家) smart devices via Xiaomi Cloud API. Use when: user wants to control smart home devices (lights, AC, heater, bath heater, switches...
clawhub install mijia-control📖 About This Skill
name: mijia-control description: | Control Xiaomi Mi Home (米家) smart devices via Xiaomi Cloud API. Use when: user wants to control smart home devices (lights, AC, heater, bath heater, switches, etc.), check device status, or create automation scenes. Triggers: "开灯", "关灯", "开空调", "关空调", "开浴霸", "我要洗澡", "设备状态", "创建场景", "自动化", "米家", "smart home", "turn on/off", any home device control request. NOT for: non-Xiaomi devices, HomeKit-only devices.
Mi Home (米家) Control
Control Xiaomi smart home devices and create automations via cloud API.
Setup
Prerequisites: pip3 install micloud
Credentials: ~/.mijia_creds.json (chmod 600)
{"userId":"...", "serviceToken":"...", "ssecurity":"...", "cUserId":"..."}
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
2. POST to serviceLoginAuth2 with hash password to get ssecurity + location
3. Follow location URL to get serviceToken from cookies
Device Control
List devices
python3 scripts/mijia.py devices
Read property
python3 scripts/mijia.py get
Set property
python3 scripts/mijia.py set
value: true/false for bool, integer for uint8, etc.
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}
]
python3 scripts/mijia.py batch /tmp/commands.json
MIoT Spec Reference
Each device has services (siid) → properties (piid). Look up specs at:
https://home.miot-spec.com/spec/
Common patterns:
Create Automation Scenes
python3 scripts/mijia.py scene_create scene.json
Scene JSON format (see references/scene_template.json):
{
"name": "回家自动开灯",
"identify": "homelight_1234",
"st_id": 8,
"setting": {"enable": 1, "enable_push": 0},
"trigger": {
"key": "event...",
"did": "",
"model": "",
"extra": "{\"siid\":2,\"eiid\":1}"
},
"action_list": [
{
"did": "",
"model": "",
"extra": "{\"props\":[{\"siid\":2,\"piid\":1,\"value\":true}]}",
"type": "device_ctrl"
}
]
}
Key fields:
trigger.key: format event... for device eventslaunch.attr_filter: optional conditions (e.g., only if light is off)action_list[].extra: JSON string with props arrayst_id: 8 = user automationhome_id and uid are auto-filled if omittedPredefined Scenes
🚿 Bath Mode (洗澡模式)
When user says "我要洗澡": 1. Read bathroom temp:get 927361805 10 1
2. If temp < 28°C → turn on bath heater: set 927361805 3 1 1 (暖风), target 30°C
3. If temp ≥ 28°C → skip heater
4. Turn on water heater: set 965879649 2 1 1, set 43°C: set 965879649 2 2 43
5. Turn on bathroom light: set 946635824 2 1 true🐟 Auto Fish Feeding (出门自动喂鱼)
Feeds fish once per day when BOSS leaves home.Architecture:
1. OpenClaw cron (every 5min): polls door lock event log via /user/get_user_device_data
2. If today has an auto-lock event (action=1) AND not fed yet → feed fish → record state
3. After feeding, all subsequent checks skip immediately (daily dedup)
4. State file ~/.fish_feed_state.json: tracks last feed date
Components:
2.1020, action=1 = auto-lock (leaving home)"in": [1] 而不是 [{"piid":5,"value":1}]
scripts/fish_auto_feed.pyManual commands:
python3 scripts/fish_auto_feed.py # Normal run
python3 scripts/fish_auto_feed.py --dry-run # Check without feeding
python3 scripts/fish_auto_feed.py --force # Feed regardless
python3 scripts/fish_auto_feed.py --status # Show state
Trigger logic:
小爱音箱 TTS
Make the XiaoAI speaker say something aloud.
python3 scripts/mijia.py tts ""
Example: python3 scripts/mijia.py tts 100783118 "你好"
Flow: unmute → play-text → wait → pause playback → re-mute. This prevents the speaker from auto-playing music after TTS.
Important:
Token Refresh
Tokens expire periodically. If API returns decode errors or auth failures:
1. Re-login via browser (same flow as initial setup)
2. Update ~/.mijia_creds.json with new serviceToken and ssecurity
The ssecurity changes each login. The serviceToken is session-bound.
⚙️ Configuration
Prerequisites: pip3 install micloud
Credentials: ~/.mijia_creds.json (chmod 600)
{"userId":"...", "serviceToken":"...", "ssecurity":"...", "cUserId":"..."}
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
2. POST to serviceLoginAuth2 with hash password to get ssecurity + location
3. Follow location URL to get serviceToken from cookies