BM2 battery monitoring using ble tracker component

Here’s my ESP build. What more information would you be looking for?

esphome:
  name: bm2-esp
  friendly_name: bm2-esp

  includes:
    - include.h

esp32:
  board: esp-wrover-kit
  framework:
    type: arduino

# Enable logging
logger:
  level: INFO

# Enable Home Assistant API
api:
  encryption:
    key: !secret bm2_key

ota:
  password: !secret ota_password

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


mqtt:
  broker: 10.10.10.10
  discovery: false
  topic_prefix: esphome/bm2
  username: !secret mqtt_user
  password: !secret mqtt_password

web_server:
  port: 80

esp32_ble_tracker:

ble_client:
  - mac_address: !secret bm2_mac
    id: bm2_battery_meter

sensor:
  - platform: ble_client   
    type: characteristic
    ble_client_id: bm2_battery_meter
    name: Voltage
    service_uuid: 'fff0'
    characteristic_uuid: 'fff4'
    unit_of_measurement: 'V'
    accuracy_decimals: 2
    state_class: measurement
    device_class: voltage
    force_update: true
    expire_after: 5min
    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;

2 Likes