Fixed reactive energy sensors in HA

This commit is contained in:
Gunnar Skjold
2023-04-29 06:40:25 +02:00
parent c1701c8ee9
commit b1ad844bc6
3 changed files with 42 additions and 40 deletions

View File

@@ -15,75 +15,75 @@ typedef struct HomeAssistantSensor {
const uint8_t List1SensorCount PROGMEM = 1;
const HomeAssistantSensor List1Sensors[List1SensorCount] PROGMEM = {
{"Active import", "/power", "P", "W", "power", "\"measurement\""}
{"Active import", "/power", "P", "W", "power", "measurement"}
};
const uint8_t List2SensorCount PROGMEM = 8;
const HomeAssistantSensor List2Sensors[List2SensorCount] PROGMEM = {
{"Reactive import", "/power", "Q", "var", "reactive_power", "\"measurement\""},
{"Reactive export", "/power", "QO", "var", "reactive_power", "\"measurement\""},
{"L1 current", "/power", "I1", "A", "current", "\"measurement\""},
{"L2 current", "/power", "I2", "A", "current", "\"measurement\""},
{"L3 current", "/power", "I3", "A", "current", "\"measurement\""},
{"L1 voltage", "/power", "U1", "V", "voltage", "\"measurement\""},
{"L2 voltage", "/power", "U2", "V", "voltage", "\"measurement\""},
{"L3 voltage", "/power", "U3", "V", "voltage", "\"measurement\""}
{"Reactive import", "/power", "Q", "var", "reactive_power", "measurement"},
{"Reactive export", "/power", "QO", "var", "reactive_power", "measurement"},
{"L1 current", "/power", "I1", "A", "current", "measurement"},
{"L2 current", "/power", "I2", "A", "current", "measurement"},
{"L3 current", "/power", "I3", "A", "current", "measurement"},
{"L1 voltage", "/power", "U1", "V", "voltage", "measurement"},
{"L2 voltage", "/power", "U2", "V", "voltage", "measurement"},
{"L3 voltage", "/power", "U3", "V", "voltage", "measurement"}
};
const uint8_t List2ExportSensorCount PROGMEM = 1;
const HomeAssistantSensor List2ExportSensors[List2ExportSensorCount] PROGMEM = {
{"Active export", "/power", "PO", "W", "power", "\"measurement\""}
{"Active export", "/power", "PO", "W", "power", "measurement"}
};
const uint8_t List3SensorCount PROGMEM = 3;
const HomeAssistantSensor List3Sensors[List3SensorCount] PROGMEM = {
{"Accumulated active import", "/energy", "tPI", "kWh", "energy", "\"total_increasing\""},
{"Accumulated reactive import","/energy", "tQI", "", "energy", "\"total_increasing\""},
{"Accumulated reactive export","/energy", "tQO", "", "energy", "\"total_increasing\""}
{"Accumulated active import", "/energy", "tPI", "kWh", "energy", "total_increasing"},
{"Accumulated reactive import","/energy", "tQI", "kvarh","", "total_increasing"},
{"Accumulated reactive export","/energy", "tQO", "kvarh","", "total_increasing"}
};
const uint8_t List3ExportSensorCount PROGMEM = 1;
const HomeAssistantSensor List3ExportSensors[List3ExportSensorCount] PROGMEM = {
{"Accumulated active export", "/energy", "tPO", "kWh", "energy", "\"total_increasing\""}
{"Accumulated active export", "/energy", "tPO", "kWh", "energy", "total_increasing"}
};
const uint8_t List4SensorCount PROGMEM = 7;
const HomeAssistantSensor List4Sensors[List4SensorCount] PROGMEM = {
{"Power factor", "/power", "PF", "%", "power_factor", "\"measurement\""},
{"L1 power factor", "/power", "PF1", "%", "power_factor", "\"measurement\""},
{"L2 power factor", "/power", "PF2", "%", "power_factor", "\"measurement\""},
{"L3 power factor", "/power", "PF3", "%", "power_factor", "\"measurement\""},
{"L1 active import", "/power", "P1", "W", "power", "\"measurement\""},
{"L2 active import", "/power", "P2", "W", "power", "\"measurement\""},
{"L3 active import", "/power", "P3", "W", "power", "\"measurement\""}
{"Power factor", "/power", "PF", "%", "power_factor", "measurement"},
{"L1 power factor", "/power", "PF1", "%", "power_factor", "measurement"},
{"L2 power factor", "/power", "PF2", "%", "power_factor", "measurement"},
{"L3 power factor", "/power", "PF3", "%", "power_factor", "measurement"},
{"L1 active import", "/power", "P1", "W", "power", "measurement"},
{"L2 active import", "/power", "P2", "W", "power", "measurement"},
{"L3 active import", "/power", "P3", "W", "power", "measurement"}
};
const uint8_t List4ExportSensorCount PROGMEM = 3;
const HomeAssistantSensor List4ExportSensors[List4ExportSensorCount] PROGMEM = {
{"L1 active export", "/power", "PO1", "W", "power", "\"measurement\""},
{"L2 active export", "/power", "PO2", "W", "power", "\"measurement\""},
{"L3 active export", "/power", "PO3", "W", "power", "\"measurement\""}
{"L1 active export", "/power", "PO1", "W", "power", "measurement"},
{"L2 active export", "/power", "PO2", "W", "power", "measurement"},
{"L3 active export", "/power", "PO3", "W", "power", "measurement"}
};
const uint8_t RealtimeSensorCount PROGMEM = 8;
const HomeAssistantSensor RealtimeSensors[RealtimeSensorCount] PROGMEM = {
{"Month max", "/realtime","max", "kWh", "energy", "\"total_increasing\""},
{"Tariff threshold", "/realtime","threshold", "kWh", "energy", "\"total_increasing\""},
{"Current hour used", "/realtime","hour.use", "kWh", "energy", "\"total_increasing\""},
{"Month max", "/realtime","max", "kWh", "energy", "total_increasing"},
{"Tariff threshold", "/realtime","threshold", "kWh", "energy", "total_increasing"},
{"Current hour used", "/realtime","hour.use", "kWh", "energy", "total_increasing"},
{"Current hour cost", "/realtime","hour.cost", "", "monetary", ""},
{"Current day used", "/realtime","day.use", "kWh", "energy", "\"total_increasing\""},
{"Current day used", "/realtime","day.use", "kWh", "energy", "total_increasing"},
{"Current day cost", "/realtime","day.cost", "", "monetary", ""},
{"Current month used", "/realtime","month.use", "kWh", "energy", "\"total_increasing\""},
{"Current month used", "/realtime","month.use", "kWh", "energy", "total_increasing"},
{"Current month cost", "/realtime","month.cost", "", "monetary", ""}
};
const uint8_t RealtimeExportSensorCount PROGMEM = 6;
const HomeAssistantSensor RealtimeExportSensors[RealtimeExportSensorCount] PROGMEM = {
{"Current hour produced", "/realtime","hour.produced", "kWh", "energy", "\"total_increasing\""},
{"Current hour produced", "/realtime","hour.produced", "kWh", "energy", "total_increasing"},
{"Current hour income", "/realtime","hour.income", "", "monetary", ""},
{"Current day produced", "/realtime","day.produced", "kWh", "energy", "\"total_increasing\""},
{"Current day produced", "/realtime","day.produced", "kWh", "energy", "total_increasing"},
{"Current day income", "/realtime","day.income", "", "monetary", ""},
{"Current month produced", "/realtime","month.produced", "kWh", "energy", "\"total_increasing\""},
{"Current month produced", "/realtime","month.produced", "kWh", "energy", "total_increasing"},
{"Current month income", "/realtime","month.income", "", "monetary", ""}
};
@@ -102,11 +102,11 @@ const HomeAssistantSensor PriceSensor PROGMEM = {"Price in %02d %s", "/prices",
const uint8_t SystemSensorCount PROGMEM = 2;
const HomeAssistantSensor SystemSensors[SystemSensorCount] PROGMEM = {
{"Status", "/state", "rssi", "dBm", "signal_strength", "\"measurement\""},
{"Supply volt", "/state", "vcc", "V", "voltage", "\"measurement\""}
{"Status", "/state", "rssi", "dBm", "signal_strength", "measurement"},
{"Supply volt", "/state", "vcc", "V", "voltage", "measurement"}
};
const HomeAssistantSensor TemperatureSensor PROGMEM = {"Temperature sensor %s", "/temperatures", "temperatures['%s']", "°C", "temperature", "\"measurement\""};
const HomeAssistantSensor TemperatureSensor PROGMEM = {"Temperature sensor %s", "/temperatures", "temperatures['%s']", "°C", "temperature", "measurement"};
#endif

View File

@@ -5,7 +5,6 @@
"obj_id" : "%s_%s",
"unit_of_meas" : "%s",
"val_tpl" : "{{ value_json.%s | is_defined }}",
"dev_cla" : "%s",
"dev" : {
"ids" : [ "%s" ],
"name" : "%s",
@@ -13,5 +12,5 @@
"sw" : "%s",
"mf" : "%s",
"cu" : "%s"
}%s %s
}%s%s%s%s%s%s
}

View File

@@ -350,15 +350,18 @@ void HomeAssistantMqttHandler::publishSensor(const HomeAssistantSensor& sensor)
deviceUid.c_str(), uid.c_str(),
sensor.uom,
sensor.path,
sensor.devcl,
deviceUid.c_str(),
deviceName.c_str(),
deviceModel.c_str(),
VERSION,
manufacturer.c_str(),
deviceUrl.c_str(),
strlen_P(sensor.stacl) > 0 ? ", \"stat_cla\" :" : "",
strlen_P(sensor.stacl) > 0 ? (char *) FPSTR(sensor.stacl) : ""
strlen_P(sensor.devcl) > 0 ? ",\"dev_cla\":\"" : "",
strlen_P(sensor.devcl) > 0 ? (char *) FPSTR(sensor.devcl) : "",
strlen_P(sensor.devcl) > 0 ? "\"" : "",
strlen_P(sensor.stacl) > 0 ? ",\"stat_cla\":\"" : "",
strlen_P(sensor.stacl) > 0 ? (char *) FPSTR(sensor.stacl) : "",
strlen_P(sensor.stacl) > 0 ? "\"" : ""
);
mqtt->publish(discoveryTopic + deviceUid + "_" + uid.c_str() + "/config", json, true, 0);
loop();