MiFlora no longer working - A story to get it backup and running

Hi All, I thought that I would share some info that I have come across to get MiFlora working again. My story starts one day when I decide that it was time to completely upgrade my Homeassistant, because I could no longer upgrade due to python. My choice of install was Hassbian. Once installed this brought my Homeassistant version to 99.3. Not to bore you with all the install details and get to the topic at hand, I was having trouble with the MiFlora sensors. Sometimes they would work and other times they would not, not like my old installation of Homeassistant which always worked, so I start to do my research.

I learned that performing a ‘sudo hciconfig hci0 down’, followed by a ‘sudo hciconfig hci0 up’ would fix things temporarily. I thought about just running a bash script to automatically do this every hour or so, but I decided not to, in favor of something else. Can you say ESPHome, I can. I had a esp32 just laying around, begging me to use it. So, I setup ESPHome as the MiFlora BLE Sensor:

https://esphome.io/components/sensor/xiaomi_miflora.html

I setup everything, and my sensor was live, but I wasn’t getting any data, why me? After, doing a bunch more research I found that the ESPHome sensor does not work with all version of the MiFlora firmware. I was on version 2.4.2, and when I looked in the MiHome app I saw that I could upgrade to version 3.1.9. Done deal, I hit upgrade and hope for the best. With the upgrade completed, and 5mins wait time I was getting data in the ESPHome sensors, yippy. Another project to put to bed, until I realized that all the sensors were getting data except the battery sensor. I though maybe the MiFlora only sends out battery info every so often, so I waited. Two days later still no battery info, do I leave it or find a solution? Solution time here I come. I thought to myself, hey maybe with the update of the MiFlora maybe the Homeassistant component would work now. I setup the original MiFlora component and waited, and waited and waited. I received no data at all now even after turning the bluetooth off then on again, damn why did I upgrade that MiFlora!

Well, when you think my story is over it is just ready to begin. I performed a ‘sudo hcitool lescan’ to see if my MiFlora would show up, and it would not and sometimes it would seem like something would crash because I could not exit out of the screen. Well back to google. If I performed a ‘sudo systemctl status bluetooth’ I would end up with this:

● bluetooth.service - Bluetooth service
Loaded: loaded (/lib/systemd/system/bluetooth.service; enabled; vendor preset
Active: active (running) since Sun 2019-10-13 17:35:20 BST; 22min ago
Docs: man:bluetoothd(8)
Main PID: 464 (bluetoothd)
Status: “Running”
Tasks: 1 (limit: 2200)
Memory: 2.1M
CGroup: /system.slice/bluetooth.service
└─464 /usr/lib/bluetooth/bluetoothd

Oct 13 17:35:20 hassbian systemd[1]: Starting Bluetooth service…
Oct 13 17:35:20 hassbian bluetoothd[464]: Bluetooth daemon 5.50
Oct 13 17:35:20 hassbian systemd[1]: Started Bluetooth service.
Oct 13 17:35:20 hassbian bluetoothd[464]: Starting SDP server
Oct 13 17:35:20 hassbian bluetoothd[464]: Bluetooth management interface 1.14 in
Oct 13 17:35:20 hassbian bluetoothd[464]: Sap driver initialization failed.
Oct 13 17:35:20 hassbian bluetoothd[464]: sap-server: Operation not permitted (1
Oct 13 17:35:20 hassbian bluetoothd[464]: Failed to set privacy: Rejected (0x0b)

Hmm, Sap driver initialization failed that can’t be good. All this for a damn plant! Ok, how do I fix that, where are we going now, hello Google. Googles reply, " The solution you are looking for is this:"

Open file:
/etc/systemd/system/bluetooth.target.wants/bluetooth.service

And change this:

ExecStart=/usr/lib/bluetooth/bluetoothd

To this:

ExecStart=/usr/lib/bluetooth/bluetoothd --noplugin=sap

Reload the systemd:

sudo systemctl daemon-reload

And, restart bluetooth:

sudo service bluetooth restart

Ok, here is the moment of true, lets run this again ‘sudo systemctl status bluetooth’, boom:

● bluetooth.service - Bluetooth service
Loaded: loaded (/lib/systemd/system/bluetooth.service; enabled; vendor preset: enabled)
Active: active (running) since Sun 2019-10-13 19:14:27 BST; 27s ago
Docs: man:bluetoothd(8)
Main PID: 7934 (bluetoothd)
Status: “Running”
Tasks: 1 (limit: 2200)
Memory: 608.0K
CGroup: /system.slice/bluetooth.service
└─7934 /usr/lib/bluetooth/bluetoothd --noplugin=sap

Oct 13 19:14:27 hassbian systemd[1]: Starting Bluetooth service…
Oct 13 19:14:27 hassbian bluetoothd[7934]: Bluetooth daemon 5.50
Oct 13 19:14:27 hassbian systemd[1]: Started Bluetooth service.
Oct 13 19:14:27 hassbian bluetoothd[7934]: Starting SDP server
Oct 13 19:14:27 hassbian bluetoothd[7934]: Excluding (cli) sap
Oct 13 19:14:27 hassbian bluetoothd[7934]: Bluetooth management interface 1.14 initialized

Hip Hip hurray, so, did this fix my original issue? Lets run ‘sudo hcitool lescan’ and see if it sees my MiFlora. Ta Dahhh, it does, and when I look at the the original MiFlora component in Homeassistant all of the sensors have been populated, hello battery sensor, how I have missed you.

Well, I am happily back up and running, I am not saying this is the end all fix, but for the time being it has me up and running, and I thought I would share and publish this info, all in one place to help others, so Happy Green Thumbing.

1 Like

Something to add to the setup, I found that after a reboot a ‘sudo systemctl status bluetooth’ would get:

● bluetooth.service - Bluetooth service
Loaded: loaded (/lib/systemd/system/bluetooth.service; enabled; vendor preset: enabled)
Active: active (running) since Sun 2019-10-20 16:53:23 BST; 18min ago
Docs: man:bluetoothd(8)
Main PID: 452 (bluetoothd)
Status: “Running”
Tasks: 1 (limit: 2200)
Memory: 2.1M
CGroup: /system.slice/bluetooth.service
└─452 /usr/lib/bluetooth/bluetoothd --noplugin=sap

Oct 20 16:53:23 hassbian systemd[1]: Starting Bluetooth service…
Oct 20 16:53:23 hassbian bluetoothd[452]: Bluetooth daemon 5.50
Oct 20 16:53:23 hassbian systemd[1]: Started Bluetooth service.
Oct 20 16:53:23 hassbian bluetoothd[452]: Starting SDP server
Oct 20 16:53:23 hassbian bluetoothd[452]: Excluding (cli) sap
Oct 20 16:53:23 hassbian bluetoothd[452]: Bluetooth management interface 1.14 initialized
Oct 20 16:53:23 hassbian bluetoothd[452]: Failed to set privacy: Rejected (0x0b)

The “Failed to set privacy: Rejected (0x0b)”, not good. So the fix for now is to restart the service after boot, and this can be done like this:

Edit /etc/crontab

sudo nano /etc/crontab

And add this:

@reboot sleep 300 && sudo systemctl restart bluetooth.service

This should get things backup and running after a reboot.