Xiaomi Mijia bluetooth temperature & humidity sensor compatibility

I was able to implement component for parsing of Bluetooth Low Energy packets payload that contains temperature/humidity/battery data and that are emitted each second by the sensor. That allows to auto-detect new sensors (both first and second versions) and passively receive data each second without battery impact at all.

There are 3 reasons why I didn’t commit my changes into home assistant repository yet (but I’m working on it):

  1. It requires 1 more command to run on Raspberry Pi itself that allows hcidump command to run without root access:
sudo setcap 'cap_net_raw+ep' `readlink -f \`which hcidump\``
  1. Configuration support is not yet ready, whole configuration looks like this now:
sensor:
  - platform: mitemp_bt
  1. I did everything inside one file that goes against coding standards of Home Assistant. I’m working on it and expect to create merge request in the next months.

In case you’d like to test it on your system, here is the link to my custom_component mitemp_bt. The folder to unpack is /home/homeassistant/.homeassistant/custom_components/ Also you need to run the command I wrote above to allow rootless access to bluetooth device for hcidump tool. I will be glad for constructive feedback.

9 Likes

It is working for me.
And a big plus, there is no any warning because of detection or polling error like with previous version.

It didn’t work initially (I suspect my BLE dongle is a bit ordinary) but second reload is working well getting temperature data every second now!

Thank you for your work, Ivan! I was pleasantly surprised by the news that this sensor sends readings every second, and does not require polling. Such operation of the sensor provides a large window for filtering its readings, but unlike the original mitemp_bt with a five-minute poll, the delay here will be acceptable even with a median of 30 or more (beautiful fast smooth graphics without affecting power consumption). We look forward to the release of your code!

On my RPI 3B+ with Raspbian Buster and HA 0.97.0 (virtualenv) your code works fine. The log has cleared of mitemp_bt timeouts.
The only thing, in addition to your instructions, I had to do:

sudo apt install bluez-hcidump

Hello,

Thanks for your code.
It seems that it requires BLE support on the dongle, right?
When I setcatp, I had this error

Set scan parameters failed: Input/output error

Thanks

Here is my attempt to implement filtering (and reduce HA DB growth rate :slight_smile:
The sensor status is updated with the average of all readings, received between updates (hardcoded 60 secs). In sensor state details you can see actual number of averaged readings ("median: ").
This is my first experience with Python and HA dev, please do not laugh much )

Yes, BLE support required.

can you write detailed, how to use your sensor? where to place file and config for it?
thanks!

My Bluetooth dongle is 4.0 and supports BLE I think. I was able to connection to Xiaomi Temp bluetooth before with this dongle.
I’m on Debian 10

Any one has an idea how to avoid the database to increase?

If you really do setcap, then there is a mistype in the cmd line. You can try to:

sudo setcap 'cap_net_raw+ep' `readlink -f  `which hcidump``

or

sudo setcap 'cap_net_raw+ep' `which hcitool`

In fact, your error is a hcitool error, and it’s easy to google. You can try to:

sudo hciconfig hci0 down
sudo hciconfig hci0 up
sudo hcitool lescan

Just follow tsymbaliuk instructions (remove the backslashes from setcap cmd), and replace sensor.py content with code from my gist.

1 Like

I found some problems with this solution.

  1. The first problem is that with such a frequent update of sensors, the database is very quickly filled with data and the server becomes slow …

As a solution, I did the update once every one minute with the average value of all the data received for that minute.

  1. Turning on debugging, I noticed that the number of actually processed values ​​is much less than expected, based on the refresh rate of the sensors.
    As far as I understand, the essence of the problem is that hcidump generates such a large amount of data that it quickly overflows the allowed size of the PIPE buffer and further output is blocked (sometimes only 6 readings actually reach a minute).

I solved the problem by redirecting the output of hcidump to a python tempfile - now I really get all the readings in a minute (total about 45-50 and more, 20+ per minute for each of my two sensors).

Updated my gist with the described changes (+ some small fixes).

2 Likes

Guys thanks for code, it does work on hassio :slight_smile:

Is this custom component fixing the non-updating data issue ?

Yes so far everything is runs smooth. No issues.

Yes, it is fixing because there is no need for polling, the sensor originaly sends the data in every seconds.

I must be a total noob, but I can’t run your command. I tried both with a SSH session on port 22 and 22222, but each time, it returns

'bash: sudo: command not found"

I am on a Raspberry Pi 3B+, with an hassio image. Is this command supposed to work on Raspbian only ?

I did it this way

works great

even got
one of these

https://www.ebay.com/itm/Xiaomi-Mi-Bluetooth-Smart-Plant-Monitor-Flora-Soil-Water-Light-Sensor-Tester/153551869395?var=453547131351&_trkparms=ispr%3D1&hash=item23c06795d3:m:m2Zuz1p2WD8-l4i0f98ZZhg&enc=AQAEAAACIBPxNw%2BVj6nta7CKEs3N0qVyy%2B%2FGlKC87xTxz8QnnNfgY%2FcP3Aogpvo3%2BstOWpIvEi8QPC7RJujUbx28Un05hvzaOlb%2B8wjR29w%2FReHtkhhVMjiaigd%2B3loBFchhLGFaDuzeZFFDQY9ekkFIGEYU7jpNGH00LaAd6xuMZ8QYP1XcWvJd2ej1Q4r0BmqLKplCdyTBdovy%2FOO%2FvzvtWMQhkf9TZoOtzRjHelhLLcT7nXvr5sjtz2RU4MfFmw7cAwfkbW5gX5ortihQwCpN3J5VwLjwrFK%2FaVzkzA3l4jt21%2Fi6JwndwMiBatHAFiOn4Xjw85PRZUYSZYOu%2BndDhyUmeM2dgJGSFEEGfj1X5h%2BqldQxNAzIt7wLPQ19rBurSFHiYVAA4GFYoJsFB1yiHxi9Fl2CMDQkj%2Fdnyd2AQZ%2FQuaEMc%2FxmxPn92rhYY8Iletv8uQNSVRv4AxH4d%2F7uDLxfON%2BrMSgOaHcujRefxU4%2FOCVqowowywT9Bq2pc%2BXb5BjpZmuPsOBE0tbJAD5klPdOJr6hs7qjT25Q7XOSGeGOqiEPVvKlpToSGSYSVSgnSuiMLr7ncfPv62vbnzyqPV7n7%2Bx2wvSK42eABcYBatJmpjKBiNkRXIcSGvJrFcjyopqCWFvAWfwuKx1yQXwNwhEDAt%2Fp%2FqN%2Bit%2FAyWUivrd52mph29mvAIszGFl7NsOXuG7rQqboDgc9QQfTBTowMO%2FaQbA%3D&checksum=15355186939506ffa6d3ab8c4c9690a0e764d5a949f9

looking to add this to it

https://www.ebay.com/itm/Xiaomi-Smart-Body-Fat-Scale-2-Bluetooth-Balance-Test-BMI-Health-Weight-Measure/153554200000?_trkparms=ispr%3D1&hash=item23c08b25c0:m:mC4aOnCOSKceJalsMyU1EPw&enc=AQAEAAAB4BPxNw%2BVj6nta7CKEs3N0qWaEWy29gF3aMsotSpZTNyqMhEdTMj%2F47c56ZfXVETWJhNxR4%2BeskTjruOU%2Fhwa%2BJtaAXsOMRYZAv%2FmShlfimiLQaaAESvjb2R85R27qCgi4tzWDdS4gI7mG%2BCwXtCCeT%2Bc8FFBQ4kmiMV0uu3jXK903mPZjV%2FuogMszOsmsqCcvgNtb1Mg2x5rliTXGDFWBnwc9QkyDTpClk6j15k9oJI6LZEDwPbH90kc57SjFSfExybtfpIH4Y8XfpIO3l588DxpyGAEV2%2By1vCeePVpnvorwGux%2BUcC4mqTqV16fq%2FvtEouwBzz9rO%2BpPqF6OXT%2FFnOcgK0%2BTBVUzu68OyE3%2FoiUdxZOx%2FjW12ZbSbQ8rcoJ%2Bws8CIlg5N6bx7yWzzeoZWGYO4cDXG9n7A9krlmgOXCD8eyYpwySWwISu4fA2SP4tYV%2BO8%2BECEFXbqAZgeCj3FyJCP%2F%2F%2B2tlUqD3XS5Jk0GAuCq6UxGfA4Gi2MieJNaA%2B7S4hwGf7J0RjMN1IUnIDGpNBjGq2hK7Sbe%2B4ivH94ajf9vsYLb%2BDK2W6tcaD9H17xjNg0jQy3coXdGfQw8jQAEK0egUC0ODkZE2mQzexYEDWd1PdUITgqW%2BRJei813Vw%3D%3D&checksum=153554200000a9abd30853464a45a6cd79a6dc5955e6

If you are running Hassio you don’t have to run any sudo commands ( at least for me I didn’t ),

Just put the mitemp_bt folder into your custom_components folder in config, and add

sensor:
  - platform: mitemp_bt

to your configuration.yaml and you’re all set.

Restart HA and you will see sensors like mi t_582D3432CFTR

Hope this helps! Huge thanks to @tsymbaliuk :heart:

4 Likes