ebw44 / ESPHome-Inkbird-ITH-2PB

GNU General Public License v3.0
5 stars 1 forks source link

Notifications don't start automatically #3

Open vezinpi opened 5 months ago

vezinpi commented 5 months ago

I am not a software developper so I apologize in advance.

I read an re-re-read the instruction but I cannot get the temperature from the IHT-2PB using and ESP32 development board. From the log I can see that I can connect for a while before disconnecting and getting error message. The temperature never gets reported to home assistant.

Here is my yaml file

esphome: name: esphome-thermostat friendly_name: esphome thermostat

esp32: board: esp32doit-devkit-v1 framework: type: esp-idf

Enable logging

logger:

Enable Home Assistant API

api: encryption: key: "uLfai8cGoNlB4Vc9DacskJP2OumT18+ZZPG4rJPNQp4="

ota: password: "935077bb3315c2ac089c23d3a0126477"

wifi: ssid: !secret wifi_ssid password: !secret wifi_password

Enable fallback hotspot (captive portal) in case wifi connection fails

ap: ssid: "Esphome-Thermostat" password: "iemMwhXKzPTT"

captive_portal:

<<: !include Inkbird_IHT_2PB.yaml

Here are the logs.

INFO ESPHome 2023.12.9 INFO Reading configuration /config/esphome-thermostat.yaml... INFO Starting log output from 192.168.2.134 using esphome API INFO Successfully connected to esphome-thermostat @ 192.168.2.134 in 0.010s INFO Successful handshake with esphome-thermostat @ 192.168.2.134 in 0.198s [22:43:05][I][app:102]: ESPHome version 2023.12.9 compiled on Feb 4 2024, 21:34:27

[22:43:05][C][wifi:405]: Local MAC: E4:65:B8:12:BB:88 [22:43:05][C][wifi:410]: SSID: [redacted] [22:43:05][C][wifi:411]: IP Address: 192.168.2.134 [22:43:05][C][wifi:413]: BSSID: [redacted]

[22:43:05][C][wifi:416]: Signal strength: -13 dB ▂▄▆█ [22:43:05][C][wifi:420]: Channel: 6 [22:43:05][C][wifi:421]: Subnet: 255.255.255.0 [22:43:05][C][wifi:422]: Gateway: 192.168.2.1 [22:43:05][C][wifi:423]: DNS1: 192.168.2.1 [22:43:05][C][wifi:424]: DNS2: 207.164.234.193

[22:43:05][C][logger:440]: Level: DEBUG [22:43:05][C][logger:441]: Log Baud Rate: 115200 [22:43:05][C][logger:443]: Hardware UART: UART0 [22:43:05][C][template.binary_sensor:028]: Template Binary Sensor 'Inkbird IHT-2PB connected' [22:43:05][C][template.sensor:022]: Template Sensor 'Inkbird IHT-2PB probe manual' [22:43:05][C][template.sensor:022]: Device Class: 'temperature' [22:43:05][C][template.sensor:022]: State Class: 'measurement' [22:43:05][C][template.sensor:022]: Unit of Measurement: '°C' [22:43:05][C][template.sensor:022]: Accuracy Decimals: 1

[22:43:05][C][template.sensor:023]: Update Interval: 60.0s [22:43:06][C][template.sensor:022]: Template Sensor 'Inkbird IHT-2PB probe 1' [22:43:06][C][template.sensor:022]: Device Class: 'temperature' [22:43:06][C][template.sensor:022]: State Class: 'measurement' [22:43:06][C][template.sensor:022]: Unit of Measurement: '°C' [22:43:06][C][template.sensor:022]: Accuracy Decimals: 1

[22:43:06][C][template.sensor:023]: Update Interval: 60.0s [22:43:06][C][template.sensor:022]: Template Sensor 'Inkbird IHT-2PB probe 2' [22:43:06][C][template.sensor:022]: Device Class: 'temperature' [22:43:06][C][template.sensor:022]: State Class: 'measurement' [22:43:06][C][template.sensor:022]: Unit of Measurement: '°C' [22:43:06][C][template.sensor:022]: Accuracy Decimals: 1

[22:43:06][C][template.sensor:023]: Update Interval: 60.0s [22:43:06][C][bluetooth_proxy:088]: Bluetooth Proxy: [22:43:06][C][bluetooth_proxy:089]: Active: YES [22:43:06][C][ble_switch:068]: BLE Client Switch 'Inkbird IHT-2PB enable'

[22:43:06][C][ble_switch:091]: Restore Mode: always OFF [22:43:06][C][ble_sensor:017]: BLE Sensor 'Inkbird IHT-2PB last probe data' [22:43:06][C][ble_sensor:017]: State Class: '' [22:43:06][C][ble_sensor:017]: Unit of Measurement: '' [22:43:06][C][ble_sensor:017]: Accuracy Decimals: 0 [22:43:06][C][ble_sensor:018]: MAC address : 62:00:A1:2F:4F:35 [22:43:06][C][ble_sensor:019]: Service UUID : 0000FFE0-0000-1000-8000-00805F9B34FB [22:43:06][C][ble_sensor:020]: Characteristic UUID: 0000FFE4-0000-1000-8000-00805F9B34FB [22:43:06][C][ble_sensor:021]: Descriptor UUID : 00000000-0000-0000-0000-000000000000 [22:43:06][C][ble_sensor:022]: Notifications : YES [22:43:06][C][ble_sensor:023]: Update Interval: 60.0s [22:43:06][C][esp32_ble:374]: ESP32 BLE: [22:43:06][C][esp32_ble:376]: MAC address: E4:65:B8:12:BB:8A [22:43:06][C][esp32_ble:377]: IO Capability: none [22:43:06][C][esp32_ble_tracker:645]: BLE Tracker: [22:43:06][C][esp32_ble_tracker:646]: Scan Duration: 300 s [22:43:06][C][esp32_ble_tracker:647]: Scan Interval: 1100.0 ms [22:43:06][C][esp32_ble_tracker:648]: Scan Window: 1100.0 ms [22:43:06][C][esp32_ble_tracker:649]: Scan Type: ACTIVE [22:43:06][C][esp32_ble_tracker:650]: Continuous Scanning: True [22:43:06][C][ble_client:027]: BLE Client: [22:43:06][C][ble_client:028]: Address: 62:00:A1:2F:4F:35 [22:43:06][C][captive_portal:088]: Captive Portal:

[22:43:06][C][mdns:116]: Hostname: esphome-thermostat [22:43:06][C][ota:097]: Over-The-Air Updates: [22:43:06][C][ota:098]: Address: esphome-thermostat.local:3232 [22:43:06][C][ota:101]: Using Password. [22:43:06][C][api:139]: API Server: [22:43:06][C][api:140]: Address: esphome-thermostat.local:6053 [22:43:06][C][api:142]: Using noise encryption: YES [22:43:38][W][ble_sensor:080]: Error reading char at handle 21, status=2 [22:43:40][D][esp-idf:000]: W (121264) BT_APPL: gattc_conn_cb: if=3 st=0 id=3 rsn=0x13

[22:43:40][D][esp-idf:000]: W (121271) BT_APPL: gattc_conn_cb: if=4 st=0 id=4 rsn=0x13

[22:43:40][D][esp-idf:000]: W (121274) BT_APPL: gattc_conn_cb: if=5 st=0 id=5 rsn=0x13

[22:43:40][D][esp-idf:000]: W (121275) BT_APPL: gattc_conn_cb: if=6 st=0 id=6 rsn=0x13

[22:43:40][D][esp-idf:000]: W (121278) BT_HCI: hcif disc complete: hdl 0x0, rsn 0x13

[22:43:40][D][ble_client.automation:069]: Disconnected from 62:00:A1:2F:4F:35 [22:43:40][D][ble_client.automation:069]: Disconnected from 62:00:A1:2F:4F:35 [22:43:40][W][ble_sensor:037]: [Inkbird IHT-2PB last probe data] Disconnected! [22:43:40][D][sensor:094]: 'Inkbird IHT-2PB last probe data': Sending state nan with 0 decimals of accuracy [22:43:40][D][ble_client_lambda:026]: Disconnected from BLE device [22:43:40][D][binary_sensor:036]: 'Inkbird IHT-2PB connected': Sending state OFF [22:43:57][D][esp32_ble_client:053]: [0] [62:00:A1:2F:4F:35] Found device [22:43:57][D][esp32_ble_tracker:215]: Pausing scan to make connection... [22:43:57][I][esp32_ble_client:069]: [0] [62:00:A1:2F:4F:35] 0x00 Attempting BLE connection [22:43:58][D][ble_client.automation:050]: Connection established with 62:00:A1:2F:4F:35 [22:43:58][D][ble_client.automation:050]: Connection established with 62:00:A1:2F:4F:35 [22:43:58][I][ble_sensor:031]: [Inkbird IHT-2PB last probe data] Connected successfully! [22:43:58][I][esp32_ble_client:201]: [0] [62:00:A1:2F:4F:35] Connected [22:43:58][D][ble_client_lambda:016]: Connected to BLE device [22:43:58][D][binary_sensor:036]: 'Inkbird IHT-2PB connected': Sending state ON [22:43:58][D][ble_client.automation:063]: Found characteristic 0000FFE9-0000-1000-8000-00805F9B34FB on device 62:00:A1:2F:4F:35 [22:43:58][D][ble_client.automation:063]: Found characteristic 0000FFE4-0000-1000-8000-00805F9B34FB on device 62:00:A1:2F:4F:35 [22:43:58][D][esp32_ble_tracker:266]: Starting scan... [22:44:37][W][ble_sensor:080]: Error reading char at handle 21, status=2 [22:44:44][D][esp-idf:000]: W (185379) BT_APPL: gattc_conn_cb: if=3 st=0 id=3 rsn=0x13

[22:44:44][D][esp-idf:000]: W (185384) BT_APPL: gattc_conn_cb: if=4 st=0 id=4 rsn=0x13

[22:44:44][D][esp-idf:000]: W (185387) BT_APPL: gattc_conn_cb: if=5 st=0 id=5 rsn=0x13

[22:44:44][D][esp-idf:000]: W (185388) BT_APPL: gattc_conn_cb: if=6 st=0 id=6 rsn=0x13

[22:44:44][D][esp-idf:000]: W (185392) BT_HCI: hcif disc complete: hdl 0x0, rsn 0x13

[22:44:44][D][ble_client.automation:069]: Disconnected from 62:00:A1:2F:4F:35 [22:44:44][D][ble_client.automation:069]: Disconnected from 62:00:A1:2F:4F:35 [22:44:44][W][ble_sensor:037]: [Inkbird IHT-2PB last probe data] Disconnected! [22:44:44][D][sensor:094]: 'Inkbird IHT-2PB last probe data': Sending state nan with 0 decimals of accuracy [22:44:44][D][ble_client_lambda:026]: Disconnected from BLE device [22:44:44][D][binary_sensor:036]: 'Inkbird IHT-2PB connected': Sending state OFF [22:45:00][D][esp32_ble_client:053]: [0] [62:00:A1:2F:4F:35] Found device [22:45:00][D][esp32_ble_tracker:215]: Pausing scan to make connection... [22:45:00][I][esp32_ble_client:069]: [0] [62:00:A1:2F:4F:35] 0x00 Attempting BLE connection [22:45:01][D][ble_client.automation:050]: Connection established with 62:00:A1:2F:4F:35 [22:45:01][D][ble_client.automation:050]: Connection established with 62:00:A1:2F:4F:35 [22:45:01][I][ble_sensor:031]: [Inkbird IHT-2PB last probe data] Connected successfully! [22:45:01][I][esp32_ble_client:201]: [0] [62:00:A1:2F:4F:35] Connected [22:45:01][D][ble_client_lambda:016]: Connected to BLE device [22:45:01][D][binary_sensor:036]: 'Inkbird IHT-2PB connected': Sending state ON [22:45:01][D][ble_client.automation:063]: Found characteristic 0000FFE9-0000-1000-8000-00805F9B34FB on device 62:00:A1:2F:4F:35 [22:45:01][D][ble_client.automation:063]: Found characteristic 0000FFE4-0000-1000-8000-00805F9B34FB on device 62:00:A1:2F:4F:35 [22:45:01][D][esp32_ble_tracker:266]: Starting scan...

Thanks for any help you can provide.

ebw44 commented 5 months ago

There is a problem with ESPHome ble on_connect event. The quick fix was to add a button to manually start the ble notification from home assistant. But adding the button press in the on_connect event actions seems to work for me. So I have committed that change. Let me know if it works or not for you.

Normally you should now get

[23:29:08][D][button:010]: 'Inkbird IHT-2PB notification' Pressed.
[23:29:08][D][ble_client.automation:026]: Write type: ESP_GATT_WRITE_TYPE_RSP
[23:29:08][E][ble_client.automation:031]: Characteristic 0000FFE4-0000-1000-8000-00805F9B34FB does not allow writing
[23:29:08][W][component:214]: Component esp32_ble took a long time for an operation (0.07 s).
[23:29:08][W][component:215]: Components should block for at most 20-30ms.
[23:29:08][D][esp32_ble_tracker:246]: Starting scan...
[23:29:09][D][sensor:094]: 'Inkbird IHT-2PB last probe data': Sending state -1.00000  with 0 decimals of accuracy
[23:29:09][D][sensor:094]: 'Inkbird IHT-2PB last probe data': Sending state -1.00000  with 0 decimals of accuracy
[23:29:09][D][sensor:094]: 'Inkbird IHT-2PB last probe data': Sending state -1.00000  with 0 decimals of accuracy
[23:29:09][D][sensor:094]: 'Inkbird IHT-2PB last probe data': Sending state -1.00000  with 0 decimals of accuracy
[23:29:09][D][sensor:094]: 'Inkbird IHT-2PB last probe data': Sending state -1.00000  with 0 decimals of accuracy
[23:29:10][D][sensor:094]: 'Inkbird IHT-2PB probe manual': Sending state 23.80000 �C with 1 decimals of accuracy
[23:29:10][D][sensor:094]: 'Inkbird IHT-2PB last probe data': Sending state 1.00000  with 0 decimals of accuracy
[23:29:13][D][sensor:094]: 'Inkbird IHT-2PB last probe data': Sending state -1.00000  with 0 decimals of accuracy
vezinpi commented 5 months ago

Hi, Thank you for your quick reply.

My bad. It is working when I push the notification button in Home Assistant. Sorry about the confusion. With your latest modification I was expecting the button push to be automatic but in my case I still need to manually press it. The command might be happening too soon in the process.

The only error still showing once in a while is the following:

[W][ble_sensor:080]: Error reading char at handle 21, status=2

Again, Many thanks

ebw44 commented 5 months ago

I should have added the information about the notification button. I thought I would find a solution before anyone else uses it. If you have a bit of time to do some testing you could try to add some delay in the on_connect or activate_temperature_notifications script to see if it allows the ble connection to finish before starting the writing of characteristic that starts the notifications.

    on_connect:
      then:
        - lambda: |-
            ESP_LOGD("ble_client_lambda", "Connected to BLE device");
            id(inkbird_IHT_2PB_connected).publish_state(true);
        # on_connect is triggered before the device is fully connected see https://github.com/esphome/issues/issues/4153
        # But even with a 10ms delay it doesn't seem to work. So the solution is to add a button that is pressed and start the script to activate
        # the notifications. This also allows the user to force the notifications manually.
        - delay: 10ms
        - button.press: inkbird_IHT_2PB_notification
script:
  - id: activate_temperature_notifications
    then:
      - delay: 10ms
      - ble_client.ble_write:
          id: inkbird_IHT_2PB_ble
          service_uuid: 0000ffe0-0000-1000-8000-00805f9b34fb
          characteristic_uuid: 0000ffe9-0000-1000-8000-00805f9b34fb
          value: [0x55, 0xAA, 0x19, 0x01, 0x00, 0x19]
      - ble_client.ble_write:
          id: inkbird_IHT_2PB_ble
          service_uuid: 0000ffe0-0000-1000-8000-00805f9b34fb
          characteristic_uuid: 0000ffe4-0000-1000-8000-00805f9b34fb
          value: [0x55, 0xAA, 0x1A, 0x01, 0x00, 0x1A]
vezinpi commented 5 months ago

Hi,

I was about to write to you. I did add a delay yesterday and it works like a charm. Upon reboot the I do not need to do anything for the data to come in.

I went a bit agressive with the delay with "delay: 1000ms". I figured what is the rush... I can try other values if you want.

Any idea about the last remaining error that poped up once in a while:

[W][ble_sensor:080]: Error reading char at handle 21, status=2

It seems to be happening every time the manual proble value is updated.

Many thanks for your assistance

vezinpi commented 5 months ago

Hi again,

2 more things I did not mentionned.

First the delay I added in the on_connect before the button.press

ble_client:

Second, I changed the last line of you script from: return (float)probeNr; to: return: (float)probeNr -1; to get the number reported to match the probe #. 0 = manual 1 = probe 1 and 2 = probe 2. This seemed more logical to me.

vezinpi commented 5 months ago

Hi, sorry for the delay I was away for a few days.

Adding: delay: 10 ms in the: on_connect or activate_temperature_notifications, as you suggested, works just fine.

Also I do not understand why the "last probe data" is sometime reporting a value of -1 (I know, I change the probeNr to probeNr - 1 but I was expecting the values to be 0,1 or 2 in my case or 1,2 or 3 as per the original code).

[W][ble_sensor:080]: Error reading char at handle 21, status=2 [10:06:29][D][sensor:094]: 'Inkbird IHT-2PB probe manual': Sending state 21.90000 °C with 1 decimals of accuracy [10:06:29][D][sensor:094]: 'Inkbird IHT-2PB last probe data': Sending state 0.00000 with 0 decimals of accuracy [10:06:29][D][sensor:094]: 'Inkbird IHT-2PB last probe data': Sending state -1.00000 with 0 decimals of accuracy [10:06:51][W][ble_sensor:080]: Error reading char at handle 21, status=2 [10:07:34][D][sensor:094]: 'Inkbird IHT-2PB probe manual': Sending state 21.90000 °C with 1 decimals of accuracy [10:07:34][D][sensor:094]: 'Inkbird IHT-2PB last probe data': Sending state 0.00000 with 0 decimals of accuracy [10:07:34][D][sensor:094]: 'Inkbird IHT-2PB probe 2': Sending state 21.70000 °C with 1 decimals of accuracy [10:07:34][D][sensor:094]: 'Inkbird IHT-2PB last probe data': Sending state 2.00000 with 0 decimals of accuracy [10:07:51][W][ble_sensor:080]: Error reading char at handle 21, status=2

vezinpi commented 4 months ago

Hi again,

Since you succesfully manage to connect and extract the information from the thermometer, do you have any information(format, protocol, etc...) that you could share with respect to the service uuid: ffe0 and its associated characteristics uuid: ffe4 and ffe9. They seemed proprietary but somehow you figured them out.

Just starting in BLE and the format used by each fields associated with the service and characteristics would go a long way helping me understand the fucntionnality.

Thanks in advance

ebw44 commented 4 months ago

Hi,

For the probeNr I just push a change to use more meaningful number for the "last probe data" entity (c943486a). For the BLE protocol, I didn't do the reverse engineering, it was done in https://gitlab.com/sensor-stuff/inkbird-iht-2pb-to-mqtt I'd like to add more features, like settings the temperature alarm of the ITH-2PB. But I haven't found the time to look at the BLE protocol. I did some recording of the inkbird app communication with the sensor. But the android log file 'btsnoop_hci.log' is not straightforward to look at. If you want to have a go at it you call look at https://medium.com/@charlie.d.anderson/how-to-get-the-bluetooth-host-controller-interface-logs-from-a-modern-android-phone-d23bde00b9fa