Code to read weight measurements from Xiaomi Body Scales.
An MQTT broker is needed as the counterpart for this daemon. Even though an MQTT-less mode is provided, it is not recommended for normal smart home automation integration. MQTT is huge help in connecting different parts of your smart home and setting up of a broker is quick and easy.
-
Body Composition Scale 2 (XMTZC05HM)
-
ESP32 (BLE SCANNER), see ESP32 Application
-
Raspberry or Linux computer
-
Python 3 (min)
-
MQTT broker
-
Libs see requirements.txt
-
Optional Homeassistant, see Settings & Views Homeassistant
-
Optional influxdb, see Influx DB & Grafana Dashboard
On a modern Linux system just a few steps are needed to get the daemon working. The following example shows the installation under Debian/Raspbian below the /opt
directory:
$ su -
$ cd /opt/
$ git clone https://github.com/zibous/ha-miscale2.git
$ cd /opt/ha-miscale2
$ sudo pip3 install -r requirements.txt
To match personal needs, all operation details can be configured using the file conf.py The file needs to be created first:
$ cd /opt/ha-miscale2
$ cp conf.py.dist conf.py
# edit configuration settings
$ nano conf.py
$ chmod +x app.py
$ chmod +x mqttservice.py
$ chmod +x service.sh
$ chmod +x service_mqtt.sh
## tests
$ python3 app.py
$ ./app.py
$ ./app.py && tail -f /var/log/syslog | grep MIBCS2
$ python3 mqttservice.py
$ ./mqttservice.py && tail -f /var/log/syslog | grep MIBCS2
Read Xiaomi Mi Body Composition Scale data from a Raspberry Pi in Python 3.
python3 app.py
A simple Python script which provides a MQTT gateway for ESP Devices, easily extensible via custom workers. Application for ESP Devices see:
Details see : ESP 32 Appliciaton
You most probably want to execute the program continuously in the background. This can be done either by using the internal daemon or cron.
Systemd service for - on systemd powered systems the recommended option
$ sudo cp /opt/ha-miscale2/service_app.template /etc/systemd/system/ha-miscale.service
$ sudo systemctl daemon-reload
$ sudo systemctl start ha-miscale.service
$ sudo systemctl stop ha-miscale.service
$ sudo systemctl status ha-miscale.service
$ sudo systemctl enable ha-miscale.service
$ sudo cp /opt/ha-miscale2/service_mqtt.template /etc/systemd/system/ha-miscale-mqtt.service
$ sudo systemctl daemon-reload
$ sudo systemctl start ha-miscale-mqtt.service
$ sudo systemctl status ha-miscale-mqtt.service
$ sudo systemctl enable ha-miscale-mqtt.service
The calculation results are saved once as a history in the data folder, published as an MQTT payload and optionally saved in the defined InfluxDB.
Sample MQTT Payload
Topic: tele/miscale2/Peter/measured
These data are published by the "ESP32 Application" and can either be processed directly in another application. In my case, this data is evaluated via the Python application and the additional data is calculated and then published (Infuxdb, Homeassistant).
{
"user": "Peter",
"sex": "male",
"athletic": true,
"age": 64.85,
"weight": 68.2,
"unit": "kg",
"impedance": 539,
"bmi": 22.27,
"water": 43.11,
"fat": 18.4,
"timestamp": "2020-10-05 06:23:03",
"version": "1.0.1",
"icon": "mdi:scale-bathroom",
"attribution": "Data provided by Peter Siebler"
}
Topic: tele/miscale2/Peter/data
This data is published by the "calc module"
{
"measured":70.65,
"calcweight":70.65,
"unit":"kg",
"impedance":485,
"timestamp":"2020-09-18T06:34:29Z",
"scantime":"2020-09-18 08:34:29",
"user":"Peter",
"sex":"male",
"athletic":true,
"age":64.8,
"metabolic_age":40.39,
"bmi":23.07,
"bodytype":"balanced",
"weight":70.65,
"idealweight":66.5,
"lbm":55.76,
"fat":13.14,
"fattype":"to_gain",
"idealfat":1.98,
"visceral":7.39,
"water":55.72,
"bone":3.64,
"muscle":52.17,
"protein":20.47,
"bmr":1222.9,
"targetweight":68.0,
"icon":"mdi:scale-bathroom",
"attribution":"Data provided by Peter Siebler"
}
Topic: tele/miscale2/Peter/scores
This data is published by the "calc module"
{
"user": "Peter",
"score": 90.0,
"deltas": {
"weight": -0.8,
"fat": -0.21,
"water": 0.0,
"muscle": 0.0,
"visceral": -0.4,
"protein": 0.02
},
"states": {
"weight": "Abgenommen",
"fat": "Abgenommen",
"water": "Keine Ver\u00e4nderung",
"muscle": "Abgenommen",
"protein": "Zugenommen"
},
"scores": {
"bmi": 0.0,
"fat": 10.0,
"visceral": 0.0,
"muscle": 0.0,
"water": 0.0,
"bones": 0.0,
"bmr": 0.0,
"protein": 0.0
},
"caloric": {
"caloricmin": 2100,
"caloricmax": 2400,
"deficitmin": 1677,
"deficitmax": 1923
},
"engergieexp": 2491,
"macronut": { "protein": 872.0, "carbohydrates": 1246.0, "fat": 374.0 },
"version": "1.0.1",
"timestamp": "2020-10-05 06:23:03",
"icon": "mdi:scale-bathroom",
"attribution": "Data provided by Peter Siebler"
}
- Thanks @lolouk44 to https://github.com/lolouk44/xiaomi_mi_scale
- Formulas to calculate the various values/measures, I've got them from https://github.com/wiecosystem/Bluetooth
- https://inbodyusa.com/general/technology/
- https://github.com/limhenry/web-bluetooth-mi-scale
- https://www.medeia.com/dtr_bioscan_bia.htm
- https://www.jstor.org/stable/41463874
- https://www.wikihow.fitness/Determine-Lean-Body-Mass
- https://www.omnicalculator.com/health/body-fat#how-to-calculate-body-fat
- https://calculator-online.net/ideal-weight-calculator/
- https://en.wikipedia.org/wiki/Body_mass_index
- https://www.calculator.net/bmi-calculator.html
- https://www.calculator.net/calorie-calculator.html
- https://www.omnicalculator.com/health/bmr-katch-mcardle#what-is-the-katch-mcardle-calculator
- https://www.omnicalculator.com/health/maintenance-calorie
- https://en.wikipedia.org/wiki/Body_water
- https://de.wikipedia.org/wiki/Ponderal-Index
- https://en.wikipedia.org/wiki/Body_fat_percentage
- https://en.wikipedia.org/wiki/Lean_body_mass
- http://elsenaju.de/AMI-Rechner.html
- http://elsenaju.de/Kalorien-Bedarf-Rechner.html
- https://www.calculator.net/lean-body-mass-calculator.html
- http://www.dev.egofit.de/biadata-org/
- https://github.com/zewelor/bt-mqtt-gateway
- https://www.mi.com/global/mi-body-composition-scale
- https://dev.to/henrylim96/reading-xiaomi-mi-scale-data-with-web-bluetooth-scanning-api-1mb9
- https://github.com/Wingjam/ReverseMiScale/blob/master/miScale.py
- https://tanita.de/hilfe-und-anleitungen/richtige-interpretation-ihrer-messwerte/
- https://github.com/oliexdev/openScale
- https://support.withings.com/hc/en-us/articles/218500778-Body-What-are-the-normal-ranges-for-body-composition-
- https://googlechrome.github.io/samples/web-bluetooth/scan.html
- https://github.com/IanHarvey/bluepy/blob/master/docs/scanner.rst
- https://passionhome.sg/xiaomi-mi-body-composition-scale-v2-tips-and-instructions/
- https://github.com/rando-calrissian/esp32_xiaomi_mi_2_hass
- https://github.com/limhenry/web-bluetooth-mi-scale