diff --git a/src/mqtt/HomeAssistantMqttHandler.cpp b/src/mqtt/HomeAssistantMqttHandler.cpp index 0f55d8a5..ffd0dcad 100644 --- a/src/mqtt/HomeAssistantMqttHandler.cpp +++ b/src/mqtt/HomeAssistantMqttHandler.cpp @@ -4,7 +4,6 @@ #include "web/root/ha1_json.h" #include "web/root/ha2_json.h" #include "web/root/ha3_json.h" -#include "web/root/ha3pf_json.h" #include "web/root/jsonsys_json.h" #include "web/root/jsonprices_json.h" #include "web/root/hadiscover1_json.h" @@ -15,15 +14,28 @@ bool HomeAssistantMqttHandler::publish(AmsData* data, AmsData* previousState) { return false; listType = data->getListType(); // for discovery stuff in publishSystem() - if(data->getListType() == 1) { + if(data->getListType() == 3) { // publish energy counts + if(data->getActiveExportCounter() > 0){ + char json[256]; + snprintf_P(json, sizeof(json), HA2_JSON, + data->getActiveImportCounter(), + data->getActiveExportCounter(), + data->getReactiveImportCounter(), + data->getReactiveExportCounter(), + data->getMeterTimestamp() + ); + mqtt->publish(topic + "/energy", json); + } + } + if(data->getListType() == 1) { // publish power counts char json[192]; snprintf_P(json, sizeof(json), HA1_JSON, data->getActiveImportPower() ); - return mqtt->publish(topic + "/sensor", json); - } else if(data->getListType() == 2) { - char json[384]; - snprintf_P(json, sizeof(json), HA2_JSON, + return mqtt->publish(topic + "/power", json); + } else if(data->getListType() == 2 || data->getListType() == 3) { + char json[768]; + snprintf_P(json, sizeof(json), HA3_JSON, data->getListId().c_str(), data->getMeterId().c_str(), data->getMeterModel().c_str(), @@ -36,69 +48,20 @@ bool HomeAssistantMqttHandler::publish(AmsData* data, AmsData* previousState) { data->getL3Current(), data->getL1Voltage(), data->getL2Voltage(), - data->getL3Voltage() + data->getL3Voltage(), + data->getPowerFactor() == 0 ? 1 : data->getPowerFactor(), + data->getPowerFactor() == 0 ? 1 : data->getL1PowerFactor(), + data->getPowerFactor() == 0 ? 1 : data->getL2PowerFactor(), + data->getPowerFactor() == 0 ? 1 : data->getL3PowerFactor() ); - return mqtt->publish(topic + "/sensor", json); - } else if(data->getListType() == 3) { - if(data->getPowerFactor() == 0) { - char json[512]; - snprintf_P(json, sizeof(json), HA3_JSON, - data->getListId().c_str(), - data->getMeterId().c_str(), - data->getMeterModel().c_str(), - data->getActiveImportPower(), - data->getReactiveImportPower(), - data->getActiveExportPower(), - data->getReactiveExportPower(), - data->getL1Current(), - data->getL2Current(), - data->getL3Current(), - data->getL1Voltage(), - data->getL2Voltage(), - data->getL3Voltage(), - data->getActiveImportCounter(), - data->getActiveExportCounter(), - data->getReactiveImportCounter(), - data->getReactiveExportCounter(), - data->getMeterTimestamp() - ); - return mqtt->publish(topic + "/sensor", json); - } else { - char json[768]; - snprintf_P(json, sizeof(json), HA3PF_JSON, - data->getListId().c_str(), - data->getMeterId().c_str(), - data->getMeterModel().c_str(), - data->getActiveImportPower(), - data->getReactiveImportPower(), - data->getActiveExportPower(), - data->getReactiveExportPower(), - data->getL1Current(), - data->getL2Current(), - data->getL3Current(), - data->getL1Voltage(), - data->getL2Voltage(), - data->getL3Voltage(), - data->getPowerFactor(), - data->getL1PowerFactor(), - data->getL2PowerFactor(), - data->getL3PowerFactor(), - data->getActiveImportCounter(), - data->getActiveExportCounter(), - data->getReactiveImportCounter(), - data->getReactiveExportCounter(), - data->getMeterTimestamp() - ); - return mqtt->publish(topic + "/sensor", json); - } + return mqtt->publish(topic + "/power", json); } return false; } bool HomeAssistantMqttHandler::publishTemperatures(AmsConfiguration* config, HwTools* hw) { int count = hw->getTempSensorCount(); - if(count == 0) - return false; + if(count == 0) return false; int size = 32 + (count * 26); @@ -269,7 +232,7 @@ bool HomeAssistantMqttHandler::publishSystem(HwTools* hw) { snprintf_P(json, sizeof(json), HADISCOVER2_JSON, "AMS active import", - (topic + "/sensor").c_str(), + (topic + "/power").c_str(), (haUID + "_activeI").c_str(), (haUID + "_activeI").c_str(), "W", @@ -282,7 +245,7 @@ bool HomeAssistantMqttHandler::publishSystem(HwTools* hw) { snprintf_P(json, sizeof(json), HADISCOVER2_JSON, "AMS accumulated active energy", - (topic + "/sensor").c_str(), + (topic + "/energy").c_str(), (haUID + "_accumI").c_str(), (haUID + "_accumI").c_str(), "kWh", diff --git a/web/ha2.json b/web/ha2.json index b854e4cc..9ffcdaf5 100644 --- a/web/ha2.json +++ b/web/ha2.json @@ -1,24 +1,7 @@ { - "lv" : "%s", - "id" : "%s", - "type" : "%s", - "P" : %d, - "Q" : %d, - "PO" : %d, - "QO" : %d, - "I1" : %.2f, - "I2" : %.2f, - "I3" : %.2f, - "U1" : %.2f, - "U2" : %.2f, - "U3" : %.2f, - "PF" : null, - "PF1" : null, - "PF2" : null, - "PF3" : null, - "tPI" : null, - "tPO" : null, - "tQI" : null, - "tQO" : null, - "rtc" : null + "tPI" : %.2f, + "tPO" : %.2f, + "tQI" : %.2f, + "tQO" : %.2f, + "rtc" : %llu } diff --git a/web/ha3.json b/web/ha3.json index 806933f3..41fad56b 100644 --- a/web/ha3.json +++ b/web/ha3.json @@ -12,13 +12,8 @@ "U1" : %.2f, "U2" : %.2f, "U3" : %.2f, - "PF" : null, - "PF1" : null, - "PF2" : null, - "PF3" : null, - "tPI" : %.2f, - "tPO" : %.2f, - "tQI" : %.2f, - "tQO" : %.2f, - "rtc" : %lu + "PF" : %.2f, + "PF1" : %.2f, + "PF2" : %.2f, + "PF3" : %.2f } diff --git a/web/ha3pf.json b/web/ha3pf.json deleted file mode 100644 index dd3e4a5b..00000000 --- a/web/ha3pf.json +++ /dev/null @@ -1,24 +0,0 @@ -{ - "lv" : "%s", - "id" : "%s", - "type" : "%s", - "P" : %d, - "Q" : %d, - "PO" : %d, - "QO" : %d, - "I1" : %.2f, - "I2" : %.2f, - "I3" : %.2f, - "U1" : %.2f, - "U2" : %.2f, - "U3" : %.2f, - "PF" : %.2f, - "PF1" : %.2f, - "PF2" : %.2f, - "PF3" : %.2f, - "tPI" : %.2f, - "tPO" : %.2f, - "tQI" : %.2f, - "tQO" : %.2f, - "rtc" : %lu -}