Integrating Xiaomi Mi Scale

you can use either. JSON’s structure is built like an array with layers. If you had a layer that had a space in its name you’d have to use the [‘xxx yyy’] method.

you don’t specifically need this. If you have it, your sensor will pull the data from that topic to populate the sensor’s attributes.

1 Like

I’ve tried to make sensor with your recomendations, but still have no in it.
Also I try to change mqtt setting - earlier it was like z2m:

MQTT_HOST: mqtt://core-mosquitto

now it’l like in mqtt explorer:

MQTT_HOST: 192.168.31.160

And i still can see info in mqtt topic via mqtt explorer, but not in sensor

Summary

image

  - platform: mqtt
    name: "Nika Weight"
    state_topic: "miscale/Nika/weight"
    value_template: "{{ value_json.weight }}"
    # value_template: "{{ value_json['weight'] }}"
    unit_of_measurement: "kg"
    # json_attributes_topic: "miscale/Nika/weight"
    icon: mdi:scale-bathroom
    # Below lines only needed if long term statistics are required
    state_class: "measurement"

  - platform: mqtt
    name: "Nika BMI"
    state_topic: "miscale/Nika/weight"
    # value_template: "{{ value_json['bmi'] }}"
    value_template: "{{ value_json.bmi }}"
    icon: mdi:human-pregnant
    unit_of_measurement: "kg/m2"
    # Below lines only needed if long term statistics are required
    state_class: "measurement"
2022-03-04 21:29:12 - Starting Xiaomi mi Scale...
2022-03-04 21:29:12 - Loading Config From Add-On Options...
2022-03-04 21:29:12 - Config Loaded...
2022-03-04 21:29:12 - Discovery Completed...
2022-03-04 21:29:13 - Publishing data to topic miscale/Nika/weight: {"weight":95.60,"weight_unit":"kg","bmi":41.38,"basal_metabolism":1589.55,"visceral_fat":13.37,"lean_body_mass":59.45,"body_fat":49.59,"water":36.00,"bone_mass":2.92,"muscle_mass":45.28,"protein":11.36,"body_type":"Thick-set","metabolic_age":80,"impedance":386,"timestamp":"2022-03-04T21:29:12+00:00"}
2022-03-04 21:29:13 - Data Published ...
2022-03-04 21:30:58 - Publishing data to topic miscale/Nika/weight: {"weight":95.60,"weight_unit":"kg","bmi":41.38,"basal_metabolism":1589.55,"visceral_fat":13.37,"lean_body_mass":59.41,"body_fat":49.63,"water":35.97,"bone_mass":2.92,"muscle_mass":45.24,"protein":11.35,"body_type":"Thick-set","metabolic_age":80,"impedance":392,"timestamp":"2022-03-04T21:30:58+00:00"}
2022-03-04 21:30:58 - Data Published ...
Summary

And in the same time i used HACS addon https://github.com/dckiller51/bodymiscale
Sensors (with different names):

  - platform: template
    sensors:
      weight_nika:
        friendly_name: "Вес Ника"
        value_template: >-
          {% set weight = states('sensor.ble_weight_mi_scale') | float(0) %}
            {% if 70.0 <= weight < 100.0 %}
              {{ states("sensor.ble_weight_mi_scale") }}
            {% else %}
              {{ states("sensor.weight_nika") }}
            {% endif %}
        unit_of_measurement: 'kg'
        icon_template: mdi:weight-kilogram

  - platform: template
    sensors:
      impedance_nika:
        friendly_name: "ИМТ Ника"
        value_template: >-
          {% set weight = states('sensor.ble_weight_mi_scale') | float(0) %}
            {% if 70.0 <= weight < 100.0 %}
              {{ states("sensor.ble_impedance_mi_scale") }}
            {% else %}
              {{ states("sensor.impedance_nika") }}
            {% endif %}
        unit_of_measurement: 'Ohm'
        icon_template: mdi:omega

And they are working!
And I still don’t understand, whats wrong with my mqtt-based sensors =(
MQTT addon configuration is more flexible for multiple users…

2 Likes

Your problem with the template sensors is that they will not persist over reboots/reloads of HA.

Is there anything in the logs? Did you setup MQTT from the UI?

Is the topic retained when you publish to it?

No, the problem is not that the sensors are not saved after the server is reload.
In principle, no information has ever been received by these sensors.
MQTT broker, z2m and other zigbee integrations and devices working perfectly.

Which logs do you mean? HA, Docker, Mosquito broker or scale integration?

This isn’t my add-on so I can’t compare, but at first sight it doesn’t use mqtt.
Are your other mqtt sensors automatically added or manually added?
If you want to use add-on have you tried my add-on which is basically the same code and config, only the sensors would be automatically added via mqtt

Yes you’re right. The HACS addon does not use mqtt.
I have a 3rd party SleepAsAndroid integration that sends sleep data from phone to mqtt. But it created the sensor automatically.
Now I have this addon installed via supervisor:

Tell me, please, what needs to be changed for the sensors are automatically added?

All instructions are documented. Can you share the full sensors section of your configuration.yaml?
Can you also share your mqtt broker config? (Hide the credentials)
Stupid questions

  • have you defined multiple instances of your mqtt broker? You only need one
  • can you try and set one of your scale sensor to pull from one of your non scale sensors?
  • you mention Sleep As Android but I can’t see it on your mqtt Explorer screenshot. Are you sure you’re using to correct broker?

Yes, but the addon triggers on startup. If you use MQTT sensors with the MQTT message retained, the sensors populate on startup. But another issue is that the addon relies on 2 sensors and does not check both have been updated within a time window, so, you can end up with it triggering on a new weight but old impedance. Double reports if the two sensors are updated at different times · Issue #51 · dckiller51/bodymiscale · GitHub

However, recently I have seen the addon trigger before the sensor has been populated - the major issue with HA is the order things get populated and dependency.

The addon needs to check for unknown or unavailable and not trigger in this state. Error reported when sensors not ready · Issue #60 · dckiller51/bodymiscale · GitHub

You were absolutely right! :sweat_smile:
I messed up with the MQTT settings.
After I corrected the settings in the addon to those that I use in the SleepAsAndroid integration, everything worked!
But now I have 2 weight sensors:
sensor.nika_weight
sensor.nika_weight_2

Sensors

I made a config according to the recommendations above:

  - platform: mqtt
    name: "Nika Weight"
    state_topic: "miscale/Nika/weight"
    value_template: "{{ value_json.weight }}"
    # value_template: "{{ value_json['weight'] }}"
    unit_of_measurement: "kg"
    # json_attributes_topic: "miscale/Nika/weight"
    icon: mdi:scale-bathroom
    # Below lines only needed if long term statistics are required
    state_class: "measurement"

  - platform: mqtt
    name: "Nika BMI"
    state_topic: "miscale/Nika/weight"
    # value_template: "{{ value_json['bmi'] }}"
    value_template: "{{ value_json.bmi }}"
    icon: mdi:human-pregnant
    unit_of_measurement: "kg/m2"
    # Below lines only needed if long term statistics are required
    state_class: "measurement"

Do I understand correctly that I need to delete the weight sensor section in the config.yaml and use the one automatically created by the addon?

And what about this block?

# Below lines only needed if long term statistics are required
    state_class: "measurement"

If I remove the sensor from the config, there will be no statistics after the restart?

Ah OK. GitHub - lolouk44/hassio-addons: Xiaomi Mi Scale Add On for Home Assistant - I’d not realised you had done your own code.

I use the GitHub - custom-components/ble_monitor: BLE monitor for passive BLE sensors addon for retrieving the BLE data then pass it on to the miscale Card.

I don’t really want 2 different addons trying to communicate with BT.

@baz123, I’m using both (Xiaomi Mi Scale Add-on and Passive BLE monitor Integration) and didn’t find any issues with both accessing BT. @lolouk44’s solution allows having weight measurements for multiple people as long as there’s a big enough difference between them.

I’ve been bitten before so that’s why I avoid. I’d rather have one integration for getting the data and something else for processing. I also keep the number of addons to a minimum where I can.

I’m doing that via Node-RED currently.

Hey there,

thanks for all your efforts so far.

I am currently planning on integrating my Xiaomi Mi scale into Home Assistant, too.

However, my instance runs in a VM on my server, so I cannot add bluetooth to it. I do have some Raspis laying around, which I’d like to use for this.

I do not have any MQTT sensors or something like that in use so my question is:
What is the best way to realise this scenario? Scale → RPi with BT LE → Home Assistant

Thanks in advance!

Mqtt is still probably the best way to communicate with HA. It’s simple to implement and does not require any additional hardware

Can you not plug in a USB BLE dongle and pass it through to the VM?

That’s what I thought too. However, I am not really experienced with this way. I already installed the mosquitto MQTT broker on my HA yesterday but i don’t know what needs to be installed on my Pi (probably the Xiaomi scale script) and how to pass it to HA. Is there a simple tutorial on how to pass information to HA via MQTT that you (or anybody else) can recommend?

@baz123 I could, but the scale is way out of reach for the server, that is in my rack outside of the living area in a shed.

MQTT is a protocol - it consists of a Broker (server) - that accepts data (that is published to it) and makes it available for other systems to read (subscribe). Your Pi would Publish the data to the Broker and HA would then subscribe to the topic and read it into a sensor. The Broker can be on any machine that is accessible by both on your LAN.

On the Pi, you would use a Pythion (probably) script to read the BLE data, parse it, and publish it to the MQTT broker on your HA install. You then create an MQTT sensor. You need to do lots of Googling, but there are enough keywords here for you to find the answers.

What @baz123 said. You only need to take care of the server, use the same credentials with the add-on / script and use the same credentials with HA

5 years later after starting this thread :

The easiest and most solid way to integrate the Miscale is ESPHome to me. I have setup a complete Bluetooth Low Energy Hub collecting several BLE sensors on a single low cost and low energy ESP32 device. I have also written a documentation (in german) about the necessary configuration and the calculation of the BMI directly on the ESP32. In case it is helpful to you throw this URL in the translator of your choice:

1 Like

I have EN local on my Home Assistant server. But how I can manually set other language in Lovelace card with [bodymiscale] intagrtion?