Why Xiaomi and Home Assistant are the perfect pair for a smart home
Xiaomiβs ecosystem offers some of the most affordable and functional smart home devices, from door-opening sensors to robot vacuum cleaners. But the standard Mi Home app limits automation by connecting users to the cloud and a closed ecosystem. Here comes Home Assistant, an open platform that allows all devices to be integrated into a single system with flexible scenarios, local management and protection from cloud failures.
In this article, you will learn how to integrate Xiaomi devices (including the Aqara, Mijia, Yeelight brands) into Home Assistant in three ways: through the official plugin, Mi Home Token, and the protocol. MQTT. We'll take a look at the nuances for different types of devices, the typical bugs, and we'll give you some tips on how to optimize your system. If you're just starting out with Home Assistant, don't worry, the instructions are tailored for users of all levels.
Before you start, make sure you have:
- π± Home Assistant installed (version 2023.12 or later) Suitable as Home Assistant OS on the Raspberry Pi and the container version on the NAS.
- π Xiaomi devices are connected to the Mi Home app and work stably.
- π Access to the local network (all devices must be on the same subnet as the Home Assistant server).
Method 1: Official integration via Mi Home (the easiest method)
The most reliable and recommended way is to use the official Xiaomi Miio integration, which supports most Xiaomi, Aqara and Mijia devices, which works through the local Miio protocol, does not require cloud servers and provides minimal latency.
To add integration:
- Enter. IP-the address of the device (how to find it β read below) and the token (receiving the token will be discussed in the next section).
How do you know? IP-address:
- π If IP not visible, use a network scanner (e.g. Fing or Advanced) IP Scanner) and look for devices called lumi., miio. or yeelink.
Make sure the device is on and connected to Wi-Fi|Check the firmware version (some older versions are not supported)|Turn it off. VPN/Proxies on the Home Assistant server|Add the device to your favorites in Mi Home (sometimes helps with errors)
-->
Limitations of the method:
- β Not all devices are officially supported (e.g. some Xiaomi cameras require separate integrations).
- β Robot vacuum cleaners (such as Mi Robot Vacuum) may require additional valetudo tuning.
- β If the device is used BLE (For example, Aqara sensors, you will need a Bluetooth proxy (say below).
What devices work with Miio?
Method 2: Getting Mi Home Token (for devices without official support)
If your device is not supported by official integration, or if you want to use it MQTT For more flexible settings, you need a Mi Home Token, a unique key that allows Home Assistant to interact directly with your device.
There are two ways to get a token:
Method 1: Using the Mi Home (Android) app
This will require:
- π± Android smartphone with Mi Home installed (version 6.0.100 or older).
- π» Computer with ADB (Android Debug Bridge).
- π Mihome-bin utility for token extraction.
Step-by-step:
- Enable Developer Mode on Android: Go to Settings β The phone. β Build number and press 7 times.
- Go back to Settings β System β For developers and enable USB debugging.
- Connect your phone to your computer and execute the command: adb backup -f mihome.ab -noapk com.xiaomi.smarthome Confirm the creation of a backup copy on the phone (do not set a password!).
- Use the mihome-bin utility to extract the token: java -jar mihome-bin.jar mihome.ab As a result, you will receive a file mihome.db with tokens of all your devices.
Method 2: Through a packet sniffer (for iOS or without) ADB)
If you have an iPhone or you canβt use it. ADB, You can intercept the token through network traffic analysis:
- π‘ Install Wireshark or Fiddler on your computer.
- π Set up a proxy on your phone (manually or through Charles Proxy).
- π± Open Mi Home and update device data (e.g., turn on/off the lamp).
- π In the sniffer, look for queries to the domain api.io.mi.com - the token will be in the Authorization header.
1.Reboot the Xiaomi device (remove the battery for 10 seconds for sensors).
2. Make sure that your device is linked to the same region (China/Europe/Russia) as your account.
3. Update the firmware of the device to the latest version.
-->
The token is valid until you reset the device or change the password in Mi Home. Store it in a safe place (for example, in the password manager).
Method 3: Connecting through MQTT (power-user)
Protocol MQTT It allows you to exchange data between Xiaomi and Home Assistant devices through a broker (for example, Mosquitto:
- π You want to reduce the load on Home Assistant (devices send data directly to the broker).
- π You have a distributed system with multiple servers.
- π οΈ You need custom topics and data format.
The setup will require:
- Install MQTT-Broker (for example, through Mosquitto add-on in Home Assistant).
- Set up Xiaomi device to send data to MQTT (This is often done using Tasmota or ESPHome firmware for devices on the ESP8266/ESP32).
- Add integration MQTT Home Assistant and set up subscriptions for topics.
Example configuration for the Aqara temperature sensor in configuration.yaml:
mqtt:
sensor:
- name:"Aqara Temperature"
state_topic:"home/sensor/aqara/temperature"
unit_of_measurement:"Β°C"
value_template:"{{ value_json.temperature }}"
- name:"Aqara Humidity"
state_topic:"home/sensor/aqara/humidity"
unit_of_measurement:"%"
value_template:"{{ value_json.humidity }}"For devices that do not support MQTT"out of the box (e.g., Yeelight lamps), you can use a Python intermediate script with a python-miio library that will redirect data from Miio to the python-miio. MQTT.
π‘
MQTT It is suitable for power users, but gives maximum flexibility. For most tasks, official integration of Xiaomi Miio is enough.
Connection BLE-Devices (Aqara, Mi Band, etc.)
Many Xiaomi and Aqara devices (e.g. door opening sensors, thermometers, Mi Band bracelets) use Bluetooth Low Energy protocol (BLE). To integrate them into Home Assistant, you will need a Bluetooth proxy β a device that will scan the airwaves and transmit data to the server.
Implementation options:
| Method | Equipment required | Pluses | Cons |
|---|---|---|---|
| ESP32 ESPHome | Payment ESP32 (for example, NodeMCU-32S) + antenna | Low price, flexible setting | Requires rations and firmware |
| Raspberry Pi with BlueZ | Raspberry Pi 3/4/5 Pi Zero W | Stable operation, support for multiple devices | High energy consumption |
| Ready. USB-adapter | adapter CSR 4.0 or BCM20702A | Ease of connection | Limited range of operation |
To set up ESP32 ESPHome:
- Install ESPHome addon in Home Assistant.
- Create a new configuration for ESP32 component esp32_ble_tracker.
- Add devices to the scan list: esp32_ble_tracker: scan_parameters: interval: 300ms window: 30ms active: true sensor: - platform: xiaomi_lywsd03mmc mac_address:"XX:XX:XX:XX:XX:XX" temperature: name:"Temperature" humidity: name:"Humidity" battery_level: name:"Battery"
- Download the firmware to ESP32 And place it next to the sensors.
π‘
To increase the range of action BLE Use an external antenna (for example, ESP32 moduled ESP-WROOM-32 antennae IPEX).
Solving Common Mistakes When Connecting Xiaomi to Home Assistant
Even if you follow the instructions accurately, you can get errors, and let's look at the most common ones and how to fix them.
Error: "Cannot connect to device"
Causes and solutions:
- π Device offline: Check that the device is on and connected to the same Wi-Fi as the Home Assistant server.For battery-powered sensors, make sure the charge is not discharged.
- π Wrong token: Make sure the token is copied in full (no spaces.
- π Regional restrictions: Some devices (like Aqara) require tethering to servers in China.In Mi Home, change region to Mainland China (but note that this can reset all devices!).
- π Outdated firmware: Update the device's firmware through Mi Home.Outd-of-the-box vacuum cleaners may need to manually upgrade through Mi Home Token.
Error: "Unsupported device"
If the device is not supported by official integration:
- π Check the list of supported devices in the documentation.
- π οΈ Use it. MQTT or custom components (e.g, xiaomi_ble for BLE-device).
- π¦ For robot vacuum cleaners, install Valetudo (alternative open-source firmware) API).
Error: "Timeout while connecting"
It's caused by slow response.
- β‘ Check the quality of Wi-Fi (Xiaomi devices do not work well at 5 GHz β use 2.4 GHz).
- π‘ Reduce the distance between the device and the router.
- β³ Increase timeout in the Home Assistant configuration: xiaomi_miio: devices: 192.168.1.100: token: YOUR_TOKEN timeout: 15
How to reset Xiaomi device to factory settings?
Optimizing the work of Xiaomi devices in Home Assistant
Once the devices are successfully connected, you should configure them for maximum efficiency. Here are some recommendations:
1. Set up a device survey
By default, Home Assistant polls devices every 30 seconds, which can put a strain on the network.
- π‘οΈ Temperature/humidity sensors: it is enough to interrogate once in a while 5-10 minute.
- π‘ Lamps and sockets: 30 seconds left (for quick response).
- πͺ Opening Sensors: Use binary_sensor with event trigger (does not require a constant survey).
Example of configuration in configuration.yaml:
xiaomi_miio:
devices:
192.168.1.100:
token: YOUR_TOKEN
scan_interval: 300 # 5 minutes2.Local management vs. cloud
Use local management (via Miio or if possible) BLE), where:
- β‘ Reduce delays (commands are executed instantly).
- π Eliminate dependence on Xiaomi servers (relevant in case of problems with the Internet).
- π‘οΈ Increase privacy (data does not go to the cloud).
Exception: Xiaomi cameras (such as Mi Home Security Camera) often require cloud connectivity through Xiaomi Camera or FFmpeg integration.
3. Automation with Xiaomi devices
Examples of useful automation:
- π Smart lighting: Turn on Yeelight lamps at sunset (use sun.sun trigger).
- πͺ Security: Notification in Telegram when the door opening sensor is triggered.
- π‘οΈ Climate control: Mi Air Conditioning at temperatures above 25Β°C.
- π€ Cleaning on schedule: launch of the robot vacuum cleaner Mi Robot Vacuum on weekdays at 10:00.
Example of automation for a motion sensor:
automation:
- alias: "Switch on the lights while driving"
trigger:
platform: state
entity_id: binary_sensor.motion_sensor_158d000xxxxxx
to:"on"
condition:
- condition: sun
after: sunset
before: sunrise
action:
service: light.turn_on
target:
entity_id: light.yeelight_color1
data:
brightness: 255
color_temp: 400