From 2e4e4328f2f264ba6997dddac37a1e7384616b89 Mon Sep 17 00:00:00 2001 From: Daniel Ekman Date: Tue, 21 Dec 2021 23:17:29 +0100 Subject: [PATCH] first working discovery --- src/mqtt/HomeAssistantMqttHandler.cpp | 53 +++++++++++++++++++++++++-- web/hadiscover1.json | 14 ++++--- web/hadiscover2.json | 13 +++++++ 3 files changed, 71 insertions(+), 9 deletions(-) create mode 100644 web/hadiscover2.json diff --git a/src/mqtt/HomeAssistantMqttHandler.cpp b/src/mqtt/HomeAssistantMqttHandler.cpp index d10630c6..0f55d8a5 100644 --- a/src/mqtt/HomeAssistantMqttHandler.cpp +++ b/src/mqtt/HomeAssistantMqttHandler.cpp @@ -7,6 +7,8 @@ #include "web/root/ha3pf_json.h" #include "web/root/jsonsys_json.h" #include "web/root/jsonprices_json.h" +#include "web/root/hadiscover1_json.h" +#include "web/root/hadiscover2_json.h" bool HomeAssistantMqttHandler::publish(AmsData* data, AmsData* previousState) { if(topic.isEmpty() || !mqtt->connected()) @@ -27,7 +29,7 @@ bool HomeAssistantMqttHandler::publish(AmsData* data, AmsData* previousState) { data->getMeterModel().c_str(), data->getActiveImportPower(), data->getReactiveImportPower(), - sequence, //data->getActiveExportPower(), + data->getActiveExportPower(), data->getReactiveExportPower(), data->getL1Current(), data->getL2Current(), @@ -244,8 +246,53 @@ bool HomeAssistantMqttHandler::publishSystem(HwTools* hw) { ); mqtt->publish(topic + "/state", json); } - if(sequence % 6 == 1 && listType > 0){ // every 60 ams message, publish mqtt discovery - mqtt->publish(topic + "/discovery", "{\"discovery\":1}"); // test + if(sequence % 60 == 1 && listType > 0){ // every 60 ams message, publish mqtt discovery + char json[512]; + String haTopic = "homeassistant/sensor/"; + String haUID = "ams-3a08"; + + snprintf_P(json, sizeof(json), HADISCOVER1_JSON, + "AMS reader status", + (topic + "/state").c_str(), + (topic + "/state").c_str(), + (haUID + "_status").c_str(), + (haUID + "_status").c_str(), + "dB", + "rssi", + haUID.c_str(), + "AMS reader", + "ESP32", + "2.0.0", + "AmsToMqttBridge" + ); + mqtt->publish(haTopic + haUID + "_status/config", json); + + snprintf_P(json, sizeof(json), HADISCOVER2_JSON, + "AMS active import", + (topic + "/sensor").c_str(), + (haUID + "_activeI").c_str(), + (haUID + "_activeI").c_str(), + "W", + "P", + "power", + "measurement", + haUID.c_str() + ); + mqtt->publish(haTopic + haUID + "_activeI/config", json); + + snprintf_P(json, sizeof(json), HADISCOVER2_JSON, + "AMS accumulated active energy", + (topic + "/sensor").c_str(), + (haUID + "_accumI").c_str(), + (haUID + "_accumI").c_str(), + "kWh", + "tPI", + "energy", + "total_increasing", + haUID.c_str() + ); + mqtt->publish(haTopic + haUID + "_accumI/config", json); + } if(listType>0) sequence++; return true; diff --git a/web/hadiscover1.json b/web/hadiscover1.json index de8f64fa..1521981e 100644 --- a/web/hadiscover1.json +++ b/web/hadiscover1.json @@ -2,14 +2,16 @@ "name" : "%s", "stat_t" : "%s", "json_attr_t" : "%s", + "uniq_id" : "%s", + "obj_id" : "%s", "unit_of_meas" : "%s", "val_tpl" : "{{value_json['%s']}}", - "uniq_id" : "%s", + "json_attr_tpl" : "{{ value_json }}", "dev" : { "ids" : [ "%s" ], - "name" : "AMS reader", - "mdl" : "ESP32", - "sw" : "2.0.0", - "mf" : "AmsToMqttBridge" + "name" : "%s", + "mdl" : "%s", + "sw" : "%s", + "mf" : "%s" } - } \ No newline at end of file +} \ No newline at end of file diff --git a/web/hadiscover2.json b/web/hadiscover2.json new file mode 100644 index 00000000..9ec370b2 --- /dev/null +++ b/web/hadiscover2.json @@ -0,0 +1,13 @@ +{ + "name" : "%s", + "stat_t" : "%s", + "uniq_id" : "%s", + "obj_id" : "%s", + "unit_of_meas" : "%s", + "val_tpl" : "{{value_json['%s']}}", + "dev_cla" : "%s", + "stat_cla" : "%s", + "dev" : { + "ids" : [ "%s" ] + } +} \ No newline at end of file