MiTemperature2 icon indicating copy to clipboard operation
MiTemperature2 copied to clipboard

BTHome v2 decoding bug?

Open mreymann opened this issue 2 months ago • 4 comments

First of all thank you for this wonderful piece of software! Made my life so much easier! One litte thing, though:

pvvx firmware plans to switch to BTHome v2 messages only. I'm using MiTemperature v6.0 and changed one of my thermometers to emit BHome v2 messages instead of the custom pvvx format. Now the value for "Battery" shows 1 % instead of 100 % (this is a fresh battery):

BLE packet - BTHome : A4:C1:38:3E:B3:93 00 120201060e16d2fc40001b016402c808036c1a -54 Packet type: Data Sensorname: new3 Temperature: 22.48 Humidity: 67.64 Battery voltage: 3.014 V RSSI: -54 dBm Battery: 1 %

Here's what ChatGPT says:


Nice — here’s the decoded BTHome v2 advertisement.

Raw hex you gave (grouped): 12 02 01 06 0e 16 d2 fc 40 00 1b 01 64 02 c8 08 03 6c 1a

Breakdown

02 01 06 — BLE Flags AD element (standard 0x020106).

0e 16 d2 fc — Service Data (UUID 0xFCD2 = BTHome) + payload. BTHome

BTHome payload (after UUID): 40 00 1b 01 64 02 c8 08 03 6c 1a

0x40 — device info byte: BTHome v2, no encryption, regular updates. BTHome

00 1b — object id 0x00 = packet id (uint8). Value 0x1B → 27. BTHome

Measurements (object id → interpretation per BTHome v2 spec)

01 64

0x01 = battery (uint8, factor 1). 0x64 = 100%. BTHome

02 c8 08

0x02 = temperature (sint16, little-endian, factor 0.01). Bytes C8 08 → value 0x08C8 = 2248 → 2248 × 0.01 = 22.48 °C. BTHome

03 6c 1a

0x03 = humidity (uint16, little-endian, factor 0.01). Bytes 6C 1A → value 0x1A6C = 6764 → 6764 × 0.01 = 67.64 %. BTHome

Human-readable summary

Packet id: 27

Battery: 100 %

Temperature: 22.48 °C

Relative Humidity: 67.64 %


Is there a bug in the decoding of BTHome v2 messages?

mreymann avatar Oct 21 '25 07:10 mreymann

Thank you very much for your feedbak.

Thanks for the Info. Just as a note: Only BTHome v2 would make the reception of devices with low signal worse. With low signals, some messages get lost. BTHomw v2 transmits data in 2 different packets, see https://github.com/JsBergbau/MiTemperature2?tab=readme-ov-file#supported-sensors

You are right, there was indeed a bug in decoding the battery values. I didn't notice since I was using a quite drained battery and 1 % seemed plausible ;)

I've updated to version 6.1.

Please test. It looks good in my tests.

JsBergbau avatar Oct 21 '25 12:10 JsBergbau

Hello, I've compared version 6.0 and version 6.1. v6.0 seems to decode everything OK except for the battery value:

[16:21:45][root@zero2:~/src/MiTemperature2]$ ./MiTemperature2.py --devicelistfile=/root/src/MiTemperature2/sensors.ini --onlydevicelist --battery --rssi
---------------------------------------------
MiTemperature2 / ATC Thermometer version 6.0
---------------------------------------------


Please read README.md in this folder. Latest version is available at https://github.com/JsBergbau/MiTemperature2#readme
This file explains very detailed about the usage and covers everything you need to know as user.


Script started
------------------------------
All devices within reach are read out, unless a devicelistfile and --onlydevicelist is specified.
In this mode debouncing is not available. Rounding option will round humidity and temperature to one decimal place.
Passive mode usually requires root rights. If you want to use it with normal user rights,
please execute "sudo setcap cap_net_raw,cap_net_admin+eip $(eval readlink -f `which python3`)"
You have to redo this step if you upgrade your python version.
----------------------------
Power ON bluetooth device 0
Bluetooth device 0 is already enabled
Enable LE scan
scan params: interval=1280.000ms window=1280.000ms own_bdaddr=public whitelist=no
socket filter set to ptype=HCI_EVENT_PKT event=LE_META_EVENT
Listening ...
BLE packet - BTHome : A4:C1:38:3E:B3:93 00 120201060e16d2fc400030016402240903421c -68
Packet type: Data
Sensorname: new3
Temperature:  23.4
Humidity:  72.34
Battery voltage: 0 V
RSSI: -68 dBm
Battery: 0 %

BLE packet - BTHome : A4:C1:38:3E:B3:93 00 110201060d16d2fc4000300cca0b10001101 -68
Packet type: Battery
Sensorname: new3
Temperature:  23.4
Humidity:  72.34
Battery voltage: 3.018 V
RSSI: -68 dBm
Battery: 1 %

^CDisable LE scan

v6.1 seems to only decode the battery value, but doesn't show any "Packet type: Data" lines. Temperature and humidity values are always "0":

[16:22:02][root@zero2:~/src/MiTemperature2]$ ./MiTemperature2_6.1.py --devicelistfile=/root/src/MiTemperature2/sensors.ini --onlydevicelist --battery --rssi
---------------------------------------------
MiTemperature2 / ATC Thermometer version 6.1
---------------------------------------------


Please read README.md in this folder. Latest version is available at https://github.com/JsBergbau/MiTemperature2#readme
This file explains very detailed about the usage and covers everything you need to know as user.


Script started
------------------------------
All devices within reach are read out, unless a devicelistfile and --onlydevicelist is specified.
In this mode debouncing is not available. Rounding option will round humidity and temperature to one decimal place.
Passive mode usually requires root rights. If you want to use it with normal user rights,
please execute "sudo setcap cap_net_raw,cap_net_admin+eip $(eval readlink -f `which python3`)"
You have to redo this step if you upgrade your python version.
----------------------------
Power ON bluetooth device 0
Bluetooth device 0 is already enabled
Enable LE scan
scan params: interval=1280.000ms window=1280.000ms own_bdaddr=public whitelist=no
socket filter set to ptype=HCI_EVENT_PKT event=LE_META_EVENT
Listening ...
BLE packet - BTHome : A4:C1:38:3E:B3:93 00 110201060d16d2fc40003f0cca0b10001101 -66
Sensorname: new3
Temperature:  0
Humidity:  0
Battery voltage: 0 V
RSSI: -66 dBm
Battery: 0 %

BLE packet - BTHome : A4:C1:38:3E:B3:93 00 120201060e16d2fc40003f0164022b09034d1c -69
Packet type: Battery
Sensorname: new3
Temperature:  0
Humidity:  0
Battery voltage: 0 V
RSSI: -69 dBm
Battery: 100 %

BLE packet - BTHome : A4:C1:38:3E:B3:93 00 110201060d16d2fc4000400cca0b10001101 -68
Sensorname: new3
Temperature:  0
Humidity:  0
Battery voltage: 0 V
RSSI: -68 dBm
Battery: 100 %

^CDisable LE scan

I'm quite happy with the pvvx format, but on their site they say:

Starting with version 6.0, support for non-standard and unregistered in Bluetooth SIG UUIDs and BLE advertising formats will be discontinued. Only BTHome ver 2 will be supported!

https://github.com/pvvx/ATC_MiThermometer?tab=readme-ov-file#firmware-version-history

mreymann avatar Oct 22 '25 14:10 mreymann

I somehow managed to get this running. Please review my changes: https://github.com/JsBergbau/MiTemperature2/pull/155

mreymann avatar Oct 24 '25 12:10 mreymann

Thanks you are right. Was in a hurry in didn't even notice, that now battery value worked and the rest not. Thanks for your correction. Works now.

JsBergbau avatar Oct 26 '25 17:10 JsBergbau