project: monitor temperature around the house
The Xiaomi Mijia sensor runs for ages on a AAA battery and it can be stuck to a wall to show the temperature and humidity. It’s an attractive Bluetooth Low Energy device that beams out measurements for under £10. If I install an app (Mi Home) I can receive those readings from across the room and wonder what’s the point. The point and the value of this sensor is that I can e.g. connect it to Home Assistant and show a graph of how well the storage heater copes against the cold outside. (Till now, knowing when to set the heater timer has been guesswork). If you have a room that’s too hot or cold, seeing its temperature history offers clues to correct that.
There’s one caveat – the Xiaomi Mijia will need to be in Bluetooth range of the device (eg Raspberry Pi) running Home Assistant. Further down the page I’ll show a great workaround so that you can measure temperature in remote areas of the house. First though, here’s what to do to connect your Xiaomi Mijia to Home Assistant:
- Use a Bluetooth app on your phone to pair or search for new devices. You’ll soon see the MJ_xxx device – click on that for details and note the Device Bluetooth address. You want to enter that in place of 4C:65:A8:D9:E0:EE in bold below.
- Now go to the Home assistant Configurator and open configuration.yaml. Add the following code and save the file.
sensor: - platform: mitemp_bt mac: '4C:65:A8:D9:E0:EE' name: Mi living room monitored_conditions: - temperature - humidity
To display the feature/s just added, we must restart Home Assistant. When Home Assistant is live again, go to the frontend or Overview. Click Configure UI > + choose a ‘card’ and add this code
entities: entity: sensor.Mi_living_room_humidity entity: sensor.Mi_living_room_temerature type: history-graph
An ESP32 can collect readings from a remote Xiaomi Mijia temperature sensor
The Xiaomi Mijia has a tiny internal aerial and it needs to be in Bluetooth range of the device (eg Raspberry Pi) running Home Assistant. To get readings from further away, I use an ESP32 board powered with a USB charger. The ESP32 picks up the temperature readings over Bluetooth and then it relays them to Home Assistant over the network wifi. That might seem extravagant and troublesome, however this Bluetooth feature can piggyback onto an ESP32 that you’ve already set up to do something else (e.g. reading the electricity meter).
How to use ESPHome to collect readings from a remote Xiaomi Mijia
I jump ahead here to assume that you have an ESP32 set up (see the tutorial) to monitor something or initiate something like turn on the lights, when you enter the kitchen. In short, you previously used ESPhome to type a list of settings, your wifi password; a network address, a title or name plus a few other things. You then used ESPhome to compile this into a firmware file which you uploaded to an ESP32 chip.
With the fiddly bit done, you now want to turn on the Bluetooth features of the ESP32 chip. Go to the ESPHome section in Home Assistant. It may already have a few entries or ‘nodes’ like this:
Edit the ‘node’ or entry that you want to add Bluetooth to, and paste in this text. You’ll need to edit the bold items below and match the Bluetooth mac address to your Xiaomi Mijia.
# XIAOMI WHITE TEMPERATURE DISC esp32_ble_tracker: scan_interval: 30s sensor: - platform: xiaomi_mijia mac_address: 4C:65:A8:D9:E0:EE temperature: name: "mijia temperature" humidity: name: "mijia humidity"
SAVE and then UPLOAD this and that’s pretty much it. When you previously uploaded firmware you added an over-the-air (OTA) capability to the ESP32 so you need not connect it with wires this time.
Finally, go to the Home Assistant Overview, click Configure UI > + > History Graph. Then add this code to display a graph of the Xiaomi Mijia which is now presumably in Bluetooth range of the ESP32 we’ve been editing:
entities: entity: sensor.mi_humidity entity: sensor.mi_temerature type: history-graph