Integrating Xiaomi Mi Scale

The whole code works, but i stuck at people check algorithm - we got only raw weights but not “person->weight” mapping. Before you can display it - you need to check which person weight it is.

I didn’t want to start home assistant as root, so I wrote a small mqtt gateway for the mi scale: https://gist.github.com/syssi/4108a54877406dc231d95514e538bde9

This is my home assistant configuration:

sensor:
  - platform: mqtt
    name: "miscale"
    state_topic: "miscale/weight/kg"
    value_template: "{{ value }}"
    unit_of_measurement: "kg"

You have to change the MISCALE_MAC, MQTT_USERNAME, MQTT_PASSWORD and the MQTT_HOST. Have fun! For a successful passive bluetooth scan root privileges are required.

4 Likes

Can you clarify how to install and use this?

Also which model does it work with? I’ll happily by one if I can get the stats to HA :slight_smile:

Just download the code (python script). Update L10-L15 and execute the script:

sudo python3 miscalegw.py

The output should look like this:

$ sudo python3 ./miscalegw.py                                                                                                                                                                                                                                                         
Scanning for devices...
MQTT connection returned result: Connection Accepted.
    Device: 88:ff:ff:ff:ff:ff (public), -59 dBm .
        Sent data to topic miscale/weight/kg: xx.x kg

The script publishs one message per execution to the mqtt host. This is the payload:

$ mosquitto_sub -v -h localhost -t '#'
miscale/weight/kg xx.x

The value/sensor can be used in home assistant with this configuration:

sensor:
  - platform: mqtt
    name: "miscale"
    state_topic: "miscale/weight/kg"
    value_template: "{{ value }}"
    unit_of_measurement: "kg"

I wrote the code for the first generation of the mi scale. The command “sudo hcitool lescan” can help to find the MAC address of the device:

$ sudo hcitool lescan
LE Scan ...
50:8C:B1:3F:EF:C5 IBEACON8
C4:FF:FF:FF:FF:FF Flower care
88:FF:FF:FF:FF:FF MI_SCALE
3 Likes

Does it work with the following ?

did you set it up to run periodically or on demand ?

The script must be called with root permissions because of the needed bluetooth stack. I assume the mentioned component will use the homeassistant user to call the scripts. I just used a cron job:

$ crontab -l -u root
30 * * * * /usr/local/bin/miscalegw.py >/dev/null
1 Like

Do you think it would be possible to combine the following 2 features and get an independent gateway to grab scale and / or plan data ?

(wishful thinking)

Why do you want to combine both applications?

The way I understand it the plant gateway is and independent tool running outside HA, querying the Xiaomi devices and sending the data to MQTT

If it can also do the job of querying the the scale - I could have a separate Pi running this and getting the data updated without caring for 2 separate tools

Since we are talking about both Xiaomi products they seem related to me…

Your understanding is correct but the bluetooth communication between the plant sensor and the scale doesn’t have anything in common. In easy words: The scale provides something like a broadcast which is captured. The plant sensor provides informations by request & response. Calling both tools successively would be the easiest solution.

I see what you mean
So if I have a dedicated rPi zero for this… I can follow install of both on the same device and have them publish to my HA mqtt ?
any additional preparations you would suggest ?

Yes. Just use both scripts on the same host. No additional preparations.

I am using rPi zero W and something is not working :frowning:

Any idea what I can do about it ?
(the plantgateway is also not working - I think it’s a BT issue with the installation?)

I am on rasbian lite fresh install after update / upgrade)

pi@raspberrypi:~ $ pi@raspberrypi:~ $ sudo python3 miscalegw.py
Scanning for devices...
MQTT connection returned result: Connection Accepted.
Traceback (most recent call last):
  File "miscalegw.py", line 99, in <module>
    main()
  File "miscalegw.py", line 96, in main
    devices = scanner.scan(5)
-bash: pi@raspberrypi:~: command not found  File "/usr/local/lib/python3.5/dist-packages/bluepy/btle.py", line 679, in scan
    self.start(passive=passive)
  File "/usr/local/lib/python3.5/dist-packages/bluepy/btle.py", line 617, in start
    self._mgmtCmd("le on")
  File "/usr/local/lib/python3.5/dist-packages/bluepy/btle.py", line 272, in _mgmtCmd
    rsp = self._waitResp('mgmt')
  File "/usr/local/lib/python3.5/dist-packages/bluepy/btle.py", line 304, in _waitResp
    raise BTLEException(BTLEException.INTERNAL_ERROR, "Helper exited")
bluepy.btle.BTLEException: Helper exited

It looks like the bluetooth stack of the rPi zero W does not support some bluetooth low energy features. That’s strange.

VERY strange

on plantgateway I am also seeing a problem with this module

any suggestion how to troubleshoot ?
is this a firmware issue or software ? would another pi work maybe ?

Exception ignored in: <bound method Peripheral.__del__ of <bluepy.btle.Peripheral object at 0xb65a52f0>>
Traceback (most recent call last):
  File "/usr/local/lib/python3.5/dist-packages/bluepy/btle.py", line 520, in __del__
    self.disconnect()
  File "/usr/local/lib/python3.5/dist-packages/bluepy/btle.py", line 416, in disconnect
    self._writeCmd("disc\n")
  File "/usr/local/lib/python3.5/dist-packages/bluepy/btle.py", line 268, in _writeCmd
    self._helper.stdin.flush()
BrokenPipeError: [Errno 32] Broken pipe

I bet another BLE capable bluetooth stick or a raspberry pi 3 will do out of the box.

Hello everyone, do you know can xiaomi scale accurate to 2 demical places? Or is there another scale which can not only display 2 demical places but also can be integrated?

Thanks @syssi.
For those who use TLS mqtt, you need to add 2 lines:
MQTT_CERT = 'path/to/your/file.crt'
and after self.mqtt_client = mqtt.Client() (around 72-73 line)
self.mqtt_client.tls_set(MQTT_CERT)

@Amir974 I’ve recently tested rpi zero w with xiaomi miscale and it worked fine. From what I’ve found to get working BLE on rpi zero you need kernel at least 4.9. Maybe you are running some older kernel and upgrade would solve this. You could also try running “hcitool lescan” to scan for BLE devices and test if bluetooth low energy is working.

I’m wondering if I got a different version of the scale or something because while I’m close but something ain’t right. I found the scale’s MAC and replaced it in the script, however it reports as “MIBCS” instead of “MISCALE” and the OUI prefix is different. The scale pairs fine with my iPhone using the Mi Fit app so I’m not sure where to go from here.

bash-4.3# python3 miscalegw.py
Scanning for devices...
MQTT connection returned result: Connection Accepted.
    Device: cb:3f:00:xx:xx:xx (random), -69 dBm . bash-4.3#

Here’s the output I get from blescan.py

Device (new): cb:3f:00:xx:xx:xx (random), -71 dBm
Flags: <06>
Incomplete 16b Services: <1b18>
16b Service Data: <1b1802c4e1070b1e13050c00002607>