Integrating Xiaomi Mi Scale

Corrected :slight_smile:

What about L51? The original code is:
if data.startswith(‘1d18’) and sdid == 22:
Your data starts with 1b18, did you change it as well?

Yes I had to change this line to if data.startswith(‘1b18’) :
Admittedly, I’ve not tried with the and sdid == 22 bit

I believe it’s like that:

String: 1b1802a6e20705150a251df401443e

Item Formula HEX Decimal
Device Type data[0:4] 1b18
Unit data[4:6] 02
User? data[6:8] a6 166
year int((data[10:12] + data[8:10]), 16) 07e2 2018
month data[12:14] 05 5
day data[14:16] 15 21
Hour? data[16:18] 0a 10
Min? data[18:20] 25 37
Sec data[20:22] 1d 29
Impedance int((data[24:26] + data[22:24]), 16) 01f4 500
Weight int((data[28:30] + data[26:28]), 16) * 0.01 / 2 3e44 79.7

Does this help? https://unix.stackexchange.com/questions/96106/bluetooth-le-scan-as-non-root

small typo for sec, should be data[20:22].
The formulas were not correct either for most

String: 1b1802a6e20705150a251df401443e
Item Formula Value Value
Device Type data[0:4] 1b18
Unit int(data[4:6],16) 02 2
? int(data[6:8],16) a6 166
year int((data[10:12] + data[8:10]), 16) 07e2 2018
month int(data[12:14],16) 05 5
day int(data[14:16],16) 15 21
Hour int(data[16:18],16) 0a 10
Min int(data[18:20],16) 25 37
Sec int(data[20:22],16) 1d 29
Impedance? int((data[24:26] + data[22:24]), 16) 01f4 500
Weight int((data[28:26] + data[26:28]), 16) * 0.01 /2 3e44 79.7

I can confirm above readings to be correct / as expected

Actual readings from the app:
Weight: 79.7 Kg
Body fat: 25.7%
Muscle 56.1Kg
water: 50.9%

my height: 175cm
my age: 39

Here’s another reading from this morning:

String: 1b1802a6e20705180c0d04d701943e
Item Formula Value Value
Device Type data[0:4] 1b18
Unit int(data[4:6],16) 02 2
User? int(data[6:8],16) a6 166
year int((data[10:12] + data[8:10]), 16) 07e2 2018
month int(data[12:14],16) 05 5
day int(data[14:16],16) 18 24
Hour int(data[16:18],16) 0c 12
Min int(data[18:20],16) 0d 13
Sec int(data[20:22],16) 04 4
Impedance int((data[24:26] + data[22:24]), 16) 01d7 471
Weight int((data[28:26] + data[26:28]), 16) * 0.01 /2 3e94 80.1

Actual readings from the app:
Weight: 80.1 Kg
Body fat: 25.7%
Muscle 56.4Kg
water: 50.9%

my height: 175cm
my age: 39

I’ve tried various formulas to try and calculate Total Body Water (TBW) Fat Mass (FM) and Fat Free Mass (FFM) from the links that @cihanulusoy provided, but I can’t find anything that gives me the same values as what’s in the app.
I’ve emailed Xiaomi asking if they can provide the formulas they use, you never know

For the purpose of reading from the Xiaomi scale, I guess the above is enough, the calculations will need to be made in HA / in the script anyway as extra data will be required, such as height, age and male/female…

@syssi I can’t find a way for the scale to send the data in jin, I only have lbs and kgs options for the scale. The app offers different units, but changing these has no incidence on the data the scale itself sends…

I’ve amended thes scrip to cater for both versions. Can people try and feedback please?
https://hastebin.com/mibikorepo.py

Thanks for the warning. I’ve corrected. Actually, 2 jins = 1 kg, therefore the weight data that the scale sends is in jin, we divide it by two to get the wieght in kg.

I know that 2 jins = 1 kg (the other way round :wink: ) I just don’t know if the scale can send measures in that unit (so as to try and catch the unit from the stream and handle correctly)
If anyone has a V2 scale that sends in jin, please provide the data so I can update the code…

Correct. So int((data[28:26] + data[26:28]), 16) * 0.01 is in jins.

I believe Mi Fit apk can be reverse engineered to find the formula.

I’m on iOS so can’t do this. If you have android and can reverser eng, that would be great

I can run android on a beaglebone however even Android Studio is not set up on any of my computers. APK is there: https://www.apkmirror.com/apk/anhui-huami-information-technology-co-ltd/mi-fit/mi-fit-3-4-0-1-release/

It can easily be disassembled and formula can be extracted.

great, I’ll wait for your feedback then :slight_smile:

Well, actually in https://github.com/oliexdev/openScale/wiki/Supported-scales-in-openScale, it has been noted that “Only the weight are supported by openScale. The rest of the body metrics are currently not supported because of the not transparent impedance calculation by the company”.

The formulae in openscale are at https://github.com/oliexdev/openScale/wiki/Body-metric-estimations but these do not consider the resistance measured by the scale.

I was just wondering how often to run the script.
@syssi listed above a run on an hourly interval at xx:30. I’m concerned that the scale will be in sleep mode by then.
Yet if I refresh too often, the scale doesn’t go into sleep mode, thus consuming more power/battery
I was wondering if anyone had found a “sweet spot” on how often to “prod” the scale to check for readings to

  1. ensure the latest weighing gets caught
  2. ensure we don’t keep the scale “awake”

So far I’m running at a 5 min interval but would like to hear other people’s experience

I am thinking of creating a switch template which would run the script.
If I call it something like “Scale”, I would be able to command Alexa “turn on scale” after I weight myself, and this will read the data off the scale.

Everything is in future tense because I just got mi scale delivered and haven’t yet tried to use it :slight_smile:

I’ve been playing about with the “sleep” time for the script. It is now called by a cron job every 5 min. It’s enough to catch a reading before the scale goes back to sleep, and not often enough that it’ll keep it awake.

image

Now I’m no android user and have no idea on how to reverse engineer an apk, but if someone had the skills to try and extract the formulae used to calculate the various body measures, it would be great

I managed to download the APK and convert it to JAR, but I have no idea what or where to look to try and get he formula.
Is there anybody who’s familiar with APK / Android programming who could have a look or at least give me some pointers? I can PM the files if required.

I was playing with it as well, didn’t find anything unfortunately.
I believe the formula is somewhere in these ARM compiled files:
lib\armeabi\libBodyfat.so
lib\armeabi\libdataProcess.so
No idea how to decompile them though. I saw some ASM/C++ converters, but they are not free. ASM is simply gibberish to me so that I have no point of looking at even :slight_smile:
We need someone who can dig deeper!

1 Like

Guys, what about OpenScale?

I’ve done a feature request: OpenScale component

It’s been a while since I’ve started this post. Today I’ve taken the time to write a blog post about the way I’ve integrated the MiScale in Homeassistant. The post is in german. Maybe it’s helpful to someone.

https://www.triumvirat.org/2018/07/28/miscale-und-homeassistant/

2 Likes