Skip to content

Instantly share code, notes, and snippets.

@M0LTE
Created December 30, 2024 13:27
Show Gist options
  • Save M0LTE/b1b45978db37ceeae299ff865923fd77 to your computer and use it in GitHub Desktop.
Save M0LTE/b1b45978db37ceeae299ff865923fd77 to your computer and use it in GitHub Desktop.
BM6 BLE battery voltage sensor esphome config
# credit https://github.com/JeffWDH/bm6-battery-monitor/blob/39bb89a095f0714d84d05e595099f28ec44647fe/ESPHome/bm6.yaml
# first you need to find the BLE mac address of the BM6:
esp32_ble_tracker:
text_sensor:
- platform: ble_scanner
name: "BLE Devices Scanner"
# read off the mac address in the esphome logs. Device name is just BM6
# then you can configure as follows:
esphome:
includes:
- "bm6.h" # this should be in config/ and contain the following:
#define MBEDTLS_AES_ALT
#include "mbedtls/aes.h"
esp32_ble_tracker:
scan_parameters:
continuous: false
duration: 5s
ble_client:
- id: bm6
mac_address: 00:00:00:00:00:00 # Put your BM6 address here
auto_connect: false
on_connect:
# Disconnect in case there was a hung connection due to poor signal
- delay: 15s
- ble_client.disconnect: bm6
interval:
- interval: 30min # How often you want to poll the BM6
then:
- esp32_ble_tracker.start_scan:
- delay: 5s
- esp32_ble_tracker.stop_scan:
- ble_client.connect: bm6
- delay: 1s
- ble_client.ble_write:
id: bm6
service_uuid: 0000fff0-0000-1000-8000-00805f9b34fb
characteristic_uuid: 0000fff3-0000-1000-8000-00805f9b34fb
value: [0x69, 0x7e, 0xa0, 0xb5, 0xd5, 0x4c, 0xf0, 0x24, 0xe7, 0x94, 0x77, 0x23, 0x55, 0x55, 0x41, 0x14] # Encrypted "d1550700000000000000000000000000"
sensor:
- platform: template
name: "Temperature"
id: bm6_temperature
unit_of_measurement: "°C"
icon: "mdi:thermometer"
accuracy_decimals: 0
- platform: template
name: "Voltage"
id: bm6_voltage
unit_of_measurement: "V"
icon: "mdi:car-battery"
accuracy_decimals: 2
- platform: ble_client
type: characteristic
ble_client_id: bm6
name: "bm6"
service_uuid: 0000fff0-0000-1000-8000-00805f9b34fb
characteristic_uuid: 0000fff4-0000-1000-8000-00805f9b34fb
notify: true
internal: true
update_interval: never
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, 48, 49, 48, 48, 48, 48, 57 };
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);
int tempC = output[4];
float volts = ((output[7] << 8) | output[8]) / 100.0f;
if (volts > 0) {
id(bm6_temperature).publish_state(tempC);
id(bm6_voltage).publish_state(volts);
id(bm6).disconnect();
}
return 0;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment