Hm… no luck yet… I have this in the yaml now:
esphome:
name: "esp32-12"
platform: ESP32
board: wemos_d1_mini32
substitutions:
esp_name: esp32_12
wifi:
ssid: "wifi"
password: !secret wifi_key
use_address: 192.168.6.95
domain: !secret domain
# Enable fallback hotspot (captive portal) in case wifi connection fails
ap:
ssid: "${esp_name} Fallback Hotspot"
password: !secret Fallback_Hotspot
captive_portal:
# Enable logging
logger:
# level: VERBOSE
# baud_rate: 9600
# Enable Home Assistant API
api:
ota:
time:
- platform: homeassistant
id: homeassistant_time
web_server:
port: 80
esp32_ble_tracker:
scan_parameters:
duration: 90s
# Define the BLE client
ble_client:
- mac_address: "30:55:44:36:66:7D" # Replace with the MAC address of your BLE device
id: my_ble_client
on_connect:
then:
- lambda: |-
id(ble_connected) = true;
id(ble_state) = "connected";
on_disconnect:
then:
- lambda: |-
id(ble_connected) = false;
id(ble_state) = "reconnecting";
- script.execute: reconnect_ble
## To find the mac adress, most easy way is to enable the part down below saying:
## - platform: ble_scanner
## name: "BLE Devices Scanner"
## This will show all found BLE devices in the area with their mac adres and name if they have one in the esphome log.
## In case of a X210 battery the macs start consistently with: DC:0D:30:
sensor:
- <<: !include includes/include-wifi-signal.yaml
- <<: !include includes/include-uptime-config.yaml
- platform: template
name: "Battery voltage"
unit_of_measurement: "V"
update_interval: 30s
accuracy_decimals: 2
lambda: |-
return id(global_voltage);
- platform: template
name: "Battery Current"
update_interval: 30s
unit_of_measurement: "A"
accuracy_decimals: 1
lambda: |-
return id(global_current);
- platform: template
name: "Battery SOC"
update_interval: 30s
unit_of_measurement: "%"
accuracy_decimals: 0
lambda: |-
return id(global_soc);
- platform: template
name: "Battery Temperature"
update_interval: 30s
unit_of_measurement: "°C"
accuracy_decimals: 1
lambda: |-
return id(global_temperature);
#Unsure about these two yet if they actually work and what they mean
- platform: template
name: "Battery Status"
update_interval: 30s
accuracy_decimals: 0
lambda: |-
return id(global_status);
- platform: template
name: "Battery AFE Status"
update_interval: 30s
accuracy_decimals: 0
lambda: |-
return id(global_afe_status);
globals:
- id: global_voltage
type: float
restore_value: no
initial_value: '0.0'
- id: global_current
type: float
restore_value: no
initial_value: '0.0'
- id: global_soc
type: int
restore_value: no
initial_value: '0'
- id: global_temperature
type: float
restore_value: no
initial_value: '0.0'
- id: global_status
type: int
restore_value: no
initial_value: '0'
- id: global_afe_status
type: int
restore_value: no
initial_value: '0'
- id: ble_connected
type: bool
restore_value: no
initial_value: 'false'
- id: ble_state
type: std::string
restore_value: no
initial_value: '"disconnected"'
# this fetches data every minute by pushing a Number 1 to the FFF2 characteristic, causing an update on the Notify FFF1
# Not sure if you actually need these two one, if it keeps dumping so mucht data it might do it itself anyway.
# You could try removing these
interval:
- interval: 1min
then:
- ble_client.ble_write:
id: my_ble_client
service_uuid: 'FFF0'
characteristic_uuid: 'FFF2'
value: [0x31]
#this button manually fetches the data by pushing a Number 1 to the FFF2 characteristic, causing an update on the Notify FFF1
button:
- platform: template
name: "Force update values"
on_press:
then:
- ble_client.ble_write:
id: my_ble_client
service_uuid: 'FFF0'
characteristic_uuid: 'FFF2'
value: [0x31]
text_sensor:
- <<: !include includes/include-uptime-human.yaml
- <<: !include includes/include-esphome-version.yaml
- <<: !include includes/include-wifi-info.yaml
- platform: template
name: "Battery BLE connection"
id: ble_state_sensor
lambda: |-
return id(ble_state);
- platform: ble_client
ble_client_id: my_ble_client
name: "BLE Service FFE0 Characteristic FFE4"
service_uuid: 'FFE0'
characteristic_uuid: 'FFE4'
notify: true
internal: true
id: ble_raw_data
on_value:
then:
- lambda: |-
std::string raw_data = id(ble_raw_data).state;
if (raw_data.length() >= 38) {
// Strip the first character '^'
raw_data = raw_data.substr(1);
auto char_to_int = [](char c) -> int {
if ('0' <= c && c <= '9') {
return c - '0';
} else if ('A' <= c && c <= 'F') {
return (c - 'A') + 10;
} else {
return 0;
}
};
auto asciitochar = [&](char b, char b2) -> int {
return ((char_to_int(b) << 4) & 0xF0) + (char_to_int(b2) & 0x0F);
};
int voltage = (((((asciitochar(raw_data[6], raw_data[7]) << 8) + asciitochar(raw_data[4], raw_data[5])) << 8) + asciitochar(raw_data[2], raw_data[3])) << 8) + asciitochar(raw_data[0], raw_data[1]);
int current = (((((asciitochar(raw_data[14], raw_data[15]) << 8) + asciitochar(raw_data[12], raw_data[13])) << 8) + asciitochar(raw_data[10], raw_data[11])) << 8) + asciitochar(raw_data[8], raw_data[9]);
int soc = (asciitochar(raw_data[30], raw_data[31]) << 8) + asciitochar(raw_data[28], raw_data[29]);
int temperature = (asciitochar(raw_data[34], raw_data[35]) << 8) + asciitochar(raw_data[32], raw_data[33]);
int status = asciitochar(raw_data[36], raw_data[37]);
int afe_status = asciitochar(raw_data[40], raw_data[41]);
float temperature_celsius = (temperature - 0xAAB) / 10.0;
float voltage_display = voltage / 1000.0;
float current_display = current / 10.0;
id(global_voltage) = voltage_display;
id(global_current) = current_display;
id(global_soc) = soc;
id(global_temperature) = temperature_celsius;
id(global_status) = status;
id(global_afe_status) = afe_status;
} else {
ESP_LOGE("main", "Received raw data is too short.");
}
- platform: ble_scanner
name: "BLE Devices Scanner"
switch:
- <<: !include includes/include-generic-restart-switch.yaml
script:
- id: reconnect_ble
then:
- delay: 5s
- ble_client.connect: my_ble_client
- delay: 30s
- if:
condition:
lambda: |-
return !id(ble_connected);
then:
- script.execute: reconnect_ble
#https://community.home-assistant.io/t/powerxtreme-emergoplus-ble-battery
EDIT: Think I did needed to add:
- platform: ble_client
ble_client_id: my_ble_client
name: "BLE Service dump 3"
service_uuid: 'FFE0'
characteristic_uuid: 'FFE4'
notify: true
internal: true
And I commented out:
# - platform: ble_scanner
# name: "BLE Devices Scanner"
Because I thought that might fill the logs…
But still no readings. Other errors:
08:41:50][D][text_sensor:064]: 'BLE Service FFE0 Characteristic FFE4': Sending state '07D2'
[08:41:50][E][main:229]: Received raw data is too short.
[08:41:50][D][text_sensor:064]: 'BLE Service dump 3': Sending state '07D2'
[08:41:50][D][esp32_ble_client:110]: [0] [30:55:44:36:66:7D] ESP_GATTC_NOTIFY_EVT
[08:41:51][D][text_sensor:064]: 'BLE Service FFE0 Characteristic FFE4': Sending state '^EF33000000000000'
[08:41:51][E][main:229]: Received raw data is too short.
[08:41:51][D][text_sensor:064]: 'BLE Service dump 3': Sending state '^EF33000000000000'
[08:41:51][D][esp32_ble_client:110]: [0] [30:55:44:36:66:7D] ESP_GATTC_NOTIFY_EVT
[08:41:51][D][text_sensor:064]: 'BLE Service FFE0 Characteristic FFE4': Sending state '3075000001005600'
[08:41:51][E][main:229]: Received raw data is too short.
[08:41:51][D][text_sensor:064]: 'BLE Service dump 3': Sending state '3075000001005600'
[08:41:51][D][esp32_ble_client:110]: [0] [30:55:44:36:66:7D] ESP_GATTC_NOTIFY_EVT
[08:41:51][D][text_sensor:064]: 'BLE Service FFE0 Characteristic FFE4': Sending state '660B00800C94'
[08:41:51][E][main:229]: Received raw data is too short.
[08:41:51][D][text_sensor:064]: 'BLE Service dump 3': Sending state '660B00800C94'
[08:41:51][D][esp32_ble_client:110]: [0] [30:55:44:36:66:7D] ESP_GATTC_NOTIFY_EVT
[08:41:51][D][text_sensor:064]: 'BLE Service FFE0 Characteristic FFE4': Sending state 'F80CFE0CFF0\xc3FA0C'
[08:41:51][E][main:229]: Received raw data is too short.
[08:41:51][D][text_sensor:064]: 'BLE Service dump 3': Sending state 'F80CFE0CFF0\xc3FA0C'
[08:41:51][D][esp32_ble_client:110]: [0] [30:55:44:36:66:7D] ESP_GATTC_NOTIFY_EVT
[08:41:51][D][text_sensor:064]: 'BLE Service FFE0 Characteristic FFE4': Sending state '0`00000000000000'
[08:41:51][E][main:229]: Received raw data is too short.
[08:41:51][D][text_sensor:064]: 'BLE Service dump 3': Sending state '0`00000000000000'
[08:41:51][D][esp32_ble_client:110]: [0] [30:55:44:36:66:7D] ESP_GATTC_NOTIFY_EVT
[08:41:51][D][text_sensor:064]: 'BLE Service FFE0 Characteristic FFE4': Sending state '0000000000000000'
[08:41:51][E][main:229]: Received raw data is too short.
[08:41:51][D][text_sensor:064]: 'BLE Service dump 3': Sending state '0000000000000000'
[08:41:51][D][esp32_ble_client:110]: [0] [30:55:44:36:66:7D] ESP_GATTC_NOTIFY_EVT
[08:41:51][D][text_sensor:064]: 'BLE Service FFE0 Characteristic FFE4': Sending state '00000`0000000000'
[08:41:51][E][main:229]: Received raw data is too short.
[08:41:51][D][text_sensor:064]: 'BLE Service dump 3': Sending state '00000`0000000000'
[08:41:51][D][esp32_ble_client:110]: [0] [30:55:44:36:66:7D] ESP_GATTC_NOTIFY_EVT
Flooded with a red error:
[08:41:50][E][main:229]: Received raw data is too short.
I really hope this will start working