The BMS condition tested most sensors with has_state(), which ESPHome sets on every publication, NaN included. A BMS that had published NaN once on a mandatory sensor (for example the PACE power template, which returns NaN when the current is unknown) stayed combinable, and the NaN corrupted the totals that drive the inverter. - Sensors are tested with !std::isnan(): a sensor's state starts at NaN, so it also covers a sensor that never published. - cell_ovp and cell_uvp use > 0, like total_voltage and battery_capacity. JK, KS and PACE packages declare them as a number, whose state starts at 0, so isnan() alone would combine the BMS before its protection settings are read. - Binary sensors keep has_state(), a bool cannot be NaN. The shunt condition only checked the switch and online_status. It now also checks the four values the combiner reads: voltage > 0, and current, power and soc with isnan(). A combined shunt replaces the BMS values, so a NaN there reached the inverter directly. Both conditions move from the binary sensor lambda to STEP 1, on the device's turn, and are published to "Can be combined". A template binary sensor with a lambda runs it on every loop, about 60 times per second per device, for a value read once per round. Design_decisions documents which test applies to which entity type, that every entity in the conditions is mandatory, and why they are evaluated in STEP 1.
259 lines
13 KiB
YAML
259 lines
13 KiB
YAML
# Updated : 2026.11.10
|
|
# Version : 1.8.2
|
|
# 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}
|
|
|
|
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})
|
|
{
|
|
// +-----------------------------------------------+
|
|
// | Can be combined |
|
|
// +-----------------------------------------------+
|
|
// Evaluated once per round, on this BMS's turn, and published to the
|
|
// binary sensor. A template lambda would run on every loop instead.
|
|
// Every entity below is mandatory and must hold a real value.
|
|
// Sensor: has_state() stays true after a NaN is published, so test isnan().
|
|
// > 0: zero is not a valid reading, and NaN fails the comparison. cell_ovp and
|
|
// cell_uvp may be a number, whose state starts at 0 instead of NaN.
|
|
// Binary sensor: has_state() is enough, a bool cannot be NaN.
|
|
const bool can_be_combined =
|
|
(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) &&
|
|
(!std::isnan(id(bms${bms_id}_current).state)) &&
|
|
(!std::isnan(id(bms${bms_id}_power).state)) &&
|
|
(!std::isnan(id(bms${bms_id}_battery_soc).state)) &&
|
|
(!std::isnan(id(bms${bms_id}_battery_soh).state)) &&
|
|
(id(bms${bms_id}_battery_capacity).state > 0) &&
|
|
(!std::isnan(id(bms${bms_id}_battery_capacity_remaining).state)) &&
|
|
(!std::isnan(id(bms${bms_id}_charging_cycles).state)) &&
|
|
(id(bms${bms_id}_max_charge_current).state >= 0) &&
|
|
(id(bms${bms_id}_max_discharge_current).state >= 0) &&
|
|
(!std::isnan(id(bms${bms_id}_min_cell_voltage).state)) &&
|
|
(!std::isnan(id(bms${bms_id}_max_cell_voltage).state)) &&
|
|
(!std::isnan(id(bms${bms_id}_min_voltage_cell).state)) &&
|
|
(!std::isnan(id(bms${bms_id}_max_voltage_cell).state)) &&
|
|
(!std::isnan(id(bms${bms_id}_min_temperature).state)) &&
|
|
(!std::isnan(id(bms${bms_id}_max_temperature).state)) &&
|
|
(!std::isnan(id(bms${bms_id}_min_temperature_sensor).state)) &&
|
|
(!std::isnan(id(bms${bms_id}_max_temperature_sensor).state)) &&
|
|
(id(bms${bms_id}_cell_ovp).state > 0) && // sensor or number
|
|
(id(bms${bms_id}_cell_uvp).state > 0) && // sensor or number
|
|
(!std::isnan(id(bms${bms_id}_balance_trigger_voltage).state)) &&
|
|
(!std::isnan(id(bms${bms_id}_yambms_errors_bitmask).state));
|
|
id(bms${bms_id}_can_be_combined).publish_state(can_be_combined);
|
|
|
|
// The BMS can be combined ?
|
|
if (can_be_combined)
|
|
{
|
|
// +-----------------------------------------------+
|
|
// | 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)++;
|
|
}
|