Integrating Xiaomi Mi Scale

after the latest update i got an error in logging:
2020-09-09 10:26:33 - Config Loaded…
Traceback (most recent call last):
File “/opt/miscale/Xiaomi_Scale.py”, line 344, in
main()
File “/opt/miscale/Xiaomi_Scale.py”, line 312, in main
if MQTT_DISCOVERY.lower() in [‘true’, ‘1’, ‘y’, ‘yes’]:
AttributeError: ‘bool’ object has no attribute ‘lower’

0.1.10 released.

Please note there are breaking changes (see below).
sensor definitions need to use lower snake_case to retrieve json attributes from the MQTT message
e.g. Change
value_template: "{{ value_json['Body Fat'] }}"
into
value_template: "{{ value_json['body_fat'] }}"

Breaking Changes

Changed

Added

  • Added unit to attributes

I updated to 0.1.10 and have found the bluetooth disconnects much less often - see below. I am still getting the “Failed to execute management command ‘scanend’” errors. However, it seems that after a few minutes of errors it does successfully retrieve the latest weight. I will keep an eye on it.

The cluster on the left is how often bluetooth went down before the upgrade. The darker bands represent night, so this is 3 day’s of data.

Hey,
Great job :slight_smile:

would it be possible to add more than 3 users of the scale?

Thanks for the feedback.
What fait drives the Bluetooth is the bluepy library which I only use. I tried to add commands to reset the Bluetooth radio when there are issues to help.
I find that using a Bluetooth dongle is better than built in one, especially is using a Pi. Also if other processes use the Bluetooth radio there can be times where both apps fight fois access…

Thanks.
How many users did you have in mind?
The key issue here will be of weights overlap. How will the program decide whose weight it is?

4 users,
Weigth between 72 and 85 1user
Weigth between 55 and 71 2user
And so on.
Hmmm, next would be my growing children… I will have to think about it because they have 2 and 3 years…
It can be done on homeassistant itself I think - making template sensor on a way I wrote above

@lolouk44 great work! I’ve bot mi-scale 2 and I found your repo and its great!

But when I am using:

image: lolouk44/xiaomi-mi-scale:latest

on my Raspberry Pi 4, I got error:

-------------------------------------
Starting Xiaomi mi Scale...
MQTT connection: Connection Accepted.
Traceback (most recent call last):
  File "/opt/miscale/Xiaomi_Scale.py", line 180, in <module>
    main()
  File "/opt/miscale/Xiaomi_Scale.py", line 173, in main
    scanner.scan(5)
  File "/usr/local/lib/python3.8/site-packages/bluepy/btle.py", line 852, in scan
    self.start(passive=passive)
  File "/usr/local/lib/python3.8/site-packages/bluepy/btle.py", line 797, in start
    self._mgmtCmd(self._cmd()+"end")
  File "/usr/local/lib/python3.8/site-packages/bluepy/btle.py", line 312, in _mgmtCmd
    raise BTLEManagementError("Failed to execute management command '%s'" % (cmd), rsp)
bluepy.btle.BTLEManagementError: Failed to execute management command 'scanend' (code: 11, error: Rejected)

When I use:

image: lolouk44/xiaomi-mi-scale:arm32v7

(updated 4 month ago, so I asume some older version)
it works (at least for a moment of test, I haven’t tried it longer)

Any ideas why latest is not working?

To be honest I have no idea. The issue is with bluepy library which I only use. Raspberry pies are known to have issues with Bluetooth and this library. Try a Bluetooth dongle or have a look at some of the issues raised on my github, there are a few things you could try…

Can i use this integration to show multiple users, and how can use that? Such as when another person want to scale it must register it to homeassistant right? You must then click somewhre on the xiami scale or on home assistant so it wil then register weight under that user. Is that possible?

You can currently have up to 3 users. Please follow the doc to know how to define users.

Finally had a minute to try setting this up after getting my MiScale delivered last month. It’s the Mi Body Composition Scale 2/XMTZC05HM mode. First of all, thanks for your work so far @lolouk44 , it looks like you’re putting tonnes of effort into this.

I’m having a bit of trouble with the setup though. I’ve made myself User 3, with USER1_GT and USER2_LT being 15kg either side of my approximate real weight. Annoyingly, my house is mostly carpeted and my HA/MiScale server is more or less in the middle of the carpeted area. If I do a test weigh-in within a meter or two of the server, the weight is about 1/3 what it should be due to the carpet so registers as Serena, and only hits the MiScale container logs about 2/3 of the time. If I move it to a solid area, weights are correct but weigh-ins don’t hit the logs at all except one time that I changed Serena’s LT to above my weight.

Gotta keep messing around, hopefully I don’t need to dedicate an rPi as a scale receiver.

EDIT: Blah, definitely seems like a signal issue. I must’ve screwed up the config earlier, after some fiddling it was reporting all user weights correctly at close range. Upping the scan interval helped but I’m still losing more than half the weigh-ins with less than 5 m (and admittedly, an external wall and a bunch of electronics) in between.

I am also trying to understand how the BTLE scan work.
It seems to me my Miscale can no longer store the weight in its memory.
And only way to get the weight data is during actual weight measurement. Therefore I have following in Xiaomi_Scale.py:

scanner.scan(5)
TIME_INTERVAL = int(os.getenv('TIME_INTERVAL', 1))

BTW, I have Mi Smart Scale 2, model XMTZCO(0?)1HM .

@lolouk44 Thanks a lot for you work.

One comment regarding the @reboot line in crontab.
I have to add:
sleep 60 &&
before:
bash /path/to/wrapper.sh
Otherwise, it will fail when trying to send out MQTT msgs, possibly due to wifi not being ready.

Thanks @Nassau
If you’re the only user you don’t autant need to set up the other users. Simply set the GT to a weight below your weight
Fire the weighing, I’ve also noticed that the scale needs to be on a sturdy flat surface to get accurate results (but to be fair tata also the case with other scales I’ve tried in the past)
I would say weigh yourself away from the carpet then move the scale. It’ll stay awake for 5-10 min

Thanks. That should be easily added :blush:

Ah, I think I tried removing Users 2 & 3 and the GT weight for User1 when it wasn’t working as expected, but yeah I just set the User1 GT less than my own while keeping 2 & 3 to get it working.

I’m a bit confused how the scale actually works now, though. I had been assuming it just sends out the weigh-in when those four bars fill up on the scale display. But based on what you and @suan are saying, it sounds like it will keep sending recent weigh-in(s) periodically? Is there any doco on things like how much history it keeps/how frequently it sends etc?

The scale will keep the last weight in memory for 5-10 min before going to sleep. During that time the script week query the scale and retrieve that info.

Hello,

Thanks for this addons.

It’s possible to have a full lovelace card with all attribute please ?

I am verry bad in Lovelace configuration.

Thanks and sorry for my bad english

Hello all,

Please, I need some help configuring or debugging my setup.
I have the @lolouk44 add-on (thank you!) started and working.
Also, (this is my first mqtt) I have started the mosquitto broker addon in the same HA to be able to run the previous add-on.
After that, a mqtt integration appeared automatically in HA and I added it as it comes.
Currently, I have three entities, also automatically created under HA, with the name of the 3 users.
After doing some test weights, all the entities are still ‘Unknown’.
Any help?
(Edit: no ‘Data Published’ in the log, just the Discovered Complete)