node-miio
v0.21.0
Published
Control Mi Home devices, such as Mi Robot Vacuums, Mi Air Purifiers, Mi Smart Home Gateway (Aqara) and more
Maintainers
Readme
[!NOTE] Beta testers wanted
The maintainer no longer owns any of the supported models, so changes are developed against automated tests and validated by users on beta releases. If you'd like to help, sign up as a beta tester in the plugin you use (Homebridge, Matterbridge) or in #18. See CONTRIBUTING.md for other ways to help. Co-maintainers are welcome too.
miIO Device Library
The intention of this library is to update the miio. Since the original repo is no longer maintained, nor reviewing the existing PRs, I decided to create a new one.
Control Mi Home devices that implement the miIO protocol, such as the Mi Air Purifier, Mi Robot Vacuum and Mi Smart Socket. These devices are commonly part of what Xiaomi calls the Mi Ecosystem which is branded as MiJia.
node-miio is MIT-licensed and requires Node 22.13.0+ or 24+.
Note: The devices are built on node-miio's own typed thing model (src/lib/things/). It replaced
abstract-things and keeps its API: the same types, capabilities,
methods and events. Testing and feedback are welcome, please open issues as needed.
Devices types
The intent of this library is to support all miIO-compatible devices and to provide an easy to use API for them. The library maps specific device models to generic device types with well defined capabilities to simplify interacting with them.
Currently supported devices are:
- Air Purifiers (1, 2 and Pro)
- Mi Humidifier
- Mi Smart Socket Plug and Power Strips
- Robot vacuums:
- Roborock / Mi Robot Vacuum (legacy miIO protocol), Viomi and Dreame (mc1808)
- Any MIoT vacuum (Xiaomi, Dreame, Mijia, ijai, …), driven by its spec from miot-spec.org (see MIoT devices)
- Mi Smart Home Gateway (Aqara) and accessories - switches, sensors, etc
- Philips Light Bulb and Eyecare Lamp
- Yeelights (White Bulb, Color Bulb, Desk Lamp and Strip)
See documentation for devices for information about the types, their API and supported device models. You can also check Missing devices if you want to know what you can do to help this library with support for your device.
MIoT devices
Newer devices use the MIoT protocol, where every feature is a numbered property or action described in a per-model spec
published on miot-spec.org. node-miio downloads the spec of your model on first use and
caches it on disk (~/.cache/node-miio, ~/Library/Caches/node-miio or %LOCALAPPDATA%\node-miio\Cache).
Each release also bundles a snapshot of the specs of every vacuum listed on miot-spec.org, so it works in air-gapped environments too:
import * as miio from "node-miio";
miio.configureMiotSpecs({
offline: true, // Never use the network (or set the env var MIIO_OFFLINE=1)
cacheDir: "/path/to/cache", // Or the env var MIIO_CACHE_DIR. `false` disables the cache
specDir: "/path/to/specs", // Optional: your own specs, named `<model>.json`
});To inspect the spec of a model (and whether it's supported as a vacuum):
$ npx node-miio spec xiaomi.vacuum.d110chMIoT vacuums expose fanSpeeds() and waterBoxModes() with the values (and names) supported by the model.
Installation
To install into your project:
npm install node-miioTo install globally for access to the command line tool:
npm install -g node-miioUsage
import * as miio from "node-miio";node-miio is an ES module. CommonJS code can still load it with const miio = require("node-miio"); (Node.js
22.13+ supports require() of ES modules).
The package includes TypeScript declarations. With TypeScript, use isVacuum() to narrow the device to a
VacuumDevice, and instanceof for the API of a specific implementation:
import * as miio from "node-miio";
import { isVacuum, Vacuum } from "node-miio";
const device = await miio.device({ address, token });
if (isVacuum(device)) {
await device.activateCleaning(); // shared by every vacuum
if (device instanceof Vacuum) {
await device.startMopWash(); // Roborock only
}
}Resolve a handle to the device:
// Resolve a device, resolving the token automatically or from storage
miio
.device({ address: "192.168.100.8" })
.then((device) => console.log("Connected to", device))
.catch((err) => handleErrorHere);
// Resolve a device, specifying the token (see below for how to get the token)
miio
.device({ address: "192.168.100.8", token: "token-as-hex" })
.then((device) => console.log("Connected to", device))
.catch((err) => handleErrorHere);Call methods to interact with the device:
// Switch the power of the device
device
.togglePower()
.then((on) => console.log("Power is now", on))
.catch((err) => handleErrorHere);
// Using async/await
await device.togglePower();Listen to events such as property changes and actions:
// Listen for power changes
device.on("power", (power) => console.log("Power changed to", power));
// The device is available for event handlers
const handler = ({ action }, device) =>
console.log("Action", action, "performed on", device);
device1.on("action", handler);
device2.on("action", handler);Capabilities and types are used to hint about what a device can do:
if (device.matches("cap:temperature")) {
console.log(await device.temperature());
}
if (device.matches("cap:switchable-power")) {
device.setPower(false).then(console.log).catch(console.error);
}If you are done with the device call destroy to stop all network traffic:
device.destroy();Tokens and device management
A few miIO devices send back their token during a handshake and can be used without figuring out the token. Most devices hide their token, such as Yeelights and the Mi Robot Vacuum.
There is a command line tool named miio that helps with finding and storing
tokens. See Device management for details
and common use cases.
The easiest way to get the tokens is from your Xiaomi account (opt-in):
npx node-miio cloud login --qr (scan the QR code with the Mi Home app), then
npx node-miio cloud devices. See Xiaomi Cloud.
Robots paired with the Roborock app don't use miIO tokens. Their support is starting (experimental):
npx node-miio roborock login (with a code sent to your email), then npx node-miio roborock devices lists
your robots with their model and protocol version, and roborockDevice() controls them (locally, or through Roborock's cloud). See Roborock app accounts.
Discovering devices
Use miio.devices() to look for and connect to devices on the local network.
This method of discovery will tell you directly if a device reveals its token
and can be auto-connected to. If you do not want to automatically connect to
devices you can use miio.browse() instead.
Example using miio.devices():
const devices = miio.devices({
cacheTime: 300, // 5 minutes. Default is 1800 seconds (30 minutes)
});
devices.on("available", (device) => {
if (device.matches("placeholder")) {
// This device is either missing a token or could not be connected to
} else {
// Do something useful with device
}
});
devices.on("unavailable", (device) => {
// The device stopped answering for `cacheTime` seconds (it is not destroyed)
});miio.devices() supports these options:
cacheTime, the maximum amount of seconds a device can be unreachable before it becomes unavailable. Default:1800filter, function used to filter what devices are connected to. Default:reg => trueskipSubDevices, if sub devices on Aqara gateways should be skipped. Default:falseuseTokenStorage, if tokens should be fetched from storage (see device management). Default:truetokens, object with manual mapping between ids and tokens (advanced, use Device management if possible)
See Advanced API for details about miio.browse().
Device API
Check documentation for devices for details about
the API for supported devices. The core API (matches, state(), on(event), the capabilities) follows the one of
abstract-things, described in Using things.
Library versioning and API stability
This library uses semantic versioning with an exception being that the API for devices is based on their type and capabilities and not their model.
This means that a device can have methods removed if its type or capabilities
change, which can happen if a better implementation is made available for the
model. When working with the library implement checks against type and
capabilities for future compatibility within the same major version of miio.
Capabilities can be considered stable across major versions, if a device
supports power no minor or patch version will introduce power-mega and
replace power. If new functionality is needed the new capability will be
added along side the older one.
Reporting issues
Reporting issues contains information that is useful for making any issue you want to report easier to fix.
Debugging
The library uses debug with two
namespaces, miio is used for packet details and network discovery and devices
use the thing:miio namespace. These are controlled via the DEBUG
environment flag. The flag can be set while running the miio command or any
NodeJS script:
Show debug info about devices during discovery:
$ DEBUG=thing\* miio discoverTo activate both namespaces set DEBUG to both:
$ DEBUG=miio\*,thing\* miio discoverSending the debug messages to your own logger
Apps (e.g. Homebridge or Matterbridge plugins) can send node-miio's debug messages to their own logger, so users don't
need to set DEBUG. Tokens and passwords are redacted from these messages.
import * as miio from "node-miio";
// The messages of this device (requests, responses, property changes…)
const device = await miio.device({
address,
token,
logger: { debug: (msg) => log.debug(msg) },
});
// Everything that isn't about a specific device (network, MIoT specs, Xiaomi Cloud…)
miio.configureLogging({ logger: { debug: (msg) => log.debug(msg) } });The messages are prefixed with their namespace, e.g. [thing:miio:1234] <- Message: .... The low-level packet details
(miio:packet) are only available through DEBUG.
Protocol documentation
This library is based on the documentation provided by OpenMiHome. See https://github.com/OpenMiHome/mihome-binary-protocol for details. For details about how to figure out the commands for new devices look at the documentation for protocol and commands.
Support
I maintain this in my spare time, with help from Claude. Sponsorships go towards the subscription, so I can take on more feature requests and devices. ❤️ GitHub Sponsors · Ko-fi
