Cell voltage min/max decimal fix

This commit is contained in:
Cabooman 2024-02-12 09:42:49 +01:00
parent 183c8ecb85
commit fe0502e66f

View file

@ -83,14 +83,14 @@ static void publish_cell_voltages(void) {
// is the string content // is the string content
// If cell voltages haven't been populated... // If cell voltages haven't been populated...
if (cellvoltages[0] == 0u / 1000) { //cell voltage is in mV and homeassistant expects V if (nof_cellvoltages == 0u)
return; return;
} }
size_t msg_length = snprintf(mqtt_msg, sizeof(mqtt_msg), "{\n\"cell_voltages\":["); size_t msg_length = snprintf(mqtt_msg, sizeof(mqtt_msg), "{\n\"cell_voltages\":[");
for (size_t i = 0; i < nof_cellvoltages; ++i) { for (size_t i = 0; i < nof_cellvoltages; ++i) {
msg_length += msg_length += snprintf(mqtt_msg + msg_length, sizeof(mqtt_msg) - msg_length, "%s%.3f", (i == 0) ? "" : ", ",
snprintf(mqtt_msg + msg_length, sizeof(mqtt_msg) - msg_length, "%s%d", (i == 0) ? "" : ", ", cellvoltages[i]); ((float)cellvoltages[i]) / 1000);
} }
snprintf(mqtt_msg + msg_length, sizeof(mqtt_msg) - msg_length, "]\n}\n"); snprintf(mqtt_msg + msg_length, sizeof(mqtt_msg) - msg_length, "]\n}\n");
@ -98,7 +98,7 @@ static void publish_cell_voltages(void) {
if (mqtt_publish_retain("battery-emulator/spec_data") == false) { if (mqtt_publish_retain("battery-emulator/spec_data") == false) {
Serial.println("Cell voltage MQTT msg could not be sent"); Serial.println("Cell voltage MQTT msg could not be sent");
} }
} }
} }
struct SensorConfig { struct SensorConfig {
@ -176,14 +176,14 @@ static void publish_common_info(void) {
" \"temperature_max\": %.3f,\n" " \"temperature_max\": %.3f,\n"
" \"stat_batt_power\": %.3f,\n" " \"stat_batt_power\": %.3f,\n"
" \"battery_current\": %.3f,\n" " \"battery_current\": %.3f,\n"
" \"cell_max_voltage\": %d,\n" " \"cell_max_voltage\": %.3f,\n"
" \"cell_min_voltage\": %d,\n" " \"cell_min_voltage\": %.3f,\n"
" \"battery_voltage\": %d\n" " \"battery_voltage\": %d\n"
"}\n", "}\n",
((float)SOC) / 100.0, ((float)StateOfHealth) / 100.0, ((float)((int16_t)temperature_min)) / 10.0, ((float)SOC) / 100.0, ((float)StateOfHealth) / 100.0, ((float)((int16_t)temperature_min)) / 10.0,
((float)((int16_t)temperature_max)) / 10.0, ((float)((int16_t)stat_batt_power)), ((float)((int16_t)temperature_max)) / 10.0, ((float)((int16_t)stat_batt_power)),
((float)((int16_t)battery_current)) / 10.0, cell_max_voltage / 1000, cell_min_voltage / 1000, ((float)((int16_t)battery_current)) / 10.0, ((float)cell_max_voltage) / 1000,
battery_voltage / 10.0); ((float)cell_min_voltage) / 1000, battery_voltage / 10.0);
bool result = client.publish(state_topic, mqtt_msg, true); bool result = client.publish(state_topic, mqtt_msg, true);
} }