Bluetooth failing so HA cannot get switchbot sensor values

Periodically my Bluetooth SwitchBot hygrometers go offline. HA reports ‘unavailable’. I have to restart the PI to get it working.

I run HA in a docker container.

After many days of trying, I finally found the error in the homeassistant log file:

2023-10-29 03:54:38.070 INFO (MainThread) [homeassistant.components.bluetooth.scanner] hci0 (B8:27:EB:A1:05:AC): Bluetooth scanner has gone quiet for 90s, restarting
2023-10-29 03:54:40.409 WARNING (MainThread) [bluetooth_auto_recovery.recover] Getting Bluetooth adapter failed hci0 [B8:27:EB:A1:05:AC] because the system cannot create a bluetooth socket: Unable to open PF_BLUETOOTH socket
2023-10-29 03:54:40.410 WARNING (MainThread) [bluetooth_auto_recovery.recover] Could not find adapter with mac address B8:27:EB:A1:05:AC or hci0
2023-10-29 03:54:40.411 WARNING (MainThread) [bluetooth_auto_recovery.recover] Closing Bluetooth adapter hci0 [B8:27:EB:A1:05:AC] failed: 'NoneType' object has no attribute 'detach'
2023-10-29 03:54:44.443 ERROR (MainThread) [homeassistant.components.bluetooth.scanner] hci0 (B8:27:EB:A1:05:AC): Failed to restart Bluetooth scanner: hci0 (B8:27:EB:A1:05:AC): Failed to start Bluetooth: [org.bluez.Error.InProgress] Operation already in progress
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/components/bluetooth/scanner.py", line 223, in _async_start
    await self.scanner.start()  # type: ignore[no-untyped-call]
    ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/bleak/__init__.py", line 198, in start
    await self._backend.start()
  File "/usr/local/lib/python3.11/site-packages/bleak/backends/bluezdbus/scanner.py", line 185, in start
    self._stop = await manager.active_scan(
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/bleak/backends/bluezdbus/manager.py", line 416, in active_scan
    assert_reply(reply)
  File "/usr/local/lib/python3.11/site-packages/bleak/backends/bluezdbus/utils.py", line 22, in assert_reply
    raise BleakDBusError(reply.error_name, reply.body)
bleak.exc.BleakDBusError: [org.bluez.Error.InProgress] Operation already in progress

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/components/bluetooth/scanner.py", line 353, in _async_restart_scanner
    await self._async_start()
  File "/usr/src/homeassistant/homeassistant/components/bluetooth/scanner.py", line 305, in _async_start
    raise ScannerStartError(
homeassistant.components.bluetooth.scanner.ScannerStartError: hci0 (B8:27:EB:A1:05:AC): Failed to start Bluetooth: [org.bluez.Error.InProgress] Operation already in progress
2023-10-29 03:55:08.072 INFO (MainThread) [homeassistant.components.bluetooth.scanner] hci0 (B8:27:EB:A1:05:AC): Bluetooth scanner has gone quiet for 90s, restarting
2023-10-29 03:55:08.075 WARNING (MainThread) [bluetooth_auto_recovery.recover] Getting Bluetooth adapter failed hci0 [B8:27:EB:A1:05:AC] because the system cannot create a bluetooth socket: Unable to open PF_BLUETOOTH socket
2023-10-29 03:55:08.077 WARNING (MainThread) [bluetooth_auto_recovery.recover] Could not find adapter with mac address B8:27:EB:A1:05:AC or hci0
2023-10-29 03:55:08.077 WARNING (MainThread) [bluetooth_auto_recovery.recover] Closing Bluetooth adapter hci0 [B8:27:EB:A1:05:AC] failed: 'NoneType' object has no attribute 'detach'
2023-10-29 03:55:10.423 WARNING (MainThread) [bluetooth_auto_recovery.recover] Getting Bluetooth adapter failed hci0 [B8:27:EB:A1:05:AC] because the system cannot create a bluetooth socket: Unable to open PF_BLUETOOTH socket
2023-10-29 03:55:10.423 WARNING (MainThread) [bluetooth_auto_recovery.recover] Could not find adapter with mac address B8:27:EB:A1:05:AC or hci0
2023-10-29 03:55:10.423 WARNING (MainThread) [bluetooth_auto_recovery.recover] Closing Bluetooth adapter hci0 [B8:27:EB:A1:05:AC] failed: 'NoneType' object has no attribute 'detach'
2023-10-29 03:55:10.434 ERROR (MainThread) [homeassistant.components.bluetooth.scanner] hci0 (B8:27:EB:A1:05:AC): Failed to restart Bluetooth scanner: hci0 (B8:27:EB:A1:05:AC): Failed to start Bluetooth: [org.bluez.Error.InProgress] Operation already in progress
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/components/bluetooth/scanner.py", line 223, in _async_start
    await self.scanner.start()  # type: ignore[no-untyped-call]
    ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/bleak/__init__.py", line 198, in start
    await self._backend.start()
  File "/usr/local/lib/python3.11/site-packages/bleak/backends/bluezdbus/scanner.py", line 185, in start
    self._stop = await manager.active_scan(
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/bleak/backends/bluezdbus/manager.py", line 416, in active_scan
    assert_reply(reply)
  File "/usr/local/lib/python3.11/site-packages/bleak/backends/bluezdbus/utils.py", line 22, in assert_reply
    raise BleakDBusError(reply.error_name, reply.body)


...
repeats
....

Does anyone know how to decode the crypic log messages and point me in the right direction please.

I note this over on GitHub but I can’t really understand it or determine what the fix is…

… and I concur, all my bluetooth worked perfectly with same hardware about 3 years ago. Is this a HA issue? I moved house and only unpacked the pi and setup from clean using latest HA - about a month ago so I have not used HA for about 3 years until now, All this used to work - same B T controller, same I, same SwitcvhBots

PROBLEM FIXED for anyone visiting here. Raspbian LITE seems to have this as a common problem. I think it’s related to dbus. read on…

summary of problem
bluetooth working sometimes in HA docker but rarely on the host.
intermittent functionality.
random failure , more so on the host.

issues found:

  1. Bluetooth stack installed in the container AND the host. Although setup was different and not the same packages.

  2. In the container dbus-broker was running. See here: Bluetooth - Home Assistant

  3. On the host no dbus whatsoever (likely this is as expected given that it’s a headless system whereas DisplayBUS is for desktops?)

Fix
Before doing anything, check your /boot/config.txt to make sure bluetooth is not disabled - overlays for bt-disable or bluetooth-disable must be commented out.

  1. Remove all bluetooth stacks from containers and the host. Just run it on the host (I did try a bluetooth container - several - but couldn’t get bt to work on any). So in BOTH…
# apt-remove \
bluez \
bluez-audio \
bluez-firmware \
bluez-input \
bluez-network \
bluez-serial \
bluez-utils \
libbluetooth3:arm64 \
pi-bluetooth \
dbus \
dbus-bin \
dbus-broker \
dbus-daemon \
dbus-session-bus \
dbus-session-bus-common \
dbus-system-bus \
dbus-system-bus-common
dbus-user-session \
default-dbus-session-bus \
default-dbus-system-bus \
libdbus-1-3:arm64 \
python-dbusmock \
python3-dbusmock
  1. reboot - important!

  2. Then on the host install the essentials only. This is all you need to get it working. Note dbus-broker which is probably why everyone on raspbian lite is having a nightmare with bt

#apt install \
pi-bluetooth \
dbus-broker
  1. reboot - important

Enjoy!