Thanks for all the info here, I can say the example code still works. (Using latest HA as of July 2024)
I have two networks, so had to add mDNS to config file and the network for the ESP to be picked up if that helps anybody.
And proximity is important, I also got errors not able to read values until I moved closer than where my phone worked.
My only remaining issue is frequency. I get a MQTT message every second, and I can’t seem to reduce it. (And I assume this is making a connection and reading the data every time) I really only need a measurement every 6 hours or so. Surely for a battery monitor something similar would be the case for most people.
Tried the below without luck. Tried various formats for the time as well. Any suggestions would be appreciated.
…
sensor:
- platform: ble_client
type: characteristic
update_interval: 2min
…
Update
In my setup, update interval is the frequency the ESP tries to make the bluetooth connection. Once connected, it keeps measuring and sending once a second. It does not seem to disconnect.
I can see this when out of reach from the BM2, and it retries to connect.
ble_client:
- mac_address: 12:34:56:78:90:CD
id: bm2_battery_meter
sensor:
- platform: ble_client
type: characteristic
update_interval: 3min
ble_client_id: bm2_battery_meter
name: Voltage
service_uuid: 'fff0'
characteristic_uuid: 'fff4'
icon: 'mdi:battery'
unit_of_measurement: 'V'
accuracy_decimals: 2
state_class: measurement
device_class: voltage
force_update: true
expire_after: 240min
notify: true
lambda: |-
mbedtls_aes_context aes;
mbedtls_aes_init(&aes);
unsigned char output[16];
unsigned char key[16] = { 108, 101, 97, 103, 101, 110, 100, 255, 254, 49, 56, 56, 50, 52, 54, 54, };
unsigned char iv[16] = {};
mbedtls_aes_setkey_dec(&aes, key, 128);
mbedtls_aes_crypt_cbc(&aes, MBEDTLS_AES_DECRYPT, 16, iv, (uint8_t*)&x[0], output);
mbedtls_aes_free(&aes);
return ((output[2] | (output[1] << 8)) >> 4) / 100.0f;