From 619f15d73090530d72bbc8d856bdcaf1d24028d2 Mon Sep 17 00:00:00 2001 From: John Date: Sat, 26 Oct 2024 08:18:29 +0200 Subject: [PATCH] remove field name from HA discovery for single-field messages --- ChangeLog.md | 2 +- contrib/etc/ebusd/mqtt-hassio.cfg | 3 ++- src/ebusd/mqtthandler.cpp | 4 +++- 3 files changed, 6 insertions(+), 3 deletions(-) diff --git a/ChangeLog.md b/ChangeLog.md index ee866c30..ee9dd613 100644 --- a/ChangeLog.md +++ b/ChangeLog.md @@ -18,7 +18,7 @@ * add option to discover device via mDNS * add dedicated log level for device messages * add option to extend MQTT variables from env/cmdline -* add date+datetime mapping and better device update check to Home Assistant MQTT discovery integration +* add date+datetime mapping, better device update check, and removed single-field-message field names to Home Assistant MQTT discovery integration ## Breaking Changes * change default config path to https://ebus.github.io/ serving files generated from new TypeSpec message definition sources diff --git a/contrib/etc/ebusd/mqtt-hassio.cfg b/contrib/etc/ebusd/mqtt-hassio.cfg index e6bf4770..921e08c2 100644 --- a/contrib/etc/ebusd/mqtt-hassio.cfg +++ b/contrib/etc/ebusd/mqtt-hassio.cfg @@ -52,6 +52,7 @@ # %field the field name and key for JSON objects (equals the field index if no name is defined or the names are not # unique in the message). # %fieldname the field name (and nothing else like in %field, might be empty though!) +# %fieldnamemult the field name unless there is a single field only (similar to %fieldname) # %type the field type (one of "number", "list", "string", "date", "time", or "datetime"). # %basetype the base data type ID (e.g. "UCH"). # %comment the field comment (if any). @@ -350,7 +351,7 @@ definition-topic ?= %haprefix/%type_topic/%{TOPIC}_%FIELD/config # HA integration: this is the config topic payload for HA's MQTT discovery. definition-payload = { "unique_id":"%{TOPIC}_%FIELD", - "name":"%name %fieldname", + "name":"%name %fieldnamemult", "device":%circuit_part, "value_template":"{{value_json[\"%field\"].value}}", "state_topic":"%topic"%field_payload diff --git a/src/ebusd/mqtthandler.cpp b/src/ebusd/mqtthandler.cpp index 39539f93..312d33c3 100755 --- a/src/ebusd/mqtthandler.cpp +++ b/src/ebusd/mqtthandler.cpp @@ -924,7 +924,9 @@ void MqttHandler::run() { StringReplacers values = msgValues; // need a copy here as the contents are manipulated values.set("index", static_cast(index)); values.set("field", fieldName); - values.set("fieldname", field->getName(-1)); + string fieldNameNonUnique = field->getName(-1); + values.set("fieldname", fieldNameNonUnique); + values.set("fieldnamemult", fieldCount == 1 ? "" : fieldNameNonUnique); values.set("type", typeStr); values.set("type_map", str); values.set("basetype", dataType->getId());