Integrating Xiaomi Mi Scale

i found the problem. You environment params from docker compose are not injected in the container. Thanks anyway

Thanks for the feedback.
Although I’m not new to using containers, I’m a noob in building them. What did you change to inject the environment params into the container please so I can amend?

Finally managed to create a proper manifest with several arch’es with latest as tag.
just need to know how to fix the environment params :slight_smile:

I did several changes to make it work, however, probably it can be made cleaner by someone who knows well python and docker stuff. this is what i did:
first, i didn’t use environment option but used an env file (i had problems with formatting the mac address)
So in the same location with the docker compose i have created a mi-scale.env file were i wrote:

MISCALE_MAC=XX:XX:XX:XX:XX
MQTT_HOST=xxx.xxx.xxx.xx
MQTT_PREFIX=miScale
MQTT_USERNAME=user
MQTT_PASSWORD=pass
MQTT_PORT=9999
MQTT_TIMEOUT=60
USER1_GT=76
USER1_SEX=male
USER1_NAME=David
USER1_HEIGHT=181
USER1_DOB=1990-01-01
USER2_LT=60
USER2_SEX=female
USER2_NAME=Ioana
USER2_HEIGHT=169
USER2_DOB=1990-01-01
USER3_SEX=female
USER3_NAME=Unknown
USER3_HEIGHT=120
USER3_DOB=1990-01-01

my docker compose part looks like that:

  mi-scale:
    image: an20dei/mi-scale:latest
    container_name: mi-scale
    restart: always
    network_mode: host
    privileged: true
    volumes:
      - /home/mi-scale/Xiaomi_Scale.py:/opt/miscale/Xiaomi_Scale.py
    env_file:
      - mi-scale.env

Besides that, i have also modified your python script, cause i ran into some issues. Your params were not injected because you were using the wrong method to grab them. It is os.environ.get and not os.getenv or at least that worked for me. Also, converted some values cause i ran into some issues due to that, now it looks like this:

MISCALE_MAC = os.environ.get('MISCALE_MAC', '') 
MQTT_USERNAME = str(os.environ.get('MQTT_USERNAME', '')) 
MQTT_PASSWORD = str(os.environ.get('MQTT_PASSWORD', '')) 
MQTT_HOST = str(os.environ.get('MQTT_HOST', '127.0.0.1')) 
MQTT_PORT = int(os.environ.get('MQTT_PORT', 1883)) 
MQTT_TIMEOUT = int(os.environ.get('MQTT_TIMEOUT', 60)) 
MQTT_PREFIX = str(os.environ.get('MQTT_PREFIX', 'miScale'))

Hope it helps.
Thanks

How do I run the script on Hass.IO on Raspberry pie ?

Probably you can take the files and using this tutorial, https://developers.home-assistant.io/docs/en/hassio_addon_tutorial.html, you can try to create your custom add-on that is using those scripts.

Thanks.
Has anyone already created an this kind of addon so the the mi scale component will work on Hass.io ?

Thanks Mate, It’s odd as it’s working well for me.
Will have another look and I wonder if there’s a difference between python on amd64 platform (which I used / designed on ) and python on arm32v7…

[edit]
If this is to be believed,

os.environ raises an exception if the environmental variable does not exist. os.getenv does not raise an exception, but returns None

Still not sure why the env params would not be set up as I can’t really see the difference between listing them in the docker-compose Vs referring to a separate file containing them…

1 Like

Are you able to pull/use containers in hass.io? If so use one of the containers I’ve built?
(I don’t have hass.io so no idea if this is possible, I just hope it is)

if you notice, you separated the param from value with : i did it with =
I know that as you wrote them, it is a valid yaml, however, docker doesn’t seems to like the params if not like param=value. And i moved them to the file mainly because of the MAC address, probably the “:” from the actual mac address raised some issues in the background, dunno.
Anyway, thanks a lot for your script, great work :slight_smile:
PS: i still want to fix the logging cause currently none of the print or logging that i ve added is actually output ed in the container log, i would like to fix that too so i can see if something goes wrong.

Cheers :slight_smile:

1 Like

Thanks
will try and rebuild with = instead of : first

and see if I need to use os.getenv instead of os.environ, though not entirely sure “hiding” errors is the way forward :slight_smile:

anyway, you can test and see if the variables are loaded in the container by using docker exec mi-scale env, when i ran that on your image, i didn’t see any of those added. When using my image with the changes, i can see them all.
Cheers

1 Like

for those that are using Raspberry Pi with docker, i bumped into an issue regarding the bluetooth, sometimes it gets stucked and script is failing. If you face this problem, you can try and add some lines so you can reset your bluetooth before the script is running. I edited the script and added:

import subprocess
user = 'xxxxx'
host = 'xxx.xxx.xxx'
subprocess.Popen("ssh {user}@{host} {cmd}".format(user=user, host=host, cmd='sudo invoke-rc.d bluetooth restart'), shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate() 
time.sleep(10)
subprocess.Popen("ssh {user}@{host} {cmd}".format(user=user, host=host, cmd='sudo hciconfig hci0 reset'), shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate()
time.sleep(10)

I take it you first need to set an ssh key with ssh-keygen and save the key in a shared volume (so it does not get deleted the next time I update the container) for the script to work without requesting a password?

for some reason I’m struggling to build other architectures on my main server. Keeps coming up with

standard_init_linux.go:211: exec user process caused "no such file or directory"

I’m lucky enough to have a Pi1 and a Pi3 so rebuilding separately on each at the moment. Will then push. Have no arm64v8 device so might need to go back to windows / Docker Desktop, but no idea if that is the correct way.

Anyway I’ve also updated the docker-compose file on my github and docker hub to fix a few things:
under environment, it should be a list starting with -, and it should be = instead of :

1 Like

you are right, ssh commands are not working from the container. i am stuck with this issue, script stops and it will not work again until i will not reset the hciconfig. i tried to add the command in the pi crontab but still it gets stucked. still searching for a solution …

Hi everyone, i want to obtain some connection between RPI 3 B+ and mi scale 2 (that with 4 silver pads) but occurs some problems…
@lolouk44 I saw that you are familiar with the same staff but it was 1,5 year ago, are you still there? or maybe somone else can give some help tips?

There’s an issue that’s just been raised on my github requesting support this scale:

I should have time tomorrow to run some investigations if you’re happy to assist

Any progress on the docker container for Hassio on RPi? Mine is also reporting


standard_init_linux.go:211: exec user process caused "no such file or directory"

Thanks!

I believe the mi body composition scale 2 is working fine now? As the issue is closed on GitHub.

As the mi plant sensor is also directly integrated in HA, wouldn’t possible to integrate this in the same way? Without the docker needs