From 74c6279f5098ef4d919c11de9dccb886d0f3ff8b Mon Sep 17 00:00:00 2001 From: Alexey Zholtikov Date: Sun, 15 Jan 2023 11:58:40 +0300 Subject: [PATCH] Version 1.23 Added value template to config message. --- src/main.cpp | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index 379c746..0ad9a8b 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -34,7 +34,7 @@ void setupWebServer(void); void connectToMqtt(void); -const String firmware{"1.22"}; +const String firmware{"1.23"}; String espnowNetName{"DEFAULT"}; @@ -184,7 +184,7 @@ void onEspnowMessage(const char *data, const uint8_t *sender) jsonConfig["unique_id"] = myNet.macToString(sender) + "-" + unit; jsonConfig["device_class"] = getValueName(json["class"].as()); jsonConfig["state_topic"] = topicPrefix + "/" + getValueName(incomingData.deviceType) + "/" + myNet.macToString(sender) + "/state"; - jsonConfig["value_template"] = "{{ value_json.state }}"; + jsonConfig["value_template"] = "{{ value_json." + json["template"].as() + " }}"; jsonConfig["command_topic"] = topicPrefix + "/" + getValueName(incomingData.deviceType) + "/" + myNet.macToString(sender) + "/set"; jsonConfig["json_attributes_topic"] = topicPrefix + "/" + getValueName(incomingData.deviceType) + "/" + myNet.macToString(sender) + "/attributes"; jsonConfig["availability_topic"] = topicPrefix + "/" + getValueName(incomingData.deviceType) + "/" + myNet.macToString(sender) + "/status"; @@ -252,19 +252,16 @@ void onEspnowMessage(const char *data, const uint8_t *sender) jsonConfig["name"] = json["name"]; jsonConfig["unique_id"] = myNet.macToString(sender) + "-" + unit; jsonConfig["state_topic"] = topicPrefix + "/" + getValueName(incomingData.deviceType) + "/" + myNet.macToString(sender) + "/state"; - jsonConfig["value_template"] = "{{ value_json.state }}"; + jsonConfig["value_template"] = "{{ value_json." + json["template"].as() + " }}"; jsonConfig["json_attributes_topic"] = topicPrefix + "/" + getValueName(incomingData.deviceType) + "/" + myNet.macToString(sender) + "/attributes"; jsonConfig["force_update"] = "true"; jsonConfig["qos"] = 2; jsonConfig["retain"] = "true"; if (type == HACT_BINARY_SENSOR) { - ha_binary_sensor_device_class_t deviceClass = json["class"].as(); - jsonConfig["device_class"] = getValueName(deviceClass); + jsonConfig["device_class"] = getValueName(json["class"].as()); jsonConfig["payload_on"] = json["payload_on"]; jsonConfig["payload_off"] = json["payload_off"]; - if (deviceClass == HABSDC_BATTERY) - jsonConfig["value_template"] = "{{ value_json.battery }}"; } char buffer[2048]{0}; serializeJsonPretty(jsonConfig, buffer);