Passive BLE Monitor integration (Xiaomi Mijia BLE MiBeacon monitor)

@Magalex Dear Aleksey,
In one statement - ALL IS WELL, my dear friend!
Very glad to report that your instructions, clear and complete as they were, did result in both sensors getting registered in HA and working fine (after the usual struggle - total 3 hours; some tears, lots of smiles).
For the benefit of others (and to serve as a reminder to my own-self when I acquire more sensors after this COVID nonsense is over), here are the steps as I followed them…

Hardware:
HA updated and current. Running on Rpi3 (I know, I know - will soon move to a SSD).
2 x LYWSD03MMC sensors.

Steps Taken:

  1. Acquire the mac address of the LYs. Easily done via ssh to HA.
$ ha help
➜  ~ bluetoothctl
Agent registered
[CHG] Controller B8:27:EB:7E:50:F2 Pairable: yes
[bluetooth]# scan on
Discovery started
[CHG] Controller B8:27:EB:7E:50:F2 Discovering: yes
[NEW] Device A4:XX:XX:XX:XX:67 LYWSD03MMC
[NEW] Device A4:XX:XX:XX:XX:4E LYWSD03MMC
[bluetooth]# scan off
Discovery stopped
  1. Copied and pasted this (to copy/paste and for added functionality, used MS Visual Studio terminal) into a blank text file and named the sensors LY1 and LY2 to avoid confusion later.
    Logged off ssh.
    This info may be gathered by many ways; and various other BT apps.

  2. Opend the Stream app on my iPhone.
    Important step - In the app’s settings, choose HTTPS sniffing otherwise the whole procedure will go a waste (I learnt it the hard way). To complete this, the usual VPN trust procedure in Settings>General>About>Profiles needs installation. Make sure this profile is verified and ticked green.
    Now, ensure that MiHome app is logged-in (ensure it is logged-out of other devices and region chosen is mainland China - I faced some grief on account of multiple log-in on other devices) and that the LYs are not added. Remove (delete) if needed. This can be done multiple times without any issues. Force close the app and restart it.

  3. On Stream, hit ‘Sniff now’ and immediately move to the MiHome app and commence the pairing procedure - don’t worry if auto discovery selects the LY. Complete the pairing and get the T and RH data from the LY. I paired only one to start with - the LY1.
    ‘Stop sniffing’ on Stream. This took about 65 seconds in my case. Now, goto the sniff history, Edit, Select all and ‘Export HAR’. I did this export to my NAS.
    Charles Proxy app (30 day trial) was used on a mac to open the said HAR file. Use the find feature and find bind_key
    It will be 32 characters long.

  4. Rinse and repeat for LY2 :slightly_smiling_face:

  5. From here on, things get easier! I had the LY1 and LY2 data ready in VS but not yet pasted in the Configuration.yaml

image
Time for an important Start/Stop procedure (I did struggle here a bit).
a) Stopped HA from here.
image
b) For additional measure, powered off and restarted the pi.
c) Logged-into HA and appended the config.yaml file as shown above.
d) Restarted HA.

  1. Waited patiently for about 15 minutes.
  2. Voila!! 4 new entities magically appeared into the Entities tab! Renamed them - will give a more meaningful name once I have tried them for a few days.

Well…that is all there is to it!

My best wishes and salute to Aleksey and all others who have contributed to this project and indirectly, to my…happiness - life is nothing but a pursuit of it, is it not :smiley:

People, stay safe and stay happy!

4 Likes

Thank you for your kind words and for such a detailed step-by-step instruction! I’ll leave a link to it in our FAQ.

1 Like

@Magalex, thanks for providing us with such a useful plugin. Had to recompile Python 3.8.2 with Bluetooth Sockets though, but after that it worked.

However, as I have LYWSDCGQ devices scattered all over the house, and remote_ha probably isn’t the best option (I’ve got Raspberry Pi Zero Ws around the house), I was wondering what it would take to turn https://github.com/algirdasc/xiaomi-ble-mqtt into the same passive-mode approach ? I’m using it today but it’s doing the active-mode-approach.

I think it will be easier to write own daemon (since periodic launch through cron is not suitable for passive mode - the code should be executed all the time in the background). To do this, I need to figure out how to correctly implement it in python, and figure out how to correctly publish data to MQTT.

You are not the first who asks for this, and there is a corresponding issue in our GitHub repository. The implementation of the component as a standalone is in the plans, but I still can’t allocate time for this…

I followed the steps but I cannot find the bind_key in the HAR file. I also add (until it reads temperature) and remove many times. Can you talk more details about this step?

Usually happens if HTTPS is not enabled. Please share more details.

Fully agree in the daemon approach. https://github.com/ThomDietrich/miflora-mqtt-daemon does something indeed similar.

Let me know if there is anything I can do to help/test - even though my python skills are limited at this time.

I figured out how to get bind_key and got temperature reading in Home Assistant. Thank you so much about your step-by-step instruction!

1 Like

Hello, thanks for the great procedure !

Unfortunately I don’t find any bind_key when I look into the .har file.
I use stream on my Ipad with https sniffing.
Should I import the certificate from the Ipad to my win10 PC (running Charles app).

thanks a lot!!

No, a certificate should be trusted only where you sniff traffic, that is, on the iPad. This does not need to be done on a PC.

It can be frustrating…(took me a few hours to nail it). If HTTPS is there, so is the bind key…assuming the MiHope pairing went well.
I suggest you try again. Get a few minutes worth of HAR file and export them ALL…there are more than plenty there. Wish I could find a ‘select all’ option.

Ok thanks for your extremely fast answer !
Ok that’s what I did.

Thanks also for you quick answer.

Ok I will try again. (I tried 4-5 times already…)

Yes the pairing went well on the Mi home.

Any suggestion for the HAR viewer to use ? Is there a better alternative than Charles ?

@Magalex Further update…
All working well. I have read on numerous forums that the RPI3 has a lousy BT adapter - well, I beg to differ.
I live in a split level house of conrete construction.
LY2 is ca 8 meters away; same level but behind 2 brick walls.
LY1 is ca 10m away on the lower level (1 conrete slab) plus 2 brick walls.
I have yet to see a missed signal or ‘Unvailable’ on HA (that only happens after HA restart until the first polling interval).
The passive component is a very clever idea and in hindsight, the frustration of the design change on account of the LY, in my opinion, has actually led to a better and more ‘green’ solution.
God bless you, droog! :smiley:

PS: Ordered 4 more LYs.

1 Like

I think that the problems with bluetooth on raspberry pi in most cases are connected with poor power supply (in terms of the power ability and its “purity”) and not very good design in terms of electromagnetic compatibility (adding devices like SSDs greatly spoils the picture). So much depends on the individual characteristics of each installation. Your result pleases, yes.

First and important, you MUST install HACS to be able to read LYWSD03MMC sensor because this intergration is different than the original intergration.

At first, I have a problem finding bind_key, too. Therefore, I wrote another instruction in iOS. This instruction is for LYWSD03MMC sensor. I don’t know the other sensors are different or not.

  1. Open Stream -> HTTPS Sniffing -> CA Install. Then you will be directed to browser to download VPN profile.

  2. Go to General -> Profile -> Stream Generated CA… -> Install the VPN profile

  3. After installing VPN profile.
    Open Stream -> HTTPS Sniffing -> hit trust CA in settings

  4. General -> About -> Certificate Trust Settings -> tick green on Stream Generated CA…

  5. Logged in to MiHome and open Stream.
    Then hit “Sniff now”.
    Immediately go to MiHome and add LYWSD03MMC sensor.
    Waiting for the app to read Temperature and Humidity.
    Then close MiHome app. Go back to Stream and click “Stop Sniffing “

  6. Now go to sniff history and search for keyword “bltbind”. Will see file

“POST https://api.io.mi.com/app/device/bltbind”

If you don’t see that file, go back to step 3 to check whether the CA was trusted or not.
You can click Edit -> Export HAR

Then use Charles Proxy on laptop to see HAR file.
Below is other way to find bind_key inside Stream app (if you don’t want to install more software to laptop).

  1. Click on that file -> Request -> “Preview Form” at bottom -> Format -> Decode -> find “bind_key”

  2. After finding bind_key, then add it to Configuration.yaml

  3. Stop HA in Server management. Then, unplug and plug back power source. HA will be start working in about 10 minutes

  4. Wait about 10 more minutes, you will see the sensor entities in Entities or States tab in HA.
    Batteries level sensor will be showed up after an hour

3 Likes

I am running home assistant on a Synology without Bluetooth. I got an Raspberry Pi and some ESP32 modules. Is it somehow possible to use this component also with the Bluetooth of my PI or ESP32? How to realize the connection between home assistant and the Bluetooth receiver?

Hi, I am using LYWSD03MMC sensors with encryptors as described above.
After adding 5th sensor my RPI3 HASSIO started to crash (no web gui/but can access files remotely).
Removing any of the 5 devices solves the problem.

Has anyone faced similar situation? Any suggestions?

@fitim our FAQ has a mention of methods appropriate to your situation. But the simplest thing in your case would be to use an external USB-BT dongle.

ESP32 has its own implementation of passive data reception from BT sensors, but there the list of supported sensors is somewhat smaller.

@dimaf Do you have the opportunity to see the HA log during the occurrence of a problem?