252 lines
12 KiB
YAML
252 lines
12 KiB
YAML
# Updated : 2026.11.05
|
|
# Version : 1.8.1
|
|
# GitHub : https://github.com/Sleeper85/esphome-yambms
|
|
|
|
# This YAML is free software: you can redistribute it and/or
|
|
# modify it under the terms of the GNU General Public License
|
|
# as published by the Free Software Foundation, either version 3
|
|
# of the License, or (at your option) any later version.
|
|
#
|
|
# This program is distributed in the hope that it will be useful,
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
# See the GNU General Public License for more details.
|
|
#
|
|
# You should have received a copy of the GNU General Public License
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/gpl.html>.
|
|
|
|
substitutions:
|
|
bms_model: "Unknown"
|
|
bms_protocol: "Unknown"
|
|
|
|
globals:
|
|
# Last valid battery_capacity this BMS contributed to the installed total
|
|
- id: bms${bms_id}_installed_capacity
|
|
type: float
|
|
restore_value: no
|
|
initial_value: "0.0"
|
|
- id: bms${bms_id}_combined
|
|
type: bool
|
|
restore_value: no
|
|
initial_value: "false"
|
|
|
|
# Combine once without conditions
|
|
esphome:
|
|
on_boot:
|
|
- priority: 600
|
|
then:
|
|
- lambda: |-
|
|
id(yambms${yambms_id}_var_bms_counter) += 1;
|
|
id(yambms${yambms_id}_var_bms_models) += "${bms_model},";
|
|
id(yambms${yambms_id}_var_bms_protocols) += "${bms_protocol},";
|
|
|
|
binary_sensor:
|
|
# Indicates whether the BMS can be combined
|
|
- platform: template
|
|
name: "Can be combined"
|
|
id: bms${bms_id}_can_be_combined
|
|
device_id: bms_${bms_id}
|
|
lambda: |-
|
|
// Ensure that the sensors have a correct value and not NAN
|
|
if ((id(bms${bms_id}_online_status).has_state()) && // binary_sensor
|
|
(id(bms${bms_id}_online_status).state) && // binary_sensor
|
|
(id(bms${bms_id}_charging_allowed).has_state()) && // binary_sensor
|
|
(id(bms${bms_id}_discharging_allowed).has_state()) && // binary_sensor
|
|
(id(bms${bms_id}_equalizing).has_state()) && // binary_sensor
|
|
(id(bms${bms_id}_total_voltage).state > 0) &&
|
|
(id(bms${bms_id}_current).has_state()) &&
|
|
(id(bms${bms_id}_power).has_state()) &&
|
|
(id(bms${bms_id}_battery_soc).has_state()) &&
|
|
(id(bms${bms_id}_battery_soh).has_state()) &&
|
|
(id(bms${bms_id}_battery_capacity).state > 0) &&
|
|
(id(bms${bms_id}_battery_capacity_remaining).has_state()) &&
|
|
(id(bms${bms_id}_charging_cycles).has_state()) &&
|
|
(id(bms${bms_id}_max_charge_current).state >= 0) &&
|
|
(id(bms${bms_id}_max_discharge_current).state >= 0) &&
|
|
(id(bms${bms_id}_min_cell_voltage).has_state()) &&
|
|
(id(bms${bms_id}_max_cell_voltage).has_state()) &&
|
|
(id(bms${bms_id}_min_voltage_cell).has_state()) &&
|
|
(id(bms${bms_id}_max_voltage_cell).has_state()) &&
|
|
(id(bms${bms_id}_min_temperature).has_state()) &&
|
|
(id(bms${bms_id}_max_temperature).has_state()) &&
|
|
(id(bms${bms_id}_min_temperature_sensor).has_state()) &&
|
|
(id(bms${bms_id}_max_temperature_sensor).has_state()) &&
|
|
(id(bms${bms_id}_cell_ovp).has_state()) &&
|
|
(id(bms${bms_id}_cell_uvp).has_state()) &&
|
|
(id(bms${bms_id}_balance_trigger_voltage).has_state()) &&
|
|
(id(bms${bms_id}_yambms_errors_bitmask).has_state()))
|
|
{
|
|
return true;
|
|
}
|
|
else return false;
|
|
|
|
interval:
|
|
- interval: ${yambms_update_interval}
|
|
then:
|
|
- lambda: |-
|
|
// +-----------------------------------------------+
|
|
// | STEP 1 - BMS combiner |
|
|
// +-----------------------------------------------+
|
|
// BMS number to combine ?
|
|
if (id(yambms${yambms_id}_var_to_combine_bms_number) == ${bms_id})
|
|
{
|
|
// The BMS can be combined ?
|
|
if (id(bms${bms_id}_can_be_combined).state)
|
|
{
|
|
// +-----------------------------------------------+
|
|
// | Installed battery capacity contribution |
|
|
// +-----------------------------------------------+
|
|
// The installed total tracks a hot capacity change, but is never
|
|
// reduced when a BMS is uncombined : an offline BMS simply stops
|
|
// updating its contribution, which stays in the total.
|
|
|
|
float capacity = id(bms${bms_id}_battery_capacity).state;
|
|
|
|
if (capacity != id(bms${bms_id}_installed_capacity))
|
|
{
|
|
id(yambms${yambms_id}_var_total_installed_battery_capacity) += capacity - id(bms${bms_id}_installed_capacity);
|
|
id(bms${bms_id}_installed_capacity) = capacity;
|
|
}
|
|
|
|
// +-----------------------------------------------+
|
|
// | Combine not continuously |
|
|
// +-----------------------------------------------+
|
|
|
|
if (id(bms${bms_id}_combined) == false)
|
|
{
|
|
// To combine bms counter
|
|
id(yambms${yambms_id}_var_to_combine_bms_counter) += 1;
|
|
|
|
// Combined
|
|
id(bms${bms_id}_combined) = true;
|
|
}
|
|
|
|
// +-----------------------------------------------+
|
|
// | Combine continuously |
|
|
// +-----------------------------------------------+
|
|
|
|
// Online bitmask
|
|
id(yambms${yambms_id}_var_bms_online_bitmask) |= (1 << (${bms_id} - 1));
|
|
// Remember that we've seen this BMS online
|
|
id(yambms${yambms_id}_var_bms_seen_online_bitmask) |= id(yambms${yambms_id}_var_bms_online_bitmask);
|
|
|
|
// YamBMS Errors Bitmask (16bit)
|
|
if (id(bms${bms_id}_yambms_errors_bitmask).state > 0)
|
|
{
|
|
uint16_t errors_bitmask = id(bms${bms_id}_yambms_errors_bitmask).state; // errors_bitmask (16bit)
|
|
id(yambms${yambms_id}_var_errors_bitmask) |= errors_bitmask; // OR bitwise
|
|
id(yambms${yambms_id}_var_errors_bitmask_counter) += 1; // errors_bitmask_counter
|
|
}
|
|
|
|
if (id(bms${bms_id}_charging_allowed).state)
|
|
{
|
|
// TOTAL charging_allowed_counter
|
|
id(yambms${yambms_id}_var_charging_allowed_counter) += 1;
|
|
// TOTAL max_charge_current
|
|
id(yambms${yambms_id}_var_total_max_charge_current) += id(bms${bms_id}_max_charge_current).state;
|
|
}
|
|
// TOTAL bms_blocking_charge_counter
|
|
else id(yambms${yambms_id}_var_bms_blocking_charge_counter) += 1;
|
|
|
|
if (id(bms${bms_id}_discharging_allowed).state)
|
|
{
|
|
// TOTAL discharging_allowed_counter
|
|
id(yambms${yambms_id}_var_discharging_allowed_counter) += 1;
|
|
// TOTAL max_discharge_current
|
|
id(yambms${yambms_id}_var_total_max_discharge_current) += id(bms${bms_id}_max_discharge_current).state;
|
|
}
|
|
// TOTAL bms_blocking_discharge_counter
|
|
else id(yambms${yambms_id}_var_bms_blocking_discharge_counter) += 1;
|
|
|
|
// TOTAL total_voltage
|
|
id(yambms${yambms_id}_var_bms_total_voltage) += id(bms${bms_id}_total_voltage).state;
|
|
|
|
// TOTAL current
|
|
id(yambms${yambms_id}_var_bms_total_current) += id(bms${bms_id}_current).state;
|
|
|
|
// TOTAL power
|
|
id(yambms${yambms_id}_var_bms_total_power) += id(bms${bms_id}_power).state;
|
|
|
|
// TOTAL battery_capacity
|
|
id(yambms${yambms_id}_var_total_battery_capacity) += id(bms${bms_id}_battery_capacity).state;
|
|
|
|
// TOTAL battery_capacity_remaining
|
|
id(yambms${yambms_id}_var_total_battery_capacity_remaining) += id(bms${bms_id}_battery_capacity_remaining).state;
|
|
|
|
// TOTAL SoC
|
|
// id(yambms${yambms_id}_var_bms_total_soc) += id(bms${bms_id}_battery_soc).state;
|
|
|
|
// TOTAL SoH
|
|
id(yambms${yambms_id}_var_bms_total_soh) += id(bms${bms_id}_battery_soh).state;
|
|
|
|
// TOTAL charging_cycles
|
|
id(yambms${yambms_id}_var_total_charging_cycles) += id(bms${bms_id}_charging_cycles).state;
|
|
|
|
// TOTAL equalizing_counter
|
|
if (id(bms${bms_id}_equalizing).state) id(yambms${yambms_id}_var_equalizing_counter) += 1;
|
|
|
|
// MIN/MAX values
|
|
// MAX balance_trigger_voltage
|
|
if (id(bms${bms_id}_balance_trigger_voltage).state > id(yambms${yambms_id}_var_max_balance_trigger_voltage))
|
|
id(yambms${yambms_id}_var_max_balance_trigger_voltage) = id(bms${bms_id}_balance_trigger_voltage).state;
|
|
|
|
// MIN min_cell_voltage
|
|
// MIN min_voltage_cell (cell number)
|
|
if (id(bms${bms_id}_min_cell_voltage).state < id(yambms${yambms_id}_var_min_cell_voltage))
|
|
{
|
|
id(yambms${yambms_id}_var_min_cell_voltage) = id(bms${bms_id}_min_cell_voltage).state;
|
|
id(yambms${yambms_id}_var_min_voltage_cell) = id(bms${bms_id}_min_voltage_cell).state + (${bms_id} * 100);
|
|
}
|
|
|
|
// MAX max_cell_voltage
|
|
// MAX max_voltage_cell (cell number)
|
|
if (id(bms${bms_id}_max_cell_voltage).state > id(yambms${yambms_id}_var_max_cell_voltage))
|
|
{
|
|
id(yambms${yambms_id}_var_max_cell_voltage) = id(bms${bms_id}_max_cell_voltage).state;
|
|
id(yambms${yambms_id}_var_max_voltage_cell) = id(bms${bms_id}_max_voltage_cell).state + (${bms_id} * 100);
|
|
}
|
|
|
|
// MIN cell_ovp
|
|
if (id(bms${bms_id}_cell_ovp).state < id(yambms${yambms_id}_var_min_cell_ovp))
|
|
id(yambms${yambms_id}_var_min_cell_ovp) = id(bms${bms_id}_cell_ovp).state;
|
|
|
|
// MAX cell_uvp
|
|
if (id(bms${bms_id}_cell_uvp).state > id(yambms${yambms_id}_var_max_cell_uvp))
|
|
id(yambms${yambms_id}_var_max_cell_uvp) = id(bms${bms_id}_cell_uvp).state;
|
|
|
|
// MIN temperature
|
|
// MIN temperature_sensor
|
|
if (id(bms${bms_id}_min_temperature).state < id(yambms${yambms_id}_var_min_temperature))
|
|
{
|
|
id(yambms${yambms_id}_var_min_temperature) = id(bms${bms_id}_min_temperature).state;
|
|
id(yambms${yambms_id}_var_min_temperature_sensor) = id(bms${bms_id}_min_temperature_sensor).state;
|
|
}
|
|
|
|
// MAX temperature
|
|
// MAX temperature_sensor
|
|
if (id(bms${bms_id}_max_temperature).state > id(yambms${yambms_id}_var_max_temperature))
|
|
{
|
|
id(yambms${yambms_id}_var_max_temperature) = id(bms${bms_id}_max_temperature).state;
|
|
id(yambms${yambms_id}_var_max_temperature_sensor) = id(bms${bms_id}_max_temperature_sensor).state;
|
|
}
|
|
}
|
|
|
|
// +-----------------------------------------------+
|
|
// | Uncombine not continuously |
|
|
// +-----------------------------------------------+
|
|
else if (id(bms${bms_id}_combined) == true)
|
|
{
|
|
// To combine bms counter
|
|
id(yambms${yambms_id}_var_to_combine_bms_counter) -= 1;
|
|
|
|
// Uncombined
|
|
id(bms${bms_id}_combined) = false;
|
|
|
|
// Remove from bitmask
|
|
id(yambms${yambms_id}_var_bms_online_bitmask) &= ~(1 << (${bms_id} - 1));
|
|
}
|
|
|
|
// Increment the counter for next BMS processing
|
|
id(yambms${yambms_id}_var_to_combine_bms_number)++;
|
|
}
|