custom-components / sensor.airthings_wave

hassio support for Airthings Wave BLE environmental radon sensor.
MIT License
100 stars 28 forks source link

All Entities Reporting as "Unavailable" #50

Closed AML225 closed 2 years ago

AML225 commented 3 years ago

I have an Airthings Wave Plus and Wave Mini that were both working fine, reporting values without issue. It appears at some point yesterday all entities for both devices started showing as "Unavailable". After re-installing the integration and rebooting Home Assistant I saw the following in the logs:

2021-03-24 06:32:29 WARNING (MainThread) [homeassistant.components.sensor] Setup of sensor platform airthings_wave is taking over 10 seconds.
2021-03-24 06:32:41 ERROR (SyncWorker_4) [custom_components.airthings_wave.airthings] Disconnected
Traceback (most recent call last):
  File "/config/custom_components/airthings_wave/airthings.py", line 202, in get_info
    char = self._dev.getCharacteristics(uuid=characteristic.uuid)[0]
  File "/usr/local/lib/python3.8/site-packages/bluepy/btle.py", line 508, in getCharacteristics
    rsp = self._getResp('find')
  File "/usr/local/lib/python3.8/site-packages/bluepy/btle.py", line 407, in _getResp
    resp = self._waitResp(wantType + ['ntfy', 'ind'], timeout)
  File "/usr/local/lib/python3.8/site-packages/bluepy/btle.py", line 362, in _waitResp
    raise BTLEDisconnectError("Device disconnected", resp)
bluepy.btle.BTLEDisconnectError: Device disconnected
2021-03-24 06:32:41 ERROR (SyncWorker_4) [custom_components.airthings_wave.sensor] Failed intial setup.
Traceback (most recent call last):
  File "/config/custom_components/airthings_wave/sensor.py", line 186, in setup_platform
    devices_info = airthingsdetect.get_info()
  File "/config/custom_components/airthings_wave/airthings.py", line 202, in get_info
    char = self._dev.getCharacteristics(uuid=characteristic.uuid)[0]
AttributeError: 'NoneType' object has no attribute 'getCharacteristics'

Here is the relevant portion of my sensor.yaml:

  - platform: airthings_wave
    scan_interval: 120
    elevation: 973
sverrham commented 3 years ago

Aaaa I was thinking my last fix to handle this was a bit to simple, I see multiple issues with the code for handling this disconnect issue, need to add better handling of disconnect, should be easy to code up better handling of this issue, but to get it to work for now should be as simple as just restarting.

SkyNet-HASSIO commented 3 years ago

I experienced the same issue, restarting Wave+ and HA did not resolve the issue.

AML225 commented 3 years ago

I have pulled the batteries from all Airthings devices and restarted home assistant without luck.

mindtripper commented 3 years ago

I have pulled the batteries from all Airthings devices and restarted home assistant without luck.

Have you tried restarting the physical device HA is running on? I had similar issues on a host running HA in docker. Reboot of the host solved the issue.

AML225 commented 3 years ago

I appreciate your time trying to help. So far I have tried several power cycles of my HA device, it has now been several hours since the last power cycle, but still no luck. One thing I wanted to try was manually entering the MAC address of the devices in my senors.yaml, for the Wave Mini and Wave Plus would this be the BLE UUID? My devices don't seem to be advertising a typical Bluetooth MAC address, nor is there one printed on them anywhere. If it is the BLE UUID what format should it be entered in?

MartyTremblay commented 3 years ago

@AML225 , are you by any chance using a USB bluetooth dongle?

A shot in the dark but you may want to verify that /dev/ttyACM0 exists on your system.

I don't think that manually configuring the MAC address will help you but you can try this script to find it www.airthings.com/tech/find_wave.py . Inustructions can be found here https://www.airthings.com/resources/raspberry-pi

I'll merge @sverrham 's PR and release it momentarily

edit: I'll do more testing before merging #52

AML225 commented 3 years ago

@AML225 , are you by any chance using a USB bluetooth dongle?

A shot in the dark but you may want to verify that /dev/ttyACM0 exists on your system.

I don't think that manually configuring the MAC address will help you but you can try this script to find it www.airthings.com/tech/find_wave.py . Inustructions can be found here https://www.airthings.com/resources/raspberry-pi

I'll merge @sverrham 's PR and release it momentarily

edit: I'll do more testing before merging #52

That's a good point, I should have included my specific hardware setup. I am running a Raspberry Pi 4B, in an Argon One V2 case. I am using the Pi's onboard Bluetooth, not a USB dongle. I do not have a /dev/ttyACM0, the closest I see listed is /dev/ttyAMA0:

  - name: ttyAMA0
    sysfs: /sys/devices/platform/soc/fe201000.serial/tty/ttyAMA0
    dev_path: /dev/ttyAMA0
    subsystem: tty
    by_id: null
    attributes:
      DEVLINKS: /dev/serial1
      DEVNAME: /dev/ttyAMA0
      DEVPATH: /devices/platform/soc/fe201000.serial/tty/ttyAMA0
      MAJOR: '204'
      MINOR: '64'
      SUBSYSTEM: tty
      TAGS: ':systemd:'
      USEC_INITIALIZED: '2520922'
AML225 commented 3 years ago

So I have rebooted Home Assistant about a thousand times trying to resolve this issue and an MQTT issue I was having. As part of my trial and error I changed my sensor.yaml to be:

  - platform: airthings_wave
    mac: 80:6f:b0:b9:e9:62 #Wave Mini, Bedroom
    scan_interval: 120
    elevation: 973
  - platform: airthings_wave
    mac: 80:6f:b0:59:99:43 #Wave Plus, Living Room
    scan_interval: 120
    elevation: 973

The Wave Plus in my Living Room is now correctly reporting values again. I'm not sure that it was specifying the MAC that fixed it, as I was doing a bunch of other stuff in parallel to troubleshoot my MQTT issue. I do not have access to the Wave Mini at the moment to pull the batteries and see if that will get it reporting to Home Assistant again, will have to try that later. I will also be watching this throughout the day to make sure it continues to report values and doesn't become unavailable again.

AML225 commented 3 years ago

To add another data point (I hope all of this is helpful), I updated airthings.py with the version in Pull Request #52 and rebooted. That caused my Wave Mini to report values, but the Wave Plus did not. I then re-installed the integration in HACs to the latest released version, rebooted, and the Wave Plus reported values, but the Wave Mini did not.

sverrham commented 3 years ago

So this is interesting, thank you for the information. Sounds like this is related to multiple airthings which I have not tested. Would be interesting to see your logs @AML225 when executed with the code in pull request #52 I am wondering if there is an issue with connecting to multiple units in rapid order causes issues, so if my fix with retries fixes the initial face but now the get sensor data fails since it tries only one time and then gives up.

I can add some changes to #52 trying also retries to the get sensor data part.

AML225 commented 3 years ago

So this is interesting, thank you for the information. Sounds like this is related to multiple airthings which I have not tested. Would be interesting to see your logs @AML225 when executed with the code in pull request #52 I am wondering if there is an issue with connecting to multiple units in rapid order causes issues, so if my fix with retries fixes the initial face but now the get sensor data fails since it tries only one time and then gives up.

I can add some changes to #52 trying also retries to the get sensor data part.

So once I got access to the Wave Mini I pulled the batteries and reinserted them. After a few minutes both the Wave Plus and Wave Mini were correctly reporting values, and remained stable all day (to this point). With them both stable, I re-incorporated the changes in Pull Request #52 restarted Home Assistant and both devices are now reporting as unavailable. Relevant logs:

2021-03-25 15:04:24 WARNING (MainThread) [homeassistant.components.sensor] Setup of sensor platform airthings_wave is taking over 10 seconds.
2021-03-25 15:04:24 WARNING (MainThread) [homeassistant.components.sensor] Setup of sensor platform airthings_wave is taking over 10 seconds.
2021-03-25 15:04:27 ERROR (SyncWorker_2) [custom_components.airthings_wave.airthings] Get info disconnected, retry: 0
Traceback (most recent call last):
File "/config/custom_components/airthings_wave/airthings.py", line 205, in get_info
char = self._dev.getCharacteristics(uuid=characteristic.uuid)[0]
File "/usr/local/lib/python3.8/site-packages/bluepy/btle.py", line 508, in getCharacteristics
rsp = self._getResp('find')
File "/usr/local/lib/python3.8/site-packages/bluepy/btle.py", line 407, in _getResp
resp = self._waitResp(wantType + ['ntfy', 'ind'], timeout)
File "/usr/local/lib/python3.8/site-packages/bluepy/btle.py", line 362, in _waitResp
raise BTLEDisconnectError("Device disconnected", resp)
bluepy.btle.BTLEDisconnectError: Device disconnected
2021-03-25 15:04:41 ERROR (SyncWorker_7) [custom_components.airthings_wave.sensor] Failed intial setup.
Traceback (most recent call last):
File "/config/custom_components/airthings_wave/sensor.py", line 202, in setup_platform
ha_entities.append(AirthingsSensor(mac, name, airthingsdetect, devices_info[mac],
KeyError: '80:6f:b0:59:99:43'
2021-03-25 15:04:48 ERROR (SyncWorker_2) [custom_components.airthings_wave.airthings] Get sensor data disconnected, retry: 0.
Traceback (most recent call last):
File "/config/custom_components/airthings_wave/airthings.py", line 249, in get_sensor_data
char = self._dev.getCharacteristics(uuid=characteristic.uuid)[0]
File "/usr/local/lib/python3.8/site-packages/bluepy/btle.py", line 508, in getCharacteristics
rsp = self._getResp('find')
File "/usr/local/lib/python3.8/site-packages/bluepy/btle.py", line 407, in _getResp
resp = self._waitResp(wantType + ['ntfy', 'ind'], timeout)
File "/usr/local/lib/python3.8/site-packages/bluepy/btle.py", line 362, in _waitResp
raise BTLEDisconnectError("Device disconnected", resp)
bluepy.btle.BTLEDisconnectError: Device disconnected
2021-03-25 15:04:53 ERROR (SyncWorker_2) [custom_components.airthings_wave.airthings] Get sensor data disconnected, retry: 1.
Traceback (most recent call last):
File "/config/custom_components/airthings_wave/airthings.py", line 249, in get_sensor_data
char = self._dev.getCharacteristics(uuid=characteristic.uuid)[0]
File "/usr/local/lib/python3.8/site-packages/bluepy/btle.py", line 508, in getCharacteristics
rsp = self._getResp('find')
File "/usr/local/lib/python3.8/site-packages/bluepy/btle.py", line 407, in _getResp
resp = self._waitResp(wantType + ['ntfy', 'ind'], timeout)
File "/usr/local/lib/python3.8/site-packages/bluepy/btle.py", line 362, in _waitResp
raise BTLEDisconnectError("Device disconnected", resp)
bluepy.btle.BTLEDisconnectError: Device disconnected
2021-03-25 15:05:13 ERROR (MainThread) [homeassistant.components.sensor] Setup of platform airthings_wave is taking longer than 60 seconds. Startup will proceed without waiting any longer.
2021-03-25 15:05:13 ERROR (SyncWorker_2) [custom_components.airthings_wave.airthings] Get sensor data disconnected, retry: 7.
Traceback (most recent call last):
File "/config/custom_components/airthings_wave/airthings.py", line 249, in get_sensor_data
char = self._dev.getCharacteristics(uuid=characteristic.uuid)[0]
File "/usr/local/lib/python3.8/site-packages/bluepy/btle.py", line 508, in getCharacteristics
rsp = self._getResp('find')
File "/usr/local/lib/python3.8/site-packages/bluepy/btle.py", line 407, in _getResp
resp = self._waitResp(wantType + ['ntfy', 'ind'], timeout)
File "/usr/local/lib/python3.8/site-packages/bluepy/btle.py", line 362, in _waitResp
raise BTLEDisconnectError("Device disconnected", resp)
bluepy.btle.BTLEDisconnectError: Device disconnected
2021-03-25 15:05:33 ERROR (SyncWorker_2) [custom_components.airthings_wave.airthings] Get sensor data disconnected, retry: 9.
Traceback (most recent call last):
File "/config/custom_components/airthings_wave/airthings.py", line 249, in get_sensor_data
char = self._dev.getCharacteristics(uuid=characteristic.uuid)[0]
File "/usr/local/lib/python3.8/site-packages/bluepy/btle.py", line 508, in getCharacteristics
rsp = self._getResp('find')
File "/usr/local/lib/python3.8/site-packages/bluepy/btle.py", line 407, in _getResp
resp = self._waitResp(wantType + ['ntfy', 'ind'], timeout)
File "/usr/local/lib/python3.8/site-packages/bluepy/btle.py", line 362, in _waitResp
raise BTLEDisconnectError("Device disconnected", resp)
bluepy.btle.BTLEDisconnectError: Device disconnected
2021-03-25 15:05:33 ERROR (SyncWorker_2) [custom_components.airthings_wave.sensor] Failed intial setup.
Traceback (most recent call last):
File "/config/custom_components/airthings_wave/sensor.py", line 202, in setup_platform
ha_entities.append(AirthingsSensor(mac, name, airthingsdetect, devices_info[mac],
KeyError: '80:6f:b0:b9:e9:62'

The next thing I'm going to try is keep the changes in #52 , but revert my sensor.yaml config to not specify the MAC addresses.

AML225 commented 3 years ago

Okay, after rebooting with a sensor.yaml that does not specify MAC addresses and configuration that has #52 incorporated the devices are still unavailable, but the logs are much shorter:

2021-03-25 15:14:42 WARNING (MainThread) [homeassistant.components.sensor] Setup of sensor platform airthings_wave is taking over 10 seconds.
2021-03-25 15:15:10 ERROR (SyncWorker_3) [custom_components.airthings_wave.sensor] Failed intial setup.
Traceback (most recent call last):
File "/config/custom_components/airthings_wave/sensor.py", line 202, in setup_platform
ha_entities.append(AirthingsSensor(mac, name, airthingsdetect, devices_info[mac],
KeyError: '80:6f:b0:59:99:43'
branob commented 3 years ago

I have the same issue. I have the original 1st gen Wave. I can read data using the vendor provided script without any problems

home-assistant:~$ python read_wave.py "f0:f8:f2:4f:5a:3d" DateTime Temperature Humidity Radon 24h avg Radon long term 2021-04-12 19:53:19 20.4 deg C 52.5 % 15.0 Bq/m3 47.0 Bq/m3 2021-04-12 19:53:23 20.4 deg C 52.5 % 15.0 Bq/m3 47.0 Bq/m3

However, the integration gives me unavailable. image

Running the script manually I get: home-assistant:~/hassio/homeassistant/custom_components/airthings_wave# python airthings.py DEBUG:main:Found 1 airthings devices Failed to connect to peripheral f0:f8:f2:4f:5a:3d, addr type: public DEBUG:main:Retrying f0:f8:f2:4f:5a:3d Failed to connect to peripheral f0:f8:f2:4f:5a:3d, addr type: public DEBUG:main:Retrying f0:f8:f2:4f:5a:3d

Christy1984 commented 3 years ago

Hei, Did you find a solution to your issue @AML225? I am having the same issue with one device reporting values and the other won't. Not sure #33 is solved?

FutureGUIs commented 3 years ago

Yep, having the same issue. 2 devices, one sometimes works or both don't.

sverrham commented 3 years ago

I tried asking Airthings if they would be so kind as to lend/give me a couple of their devices so I could improve support for multiple devices but they have not gotten back to me. It is difficult for me to test verify this when I only have one device.

AML225 commented 3 years ago

@Christy1984 @FutureGUIs @sverrham I did not take any steps to fix it. For whatever reason if I repeatedly restart home assistant, eventually everything works. Typically that is only 1 or 2 restarts.

DavidS commented 3 years ago

I'm getting very similar stacktraces:

2021-07-31 23:41:28 ERROR (SyncWorker_2) [custom_components.airthings_wave.airthings] Get sensors disconnected, retry: 1.
Traceback (most recent call last):
File "/config/custom_components/airthings_wave/airthings.py", line 225, in get_sensors
characteristics = self._dev.getCharacteristics()
File "/config/deps/lib/python3.9/site-packages/bluepy/btle.py", line 508, in getCharacteristics
rsp = self._getResp('find')
File "/config/deps/lib/python3.9/site-packages/bluepy/btle.py", line 407, in _getResp
resp = self._waitResp(wantType + ['ntfy', 'ind'], timeout)
File "/config/deps/lib/python3.9/site-packages/bluepy/btle.py", line 362, in _waitResp
raise BTLEDisconnectError("Device disconnected", resp)
bluepy.btle.BTLEDisconnectError: Device disconnected
2021-07-31 23:41:34 ERROR (SyncWorker_2) [custom_components.airthings_wave.sensor] Failed intial setup.
Traceback (most recent call last):
File "/config/custom_components/airthings_wave/sensor.py", line 191, in setup_platform
devices_sensors = airthingsdetect.get_sensors()
File "/config/custom_components/airthings_wave/airthings.py", line 225, in get_sensors
characteristics = self._dev.getCharacteristics()
File "/config/deps/lib/python3.9/site-packages/bluepy/btle.py", line 508, in getCharacteristics
rsp = self._getResp('find')
File "/config/deps/lib/python3.9/site-packages/bluepy/btle.py", line 407, in _getResp
resp = self._waitResp(wantType + ['ntfy', 'ind'], timeout)
File "/config/deps/lib/python3.9/site-packages/bluepy/btle.py", line 361, in _waitResp
self._stopHelper()
File "/config/deps/lib/python3.9/site-packages/bluepy/btle.py", line 293, in _stopHelper
self._helper.stdin.flush()
BrokenPipeError: [Errno 32] Broken pipe

They're sadly quite consistent, even though I can see the device (whose mac I've configured in the hass configuration.yaml) in bluetoothctl:

[bluetooth]# scan on
Discovery started
[CHG] Controller 00:1A:7D:DA:71:13 Discovering: yes
[NEW] Device D8:71:4D:A2:90:6C D8-71-4D-A2-90-6C <<-- this is the device
[bluetooth]# 

One thing I noticed is that bluepy-helper seems to be crashing:

Aug 01 00:41:28 backup kernel: bluepy-helper[25530]: segfault at 9 ip 00007facddfc7324 sp 00007ffce2471050 error 4 in libglib-2.0.so.0.6600.8[7facddfbd000+64000]
Aug 01 00:41:28 backup kernel: Code: 0d 00 e8 54 b7 03 00 48 8d 3d 20 05 0e 00 48 89 c3 e8 49 8f 05 00 e8 c9 93 05 00 48 8d 3d 0c 05 0e 00 49 89 c4 e8 48 8f 05 00 <48> 8b 6b 08 48 85 ed 74 13 4c 89 e6 48 89 ef e8 e8 5e ff ff 85 c0
Aug 01 00:41:34 backup kernel: bluepy-helper[25540]: segfault at 9 ip 00007f40e9853324 sp 00007ffed7205c30 error 4 in libglib-2.0.so.0.6600.8[7f40e9849000+64000]
Aug 01 00:41:34 backup kernel: Code: 0d 00 e8 54 b7 03 00 48 8d 3d 20 05 0e 00 48 89 c3 e8 49 8f 05 00 e8 c9 93 05 00 48 8d 3d 0c 05 0e 00 49 89 c4 e8 48 8f 05 00 <48> 8b 6b 08 48 85 ed 74 13 4c 89 e6 48 89 ef e8 e8 5e ff ff 85 c0

This system is running home assistant container with manually started dbus-daemon and bluetoothd inside a docker container.

B1ob commented 3 years ago

I don’t know I I have the same problem as described in this bug report. However, I always had problems with airthings_wave reporting all entities as unavailable after a reboot of the host. A restart of Home Assistant then fixed this until the next reboot. After digging a bit into the problem I noticed that when setup_platform() calls get_sensor_data() the function does not do much as last_scan is still -1. My fix now also executes the first if statement in get_sensor_data() when last_scan is still -1.

https://github.com/custom-components/sensor.airthings_wave/pull/55

B1ob commented 3 years ago

@Christy1984 @FutureGUIs @sverrham I did not take any steps to fix it. For whatever reason if I repeatedly restart home assistant, eventually everything works. Typically that is only 1 or 2 restarts.

I think my pull request #55 should fix at least this issue as once home assistant is started after the system uptime is bigger than the defined scan_interval it will work also with the current version.

TomTorger commented 3 years ago

Hello all, I've added pull request #57. There was a rather obvious bug in the connect function. I added a simple fix which solved the issue in my case.

bobloadmire commented 2 years ago

I have the same issue. Anyway to update my HA with this fix or do we have to wait for a version update?

TomTorger commented 2 years ago

@MartyTremblay: Any chance you can review and approve pull request #57? 🙂

I believe it solves this issue.