Generic BLE sensors

I’ve built a bunch of binary sensors with ESP32 that should transmit their data using Bluetooth low energy and not wifi, since they are supposed to be battery-powered. However, while researching how to run a BLE client on home assistant I couldn’t find any components available. What I could find (and have running currently) is the BLE-device-tracker and some device specific implementations (like the miflora sensors).

If I understand BLE correctly, if I set my ESP32 sensors as a BLE server, I should be able to capture the info on my server if I setup a BLE client there. Looking around I could also find several possible python packages that interface with the bluetooth dongle, so this appears to be relatively straightforward.

What I couldn’t find to my surprise is a generic BLE component, somewhat like we have zwave and zigbee components. I would expect that this would be the basis for using BLE as a way to transmit data for home assistant.
Since i couldn’t find it, is there something I misunderstand about BLE? If I create a specific component for binary_sensors using BLE using pygatt for instance, will it not work? What issue am i overlooking that stops home assistant from using this inexpensive way of communication currently?

You will get the information sent to your device if you load ESPHome on your ESP32. You can then add the ESPHome as an integration through HA.

The is a BLE component in HA too. People have had mixed results with this

You can also look at running OpenMQTTGateway on your ESP32 and send information to HA

I have been using ESPHome over the last month and have found this to be the most reliable.

I’ve used ESPHome up to now, but it can only handle WiFi, so it won’t give me what I need. When running the sensor using ESPHome it only lasts about 1,5 day on a (relatively huge) 18650 battery, which I’ll never get down to acceptable levels. Asking and looking around it becomes clear that using BLE to commincate isn’t likely to happen within esphome.

The BLE component is just the tracker, you cannot receive information with it as far as I can see, only detect devices.

OpenMQTT is also not an option, since that’ll again use wifi.

Do you expect more elaborate client-server protocol with your BLE device or just simple BLE broadcast of sensor data similar to miflora sensor or miscale ?

Every Bluetooth device has its own protocol/ message structure. Even different Xiaomi devices vary between themselves regarding the message structure. I think this is the reason why there’s no any generic component in HA for BLE tracker. But I think you could take one if HA BLE tracker sensors, rework it and build the integration this way.

I also suggest you post a feature request to the ESPhome Team since this is a very interesting case to make a sensor from ESP32 communicating via BLE.
ESPhome currently only supports TCP communication with HA. Building a BLE communication would be a lot of work but also may be useful when one wants to build a sensor with esp32.

What I’m not sure however is if esp32 is designed to work as a battery sensor. I mean sensors based for example on Z-Wave most of the time spent on being in sleep mode thus not really draining battery. I’m not sure if esp32 supports being in sleep mode. The battery drain you experienced using Wi-Fi may not only be caused by the Wi-Fi itself but also because esp32 constantly doing some work some work in the background or just being active all the time. I think that battery powered devices only wake up their chip If there is anything to be sent to the outside world. Perhaps you need to design your own PCB with esp32 and wake it when you have something to be sent via ble

Not quite true, it can get info from some xiaomi bluetooth devices, but this is not what you want anyway,

Have you thought about sleeping the esp and only waking when it has new data? This is quite a popular strategy with esp (not just in esphome, but esphome does support this).

I’m not sure yet. In principle just broadcasting from the sensors is enough, but I don’t know how reliable it is and how much extra battery is required to indicate back that the message was received. What I’ve understood so far from online sources is that both is possible at least.

The ESP32 supports several sleep modes including a ‘deep sleep’ and I’ve read articles that say it is very effective (about 10 micorA )usage. I have no practical experience with it, but it looks like an average usage of 1mA should be obtainable, which I think is sufficient.

Regarding the protocol, isn’t it correct to say that there is some standerdisation in the characteristics? On this website I see a whole range of IDs: https://www.bluetooth.com/specifications/gatt/characteristics/. What I’ve understood is that in this way different components can still be used interchangebly (like multiple heart monitors for instance).

My Idea basically is that it would be nice to at least implement a ‘template’ characteristic where you provide the ID of the service and characteristics, that way you have a ‘basic’ sensor. Than you could expand this to other types and specific platforms, just as (at least to my understanding) is the case with zwave/zigbee.

now this idea makes so much sense to me, that I’m wondering why it isn’t there yet, which is basically why I started the thread. I’m unsure why the xiaomi flora sensor is a seperate component for instance, and from the forums it looks like implementing a seperate component for each device leads to many issues (not the least of which multiple APIs than can’t be used at the same time).

I want the sensor to react asap (detection of passage through doors, including direction), so I don’t think this’ll work for me with wifi. I haven’t tried it though, but in general the wifi connection time will be too much for me to be practical I think.

good afternoon ! there are successes in creating ble sensors for esphome ??? did you manage to create?