custom-components / ble_monitor

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

Can you add support for bluemaestro tempo disc? #530

Closed tempesta-home closed 2 years ago

tempesta-home commented 2 years ago

Please, I think it should be quite similar to what you've already done.

Here some link of interest: https://jenswilly.dk/2019/06/blue-maestro-sensor-openhab-and-homekit/ https://community.home-assistant.io/t/support-for-tempo-disc-bluetooth-temperature-humidity-and-dew-point-monitor-and-logger/169145

Ernst79 commented 2 years ago

Yes, this seems to be possible. I found this info about the BLE format.

https://usermanual.wiki/Document/TemperatureHumidityDataLoggerCommandsAPI24.2837071165/view

I will look into it a.s.a.p.

image

Ernst79 commented 2 years ago

I've created a 5.5.0-beta release which adds support for the Tempo Disc THPD and THD sensor. Please give it a try and let me know if it works, as I had to do some assumptions on the BLE advertisement format.

tempesta-home commented 2 years ago

Hi, first of all thanks for your quick answer. I tried the beta version: something works and something not. What it works:

What it doesn't work:

2021-10-14 08:09:53 ERROR (Thread-4) [custom_components.ble_monitor] HCIdump thread: Runtime error while sending scan request on hci1: Event loop stopped before Future completed. 2021-10-14 08:10:23 ERROR (MainThread) [custom_components.ble_monitor] Waiting for the HCIdump thread to finish took too long! (>10s)

If it can helps:

[NEW] Device DB:46:8F:02:9B:D9 DB468F02 [CHG] Device DB:46:8F:02:9B:D9 ManufacturerData Key: 0x0133 [CHG] Device DB:46:8F:02:9B:D9 ManufacturerData Value: 00 c5 03 89 fe b3 01 76 ff 3c 02 b5 ff 25 02 07 .......v.<...%.. ff 2e 02 4e 00 7d a7 11 4d ...N.}..M

If it can helps, when I tried to play with this sensor without success, I was arrived here:

            for i in range(0, num_reports):
                company = returnstringpacket( pkt[report_pkt_offset + 15: report_pkt_offset + 17] )

                if (DEBUG == True):
                    _LOGGER.warn("fullpacket: " + returnstringpacket(pkt))
                    _LOGGER.warn("Company: " + company)

                if (company == "3301"):
                    sensor = {}
        #           print "\tCompany: ",company
                    udid = returnstringpacket(pkt[report_pkt_offset + 22: report_pkt_offset - 6])
        #           print "\tUDID: ", udid
                    sensor["udid"] = udid

        #           print "\tMAJOR: ", printpacket(pkt[report_pkt_offset -6: report_pkt_offset - 4])
        #           print "\tMINOR: ", printpacket(pkt[report_pkt_offset -4: report_pkt_offset - 2])
        #           print "\tMAC address: ", packed_bdaddr_to_string(pkt[report_pkt_offset + 3:report_pkt_offset + 9])
                    mac = returnstringpacket(pkt[report_pkt_offset + 3: report_pkt_offset + 9])
                    sensor["macaddress"] = mac
        #           print "\tMAC Address string: ", returnstringpacket(pkt[report_pkt_offset + 3:report_pkt_offset + 9])
                    tempString = returnstringpacket(pkt[report_pkt_offset + 23: report_pkt_offset + 25])
        #           print "\tTemp: " , tempString 
                    temp = float(returnnumberpacket(pkt[report_pkt_offset + 23:report_pkt_offset + 25]))/10
                    if (temp > 100):
                        temp = temp - 6553.6
        #           print "\tTemp: " , temp
                    sensor["temperature"] = temp

        #           print "\tHumidity: " ,printpacket(pkt[report_pkt_offset + 25:report_pkt_offset + 27])
                    humidity = float(returnnumberpacket(pkt[report_pkt_offset + 25:report_pkt_offset + 27]))/10
        #           print "\tHumidity: " ,humidity 
                    sensor["humidity"] = humidity 

                    dewpoint = float(returnnumberpacket(pkt[report_pkt_offset + 27:report_pkt_offset + 29]))/10
                    if (dewpoint > 100):
                        dewpoint = dewpoint - 6553.6
        #           print "\tDewpoint: " ,dewpoint 
                    sensor["dewpoint"] = dewpoint

                    nameLength = int(returnstringpacket(pkt[report_pkt_offset + 32:report_pkt_offset + 33]))
        #           print "\tNameLength: ",nameLength

                    name = returnstringpacket(pkt[report_pkt_offset + 33:report_pkt_offset + (33+nameLength-1)])
        #           print "\tName: %s %d " % (name.decode("hex"),nameLength)
                    sensor["name"] = name

        #           print "\tBattery: " ,printpacket(pkt[report_pkt_offset + 18:report_pkt_offset + 19])
                    battery = float(float(returnnumberpacket(pkt[report_pkt_offset + 18:report_pkt_offset + 19]) / float(25500) ) * 100)
        #           print "\tBattery: " ,battery
                    sensor["battery"] = battery
                    done = True

                    myFullList.append( sensor )

Let me know if I can do something more that can help you. L.

Ernst79 commented 2 years ago

Could you do the following. In the file custom_components/ble_monitor/ble_parser/bluemaestro.py, in line 8-10

def parse_bluemaestro(self, data, source_mac, rssi):
    """Parse BlueMaestro advertisement."""
    msg_length = len(data)

Can you replace this part with the following 4 lines (I added one line)

def parse_bluemaestro(self, data, source_mac, rssi):
    """Parse BlueMaestro advertisement."""
    _LOGGER.error("BlueMaestro data message: %s", data.hex())
    msg_length = len(data)

Next, restart HA and after a few minutes, have a look in your HA logs, it should have collected some advertisements. Can you post these here, preferably more than one, as there seem to be two parts that start with 3301

You don't have to set an encryption key btw. Just leave it empty.

Having it in the fridge could cause reception issues due to all the steel and insulation, so keep that in mind if it becomes less responsive (e.g. due to lower battery in the future).

tempesta-home commented 2 years ago

Here are some messages, just for testing I took the sensor out of the fridge for some minutes (1 or 2), and then again inside. At the end I decided to put it close to the HA just to exclude communication problems.

2021-10-14 15:10:01 ERROR (Thread-4) [custom_components.ble_monitor.ble_parser.bluemaestro] BlueMaestro data message: 11ff330117550e10061eff2f02a5ff030100 2021-10-14 15:10:03 ERROR (Thread-4) [custom_components.ble_monitor.ble_parser.bluemaestro] BlueMaestro data message: 11ff330117550e10061eff2f02a6ff030100 2021-10-14 15:10:11 ERROR (Thread-4) [custom_components.ble_monitor.ble_parser.bluemaestro] BlueMaestro data message: 11ff330117550e10061eff2f02a6ff030100 2021-10-14 15:11:05 ERROR (Thread-4) [custom_components.ble_monitor.ble_parser.bluemaestro] BlueMaestro data message: 11ff330117550e10061eff3002a8ff040100 2021-10-14 15:12:02 ERROR (Thread-4) [custom_components.ble_monitor.ble_parser.bluemaestro] BlueMaestro data message: 11ff330117550e10061eff4e02c0ff250100 2021-10-14 15:14:57 ERROR (Thread-4) [custom_components.ble_monitor.ble_parser.bluemaestro] BlueMaestro data message: 11ff330117550e10061e000c039d00010100 2021-10-14 15:17:13 ERROR (Thread-4) [custom_components.ble_monitor.ble_parser.bluemaestro] BlueMaestro data message: 11ff330117550e10061effe1032cffc60100 2021-10-14 15:17:58 ERROR (Thread-4) [custom_components.ble_monitor.ble_parser.bluemaestro] BlueMaestro data message: 11ff330117550e10061effcb02e1ffa40100 2021-10-14 15:18:35 ERROR (Thread-4) [custom_components.ble_monitor.ble_parser.bluemaestro] BlueMaestro data message: 11ff330117550e10061effba02a4ff890100 2021-10-14 15:20:10 ERROR (Thread-4) [custom_components.ble_monitor.ble_parser.bluemaestro] BlueMaestro data message: 11ff330117550e10061eff9e025fff610100 2021-10-14 15:20:14 ERROR (Thread-4) [custom_components.ble_monitor.ble_parser.bluemaestro] BlueMaestro data message: 11ff330117550e10061eff9e025fff610100 2021-10-14 15:29:05 ERROR (Thread-4) [custom_components.ble_monitor.ble_parser.bluemaestro] BlueMaestro data message: 11ff330117550e10061e0082033c00650100

Let me know how I can help. Thx.

tempesta-home commented 2 years ago

Here some more:

2021-10-14 15:31:08 ERROR (Thread-4) [custom_components.ble_monitor.ble_parser.bluemaestro] BlueMaestro data message: 11ff330117550e10061e009b02b800640100 2021-10-14 15:32:05 ERROR (Thread-4) [custom_components.ble_monitor.ble_parser.bluemaestro] BlueMaestro data message: 11ff330117550e10061e00a4029600650100 2021-10-14 15:39:06 ERROR (Thread-4) [custom_components.ble_monitor.ble_parser.bluemaestro] BlueMaestro data message: 11ff330117550e10061e00c5022600680100 2021-10-14 15:43:08 ERROR (Thread-4) [custom_components.ble_monitor.ble_parser.bluemaestro] BlueMaestro data message: 11ff330117550e10061e00ce02070068010 2021-10-14 15:49:08 ERROR (Thread-4) [custom_components.ble_monitor.ble_parser.bluemaestro] BlueMaestro data message: 11ff330117620e10061f00d401ed00660100 2021-10-14 16:02:11 ERROR (Thread-4) [custom_components.ble_monitor.ble_parser.bluemaestro] BlueMaestro data message: 11ff330117620e10061f00d901d200620100 2021-10-14 16:03:10 ERROR (Thread-4) [custom_components.ble_monitor.ble_parser.bluemaestro] BlueMaestro data message: 11ff330117620e10061f00da01d200630100

Do you find interesting that the time interval between lines is not constant?

Ernst79 commented 2 years ago

Can you add the actual temperature and humidity of the last readings, this will make it easier to debug. It looks like its big endian in stead of little endian (which is stated in the docs), but I'm not sure.

You could try to change

unpack("<BhhhHhH", msg)

to

unpack("!BhhhHhH", msg)

Dew point is missing the /10. Or just send me the corresponding temp and humidity and I will look into it further later.

Ernst79 commented 2 years ago

Can you try with 5.5.1-beta. I changed the parser to use network big endian (!) in stead of little endian (<)

image

Strange thing is that they say it should be little endian in their docs, but the 2nd link you gave is also using network big endian (!)

tempesta-home commented 2 years ago

Hi, the fix on unpack seems to work, give me some hours I'm collecting some more results (put again the sensor into the fridge to check negative temperatures). I'm getting back to you asap with the new beta results.

Ernst79 commented 2 years ago

Can you also check the packet number in the sensor attributes. Is it changing on each message or not? And can you estimate how often the data is updated per minute for the docs.

tempesta-home commented 2 years ago

mmmmm 5.5.1-beta not good.

image

No errors or other messages in the log.

--- UPDATE: it seems that the bluemaestro.py file disappeared in the ble_parser directory

tempesta-home commented 2 years ago

UPDATE on 5.5.0+ ! Fix: the packet Id attributes changes, but not on every packet. Could we miss the lowest figures? It seems that the tempo disc sends a packet about every minute or more frequently if the temperature changes abruptly (when I took the sensor out of the fridge)

Ernst79 commented 2 years ago

I’ve released 5.5.2-beta. I think I’ve selected the master branch accidentally in 5.5.1-beta, sorry.

I forgot that we are averaging data every minute, the attribute shows how many packets are used for averaging each minute (= default period). I think we just use all messages for bluemaestro, like we do now. Xiaomi MiBeacon is, for example, filtering messages with the same packet id, as these messages are exactly the same. But I guess that this is not the case for bluemaestro, as I also see the same packet id, but different temperature data.

tempesta-home commented 2 years ago

It seems that 5.5.2-beta works fine. I added two statistics panel on temperature dew point and humidity, I’ll let you know tomorrow when some data will flow in. Thanks for your great support.

tempesta-home commented 2 years ago

Hi, everything seemed to work perfectly, but I made a last check on the logs and found around 30 lines like this in the last 30'.

2021-10-15 18:06:19 ERROR (Thread-4) [custom_components.ble_monitor] HCIdump thread: Runtime error while stop scan request on hci0: Event loop stopped before Future completed.

I tried a core restart but nothing changed, same error. The integration doesn't update entities value. An host reboot solved the problem.

Could it be related to my HW? (pi4)

Ernst79 commented 2 years ago

It’s a common issue, see #295. We still don’t understand the problem, it seems to be related to the Bluetooth firmware. Strange thing is that not most people do not have this issue, while others do. I also have a raspberry 4 and no issues at all.but please discuss this issue in #295.

tempesta-home commented 2 years ago

Ok thx. By my side you can release 5.5.2. Good job!

Ernst79 commented 2 years ago

5.5.2 has been released.