Skip to content

Commit

Permalink
Introduce min/max voltage cell sensors (Closes: #59) (#61)
Browse files Browse the repository at this point in the history
  • Loading branch information
syssi committed Mar 27, 2022
1 parent 2e60bef commit bae6958
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 1 deletion.
12 changes: 11 additions & 1 deletion components/jk_bms/jk_bms.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,19 +90,25 @@ void JkBms::on_status_data_(const std::vector<uint8_t> &data) {

float min_cell_voltage = 100.0f;
float max_cell_voltage = -100.0f;
uint8_t min_voltage_cell = 0;
uint8_t max_voltage_cell = 0;
for (uint8_t i = 0; i < cells; i++) {
float cell_voltage = (float) jk_get_16bit(i * 3 + 3) * 0.001f;
if (cell_voltage < min_cell_voltage) {
min_cell_voltage = cell_voltage;
min_voltage_cell = i + 1;
}
if (cell_voltage > max_cell_voltage) {
max_cell_voltage = cell_voltage;
max_voltage_cell = i + 1;
}
this->publish_state_(this->cells_[i].cell_voltage_sensor_, cell_voltage);
}

this->publish_state_(this->min_cell_voltage_sensor_, min_cell_voltage);
this->publish_state_(this->max_cell_voltage_sensor_, max_cell_voltage);
this->publish_state_(this->max_voltage_cell_sensor_, (float) max_voltage_cell);
this->publish_state_(this->min_voltage_cell_sensor_, (float) min_voltage_cell);
this->publish_state_(this->delta_cell_voltage_sensor_, max_cell_voltage - min_cell_voltage);

uint16_t offset = data[1] + 3;
Expand Down Expand Up @@ -468,7 +474,11 @@ std::string JkBms::mode_bits_to_string_(const uint16_t mask) {
void JkBms::dump_config() { // NOLINT(google-readability-function-size,readability-function-size)
ESP_LOGCONFIG(TAG, "JkBms:");
ESP_LOGCONFIG(TAG, " Address: 0x%02X", this->address_);

LOG_SENSOR("", "Minimum Cell Voltage", this->min_cell_voltage_sensor_);
LOG_SENSOR("", "Maximum Cell Voltage", this->max_cell_voltage_sensor_);
LOG_SENSOR("", "Minimum Voltage Cell", this->min_voltage_cell_sensor_);
LOG_SENSOR("", "Maximum Voltage Cell", this->max_voltage_cell_sensor_);
LOG_SENSOR("", "Delta Cell Voltage", this->delta_cell_voltage_sensor_);
LOG_SENSOR("", "Cell Voltage 1", this->cells_[0].cell_voltage_sensor_);
LOG_SENSOR("", "Cell Voltage 2", this->cells_[1].cell_voltage_sensor_);
LOG_SENSOR("", "Cell Voltage 3", this->cells_[2].cell_voltage_sensor_);
Expand Down
8 changes: 8 additions & 0 deletions components/jk_bms/jk_bms.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@ class JkBms : public PollingComponent, public jk_modbus::JkModbusDevice {
void set_max_cell_voltage_sensor(sensor::Sensor *max_cell_voltage_sensor) {
max_cell_voltage_sensor_ = max_cell_voltage_sensor;
}
void set_min_voltage_cell_sensor(sensor::Sensor *min_voltage_cell_sensor) {
min_voltage_cell_sensor_ = min_voltage_cell_sensor;
}
void set_max_voltage_cell_sensor(sensor::Sensor *max_voltage_cell_sensor) {
max_voltage_cell_sensor_ = max_voltage_cell_sensor;
}
void set_delta_cell_voltage_sensor(sensor::Sensor *delta_cell_voltage_sensor) {
delta_cell_voltage_sensor_ = delta_cell_voltage_sensor;
}
Expand Down Expand Up @@ -221,6 +227,8 @@ class JkBms : public PollingComponent, public jk_modbus::JkModbusDevice {
protected:
sensor::Sensor *min_cell_voltage_sensor_;
sensor::Sensor *max_cell_voltage_sensor_;
sensor::Sensor *min_voltage_cell_sensor_;
sensor::Sensor *max_voltage_cell_sensor_;
sensor::Sensor *delta_cell_voltage_sensor_;
sensor::Sensor *power_tube_temperature_sensor_;
sensor::Sensor *temperature_sensor_1_sensor_;
Expand Down
21 changes: 21 additions & 0 deletions components/jk_bms/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@

CONF_MIN_CELL_VOLTAGE = "min_cell_voltage"
CONF_MAX_CELL_VOLTAGE = "max_cell_voltage"
CONF_MIN_VOLTAGE_CELL = "min_voltage_cell"
CONF_MAX_VOLTAGE_CELL = "max_voltage_cell"
CONF_DELTA_CELL_VOLTAGE = "delta_cell_voltage"
CONF_CELL_VOLTAGE_1 = "cell_voltage_1"
CONF_CELL_VOLTAGE_2 = "cell_voltage_2"
Expand Down Expand Up @@ -121,6 +123,9 @@
CONF_ACTUAL_BATTERY_CAPACITY = "actual_battery_capacity"
CONF_PROTOCOL_VERSION = "protocol_version"

ICON_MIN_VOLTAGE_CELL = "mdi:battery-minus-outline"
ICON_MAX_VOLTAGE_CELL = "mdi:battery-plus-outline"

ICON_BATTERY_STRINGS = "mdi:car-battery"
ICON_CAPACITY_REMAINING = "mdi:battery-50"
ICON_CAPACITY_REMAINING_DERIVED = "mdi:battery-50"
Expand Down Expand Up @@ -166,6 +171,8 @@
SENSORS = [
CONF_MIN_CELL_VOLTAGE,
CONF_MAX_CELL_VOLTAGE,
CONF_MIN_VOLTAGE_CELL,
CONF_MAX_VOLTAGE_CELL,
CONF_DELTA_CELL_VOLTAGE,
CONF_POWER_TUBE_TEMPERATURE,
CONF_TEMPERATURE_SENSOR_1,
Expand Down Expand Up @@ -236,6 +243,20 @@
device_class=DEVICE_CLASS_VOLTAGE,
state_class=STATE_CLASS_MEASUREMENT,
),
cv.Optional(CONF_MIN_VOLTAGE_CELL): sensor.sensor_schema(
unit_of_measurement=UNIT_EMPTY,
icon=ICON_MIN_VOLTAGE_CELL,
accuracy_decimals=0,
device_class=DEVICE_CLASS_EMPTY,
state_class=STATE_CLASS_MEASUREMENT,
),
cv.Optional(CONF_MAX_VOLTAGE_CELL): sensor.sensor_schema(
unit_of_measurement=UNIT_EMPTY,
icon=ICON_MAX_VOLTAGE_CELL,
accuracy_decimals=0,
device_class=DEVICE_CLASS_EMPTY,
state_class=STATE_CLASS_MEASUREMENT,
),
cv.Optional(CONF_DELTA_CELL_VOLTAGE): sensor.sensor_schema(
unit_of_measurement=UNIT_VOLT,
icon=ICON_EMPTY,
Expand Down
4 changes: 4 additions & 0 deletions esp32-example.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ sensor:
name: "${name} min cell voltage"
max_cell_voltage:
name: "${name} max cell voltage"
min_voltage_cell:
name: "${name} min voltage cell"
max_voltage_cell:
name: "${name} max voltage cell"
delta_cell_voltage:
name: "${name} delta cell voltage"
cell_voltage_1:
Expand Down
4 changes: 4 additions & 0 deletions esp8266-example.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ sensor:
name: "${name} min cell voltage"
max_cell_voltage:
name: "${name} max cell voltage"
min_voltage_cell:
name: "${name} min voltage cell"
max_voltage_cell:
name: "${name} max voltage cell"
delta_cell_voltage:
name: "${name} delta cell voltage"
cell_voltage_1:
Expand Down

0 comments on commit bae6958

Please sign in to comment.