BLE service’s characteristic

Hi.
Using a BLE client sensor I see only the first character of the BLE service’s characteristic in decimal format .

sensor:
- platform: ble_client
    ble_client_id: ESP32_BLE_Remote
    name: "ESP32 Remote BLE-2"
    service_uuid: '1800'
    characteristic_uuid: '2a00'

The question are:

  1. is it possible to show/convert this first character in ASCII ?
  2. is it possible to show the entire service’s characteristic (i.e. “MyESP32”) instead of only the first character?

ide

sensor

Yes, see the second example under the title " Reading arbitrary characteristic values" in my blog post about BLE connections in ESPHome:

First define a template text sensor:

text_sensor:
  - platform: template
    name: "${node_name} heart rate sensor name"
    id: heart_rate_sensor_name

And then define a BLE client sensor that accesses the raw bytes of the Device Name characteristic, converts it to a string and publishes it to the template text sensor:

sensor:
  - platform: ble_client
    ble_client_id: heart_rate_monitor
    id: device_name
    service_uuid: '1800'  # Generic Access Profile
    characteristic_uuid: '2a00'  # Device Name
    lambda: |-
      std::string data_string(x.begin(), x.end());
      id(heart_rate_sensor_name).publish_state(data_string.c_str());
      return (float)x.size();

The only weirdness is that you need to return a float in the lambda, because it’s a sensor that’s expected to return a float value.

1 Like

Hi Koen.
Thanks for you reply.
Great job.
It works !
Only two questions:

  1. Why "${node_name} ...
  2. I’ve noted that even if the BLE ESP32 is OFF ( and disconnected) in HA’s dashboard the service’s characteristic name is ever displayed. (I’d like that when the BLE ESP32 is OFF even the name is not displayed).
    Any idea?
    Thanks in advance.

The ${node_name} is just a substitution that I’m using in all my ESPHome devices. If you don’t use it, just use your own preferred name.

I don’t know the answer to your second question.

1 Like

Just an addition for your second question. You could update the text sensor on disconnection of the client. This would look like this (untested, and you should merge this with the code you already have):

sensor:
- platform: ble_client
    ble_client_id: ESP32_BLE_Remote
    name: "ESP32 Remote BLE-2"
    service_uuid: '1800'
    characteristic_uuid: '2a00'
    on_disconnect:
      then:
        - lambda: |-
            id(text_sensor_name).publish_state("OFF");
1 Like

Hi Koen.
Thanks again for your support.
It works but with a simple modification:
on-disconnect option isn’t allowed in ble sensor, but in ble client.
In this manner works fine when the ESP32 device is disconnected:

ble_client:
  - mac_address: 34:94:54:25:b8:fe
    id: ESP32_BLE_Remote
    on_disconnect:
         then:
           - lambda: |-
               id(ESP32_text_sensor_name).publish_state("OFF");

Oy yes, I should’ve read the documentation more carefully. I’m glad you got it working, nice!

Hi i am trying to use it but its not working to send the raw data
the logs show
[15:48:06][D][sensor:094]: ‘device_name’: Sending state 20.00000 with 0 decimals of accuracy
[15:48:06][D][text_sensor:064]: ‘Bedjet Bet Notify’: Sending state ‘G’

if possible to help
Thanks