val_tpl for undefined: 'None'

This commit is contained in:
MichaelDvP
2026-08-16 10:16:34 +02:00
parent 48c118fed7
commit 574c4e45d5
6 changed files with 9 additions and 8 deletions
+1 -1
View File
@@ -729,7 +729,7 @@ void AnalogSensor::publish_values(const bool force) {
}
// don't bother with value template conditions if using Domoticz which doesn't fully support MQTT Discovery
if (Mqtt::discovery_type() == Mqtt::discoveryType::HOMEASSISTANT) {
config["val_tpl"] = (std::string) "{{" + val_obj + " if " + val_cond + "}}";
config["val_tpl"] = (std::string) "{{" + val_obj + " if " + val_cond + " else 'None'}}";
} else {
config["val_tpl"] = (std::string) "{{" + val_obj + "}}";
}
+4 -3
View File
@@ -1019,7 +1019,7 @@ bool Mqtt::publish_ha_sensor_config(uint8_t type, // EMSdev
std::string topic_str(topic);
doc["def_ent_id"] = Helpers::toLower(topic_str.substr(0, topic_str.find("/"))) + "." + Helpers::toLower(uniq_id);
char sample_val[30] = "'None'"; // sample, correct(!) entity value, used only to prevent warning/error in HA if real value is not published yet
// char sample_val[30] = "'None'"; // sample, correct(!) entity value, used only to prevent warning/error in HA if real value is not published yet
// we add the command topic parameter for commands
if (has_cmd) {
@@ -1136,10 +1136,11 @@ bool Mqtt::publish_ha_sensor_config(uint8_t type, // EMSdev
doc["val_tpl"] = val_tpl;
} else if (uom == DeviceValueUOM::UPTIME) {
// uptime
doc["val_tpl"] = (std::string) "{{" + val_obj + " if " + val_cond + " else 'None' }}";
doc["val_tpl"] = (std::string) "{{" + val_obj + " if " + val_cond + " else 'None'}}";
} else {
// default
doc["val_tpl"] = (std::string) "{{" + val_obj + " if " + val_cond + " else " + sample_val + "}}";
// doc["val_tpl"] = (std::string) "{{" + val_obj + " if " + val_cond + " else " + sample_val + "}}";
doc["val_tpl"] = (std::string) "{{" + val_obj + " if " + val_cond + " else 'None'}}";
}
add_ha_avty_section(doc.as<JsonObject>(), stat_t, val_cond); // adds availability section
} else {
+1 -1
View File
@@ -229,7 +229,7 @@ void Shower::create_ha_discovery() {
// don't bother with value template conditions if using Domoticz which doesn't fully support MQTT Discovery
if (Mqtt::discovery_type() == Mqtt::discoveryType::HOMEASSISTANT) {
doc["val_tpl"] = "{{value_json.duration if value_json.duration is defined else 0}}";
doc["val_tpl"] = "{{value_json.duration if value_json.duration is defined else 'None'}}";
} else {
doc["val_tpl"] = "{{value_json.duration}}";
}
+1 -1
View File
@@ -543,7 +543,7 @@ void TemperatureSensor::publish_values(const bool force) {
// for the value template, there's a problem still with Domoticz probably due to the special characters.
// See https://github.com/emsesp/EMS-ESP32/issues/1360
if (Mqtt::discovery_type() == Mqtt::discoveryType::HOMEASSISTANT) {
config["val_tpl"] = (std::string) "{{" + val_obj + " if " + val_cond + " else -55}}";
config["val_tpl"] = (std::string) "{{" + val_obj + " if " + val_cond + " else 'None'}}";
} else {
config["val_tpl"] = (std::string) "{{" + val_obj + "}}"; // omit value conditional Jinja2 template code
}
+1 -1
View File
@@ -501,7 +501,7 @@ void WebCustomEntityService::publish(const bool force) {
snprintf(val_cond, sizeof(val_cond), "%s is defined", val_obj);
// don't bother with value template conditions if using Domoticz which doesn't fully support MQTT Discovery
if (Mqtt::discovery_type() == Mqtt::discoveryType::HOMEASSISTANT) {
config["val_tpl"] = (std::string) "{{" + val_obj + " if " + val_cond + "}}";
config["val_tpl"] = (std::string) "{{" + val_obj + " if " + val_cond + " else 'None'}}";
} else {
config["val_tpl"] = (std::string) "{{" + val_obj + "}}";
}
+1 -1
View File
@@ -275,7 +275,7 @@ void WebSchedulerService::publish(const bool force) {
char val_tpl[150];
if (Mqtt::discovery_type() == Mqtt::discoveryType::HOMEASSISTANT) {
snprintf(val_tpl, sizeof(val_tpl), "{{%s if %s}}", val_obj, val_cond);
snprintf(val_tpl, sizeof(val_tpl), "{{%s if %s else 'None'}}", val_obj, val_cond);
} else {
snprintf(val_tpl, sizeof(val_tpl), "{{%s}}", val_obj); // omit value conditional Jinja2 template code
}