I recently bought a Xiaomi Air Purifier (4 Lite) and it does not have historical data. So as any normal human would do I export its data and create the history myself.

I have made the exporter here, together with a Grafana dashboard.

What can we monitor?

This is a list of things we can monitor from the device:

  • Environment:
    • Air Quality Index (PM2.5 Concentration)
    • Temperature
    • Relative Humidity
  • Device Status:
    • Power Status ON/OFF State (Uptime)
    • Current Mode (Auto, Sleep, Manual)
    • Fan level (Manual mode only)
    • Fan RPM (Not shown in Xiaomi Home app)
    • Device Fault (Normal, Sensor Lost, Motor Stop)
  • Filter Life:
    • % Filter Life Used
    • Filter Life Used (in days)
    • Filter Life Remaining (in days)
Grafana Dashboard

Grafana Dashboard

Prerequisites

Install python-miio and prometheus-client.

Device IP & token

Xiaomi devices are authenticated with a 32-character hexadecimal token. Normally you can get this by signing into Xiaomi Cloud via miiocli cloud from the python-miio library. Unfortunately, Xiaomi has changed some things internally so we use mik-laj’s script to request login via QR Code.

If you don’t see your device after successfully authenticating, it is possible that it was registered in a different region. You can try my modified script to find it across regions.

Device IP can be found with miio discover command.

Device Model

If you want to use the exporter with any Xiaomi air purifier other than the 4 Lite, you may want to verify and adjust the script in accordance with the Xiaomi’s MIoT Specs.

The device model can be retrieved with miioclicommand line:

1
miiocli device --ip <DEVICE_IP> --token <DEVICE_TOKEN> info

If the command line tool does not work, you can try this python script:

1
2
3
4
5
6
7
8
9
from miio import Device
IP = "192.168.1.xxx"
TOKEN = "2c82ea271c375619b845c3d1f3e775f3"
try:
    device = Device(IP, TOKEN)
    info = device.info()
    print("Model:", info.model)
except Exception as e:
    print("Error:", e)

Modify the IP and Token accordingly.

Once that’s done, you’ll see your device’s identifier - for example, zhimi.airp.rmb1 for the 4 Lite Air Purifier. You can then lookup Xiaomi’s MIoT Specs and modify the exporter script according to your device. Each element usually comes with its own SIID and PIID.

Usage

Navigate to the exporter script here.

Running the exporter

Execute the script:

1
export.py JSON_FILE

JSON Configuration is as follow:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
{
  "prometheus_port": 8000,
  "polling_interval_seconds": 60,
  "purifiers": [
    {
      "ip": "192.168.1.255",
      "token": "2c82ea271c375619b845c3d1f3e775f3",
      "name": "Bedroom"
     }
  ]
}

Importing the dashboard

The dashboard is provided as a JSON file inside the repository or on Grafana Labs for easy importing. As previously mentioned, you may need to adjust settings appopriately for different devices.

And that’s it! You now have a functional dashboard with Air Quality Monitoring, Temperature, Humidity and Filter Life all in one spot!