Hi, I managed to obtain, thanks to chatGpt, the list of errors of my inverter with this code.
Unfortunately the list is long and contains 240 IDs and I don’t want to make the main yaml unreadable. Is there a way to write the list into another external yaml file or text file or whatever and read it with the same code?
text_sensor:
- platform: modbus_controller
modbus_controller_id: zcs
name: Messaggio Errore
id: fault
register_type: holding
address: 0x0405
register_count: 18
response_size: 36
raw_encode: HEXBYTES
lambda: |-
std::string z;
// Mappa degli ID degli errori ai loro significati
std::map<int, std::string> errorMap = {
{1, "GridOVP"},
{2, "GridUVP"},
{3, "GridOFP"},
{4, "GridUFP"},
{5, "GFCI"},
{6, "OVRT"},
{7, "LVRT"},
{8, "IslandFault"},
{10, ""},
{11, ""},
{12, ""},
{13, ""},
{14, ""},
{15, ""},
{16, ""},
// Aggiungi altri ID e significati qui.....
{240, ""},
};
for (int i = 0; i < 18; i++) {
int idx = item->offset + (i * 2);
int value = (data[idx] << 8) | data[idx + 1];
for (int bit = 0; bit < 16; bit++) {
if (value & (1 << bit)) {
int errorID = i * 16 + bit + 1;
// Cerca il significato nell'amppa
auto it = errorMap.find(errorID);
if (it != errorMap.end()) {
z += "ID" + std::to_string(errorID) + " (" + it->second + "), ";
} else {
z += "ID" + std::to_string(errorID) + " (Significato sconosciuto), ";
}
}
}
}
if (!z.empty()) {
z.pop_back(); // Rimuove l'ultima virgola
z.pop_back(); // Rimuove lo spazio finale
}
return {z};