flat data messages

This commit is contained in:
Daniel Ekman 2021-12-21 14:21:02 +01:00
parent 92e8beadc0
commit d7d25083dc
7 changed files with 80 additions and 46 deletions

View File

@ -1,10 +1,10 @@
#include "HomeAssistantMqttHandler.h"
#include "hexutils.h"
#include "Uptime.h"
#include "web/root/json1_json.h"
#include "web/root/json2_json.h"
#include "web/root/json3_json.h"
#include "web/root/json3pf_json.h"
#include "web/root/json1ha_json.h"
#include "web/root/json2ha_json.h"
#include "web/root/json3ha_json.h"
#include "web/root/json3hapf_json.h"
#include "web/root/jsonsys_json.h"
#include "web/root/jsonprices_json.h"
@ -14,27 +14,13 @@ bool HomeAssistantMqttHandler::publish(AmsData* data, AmsData* previousState) {
if(data->getListType() == 1) {
char json[192];
snprintf_P(json, sizeof(json), JSON1_JSON,
WiFi.macAddress().c_str(),
clientId.c_str(),
(uint32_t) (millis64()/1000),
data->getPackageTimestamp(),
hw->getVcc(),
hw->getWifiRssi(),
hw->getTemperature(),
snprintf_P(json, sizeof(json), JSON1HA_JSON,
data->getActiveImportPower()
);
return mqtt->publish(topic, json);
return mqtt->publish(topic + "/data1", json);
} else if(data->getListType() == 2) {
char json[384];
snprintf_P(json, sizeof(json), JSON2_JSON,
WiFi.macAddress().c_str(),
clientId.c_str(),
(uint32_t) (millis64()/1000),
data->getPackageTimestamp(),
hw->getVcc(),
hw->getWifiRssi(),
hw->getTemperature(),
snprintf_P(json, sizeof(json), JSON2HA_JSON,
data->getListId().c_str(),
data->getMeterId().c_str(),
data->getMeterModel().c_str(),
@ -49,18 +35,11 @@ bool HomeAssistantMqttHandler::publish(AmsData* data, AmsData* previousState) {
data->getL2Voltage(),
data->getL3Voltage()
);
return mqtt->publish(topic, json);
return mqtt->publish(topic + "/data2", json);
} else if(data->getListType() == 3) {
if(data->getPowerFactor() == 0) {
char json[512];
snprintf_P(json, sizeof(json), JSON3_JSON,
WiFi.macAddress().c_str(),
clientId.c_str(),
(uint32_t) (millis64()/1000),
data->getPackageTimestamp(),
hw->getVcc(),
hw->getWifiRssi(),
hw->getTemperature(),
snprintf_P(json, sizeof(json), JSON3HA_JSON,
data->getListId().c_str(),
data->getMeterId().c_str(),
data->getMeterModel().c_str(),
@ -80,17 +59,10 @@ bool HomeAssistantMqttHandler::publish(AmsData* data, AmsData* previousState) {
data->getReactiveExportCounter(),
data->getMeterTimestamp()
);
return mqtt->publish(topic, json);
return mqtt->publish(topic + "/data3", json);
} else {
char json[768];
snprintf_P(json, sizeof(json), JSON3PF_JSON,
WiFi.macAddress().c_str(),
clientId.c_str(),
(uint32_t) (millis64()/1000),
data->getPackageTimestamp(),
hw->getVcc(),
hw->getWifiRssi(),
hw->getTemperature(),
snprintf_P(json, sizeof(json), JSON3HAPF_JSON,
data->getListId().c_str(),
data->getMeterId().c_str(),
data->getMeterModel().c_str(),
@ -114,7 +86,7 @@ bool HomeAssistantMqttHandler::publish(AmsData* data, AmsData* previousState) {
data->getReactiveExportCounter(),
data->getMeterTimestamp()
);
return mqtt->publish(topic, json);
return mqtt->publish(topic + "/data3pf", json);
}
}
return false;
@ -144,7 +116,7 @@ bool HomeAssistantMqttHandler::publishTemperatures(AmsConfiguration* config, HwT
}
char* pos = buf+strlen(buf);
snprintf(count == 0 ? pos : pos-1, 8, "}}");
return mqtt->publish(topic, buf);
return mqtt->publish(topic + "/temperatures", buf);
}
bool HomeAssistantMqttHandler::publishPrices(EntsoeApi* eapi) {
@ -250,13 +222,13 @@ bool HomeAssistantMqttHandler::publishPrices(EntsoeApi* eapi) {
ts3hr,
ts6hr
);
return mqtt->publish(topic, json);
return mqtt->publish(topic + "/prices", json);
}
bool HomeAssistantMqttHandler::publishSystem(HwTools* hw) {
if(init || topic.isEmpty() || !mqtt->connected())
return false;
if(!topic.endsWith("/")) topic += "/";
//if(!topic.endsWith("/")) topic += "/";
char json[192];
snprintf_P(json, sizeof(json), JSONSYS_JSON,
@ -267,6 +239,6 @@ bool HomeAssistantMqttHandler::publishSystem(HwTools* hw) {
hw->getWifiRssi(),
hw->getTemperature()
);
init = mqtt->publish(topic, json);
init = mqtt->publish(topic + "/state", json);
return init;
}

View File

@ -539,7 +539,7 @@ void AmsWebServer::configMqttHtml() {
html.replace("{u}", mqtt.username);
html.replace("{pw}", mqtt.password);
html.replace("{f}", String(mqtt.payloadFormat));
for(int i = 0; i<4; i++) {
for(int i = 0; i<5; i++) {
html.replace("{f" + String(i) + "}", mqtt.payloadFormat == i ? "selected" : "");
}

3
web/json1ha.json Normal file
View File

@ -0,0 +1,3 @@
{
"P" : %d
}

15
web/json2ha.json Normal file
View File

@ -0,0 +1,15 @@
{
"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
}

20
web/json3ha.json Normal file
View File

@ -0,0 +1,20 @@
{
"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,
"tPI" : %.2f,
"tPO" : %.2f,
"tQI" : %.2f,
"tQO" : %.2f,
"rtc" : %lu
}

24
web/json3hapf.json Normal file
View File

@ -0,0 +1,24 @@
{
"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
}

View File

@ -62,7 +62,7 @@
<option value="1" {f1}>Raw (minimal)</option>
<option value="2" {f2}>Raw (full)</option>
<option value="3" {f3}>Domoticz</option>
<option value="4" {f3}>Home-Assistant</option>
<option value="4" {f4}>Home-Assistant</option>
</select>
</div>
</div>