custom-components / ble_monitor

BLE monitor for passive BLE sensors
https://community.home-assistant.io/t/passive-ble-monitor-integration/
MIT License
1.92k stars 248 forks source link

[Bug]: Sensirion SHT40 Smart Gadget - High Humidity Value #1051

Closed amagr0 closed 2 years ago

amagr0 commented 2 years ago

What happened?

I have the SHT40 Smart Gadget already in HA through Bluetooth Low Energy Monitor Integration but the Humidity data that I'm getting in HA is relatively low compared to the Gadget values, aprox. 10% of Humidity lower.

Ex.: -------------------------- Temp | Humidity SHT4X Smart Gadget -- 22.09C | 71.18% HA BLE ----------------- 22.09C | 61.71%

SHT40 Gadget Firmware: 3.2 (up to date)

Passive BLE Monitor integration 10.6.0

Home Assistant 2022.11.2 Supervisor 2022.10.2 Operating System 9.3

Sensor type

Sensirion SHT40 Smart Gadget

Relevant log output

2022-11-16 19:01:02.556 DEBUG (MainThread) [custom_components.ble_monitor.bt_helpers] Response(header=<event_code=CommandCompleteEvent, controller_idx=0, param_len=283>, event_frame=<command_opcode=ReadControllerInformation, status=Success>, cmd_response_frame=<address=DC:A6:32:BE:FE:34, bluetooth_version=9, manufacturer=305, supported_settings=114431, current_settings={Powered: True, Connectable: False, FastConnectable: False, Discoverable: False, Bondable: False, LinkLevelSecurity: False, SecureSimplePairing: True, BREDR: True, HighSpeed: False, LowEnergy: True, Advertising: False, SecureConnections: True, DebugKeys: False, Privacy: False, ControllerConfiguration: False, StaticAddress: False, PHYConfiguration: False, WidebandSpeech: False}, class_of_device=2883584, name=b'homeassistant', short_name=0>)

2022-11-16 19:01:02.742 DEBUG (MainThread) [custom_components.ble_monitor] Initializing BLE Monitor entry (config entry): <homeassistant.config_entries.ConfigEntry object at 0x7f919636b0>

2022-11-16 19:01:02.750 DEBUG (MainThread) [custom_components.ble_monitor] async_setup_entry: domain {}

2022-11-16 19:01:02.750 DEBUG (MainThread) [custom_components.ble_monitor] HCI interface is [0]

2022-11-16 19:01:02.750 DEBUG (MainThread) [custom_components.ble_monitor] async_setup_entry: {'bt_interface': ['DC:A6:32:BE:FE:34'], 'bt_auto_restart': False, 'active_scan': False, 'discovery': True, 'period': 60, 'use_median': False, 'decimals': 2, 'log_spikes': False, 'restore_state': False, 'report_unknown': 'Off', 'devices': [{'mac': 'F5:2C:18:48:00:01', 'name': 'F52C18480001', 'temperature_unit': '°C', 'decimals': 2, 'use_median': 'default', 'restore_state': 'default', 'reset_timer': 35, 'report_unknown': False, 'track_device': False, 'tracker_scan_interval': 20, 'consider_home': 180, 'delete device': False, 'uuid': ''}], 'is_flow': True, 'hci_interface': [0]}

2022-11-16 19:01:02.751 DEBUG (MainThread) [custom_components.ble_monitor] Spawning HCIdump thread

2022-11-16 19:01:02.751 DEBUG (MainThread) [custom_components.ble_monitor] HCIdump thread: Init

2022-11-16 19:01:02.751 DEBUG (MainThread) [custom_components.ble_monitor] 0 encryptors mac:key pairs loaded

2022-11-16 19:01:02.751 DEBUG (MainThread) [custom_components.ble_monitor] sensor whitelist: []

2022-11-16 19:01:02.752 DEBUG (MainThread) [custom_components.ble_monitor] 0 sensor whitelist item(s) loaded

2022-11-16 19:01:02.752 DEBUG (MainThread) [custom_components.ble_monitor] 0 device tracker(s) being monitored

2022-11-16 19:01:02.754 DEBUG (Thread-3) [custom_components.ble_monitor] HCIdump thread: Run

2022-11-16 19:01:02.772 DEBUG (Thread-3) [custom_components.ble_monitor] HCIdump thread: BLEScanRequester._initialized is True for hci0, waiting for connection...

2022-11-16 19:01:02.773 DEBUG (Thread-3) [custom_components.ble_monitor] HCIdump thread: connected to hci0

2022-11-16 19:01:02.775 DEBUG (Thread-3) [custom_components.ble_monitor] HCIdump thread: BLEScanRequester._initialized is True for hci0, connection established, send_scan_request succeeded.

2022-11-16 19:01:02.775 DEBUG (Thread-3) [custom_components.ble_monitor] HCIdump thread: start main event_loop

2022-11-16 19:01:02.875 DEBUG (MainThread) [custom_components.ble_monitor.device_tracker] Starting device tracker entry startup

2022-11-16 19:01:02.875 DEBUG (MainThread) [custom_components.ble_monitor.device_tracker] BLE device tracker updater initialization

2022-11-16 19:01:02.875 DEBUG (MainThread) [custom_components.ble_monitor.device_tracker] BLE device tracker updater initialized

2022-11-16 19:01:02.876 DEBUG (MainThread) [custom_components.ble_monitor.device_tracker] Device Tracker entry setup finished

2022-11-16 19:01:02.876 DEBUG (MainThread) [custom_components.ble_monitor.binary_sensor] Starting binary sensor entry startup

2022-11-16 19:01:02.876 DEBUG (MainThread) [custom_components.ble_monitor.binary_sensor] BLE binary sensors updater initialization

2022-11-16 19:01:02.876 DEBUG (MainThread) [custom_components.ble_monitor.binary_sensor] BLE binary sensors updater initialized

2022-11-16 19:01:02.876 DEBUG (MainThread) [custom_components.ble_monitor.binary_sensor] Binary sensor entry setup finished

2022-11-16 19:01:02.876 DEBUG (MainThread) [custom_components.ble_monitor.sensor] Starting measuring sensor entry startup

2022-11-16 19:01:02.876 DEBUG (MainThread) [custom_components.ble_monitor.sensor] BLE sensors updater initialization

2022-11-16 19:01:02.876 DEBUG (MainThread) [custom_components.ble_monitor.sensor] BLE sensors updater initialized

2022-11-16 19:01:02.877 DEBUG (MainThread) [custom_components.ble_monitor.sensor] Measuring sensor entry setup finished

2022-11-16 19:01:02.904 DEBUG (MainThread) [custom_components.ble_monitor.device_tracker] Device tracker updater loop started!

2022-11-16 19:01:02.905 DEBUG (MainThread) [custom_components.ble_monitor.binary_sensor] Binary entities updater loop started!

2022-11-16 19:01:02.905 DEBUG (MainThread) [custom_components.ble_monitor.sensor] Entities updater loop started!

2022-11-16 19:01:02.957 DEBUG (MainThread) [custom_components.ble_monitor.sensor] Sensor device with mac_address F5:2C:18:48:00:01 has the following settings. Name: F52C18480001. Temperature unit: °C. Decimals: 2. Use Median: False. Restore state: False. Reset Timer: 35

2022-11-16 19:01:02.957 DEBUG (MainThread) [custom_components.ble_monitor.sensor] Sensor device with mac_address F5:2C:18:48:00:01 has the following settings. Name: F52C18480001. Temperature unit: °C. Decimals: 2. Use Median: False. Restore state: False. Reset Timer: 35

2022-11-16 19:01:02.958 DEBUG (MainThread) [custom_components.ble_monitor.sensor] Sensor device with mac_address F5:2C:18:48:00:01 has the following settings. Name: F52C18480001. Temperature unit: °C. Decimals: 2. Use Median: False. Restore state: False. Reset Timer: 35

2022-11-16 19:01:03.032 DEBUG (MainThread) [custom_components.ble_monitor.sensor] Data measuring sensor received: {'firmware': 'Sensirion', 'temperature': 22.06, 'humidity': 61.58, 'rssi': -60, 'mac': 'F52C18480001', 'type': 'SHT40 Gadget', 'packet': 'no packet id', 'data': True}

2022-11-16 19:01:03.074 DEBUG (MainThread) [custom_components.ble_monitor.sensor] async_added_to_hass called for ble temperature F52C18480001

2022-11-16 19:01:03.075 DEBUG (MainThread) [custom_components.ble_monitor.sensor] async_added_to_hass called for ble humidity F52C18480001

2022-11-16 19:01:03.076 DEBUG (MainThread) [custom_components.ble_monitor.sensor] async_added_to_hass called for ble rssi F52C18480001

---

2022-11-16 19:02:13.151 DEBUG (MainThread) [custom_components.ble_monitor.sensor] Data measuring sensor received: {'firmware': 'Sensirion', 'temperature': 22.06, 'humidity': 61.66, 'rssi': -61, 'mac': 'F52C18480001', 'type': 'SHT40 Gadget', 'packet': 'no packet id', 'data': True}
Ernst79 commented 2 years ago

I think I have an idea where the issue is.

If you look into the code of sensirion.py, you can see that there are two types of humidity conversion. Only V1 is implemented.

def _decodeHumidityV1(byte_data):
    # GadgetBle::_convertHumidityV1 - return static_cast<uint16_t>(((value / 100) * 65535) + 0.5f);
    return round((int.from_bytes(byte_data, byteorder='little') / 65535) * 100, 2)

def _decodeHumidityV2(byte_data):
    # GadgetBle::_convertHumidityV2 - return static_cast<uint16_t>((((value + 6.0) * 65535) / 125.0) + 0.5f);
    return

So, lets assume the sensor is using V2 in stead of V1. The sensor will than send the following value

((71.18 + 6.0) * 65535) / 125 = 40464 (9E10 in bytes)

So, BLE monitor receives 40464, but assumes it is V1.

40464 / 65535 * 100 = 61.74%

So, what needs to be done is that we use V2 decoding for humidity. I don't know if if there is a Version flag in the data, will have a look.

Ernst79 commented 2 years ago

ah, it looks like SHT4xx always uses V2, so we should use V2 in stead of V1. I will fix this.

amagr0 commented 2 years ago

Brilliant! Thanks @Ernst79

Ernst79 commented 2 years ago

Should be fixed in 10.6.1

amagr0 commented 2 years ago

Should be fixed in 10.6.1

Fixed!