Integrating Xiaomi Mi Scale

Hello,
sorry for late reply. Only now i was able to test it. But not sucessfully :frowning:

It does not retrieve any information.
If someone (75kg) is over the scale it retrieve this errors.

Traceback (most recent call last):
  File "Xiaomi_scale_scan.py", line 162, in <module>
    main()
  File "Xiaomi_scale_scan.py", line 159, in main
    devices = scanner.scan(5)
  File "/usr/local/lib/python3.5/dist-packages/bluepy/btle.py", line 853, in scan
    self.process(timeout)
  File "/usr/local/lib/python3.5/dist-packages/bluepy/btle.py", line 842, in process
    self.delegate.handleDiscovery(dev, (dev.updateCount <= 1), isNewData)
  File "Xiaomi_scale_scan.py", line 92, in handleDiscovery
    self._publish(round(measured, 2), unit, str(mitdatetime), miimpedance)
  File "Xiaomi_scale_scan.py", line 132, in _publish
    lib = Xiaomi_Scale_Body_Metrics.bodyMetrics(weight, height, age, sex, 0)
  File "/home/burro/.homeassistant/Xiaomi_Scale_Body_Metrics.py", line 19, in __init__
    raise Exception("Age is too high (limit >99 years)")
Exception: Age is too high (limit >99 years)

Any ideias?

Best regards,
Laser

doesn’t look to be related to your weight but your date or birth.
make sure you set the correct date(s) of birth in lines 117-1131 of Xiaomi_Scale.py
Check again setup instructions, especially points 4 & 5

Yes… my fault…
Now everything is working.
Great job. Thank you very much for your precious help.

1 Like

Does this work with hassio?

Not unless you can start it via a cron job as it requires root privileges

I should be able to use “tasker” and “notify&fitness” to get the values from the scale I think. Someone else posted a tutorial on getting the miband into HA through tasker integration. The mifit app has the scale as an option to be added. I guess this is about the only option I have then.

Can you ssh into the pi as root?
I don’t have a pi with hass.io on it so can’t test/try but I since the script is called via a cron job every 5 min, it doesn’t require HA to call it and anything kicked from a cron job runs with root privileges.
I’d be interested to know if that works, if so I’ll update my github to reflect this as hass.io support has been requested multiple times already…

I am fascinated by all the discussion going on here; I have a v1 MiScale at home, and I really wanted to integrate it with HomeAssistant. I got @lolouk44 script reviewed, and he did some quality work, however it didn’t really work for my MiScale, and the messages were spewing far too often. His work, and the OpenScale Project gave me some ideas. So, here I come with my first attempt at getting MiScale v1 to work: https://gist.github.com/alryaz/bdd90afac94e9c1499adbccf1c1e8a33

Installation is: run python script as a service daemon (such as systemd, or using screen), or test it by running it in commandline. Edit it prior to launching so that it works for your device.

1 Like

Hi @alryaz,

I’ll have a look at your code to see the differences and will try to include them in my code if you don’t mind to try and have a single place for everyone
Are you running this on hass.io or something else?

For those who are interested in contributing for this effort, check the ESPHome github, as OttoWinter and tsunglung are developing this future integration through that.

Thanks!

No, I am running it alongside a dockerized HomeAssistant image. It simply pushes some info over MQTT with autodiscovery, and it has been working fine so far. Unfortunately, I don’t own a v2, so I can’t call my option a complete realization; but I will make an attempt. As I already mentioned, I am using openScale's approach to poll Mi Scale, and it is working almost flawlessly (few disconnects here and there, device going away, etc.)

I am looking into developing a component, however as I am not completely familiar with correct polling setup using BTLE/GATT Notifications within HomeAssistant’s polling loop, I am currently unable to determine a solution.

I was just coming here to post that I took lolouk44’s code as a base and created an ESP32 bridge to MQTT which combos with an appdaemon app for processing the scale data per-user, creating sensors, and sending notifications with deltas and such. I looked around for anyone already using an ESP32 for this and hadn’t come up with anything. I figured maybe others would be interested.

Of course now that I’ve created my own ESPHome is on the case! I’ll go check it out!

cool. Because of the (so far) limitations in that the script needs to run as root, I’ve not tried to convert this into a component, but more than happy to help you with this as I own a V2 scale. Just give me a shout if you need me to test anything

@lolouk44, many thanks for you and any other contributors who have may worked on this!
I’ve followed all guidelines, but it looks like the values I’m getting aren’t making any sense:

$ sudo python3 Xiaomi_Scale.py
Sent data to topic kuchi/weight: {“Weight”:“27.35”,“BMI”:“25.78”,“Basal Metabolism”:“854.57”,“Visceral Fat”:“2.92”,“Lean Body Mass”:“28.44”,“Body Fat”:“5.00”,“Water”:“73.50”,“Bone Mass”:“1.19”,“Muscle Mass”:“24.80”,“Protein”:“17.17”,“TimeStamp”:“2019-09-06 00:01:52”}

Obviously my weight isn’t 27.35 KG.
Any ideas how to troubleshoot?

P.S.
I’m using “Mi Body Composition Scale 2”.

Thanks again.

Never mind… I found the problem. A classic user problem :wink:
Apparently if you stall while getting off from the scale, the new figures that will pop up once you get off will be synced as last values stored for transmission. If you get off from the scale in time without stalling, all is well.

Thanks again for everything! works like a charm.

Hi Scott could you share ESP32 code?

Sure thing - https://github.com/rando-calrissian/esp32_xiaomi_mi_2_hass

I hope it’s useful to you!

Thank you for sharing.

What platform do you recommend to use with your code?
With PlatformIO I’m getting this error:
Error: The program size (1546310 bytes) is greater than maximum allowed (1310720 bytes)

Indeed - I forgot to include that you should add this to your platformio.ini :

board_build.partitions = min_spiffs.csv

Sorry about that!

First of all a big thanks to all the contributors to this project and especially to @lolouk44 for pointing me to this thread!

I was wondering if it is possible to use BLE presence detection to trigger @lolouk44’s script whenever the mi scale becomes available. Or could this lead to errors because the scale is probably in mid-measurement at the time the script runs (although this should be easy to circumvent). I will try this as soon as I find the time.