202 lines
6.9 KiB
YAML
202 lines
6.9 KiB
YAML
# Updated : 2026.03.23
|
|
# Version : 1.1.3
|
|
# GitHub : https://github.com/Sleeper85/esphome-yambms
|
|
|
|
# YamBMS ( Yet another multi-BMS Merging Solution )
|
|
|
|
# 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:
|
|
# --- CATEGORY DICTIONARY ---
|
|
# Use these human-readable names in component yaml substitutions, up to 16 categories
|
|
CAT_SYSTEM: "0"
|
|
CAT_BMS: "1"
|
|
CAT_INV_CAN_BUS: "2"
|
|
CAT_INV_RS485: "3"
|
|
CAT_SHUNT: "4"
|
|
CAT_BALANCER: "5"
|
|
CAT_NETWORK: "6"
|
|
|
|
enable_health_logging: 'false' # Set to true to see if component health for all component instances, logged every 60 seconds
|
|
|
|
globals:
|
|
|
|
# Tracks True/False fault status for every component instance
|
|
# Structure: [Component Category (0-15)] -> [Specific Instance ID (0-N)]
|
|
# 0 = Healthy, 1 = Fault, 2 = Unregistered
|
|
- id: global_instance_faults
|
|
type: std::vector<std::vector<uint8_t>>
|
|
initial_value: std::vector<std::vector<uint8_t>>(16)
|
|
restore_value: false
|
|
|
|
# Component Fault Bitmask: A 16-bit summary of the component instance health.
|
|
# If Bit 4 is "1", it means "At least one instance in Category 4 is failing."
|
|
- id: global_system_fault_mask
|
|
type: uint16_t
|
|
initial_value: '0'
|
|
restore_value: false
|
|
|
|
# -------------------------------------------------------
|
|
# Global Function: Update Fault Sensor
|
|
# -------------------------------------------------------
|
|
- id: global_update_health_sensor
|
|
type: "std::function<void()>"
|
|
initial_value: |-
|
|
[]() {
|
|
std::string json = "[";
|
|
for (int cat = 0; cat < 16; cat++) {
|
|
auto &faults = id(global_instance_faults)[cat];
|
|
if (cat > 0) json += ",";
|
|
json += "[";
|
|
for (size_t i = 0; i < faults.size(); i++) {
|
|
if (i > 0) json += ",";
|
|
// 0 = Healthy, 1= Fault, 2 = Unregistered
|
|
json += to_string(faults[i]);
|
|
}
|
|
json += "]";
|
|
}
|
|
json += "]";
|
|
id(sys_fault_data).publish_state(json);
|
|
}
|
|
|
|
# ---------------------------------------------------------
|
|
# Global Function: Update Fault Status
|
|
# ---------------------------------------------------------
|
|
- id: global_update_fault
|
|
type: std::function<void(int, int, bool)>
|
|
initial_value: |-
|
|
[](int category, int instance, bool is_failing) {
|
|
int computer_instance = instance - 1;
|
|
|
|
if (category < 0 || category >= 16 || computer_instance < 0) return;
|
|
|
|
auto &faults = id(global_instance_faults)[category];
|
|
|
|
// Resize with default value '2' (Unregistered).
|
|
if (computer_instance >= faults.size()) {
|
|
faults.resize(computer_instance + 1, 2);
|
|
}
|
|
|
|
// Convert input boolean to integer
|
|
// Input false (Healthy) -> 0
|
|
// Input true (Fault) -> 1
|
|
uint8_t new_state = is_failing ? 1 : 0;
|
|
|
|
// Update only if state changed
|
|
if (faults[computer_instance] != new_state) {
|
|
|
|
faults[computer_instance] = new_state;
|
|
id(global_update_health_sensor)();
|
|
|
|
// Update Master Mask
|
|
// State 2 (unregistered) is ignored
|
|
bool cat_has_fault = false;
|
|
for(uint8_t f : faults) {
|
|
if(f == 1) {
|
|
cat_has_fault = true;
|
|
break;
|
|
}
|
|
}
|
|
|
|
if(cat_has_fault) {
|
|
id(global_system_fault_mask) |= (1 << category);
|
|
} else {
|
|
id(global_system_fault_mask) &= ~(1 << category);
|
|
}
|
|
}
|
|
}
|
|
|
|
interval:
|
|
- interval: 60s
|
|
then:
|
|
- lambda: |-
|
|
// Check if logging is enabled
|
|
if (!${enable_health_logging}) return;
|
|
|
|
ESP_LOGI("health_rpt", "┌──────────────────────────────┐");
|
|
ESP_LOGI("health_rpt", "│ SYSTEM HEALTH REPORT │");
|
|
ESP_LOGI("health_rpt", "└──────────────────────────────┘");
|
|
|
|
bool system_clean = true;
|
|
|
|
// Iterate through all 16 Categories
|
|
for (int cat = 0; cat < 16; cat++) {
|
|
auto &faults = id(global_instance_faults)[cat];
|
|
if (faults.empty()) continue;
|
|
|
|
// Define Category Names
|
|
std::string cat_name;
|
|
switch(cat) {
|
|
case 0: cat_name = "API / MQTT "; break;
|
|
case 1: cat_name = "BMS "; break;
|
|
case 2: cat_name = "CAN->INV "; break;
|
|
case 3: cat_name = "RS485->INV "; break;
|
|
case 4: cat_name = "SHUNT "; break;
|
|
case 5: cat_name = "BALANCER "; break;
|
|
case 6: cat_name = "NETWORK "; break;
|
|
default: cat_name = "CAT_" + to_string(cat) + " "; break;
|
|
}
|
|
|
|
std::string status_line = "";
|
|
bool cat_has_fault = false;
|
|
bool cat_has_real_devices = false;
|
|
|
|
// Determine status
|
|
for (size_t i = 0; i < faults.size(); i++) {
|
|
uint8_t state = faults[i];
|
|
|
|
if (state == 0) {
|
|
// Healthy
|
|
status_line += "[ OK ] ";
|
|
cat_has_real_devices = true;
|
|
}
|
|
else if (state == 1) {
|
|
// Fault
|
|
status_line += "[FAIL] ";
|
|
cat_has_fault = true;
|
|
cat_has_real_devices = true;
|
|
system_clean = false;
|
|
}
|
|
else {
|
|
// Unregistered devices
|
|
status_line += "[ -- ] ";
|
|
}
|
|
}
|
|
|
|
// Log the component if instances exist
|
|
if (cat_has_real_devices) {
|
|
if (cat_has_fault) {
|
|
// Set to WARN if fault
|
|
ESP_LOGW("health_rpt", ">> %s: %s", cat_name.c_str(), status_line.c_str());
|
|
} else {
|
|
// Set to INFO if healthy
|
|
ESP_LOGI("health_rpt", " %s: %s", cat_name.c_str(), status_line.c_str());
|
|
}
|
|
}
|
|
}
|
|
|
|
// Footer
|
|
if (system_clean) {
|
|
ESP_LOGI("health_rpt", ">> ALL SYSTEMS NORMAL");
|
|
}
|
|
ESP_LOGI("health_rpt", "--------------------------------");
|
|
|
|
text_sensor:
|
|
# JSON sensor representing system fault data, use with markdown card in Home Assistant
|
|
- platform: template
|
|
name: "System Fault Data"
|
|
id: sys_fault_data
|
|
device_id: ${board_id}
|
|
entity_category: diagnostic
|