5 Commits

Author SHA1 Message Date
Proddy
9f467ecec1 Merge pull request #2782 from proddy/dev
MQTT base fixes
2025-11-30 23:28:16 +01:00
proddy
273d87dbf1 use ~ as MQTT base 2025-11-30 23:27:28 +01:00
proddy
befd21f8cb fixe #2780 2025-11-30 23:27:18 +01:00
Proddy
15e05c4abc Merge pull request #2778 from proddy/dev
update to show heap
2025-11-30 20:51:42 +01:00
proddy
748a2f5fcf update to show heap 2025-11-30 20:51:17 +01:00
6 changed files with 31 additions and 41 deletions

View File

@@ -667,9 +667,10 @@ void AnalogSensor::publish_values(const bool force) {
LOG_DEBUG("Recreating HA config for analog sensor GPIO %02d", sensor.gpio()); LOG_DEBUG("Recreating HA config for analog sensor GPIO %02d", sensor.gpio());
JsonDocument config; JsonDocument config;
config["~"] = Mqtt::base();
char stat_t[50]; char stat_t[50];
snprintf(stat_t, sizeof(stat_t), "%s/%s_data", Mqtt::base().c_str(), F_(analogsensor)); // use base path snprintf(stat_t, sizeof(stat_t), "~/%s_data", F_(analogsensor)); // use base path
config["stat_t"] = stat_t; config["stat_t"] = stat_t;
char val_obj[50]; char val_obj[50];
@@ -699,6 +700,7 @@ void AnalogSensor::publish_values(const bool force) {
snprintf(uniq_s, sizeof(uniq_s), "%s_%02d", F_(analogsensor), sensor.gpio()); snprintf(uniq_s, sizeof(uniq_s), "%s_%02d", F_(analogsensor), sensor.gpio());
} }
config["~"] = Mqtt::base();
config["uniq_id"] = uniq_s; config["uniq_id"] = uniq_s;
char name[50]; char name[50];

View File

@@ -197,6 +197,8 @@ void Shower::create_ha_discovery() {
char str[70]; char str[70];
char stat_t[50]; char stat_t[50];
doc["~"] = Mqtt::base();
// shower active // shower active
doc["name"] = "Shower Active"; doc["name"] = "Shower Active";
@@ -207,9 +209,7 @@ void Shower::create_ha_discovery() {
} }
doc["uniq_id"] = str; doc["uniq_id"] = str;
doc["def_ent_id"] = (std::string) "binary_sensor." + str; doc["def_ent_id"] = (std::string) "binary_sensor." + str;
doc["stat_t"] = "~/shower_active";
snprintf(stat_t, sizeof(stat_t), "%s/shower_active", Mqtt::base().c_str());
doc["stat_t"] = stat_t;
Mqtt::add_ha_bool(doc.as<JsonObject>()); Mqtt::add_ha_bool(doc.as<JsonObject>());
Mqtt::add_ha_dev_section(doc.as<JsonObject>(), "Shower Sensor", nullptr, nullptr, nullptr, false); Mqtt::add_ha_dev_section(doc.as<JsonObject>(), "Shower Sensor", nullptr, nullptr, nullptr, false);
@@ -225,10 +225,7 @@ void Shower::create_ha_discovery() {
doc["uniq_id"] = str; doc["uniq_id"] = str;
doc["def_ent_id"] = (std::string) "sensor." + str; doc["def_ent_id"] = (std::string) "sensor." + str;
doc["stat_t"] = "~/shower_data",
snprintf(stat_t, sizeof(stat_t), "%s/shower_data", Mqtt::base().c_str());
doc["stat_t"] = stat_t;
doc["name"] = "Shower Duration"; doc["name"] = "Shower Duration";
// don't bother with value template conditions if using Domoticz which doesn't fully support MQTT Discovery // don't bother with value template conditions if using Domoticz which doesn't fully support MQTT Discovery
@@ -248,29 +245,6 @@ void Shower::create_ha_discovery() {
snprintf(topic, sizeof(topic), "sensor/%s/shower_duration/config", Mqtt::basename().c_str()); snprintf(topic, sizeof(topic), "sensor/%s/shower_duration/config", Mqtt::basename().c_str());
Mqtt::queue_ha(topic, doc.as<JsonObject>()); // publish the config payload with retain flag Mqtt::queue_ha(topic, doc.as<JsonObject>()); // publish the config payload with retain flag
//
// shower timestamp
//
/* commented out as the publish of timestamp
doc.clear();
snprintf(str, sizeof(str), "%s_shower_timestamp", Mqtt::basename().c_str());
doc["uniq_id"] = str;
snprintf(stat_t, sizeof(stat_t), "%s/shower_data", Mqtt::base().c_str());
doc["stat_t"] = stat_t;
doc["name"] = "Shower Timestamp";
doc["val_tpl"] = "{{value_json.timestamp if value_json.timestamp is defined else 0}}";
// doc["ent_cat"] = "diagnostic";
Mqtt::add_ha_sections_to_doc("shower", stat_t, doc, false, "value_json.timestamp is defined");
snprintf(topic, sizeof(topic), "sensor/%s/shower_timestamp/config", Mqtt::basename().c_str());
Mqtt::queue_ha(topic, doc.as<JsonObject>()); // publish the config payload with retain flag
*/
} }
} }

View File

@@ -507,11 +507,12 @@ void TemperatureSensor::publish_values(const bool force) {
LOG_DEBUG("Recreating HA config for sensor ID %s", sensor.id().c_str()); LOG_DEBUG("Recreating HA config for sensor ID %s", sensor.id().c_str());
JsonDocument config; JsonDocument config;
config["~"] = Mqtt::base();
config["dev_cla"] = "temperature"; config["dev_cla"] = "temperature";
config["stat_cla"] = "measurement"; config["stat_cla"] = "measurement";
char stat_t[50]; char stat_t[50];
snprintf(stat_t, sizeof(stat_t), "%s/%s_data", Mqtt::base().c_str(), F_(temperaturesensor)); // use base path snprintf(stat_t, sizeof(stat_t), "~/%s_data", F_(temperaturesensor)); // use base path
config["stat_t"] = stat_t; config["stat_t"] = stat_t;
config["unit_of_meas"] = EMSdevice::uom_to_string(DeviceValueUOM::DEGREES); config["unit_of_meas"] = EMSdevice::uom_to_string(DeviceValueUOM::DEGREES);

View File

@@ -411,8 +411,10 @@ void WebCustomEntityService::publish(const bool force) {
// create HA config // create HA config
if (Mqtt::ha_enabled() && !ha_registered_) { if (Mqtt::ha_enabled() && !ha_registered_) {
JsonDocument config; JsonDocument config;
config["~"] = Mqtt::base();
char stat_t[50]; char stat_t[50];
snprintf(stat_t, sizeof(stat_t), "%s/%s_data", Mqtt::base().c_str(), F_(custom)); snprintf(stat_t, sizeof(stat_t), "~/%s_data", F_(custom));
config["stat_t"] = stat_t; config["stat_t"] = stat_t;
char val_obj[50]; char val_obj[50];
@@ -445,7 +447,7 @@ void WebCustomEntityService::publish(const bool force) {
snprintf(topic, sizeof(topic), "sensor/%s/%s_%s/config", Mqtt::basename().c_str(), F_(custom), entityItem.name.c_str()); snprintf(topic, sizeof(topic), "sensor/%s/%s_%s/config", Mqtt::basename().c_str(), F_(custom), entityItem.name.c_str());
} }
char command_topic[Mqtt::MQTT_TOPIC_MAX_SIZE]; char command_topic[Mqtt::MQTT_TOPIC_MAX_SIZE];
snprintf(command_topic, sizeof(command_topic), "%s/%s/%s", Mqtt::base().c_str(), F_(custom), entityItem.name.c_str()); snprintf(command_topic, sizeof(command_topic), "~/%s/%s", F_(custom), entityItem.name.c_str());
config["cmd_t"] = command_topic; config["cmd_t"] = command_topic;
} else { } else {
if (entityItem.value_type == DeviceValueType::BOOL) { if (entityItem.value_type == DeviceValueType::BOOL) {

View File

@@ -263,9 +263,12 @@ void WebSchedulerService::publish(const bool force) {
// create HA config // create HA config
if (Mqtt::ha_enabled() && !ha_registered_) { if (Mqtt::ha_enabled() && !ha_registered_) {
JsonDocument config; JsonDocument config;
config["~"] = Mqtt::base();
char stat_t[50]; char stat_t[50];
snprintf(stat_t, sizeof(stat_t), "%s/%s_data", Mqtt::base().c_str(), F_(scheduler)); snprintf(stat_t, sizeof(stat_t), "~/%s_data", F_(scheduler));
config["stat_t"] = stat_t; config["stat_t"] = stat_t;
char val_obj[50]; char val_obj[50];
@@ -290,7 +293,7 @@ void WebSchedulerService::publish(const bool force) {
char command_topic[Mqtt::MQTT_TOPIC_MAX_SIZE]; char command_topic[Mqtt::MQTT_TOPIC_MAX_SIZE];
snprintf(topic, sizeof(topic), "switch/%s/%s_%s/config", Mqtt::basename().c_str(), F_(scheduler), scheduleItem.name.c_str()); snprintf(topic, sizeof(topic), "switch/%s/%s_%s/config", Mqtt::basename().c_str(), F_(scheduler), scheduleItem.name.c_str());
snprintf(command_topic, sizeof(command_topic), "%s/%s/%s", Mqtt::base().c_str(), F_(scheduler), scheduleItem.name.c_str()); snprintf(command_topic, sizeof(command_topic), "~/%s/%s", F_(scheduler), scheduleItem.name.c_str());
config["cmd_t"] = command_topic; config["cmd_t"] = command_topic;
Mqtt::add_ha_bool(config.as<JsonObject>()); Mqtt::add_ha_bool(config.as<JsonObject>());

View File

@@ -8,8 +8,9 @@ async function testAPI(ip = "ems-esp.local", apiPath = "system", loopCount = 1,
const results = []; const results = [];
for (let i = 0; i < loopCount; i++) { for (let i = 0; i < loopCount; i++) {
let logMessage = '';
if (loopCount > 1) { if (loopCount > 1) {
console.log(`\n--- Request ${i + 1} of ${loopCount} ---`); logMessage = `--- Request ${i + 1} of ${loopCount} ---`;
} }
try { try {
@@ -20,10 +21,17 @@ async function testAPI(ip = "ems-esp.local", apiPath = "system", loopCount = 1,
} }
}); });
console.log('Status:', response.status); // console.log('Status:', response.status);
console.log('Data:', JSON.stringify(response.data, null, 2)); // console.log('Data:', JSON.stringify(response.data, null, 2));
results.push(response.data); // Extract and print freeMem
const freeMem = response.data?.freeMem || response.data?.system?.freeMem;
if (freeMem !== undefined) {
logMessage += (logMessage ? ' ' : '') + `System Free Memory: ${freeMem}`;
} else {
logMessage += (logMessage ? ' ' : '') + 'freeMem not found in response';
}
console.log(logMessage);
// Delay before next request (except for the last one) // Delay before next request (except for the last one)
if (i < loopCount - 1) { if (i < loopCount - 1) {
@@ -47,7 +55,7 @@ async function testAPI(ip = "ems-esp.local", apiPath = "system", loopCount = 1,
// testAPI("192.168.1.65", "system") - single call // testAPI("192.168.1.65", "system") - single call
// testAPI("192.168.1.65", "system", 5) - 5 calls with 1000ms delay // testAPI("192.168.1.65", "system", 5) - 5 calls with 1000ms delay
// testAPI("192.168.1.65", "system", 10, 2000) - 10 calls with 2000ms delay // testAPI("192.168.1.65", "system", 10, 2000) - 10 calls with 2000ms delay
testAPI("192.168.1.65", "system", 1000) testAPI("192.168.1.65", "system", 20000, 5)
.then(() => { .then(() => {
console.log('Test completed successfully'); console.log('Test completed successfully');
process.exit(0); process.exit(0);