Code standardization (auto functions)

This commit is contained in:
Sleeper85 committed 2025-11-23 17:46:31 +01:00
1 parent 59ad20f447
commit af0ac536a2
4 files changed
+118 -118

No files matched your search

+27 -27
View File
@@ -1,5 +1,5 @@
# Updated : 2025.07.30
# Version : 1.2.5
# Updated : 2025.11.23
# Version : 1.2.6
# GitHub : https://github.com/Sleeper85/esphome-yambms
# YamBMS ( Yet another multi-BMS Merging Solution )
@@ -24,11 +24,11 @@ substitutions:
# Auto charge current control will automatically reduce the charging current as a cell reaches the BMS "cell_voltage_overvoltage_recovery".
# If unsure, leave the below settings as default, this will ensure charge current will taper correctly.
# Factor to control the end of the charge current curve. A setting below 2 will extend the curve, above 2 will shorten the curve.
charge_a_factor_curve_end: "2.0"
charge_a_factor_curve_end: '2.0'
# Factor to adjust the shape of the charge current curve.
# A setting below 1 will reduce current at a slow rate after the float voltage is reached, then increase as a cell reaches the BMS "cell_voltage_overvoltage_recovery".
# A factor above 1 will reduce current at a fast rate after the float voltage is reached, then slow as a cell reaches the BMS "cell_voltage_overvoltage_recovery".
charge_a_factor_curve_shape: "1.8"
charge_a_factor_curve_shape: '1.8'
switch:
- platform: template
@@ -51,25 +51,25 @@ sensor:
device_class: current
internal: true
filters:
- lambda: !lambda |-
// Variables
double max_charge_a = id(${yambms_id}_max_charge_current).state;
double cell_bulk_v = (id(${yambms_id}_bulk_voltage).state / id(${yambms_id}_cell_count).state);
double cell_float_v = (id(${yambms_id}_float_voltage).state / id(${yambms_id}_cell_count).state);
double cell_high_v = ((id(${yambms_id}_cell_ovp).state + cell_bulk_v) / 2);
- lambda: |-
// Variables
double max_charge_a = id(${yambms_id}_max_charge_current).state;
double cell_bulk_v = (id(${yambms_id}_bulk_voltage).state / id(${yambms_id}_cell_count).state);
double cell_float_v = (id(${yambms_id}_float_voltage).state / id(${yambms_id}_cell_count).state);
double cell_high_v = ((id(${yambms_id}_cell_ovp).state + cell_bulk_v) / 2);
// Auto Charge Current function
if (!id(${yambms_id}_switch_auto_ccl).state) {
// No change to charge current required
return 0.0;
} else if (id(${yambms_id}_max_cell_voltage).state > cell_high_v) {
// Set CCL delta to maximum as max cell voltage too high
return -max_charge_a;
} else {
// Set CCL delta to automatic value
return max(0.0,(- pow(${charge_a_factor_curve_end}, pow( max(0.0, ((x - cell_float_v) / (cell_high_v - cell_float_v))),${charge_a_factor_curve_shape}))+2) * max_charge_a)
- max_charge_a;
}
// Auto Charge Current function
if (!id(${yambms_id}_switch_auto_ccl).state) {
// No change to charge current required
return 0.0;
} else if (id(${yambms_id}_max_cell_voltage).state > cell_high_v) {
// Set CCL delta to maximum as max cell voltage too high
return -max_charge_a;
} else {
// Set CCL delta to automatic value
return max(0.0,(- pow(${charge_a_factor_curve_end}, pow( max(0.0, ((x - cell_float_v) / (cell_high_v - cell_float_v))),${charge_a_factor_curve_shape}))+2) * max_charge_a)
- max_charge_a;
}
# Second, an exponential moving average is calculated. This will be used to smooth the transition between different initial charge current values.
- platform: copy
@@ -96,9 +96,9 @@ sensor:
accuracy_decimals: 1
internal: true
filters:
- lambda: !lambda |-
// Variables
double auto_ccl = round((min(id(auto_ccl_moving_average).state, x) * 10) / 10);
- lambda: |-
// Variables
double auto_ccl = round((min(id(auto_ccl_moving_average).state, x) * 10) / 10);
id(${yambms_id}_auto_ccl) = auto_ccl;
return auto_ccl;
id(${yambms_id}_auto_ccl) = auto_ccl;
return auto_ccl;
+61 -61
View File
@@ -1,5 +1,5 @@
# Updated : 2025.06.10
# Version : 2.4.3
# Updated : 2025.11.23
# Version : 2.4.4
# GitHub : https://github.com/Sleeper85/esphome-yambms
# YamBMS ( Yet another multi-BMS Merging Solution )
@@ -23,10 +23,10 @@ substitutions:
# +--------------------------------------+
# Factor to adjust the proportional gain of the auto voltage control PI logic.
# The higher the setting, the faster the correcting response. Too high a value will cause oscillation.
charge_v_kp: "0.05"
charge_v_kp: '0.05'
# Factor to adjust the integral gain of the auto voltage control PI logic.
# The higher the setting, the more the correction response will be increased over time. Too high a value will cause oscillation.
charge_v_ki: "0.5"
charge_v_ki: '0.5'
switch:
- platform: template
@@ -62,61 +62,61 @@ sensor:
unit_of_measurement: V
device_class: voltage
internal: true
lambda: |-
// Variables
double static integral = 0.0; // Integral term initialization
double static prev_integral = 0.0; // Prior Integral term initialization
double cell_bulk_v = (id(${yambms_id}_bulk_voltage).state / id(${yambms_id}_cell_count).state);
double balance_delta = id(${yambms_id}_balance_trigger_voltage).state;
double max_allowed_voltage = cell_bulk_v + balance_delta;
double max_cell_v = id(${yambms_id}_max_cell_voltage).state;
double max_CVL = id(${yambms_id}_bulk_voltage).state + id(${yambms_id}_auto_cvl_boost_charge_v).state; // Maximum CVL
double min_CVL = id(${yambms_id}_float_voltage).state; // Minimum CVL
double CVL = id(${yambms_id}_bulk_voltage).state; // Starting CVL
double CVL_orig = id(${yambms_id}_bulk_voltage).state; // Starting CVL snapshot
double kp = ${charge_v_kp}; // Proportional gain, adjust as needed
double ki = ${charge_v_ki}; // Integral gain, adjust as needed
double dt = 1.0; // Time step, adjust based on update_interval
double error = 0.0; // Starting error
double output = 0.0; // Starting output
// Check feature enabled
if (!id(${yambms_id}_switch_auto_cvl).state){
id(${yambms_id}_auto_cvl) = 0.0;
return id(${yambms_id}_bulk_voltage).state;
} else {
// PI Algorithm
error = cell_bulk_v - max_cell_v;
// It's possible for PI control to start before BMS values are available, so don't store non-number values
if(isnan(prev_integral)) {
prev_integral = 0;
integral = 0;
}
// Check if max cell voltage is in deadband
if ((max_cell_v <= max_allowed_voltage) && (max_cell_v >= cell_bulk_v)) {
// No integral error within deadband zone
} else {
integral = prev_integral + error * dt;
}
// Calculate PI output
output = kp * error + ki * integral;
// Update CVL
CVL += output;
// If CVL is less than lower limit, enforce limit
if (CVL < min_CVL) {
CVL = min_CVL;
}
// If CVL is greater than upper limit, enforce limit
else if (CVL > max_CVL) {
CVL = max_CVL;
}
else {
prev_integral = integral;
}
id(${yambms_id}_auto_cvl) = CVL - CVL_orig;
return CVL - CVL_orig;
}
filters:
- round: 1
- round: 1
lambda: |-
// Variables
double static integral = 0.0; // Integral term initialization
double static prev_integral = 0.0; // Prior Integral term initialization
double cell_bulk_v = (id(${yambms_id}_bulk_voltage).state / id(${yambms_id}_cell_count).state);
double balance_delta = id(${yambms_id}_balance_trigger_voltage).state;
double max_allowed_voltage = cell_bulk_v + balance_delta;
double max_cell_v = id(${yambms_id}_max_cell_voltage).state;
double max_CVL = id(${yambms_id}_bulk_voltage).state + id(${yambms_id}_auto_cvl_boost_charge_v).state; // Maximum CVL
double min_CVL = id(${yambms_id}_float_voltage).state; // Minimum CVL
double CVL = id(${yambms_id}_bulk_voltage).state; // Starting CVL
double CVL_orig = id(${yambms_id}_bulk_voltage).state; // Starting CVL snapshot
double kp = ${charge_v_kp}; // Proportional gain, adjust as needed
double ki = ${charge_v_ki}; // Integral gain, adjust as needed
double dt = 1.0; // Time step, adjust based on update_interval
double error = 0.0; // Starting error
double output = 0.0; // Starting output
// Check feature enabled
if (!id(${yambms_id}_switch_auto_cvl).state){
id(${yambms_id}_auto_cvl) = 0.0;
return id(${yambms_id}_bulk_voltage).state;
} else {
// PI Algorithm
error = cell_bulk_v - max_cell_v;
// It's possible for PI control to start before BMS values are available, so don't store non-number values
if(isnan(prev_integral)) {
prev_integral = 0;
integral = 0;
}
// Check if max cell voltage is in deadband
if ((max_cell_v <= max_allowed_voltage) && (max_cell_v >= cell_bulk_v)) {
// No integral error within deadband zone
} else {
integral = prev_integral + error * dt;
}
// Calculate PI output
output = kp * error + ki * integral;
// Update CVL
CVL += output;
// If CVL is less than lower limit, enforce limit
if (CVL < min_CVL) {
CVL = min_CVL;
}
// If CVL is greater than upper limit, enforce limit
else if (CVL > max_CVL) {
CVL = max_CVL;
}
else {
prev_integral = integral;
}
id(${yambms_id}_auto_cvl) = CVL - CVL_orig;
return CVL - CVL_orig;
}
+26 -26
View File
@@ -1,5 +1,5 @@
# Updated : 2025.07.30
# Version : 1.2.5
# Updated : 2025.11.23
# Version : 1.2.6
# GitHub : https://github.com/Sleeper85/esphome-yambms
# YamBMS ( Yet another multi-BMS Merging Solution )
@@ -24,13 +24,13 @@ substitutions:
# Auto discharge current control will automatically reduce the charging current as a cell reaches the BMS "cell_voltage_undervoltage_recovery".
# If unsure, leave the below settings as default, this will ensure discharge current will taper correctly.
# Factor to control the end of the discharge current curve. A setting below 2 will extend the curve, above 2 will shorten the curve.
discharge_a_factor_curve_end: "2.0"
discharge_a_factor_curve_end: '2.0'
# Factor to adjust the shape of the charge current curve.
# A setting below 1 will reduce current at a slow rate after the float voltage is reached, then increase as a cell reaches the BMS "cell_voltage_undervoltage_recovery".
# A factor above 1 will reduce current at a fast rate after the float voltage is reached, then slow as a cell reaches the BMS "cell_voltage_undervoltage_recovery".
discharge_a_factor_curve_shape: "1.8"
discharge_a_factor_curve_shape: '1.8'
# Discharge knee voltage, the point at which a cell will reduce in voltage more rapidly. For LiFePO4 cells, this is generally considered to be 3.1v.
discharge_knee_v: "3.1"
discharge_knee_v: '3.1'
switch:
- platform: template
@@ -53,23 +53,23 @@ sensor:
device_class: current
internal: true
filters:
- lambda: !lambda |-
// Variables
double max_discharge_a = id(${yambms_id}_max_discharge_current).state;
double cell_low_v = id(${yambms_id}_cell_uvp).state + 0.2;
- lambda: |-
// Variables
double max_discharge_a = id(${yambms_id}_max_discharge_current).state;
double cell_low_v = id(${yambms_id}_cell_uvp).state + 0.2;
// Auto Discharge Current function
if (!id(${yambms_id}_switch_auto_dcl).state) {
// No change to discharge current required
return 0.0;
} else if (id(${yambms_id}_min_cell_voltage).state < cell_low_v) {
// Set DCL delta to maximum as min cell voltage too low
return -max_discharge_a;
} else {
// Set DCL delta to automatic value
return max(0.0,(- pow(${discharge_a_factor_curve_end}, pow( max(0.0, ((x - ${discharge_knee_v}) / (cell_low_v - ${discharge_knee_v}))),${discharge_a_factor_curve_shape}))+2) * max_discharge_a)
- max_discharge_a;
}
// Auto Discharge Current function
if (!id(${yambms_id}_switch_auto_dcl).state) {
// No change to discharge current required
return 0.0;
} else if (id(${yambms_id}_min_cell_voltage).state < cell_low_v) {
// Set DCL delta to maximum as min cell voltage too low
return -max_discharge_a;
} else {
// Set DCL delta to automatic value
return max(0.0,(- pow(${discharge_a_factor_curve_end}, pow( max(0.0, ((x - ${discharge_knee_v}) / (cell_low_v - ${discharge_knee_v}))),${discharge_a_factor_curve_shape}))+2) * max_discharge_a)
- max_discharge_a;
}
# Second, an exponential moving average is calculated. This will be used to smooth the transition between different initial discharge current values.
- platform: copy
@@ -96,9 +96,9 @@ sensor:
accuracy_decimals: 1
internal: true
filters:
- lambda: !lambda |-
// Variables
double auto_dcl = round((min(id(auto_dcl_moving_average).state, x) * 10) / 10);
- lambda: |-
// Variables
double auto_dcl = round((min(id(auto_dcl_moving_average).state, x) * 10) / 10);
id(${yambms_id}_auto_dcl) = auto_dcl;
return auto_dcl;
id(${yambms_id}_auto_dcl) = auto_dcl;
return auto_dcl;
+4 -4
View File
@@ -1,5 +1,5 @@
# Updated : 2025.11.16
# Version : 1.1.2
# Updated : 2025.11.23
# Version : 1.1.3
# GitHub : https://github.com/Sleeper85/esphome-yambms
# YamBMS ( Yet another multi-BMS Merging Solution )
@@ -22,10 +22,10 @@ substitutions:
# | Use the settings below with caution |
# +--------------------------------------+
# Alpha value for the exponential moving average filter.
required_charge_current_alpha: "0.25"
required_charge_current_alpha: '0.25'
# Filter delay in minutes. The charge current will not be updated more often than this value.
# This is to prevent the charge current from fluctuating too much.
required_charge_current_filter_delay: "3"
required_charge_current_filter_delay: '3'
switch:
- platform: template