Войти

Adding a Xiaomi Air Purifier to Home Assistant: HACS, Xiaomi Miot and a Token

B Автор Bisonar Team · 6 min read · 2026-11-11

Adding a Xiaomi Air Purifier to Home Assistant: HACS, Xiaomi Miot and a Token

This post is a side topic of my series on building my own ESP32 voice assistant ("Hello Bex"). Start of the series: Building My Own ESP32 Voice Assistant – Part 1.

Türkçe versiyonu

My ESP32 voice assistant, Bex, can now hear and talk. In this post I add the new Xiaomi Smart Air Purifier 4 Compact to Home Assistant, so Bex can turn it on and off by voice too.

I actually bought this unit for the balcony, to clear cigarette smoke. But it didn't perform the way I wanted out there. So for now I swapped them: the big Air Purifier 4 moved to the balcony, and the small Compact went to the living room.

How was the first purifier added?

My first unit is a Xiaomi Smart Air Purifier 4 (model zhimi.airp.mb5). I had added it to Home Assistant like this:

  • I installed the Xiaomi Miot integration through HACS (al-one/hass-xiaomi-miot).
  • When adding the integration, I used the "Add device using host/token" option, i.e. not with a Xiaomi account, but with the device's IP address and token.
  • I got the token from the terminal with the Xiaomi Cloud Tokens Extractor tool.

I added the new unit the same way.

Why not just log in with a Xiaomi account?

The first option in Xiaomi Miot is "Add devices using Mi Account". When I tried it, Home Assistant answered "Xiaomi rejected credentials".

So Xiaomi's standard path didn't work here, and I went with host/token. That method has a bonus: Home Assistant talks to the device directly over the home network, no cloud needed.

Step 1: Get the token

The token is a 32-character key the device uses to accept commands on the local network. Xiaomi Cloud Tokens Extractor lists the tokens and IP addresses of every device in your Xiaomi account.

I opened Terminal on my Mac and ran these one by one:

mkdir -p ~/Downloads/token_extractor
cd ~/Downloads/token_extractor
curl -sSfL -o te.zip https://github.com/PiotrMachowski/Xiaomi-cloud-tokens-extractor/releases/latest/download/token_extractor.zip
unzip -oq te.zip
python3 -m venv .venv
.venv/bin/pip install -q -r $(find . -name requirements.txt | head -1)
.venv/bin/python $(find . -name token_extractor.py | head -1)

The tool first asks how you want to log in:

Xiaomi Cloud Tokens Extractor login screen: log in with password (p) or QR code (q)

I picked q for the QR code. The tool showed a QR code at http://127.0.0.1:31415; after I scanned it with the Mi Home app on my phone, the terminal printed "Logged in."

After login the tool checks every Xiaomi server (cn, de, us, ru, tw, sg, in, i2) and lists the devices it finds like this:

   NAME:     <device name>
   ID:       <hidden>
   MAC:      <hidden>
   IP:       <device-ip>
   TOKEN:    <hidden>
   MODEL:    <model>

I left the server question empty and pressed Enter, so it checked all of them. My devices showed up in the same home on the tw, sg and in servers; the new one appeared as Xiaomi Smart Air Purifier 4 Compact, model xiaomi.airp.cpa4.

⚠️ Don't share this output with anyone. Anyone who knows the token can control your device if they're on the same network.

Step 2: Add it to Home Assistant

  1. Settings → Devices & Services → Xiaomi Miot → Add entry
  2. Choose "Add device using host/token" and press Next.
  3. Fill in the form:
  4. Host: the device's IP address
  5. Token: the token from the tool
  6. Name: whatever you want to call the device
  7. Press Submit.

Watch out: Chrome auto-filled the "Host" and "Token" fields in this form with a saved username and password. Clear both and type the correct values before submitting.

After Submit, the device was added right away, together with its sensors.

Step 3: Introduce it to Bex

Once added, Home Assistant created a fan entity called fan.xiaomi_cpa4_4b15_air_purifier plus sensors like PM2.5 and filter life. It was exposed to Assist automatically.

With two purifiers in the house, Bex needs to know which one I mean. The first one already has the alias "hava temizleyici" (air purifier). I named the new one "Küçük hava temizleyici" (small air purifier) and gave it the alias "kompakt hava temizleyici" (Settings → Entities → the device → Aliases).

I hit a trap here: after changing the entity's name and aliases, "küçük hava temizleyiciyi kapat" (turn off the small air purifier) got the answer "no device by that name". "Kompakt hava temizleyiciyi aç" (turn on the compact air purifier) worked, though. The reason: while editing the alias list, the entity's own name had dropped out of it. In Home Assistant the entity's own name sits in the alias list as a separate row; adding that row back fixed it.

Test sentences (Bex speaks Turkish):

CommandBex's answer
"küçük hava temizleyiciyi kapat" (turn off the small air purifier)küçük hava temizleyici kapatıldı (small air purifier turned off)
"küçük hava temizleyiciyi aç" (turn on the small air purifier)küçük hava temizleyici açıldı (small air purifier turned on)
"hava temizleyiciyi aç" (turn on the air purifier)hava temizleyici açıldı (the first unit)

Since I moved the units, I also updated their areas in Home Assistant: I created a new Balkon (balcony) area and moved the big purifier there, and the small one to Salon (living room). Now "balkondaki hava temizleyiciyi aç" (turn on the air purifier on the balcony) works too; Bex understands both the area and the device and turns on the big one.

"Salondaki hava temizleyiciyi aç" (turn on the air purifier in the living room) didn't work at first: there was no device called "hava temizleyici" in the living room area, since the small one is named "küçük hava temizleyici". If I had given the small one the alias "hava temizleyici", it would have clashed with the one on the balcony. Instead I wrote a small automation that sends "salondaki hava temizleyiciyi aç/kapat" straight to the small purifier:

triggers:
  - trigger: conversation
    id: ac
    command:
      - "salon[daki|da] hava temizleyici[yi|i] aç"
  - trigger: conversation
    id: kapat
    command:
      - "salon[daki|da] hava temizleyici[yi|i] kapat"
actions:
  - choose:
      - conditions: [{ condition: trigger, id: ac }]
        sequence:
          - action: fan.turn_on
            target: { entity_id: fan.xiaomi_cpa4_4b15_air_purifier }
          - set_conversation_response: Salondaki hava temizleyici açıldı.
      - conditions: [{ condition: trigger, id: kapat }]
        sequence:
          - action: fan.turn_off
            target: { entity_id: fan.xiaomi_cpa4_4b15_air_purifier }
          - set_conversation_response: Salondaki hava temizleyici kapatıldı.

I told Bex "Hello Bex… küçük hava temizleyiciyi kapat" and the living room purifier turned off. After adding the automation, "Hello Bex… salondaki hava temizleyiciyi kapat" worked too.

In the video I say "Hello Bex, küçük hava temizleyiciyi kapat"; Bex answers "Küçük hava temizleyici kapatıldı" and the purifier in the living room turns off:

▶️ Watch the video with sound

Adding it to the dashboard

In the "Hava Temizleyici" section of my main dashboard I added two cards for the new unit: a tile to control the fan speed and a tile showing the PM2.5 value.

Clicking the card opens the device's detail dialog, where you can change the speed, turn it on or off, and switch the preset mode (Auto, Sleep, Favorite):

Home Assistant dashboard: detail dialog of the small air purifier, with both purifiers' cards on the right

What I learned in this part

  • Adding Xiaomi devices sometimes means stepping off the standard path. The integration's own Mi account login was rejected; getting the token with a community tool and adding the device by IP and token solved it. Is it always going to be the Chinese hacking things together? :)
  • A token is like a password. The tool's output lists the token, IP and MAC address of every Xiaomi device in the house in plain text; don't share it anywhere.
  • Watch out for browser auto-fill. Chrome had put a saved username and password into the Host and Token fields of the Home Assistant form.
  • With two similar devices, names matter. Calling one "air purifier" and the other "small air purifier", and putting each device in the right area, kept Bex from mixing them up. A small automation covered the one sentence that didn't work with the area name.
  • The empty-looking row in the alias list matters. That row is the entity's own name; delete it and Bex can no longer find the device by name.

What's next?

Next up: closing the case and turning the volume up a little more with a 100 kΩ resistor. If I come across an LED strip, I'll add it in a separate part.

💻 The full living room automation is on GitHub: https://github.com/justicemilitia/bisonar-ai/blob/main/esp32-voice-assistant/ha-config/salon-hava-temizleyici-sesli.yaml

🛠️ Step-by-step guide on Instructables: https://www.instructables.com/DIY-ESP32-Voice-Assistant-for-Home-Assistant-With-/


Case design: ImpatientMaker (remix of JakeB's "Voice Assist Puck"), CC BY-NC 4.0. ESPHome code: impatientmaker/esphome-voice-assist (MIT). Full credits in Part 1.

Xiaomi Miot integration: al-one/hass-xiaomi-miot · Token tool: PiotrMachowski/Xiaomi-cloud-tokens-extractor

AI Automationn8nBusiness
Поделиться:
← Все статьи