From bae6958e8c260a30be3f7d1be323b7e37630aa47 Mon Sep 17 00:00:00 2001 From: Sebastian Muszynski Date: Sun, 27 Mar 2022 15:16:32 +0200 Subject: [PATCH] Introduce min/max voltage cell sensors (Closes: #59) (#61) --- components/jk_bms/jk_bms.cpp | 12 +++++++++++- components/jk_bms/jk_bms.h | 8 ++++++++ components/jk_bms/sensor.py | 21 +++++++++++++++++++++ esp32-example.yaml | 4 ++++ esp8266-example.yaml | 4 ++++ 5 files changed, 48 insertions(+), 1 deletion(-) diff --git a/components/jk_bms/jk_bms.cpp b/components/jk_bms/jk_bms.cpp index 3bde38fd..cc1fea90 100644 --- a/components/jk_bms/jk_bms.cpp +++ b/components/jk_bms/jk_bms.cpp @@ -90,19 +90,25 @@ void JkBms::on_status_data_(const std::vector &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; @@ -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_); diff --git a/components/jk_bms/jk_bms.h b/components/jk_bms/jk_bms.h index 66220ba9..129d51d2 100644 --- a/components/jk_bms/jk_bms.h +++ b/components/jk_bms/jk_bms.h @@ -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; } @@ -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_; diff --git a/components/jk_bms/sensor.py b/components/jk_bms/sensor.py index 7cfc71ca..5a3259cd 100644 --- a/components/jk_bms/sensor.py +++ b/components/jk_bms/sensor.py @@ -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" @@ -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" @@ -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, @@ -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, diff --git a/esp32-example.yaml b/esp32-example.yaml index 34e47d6a..1d7641bb 100644 --- a/esp32-example.yaml +++ b/esp32-example.yaml @@ -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: diff --git a/esp8266-example.yaml b/esp8266-example.yaml index b2a27eff..e68434fc 100644 --- a/esp8266-example.yaml +++ b/esp8266-example.yaml @@ -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: