From d40b7cb85a7d6d0a682f39e33488108240915b26 Mon Sep 17 00:00:00 2001 From: Yurii Date: Sun, 29 Nov 2026 05:43:52 +0300 Subject: [PATCH] feat: publish MQTT field changes with periodic refresh Track 89 settings and state fields with partial structure snapshots and a fixed-capacity map of publication timestamps. Publish changed or expired values in bounded passes, updating snapshots only after successful queue acceptance and keeping failed fields eligible for retry. Refresh fields after reconnecting and affected temperatures after unit or Equitherm mode changes. Keep the status heartbeat independent, cache DHW limits for MQTT and API consumers, and clear non-finite retained readings. Default the forced publication interval to 60 seconds and accept 15 to 3600 seconds in the API and UI. Set Home Assistant state expiration to twice this interval and refresh discovery when the interval changes. Remove network connection and dedicated fault/diagnostic availability publications. Use existing active states and RSSI for HA availability. --- include/Settings.h | 4 +- include/StructSnapshot.h | 71 +++ include/StructTimestampMap.h | 47 ++ src/config.cpp | 6 +- src/ha_discovery.cpp | 12 +- src/mqtt.cpp | 1104 ++++++++++++++++++++++++++++------ src/opentherm.cpp | 2 + src_data/pages/settings.html | 2 +- 8 files changed, 1056 insertions(+), 192 deletions(-) create mode 100644 include/StructSnapshot.h create mode 100644 include/StructTimestampMap.h diff --git a/include/Settings.h b/include/Settings.h index 9605277..d707881 100644 --- a/include/Settings.h +++ b/include/Settings.h @@ -116,7 +116,7 @@ struct MqttSettings { char user[33] = DEFAULT_MQTT_USER; char password[33] = DEFAULT_MQTT_PASSWORD; char prefix[33] = DEFAULT_MQTT_PREFIX; - unsigned short interval = 5; + unsigned short interval = 60; bool homeAssistantDiscovery = DEFAULT_HA_DISCOVERY_ENABLED; char homeAssistantDiscoveryPrefix[33] = DEFAULT_HA_DISCOVERY_PREFIX; }; @@ -285,6 +285,8 @@ struct Variables { float targetTemp = 0.0f; float currentTemp = 0.0f; float returnTemp = 0.0f; + uint8_t minTemp = 0; + uint8_t maxTemp = 0; } dhw; struct { diff --git a/include/StructSnapshot.h b/include/StructSnapshot.h new file mode 100644 index 0000000..f9185e9 --- /dev/null +++ b/include/StructSnapshot.h @@ -0,0 +1,71 @@ +#pragma once + +#include +#include +#include +#include +#include +#include + +#include "utils.h" + +template +class StructSnapshot { +public: + explicit StructSnapshot(const Structure& source) : source(source), snapshot(source) {} + + // Compare the live field with the corresponding field in the private snapshot. + template + bool equals(const Field* field, float tolerance = 0.0001f) const { + const auto index = offset(field); + if (index >= sizeof(Structure) || sizeof(Field) > sizeof(Structure) - index) { + return false; + } + + const auto* previous = reinterpret_cast( + reinterpret_cast(&snapshot) + index + ); + + if constexpr (std::is_array_v) { + return text(*field) == text(*previous); + + } else if constexpr (std::is_same_v) { + return !isFloatDifferent(*field, *previous, tolerance); + + } else { + return *field == *previous; + } + } + + template + void set(const Field* field, const Field* value = nullptr) { + static_assert(std::is_trivially_copyable_v, "Field must be trivially copyable"); + const auto index = offset(field); + if (index >= sizeof(Structure) || sizeof(Field) > sizeof(Structure) - index) { + return; + } + + std::memcpy(reinterpret_cast(&snapshot) + index, value != nullptr ? value : field, sizeof(Field)); + } + +private: + const Structure& source; + Structure snapshot; + + // A fixed-size settings buffer may have no terminator or have unused trailing bytes. + template + static std::string_view text(const char (&value)[N]) { + const std::string_view buffer(value, N); + return buffer.substr(0, buffer.find('\0')); + } + + std::size_t offset(const void* field) const { + const auto base = reinterpret_cast(&source); + const auto address = reinterpret_cast(field); + if (field == nullptr || address < base) { + return sizeof(Structure); + } + + return address - base; + } +}; diff --git a/include/StructTimestampMap.h b/include/StructTimestampMap.h new file mode 100644 index 0000000..8eac59b --- /dev/null +++ b/include/StructTimestampMap.h @@ -0,0 +1,47 @@ +#pragma once + +#include +#include +#include +#include +#include +#include +#include + +template +class StructTimestampMap { +public: + // Adding a key can move entries; use the reference before the next insertion. + unsigned long& get(const void* field) { + const auto end = times.begin() + count; + const auto entry = std::lower_bound(times.begin(), end, field, [](const auto& item, const void* ptr) { + return reinterpret_cast(item.first) < reinterpret_cast(ptr); + }); + + if (entry != end && entry->first == field) { + return entry->second; + } + + assert(count < times.size()); + std::move_backward(entry, end, end + 1); + *entry = {field, 0}; + ++count; + return entry->second; + } + + void reset(std::initializer_list fields) { + for (const void* field : fields) { + get(field) = 0; + } + } + + void resetAll() { + for (std::size_t index = 0; index < count; ++index) { + times[index].second = 0; + } + } + +private: + std::array, Capacity> times{}; + std::size_t count = 0; +}; diff --git a/src/config.cpp b/src/config.cpp index 35082a9..ec25a74 100644 --- a/src/config.cpp +++ b/src/config.cpp @@ -1303,7 +1303,7 @@ SettingResult fromJson(JsonVariantConst src, MqttSettings& dst) { if (src[S_INTERVAL].is()) { const auto value = src[S_INTERVAL].as(); - if (value >= 3 && value <= 60) { + if (value >= 15 && value <= 3600) { result.accepted = true; if (value != dst.interval) { dst.interval = value; @@ -3566,8 +3566,8 @@ JsonVariant toJson(const Variables& src, JsonVariant dst) { mDhw[S_TARGET_TEMP] = roundf((src.master.dhw.targetTemp == 0 ? 0 : convertTemp(src.master.dhw.targetTemp, UnitSystem::METRIC, settings.system.unitSystem)), 2); mDhw[S_CURRENT_TEMP] = roundf(convertTemp(src.master.dhw.currentTemp, UnitSystem::METRIC, settings.system.unitSystem), 2); mDhw[S_RETURN_TEMP] = roundf(convertTemp(src.master.dhw.returnTemp, UnitSystem::METRIC, settings.system.unitSystem), 2); - mDhw[S_MIN_TEMP] = convertTemp(settings.dhw.minTemp, UnitSystem::METRIC, settings.system.unitSystem); - mDhw[S_MAX_TEMP] = convertTemp(settings.dhw.maxTemp, UnitSystem::METRIC, settings.system.unitSystem); + mDhw[S_MIN_TEMP] = convertTemp(src.master.dhw.minTemp, UnitSystem::METRIC, settings.system.unitSystem); + mDhw[S_MAX_TEMP] = convertTemp(src.master.dhw.maxTemp, UnitSystem::METRIC, settings.system.unitSystem); master[S_NETWORK][S_CONNECTED] = isNetworkConnected(); master[S_NETWORK][S_RSSI] = src.network.rssi; diff --git a/src/ha_discovery.cpp b/src/ha_discovery.cpp index 7109623..1aaed28 100644 --- a/src/ha_discovery.cpp +++ b/src/ha_discovery.cpp @@ -1,13 +1,11 @@ #include -#include - std::unique_ptr haDiscovery; bool publishHaEntity(HaEntity entityId, bool remove) { std::unique_ptr entity; const UnitSystem unit = settings.system.unitSystem; - const uint32_t stateExpiration = std::max(60u, 2u * settings.mqtt.interval); + const uint32_t stateExpiration = 2u * settings.mqtt.interval; switch (entityId) { case HA_ENTITY_HEATING_TURBO: { @@ -498,7 +496,7 @@ bool publishHaEntity(HaEntity entityId, bool remove) { auto value = std::make_unique("fault_code", "Fault code"); if (!remove) { value->stateTopic = "~/state/slave/fault/code"; - value->availabilityTopic = "~/state/slave/fault/available"; + value->availabilityTopic = "~/state/slave/fault/active"; value->category = HA::Category::DIAGNOSTIC; value->icon = "mdi:cog-box"; value->valueTemplate = "{{ '%02d (0x%02X)' | format(value | int, value | int) }}"; @@ -515,7 +513,7 @@ bool publishHaEntity(HaEntity entityId, bool remove) { auto value = std::make_unique("diagnostic_code", "Diagnostic code"); if (!remove) { value->stateTopic = "~/state/slave/diag/code"; - value->availabilityTopic = "~/state/slave/diag/available"; + value->availabilityTopic = "~/state/slave/diag/active"; value->category = HA::Category::DIAGNOSTIC; value->icon = "mdi:information-box"; value->valueTemplate = "{{ '%02d (0x%02X)' | format(value | int, value | int) }}"; @@ -532,8 +530,8 @@ bool publishHaEntity(HaEntity entityId, bool remove) { auto gauge = std::make_unique("rssi", "RSSI"); if (!remove) { gauge->stateTopic = "~/state/master/network/rssi"; - gauge->availabilityTopic = "~/state/master/network/connection_type"; - gauge->availabilityTemplate = "{{ 'online' if value == 'wifi' else 'offline' }}"; + gauge->availabilityTopic = gauge->stateTopic; + gauge->availabilityTemplate = "{{ 'online' if value | int(0) < 0 else 'offline' }}"; gauge->category = HA::Category::DIAGNOSTIC; gauge->deviceClass = "signal_strength"; gauge->icon = "mdi:signal"; diff --git a/src/mqtt.cpp b/src/mqtt.cpp index f8a275e..91f8c37 100644 --- a/src/mqtt.cpp +++ b/src/mqtt.cpp @@ -1,6 +1,8 @@ #define LOG_LOCAL_LEVEL ESP_LOG_VERBOSE #include +#include "StructSnapshot.h" +#include "StructTimestampMap.h" #include #include @@ -15,6 +17,8 @@ #include #include #include +#include +#include static std::unique_ptr mqttClient; @@ -23,6 +27,12 @@ static std::string mqttPrefix; // NOLINT(runtime/string) static std::atomic mqttConnectedAt{0}; static unsigned long mqttPublishedSensorAt[SENSORS_AMOUNT] = {}; +// Only Core accesses snapshots and the sorted publication timestamps. +static StructSnapshot settingsSnapshot(settings); +static StructSnapshot varsSnapshot(vars); +static StructTimestampMap<96> mqttPublishTimes; + +static std::atomic mqttRefreshRequested{false}; static std::atomic mqttHaRefreshRequested{false}; // Discovery and its synchronous publisher run only on Core. static std::bitset mqttHaPendingEntities; @@ -127,183 +137,904 @@ static bool publishValue(const std::string& topic, float value, uint8_t decimals return publishValue(topic, payload); } -static void publishHeatingSettings() { - publishValue(mqttTopic("settings/heating/enabled"), settings.heating.enabled); - publishValue(mqttTopic("settings/heating/turbo"), settings.heating.turbo); - publishValue(mqttTopic("settings/heating/target"), convertTemp(settings.heating.target, UnitSystem::METRIC, settings.system.unitSystem), 2); - publishValue(mqttTopic("settings/heating/hysteresis/enabled"), settings.heating.hysteresis.enabled); - publishValue(mqttTopic("settings/heating/hysteresis/value"), settings.heating.hysteresis.value, 3); - publishValue(mqttTopic("settings/heating/turbo_factor"), settings.heating.turboFactor, 3); - publishValue(mqttTopic("settings/heating/min_temp"), convertTemp(settings.heating.minTemp, UnitSystem::METRIC, settings.system.unitSystem), 2); - publishValue(mqttTopic("settings/heating/max_temp"), convertTemp(settings.heating.maxTemp, UnitSystem::METRIC, settings.system.unitSystem), 2); - publishValue(mqttTopic("settings/heating/max_modulation"), std::to_string(settings.heating.maxModulation)); - publishValue(mqttTopic("settings/heating/indoor_temp_avg_type"), std::to_string(static_cast(settings.heating.indoorTempAvgType))); - publishValue(mqttTopic("settings/heating/outdoor_temp_avg_type"), std::to_string(static_cast(settings.heating.outdoorTempAvgType))); - publishValue( - mqttTopic("settings/heating/overheat_protection/high_temp"), - settings.heating.overheatProtection.highTemp == 0 - ? 0 - : convertTemp(settings.heating.overheatProtection.highTemp, UnitSystem::METRIC, settings.system.unitSystem), - 2 - ); - publishValue( - mqttTopic("settings/heating/overheat_protection/low_temp"), - settings.heating.overheatProtection.lowTemp == 0 - ? 0 - : convertTemp(settings.heating.overheatProtection.lowTemp, UnitSystem::METRIC, settings.system.unitSystem), - 2 - ); - publishValue(mqttTopic("settings/heating/freeze_protection/high_temp"), convertTemp(settings.heating.freezeProtection.highTemp, UnitSystem::METRIC, settings.system.unitSystem), 2); - publishValue(mqttTopic("settings/heating/freeze_protection/low_temp"), convertTemp(settings.heating.freezeProtection.lowTemp, UnitSystem::METRIC, settings.system.unitSystem), 2); +static void collectMqttChanges() { + if (mqttRefreshRequested.exchange(false)) { + mqttPublishTimes.resetAll(); + } + + if (!settingsSnapshot.equals(&settings.system.unitSystem)) { + mqttPublishTimes.reset({ + &settings.heating.target, &settings.heating.minTemp, &settings.heating.maxTemp, + &settings.heating.overheatProtection.highTemp, &settings.heating.overheatProtection.lowTemp, + &settings.heating.freezeProtection.highTemp, &settings.heating.freezeProtection.lowTemp, + &settings.dhw.target, &settings.dhw.minTemp, &settings.dhw.maxTemp, + &settings.dhw.overheatProtection.highTemp, &settings.dhw.overheatProtection.lowTemp, + &settings.pid.minTemp, &settings.pid.maxTemp, &settings.emergency.target + }); + + mqttPublishTimes.reset({ + &vars.slave.heating.minTemp, &vars.slave.heating.maxTemp, + &vars.slave.dhw.minTemp, &vars.slave.dhw.maxTemp, + &vars.master.heating.setpointTemp, &vars.master.heating.targetTemp, + &vars.master.heating.currentTemp, &vars.master.heating.returnTemp, + &vars.master.heating.indoorTemp, &vars.master.heating.outdoorTemp, + &vars.master.heating.minTemp, &vars.master.heating.maxTemp, + &vars.master.dhw.targetTemp, &vars.master.dhw.currentTemp, &vars.master.dhw.returnTemp, + &vars.master.dhw.minTemp, &vars.master.dhw.maxTemp + }); + settingsSnapshot.set(&settings.system.unitSystem); + } + + if (!settingsSnapshot.equals(&settings.equitherm.enabled)) { + mqttPublishTimes.reset({&settings.pid.minTemp, &settings.pid.maxTemp}); + } } -static void publishDhwSettings() { - publishValue(mqttTopic("settings/dhw/enabled"), settings.dhw.enabled); - publishValue(mqttTopic("settings/dhw/target"), convertTemp(settings.dhw.target, UnitSystem::METRIC, settings.system.unitSystem), 1); - publishValue(mqttTopic("settings/dhw/min_temp"), convertTemp(settings.dhw.minTemp, UnitSystem::METRIC, settings.system.unitSystem), 2); - publishValue(mqttTopic("settings/dhw/max_temp"), convertTemp(settings.dhw.maxTemp, UnitSystem::METRIC, settings.system.unitSystem), 2); - publishValue(mqttTopic("settings/dhw/max_modulation"), std::to_string(settings.dhw.maxModulation)); - publishValue( - mqttTopic("settings/dhw/overheat_protection/high_temp"), - settings.dhw.overheatProtection.highTemp == 0 - ? 0 - : convertTemp(settings.dhw.overheatProtection.highTemp, UnitSystem::METRIC, settings.system.unitSystem), - 2 - ); - publishValue( - mqttTopic("settings/dhw/overheat_protection/low_temp"), - settings.dhw.overheatProtection.lowTemp == 0 - ? 0 - : convertTemp(settings.dhw.overheatProtection.lowTemp, UnitSystem::METRIC, settings.system.unitSystem), - 2 - ); +template +static bool publishField( + StructSnapshot& snapshot, const Field* field, + unsigned long intervalMs, Publish publish +) { + const unsigned long now = millis(); + unsigned long& lastPublishAt = mqttPublishTimes.get(field); + if (lastPublishAt != 0 && now - lastPublishAt < intervalMs && snapshot.equals(field)) { + return false; + } + + const Field value = *field; + if (publish(value)) { + lastPublishAt = now; + snapshot.set(field, &value); + } + + // A failed send still consumes this pass; unchanged state allows a retry. + return true; } -static void publishPidSettings() { - publishValue(mqttTopic("settings/pid/enabled"), settings.pid.enabled); - publishValue(mqttTopic("settings/pid/p_factor"), settings.pid.p_factor, 3); - publishValue(mqttTopic("settings/pid/i_factor"), settings.pid.i_factor, 4); - publishValue(mqttTopic("settings/pid/d_factor"), settings.pid.d_factor, 1); - publishValue(mqttTopic("settings/pid/dt"), std::to_string(settings.pid.dt)); - publishValue( - mqttTopic("settings/pid/min_temp"), - settings.equitherm.enabled - ? settings.pid.minTemp - : convertTemp(settings.pid.minTemp, UnitSystem::METRIC, settings.system.unitSystem), - 2 - ); - publishValue( - mqttTopic("settings/pid/max_temp"), - settings.equitherm.enabled - ? settings.pid.maxTemp - : convertTemp(settings.pid.maxTemp, UnitSystem::METRIC, settings.system.unitSystem), - 2 - ); - publishValue(mqttTopic("settings/pid/deadband/enabled"), settings.pid.deadband.enabled); - publishValue(mqttTopic("settings/pid/deadband/p_multiplier"), settings.pid.deadband.p_multiplier, 3); - publishValue(mqttTopic("settings/pid/deadband/i_multiplier"), settings.pid.deadband.i_multiplier, 3); - publishValue(mqttTopic("settings/pid/deadband/d_multiplier"), settings.pid.deadband.d_multiplier, 3); - publishValue(mqttTopic("settings/pid/deadband/threshold_high"), settings.pid.deadband.thresholdHigh); - publishValue(mqttTopic("settings/pid/deadband/threshold_low"), settings.pid.deadband.thresholdLow); +template +static bool publishField( + StructSnapshot& snapshot, const Field* field, + unsigned long intervalMs, const char* topicSuffix +) { + return publishField(snapshot, field, intervalMs, [topicSuffix](const Field& value) { + const auto topic = mqttTopic(topicSuffix); + if constexpr (std::is_enum_v) { + return publishValue(topic, std::to_string(static_cast>(value))); + + } else if constexpr (std::is_integral_v && !std::is_same_v) { + return publishValue(topic, std::to_string(value)); + + } else { + return publishValue(topic, value); + } + }); } -static void publishEquithermSettings() { - publishValue(mqttTopic("settings/equitherm/enabled"), settings.equitherm.enabled); - publishValue(mqttTopic("settings/equitherm/slope"), settings.equitherm.slope, 3); - publishValue(mqttTopic("settings/equitherm/exponent"), settings.equitherm.exponent, 3); - publishValue(mqttTopic("settings/equitherm/shift"), settings.equitherm.shift, 2); - publishValue(mqttTopic("settings/equitherm/target_diff_factor"), settings.equitherm.targetDiffFactor, 3); -} +static bool publishHeatingSettings(unsigned long intervalMs) { + bool published = publishField(settingsSnapshot, &settings.heating.enabled, intervalMs, "settings/heating/enabled"); + if (published) { + return true; + } -static void publishSettings() { - publishValue(mqttTopic("settings/emergency/target"), convertTemp(settings.emergency.target, UnitSystem::METRIC, settings.system.unitSystem), 2); - publishHeatingSettings(); - publishDhwSettings(); - publishPidSettings(); - publishEquithermSettings(); -} + published = publishField(settingsSnapshot, &settings.heating.turbo, intervalMs, "settings/heating/turbo"); + if (published) { + return true; + } -static void publishSlaveState() { - publishValue(mqttTopic("state/slave/member_id"), std::to_string(vars.slave.memberId)); - publishValue(mqttTopic("state/slave/flags"), std::to_string(vars.slave.flags)); - publishValue(mqttTopic("state/slave/type"), std::to_string(vars.slave.type)); - publishValue(mqttTopic("state/slave/app_version"), std::to_string(vars.slave.appVersion)); - publishValue(mqttTopic("state/slave/protocol_version"), vars.slave.protocolVersion); - publishValue(mqttTopic("state/slave/connected"), vars.slave.connected); - publishValue(mqttTopic("state/slave/flame"), vars.slave.flame); - publishValue(mqttTopic("state/slave/cooling/active"), vars.slave.cooling.active); - publishValue(mqttTopic("state/slave/cooling/setpoint"), std::to_string(vars.slave.cooling.setpoint)); - publishValue(mqttTopic("state/slave/modulation/min"), std::to_string(vars.slave.modulation.min)); - publishValue(mqttTopic("state/slave/modulation/max"), std::to_string(vars.slave.modulation.max)); - publishValue(mqttTopic("state/slave/power/min"), vars.slave.power.min, 2); - publishValue(mqttTopic("state/slave/power/max"), vars.slave.power.max, 2); - publishValue(mqttTopic("state/slave/heating/active"), vars.slave.heating.active); - publishValue(mqttTopic("state/slave/heating/min_temp"), convertTemp(vars.slave.heating.minTemp, UnitSystem::METRIC, settings.system.unitSystem), 2); - publishValue(mqttTopic("state/slave/heating/max_temp"), convertTemp(vars.slave.heating.maxTemp, UnitSystem::METRIC, settings.system.unitSystem), 2); - publishValue(mqttTopic("state/slave/dhw/active"), vars.slave.dhw.active); - publishValue(mqttTopic("state/slave/dhw/min_temp"), convertTemp(vars.slave.dhw.minTemp, UnitSystem::METRIC, settings.system.unitSystem), 2); - publishValue(mqttTopic("state/slave/dhw/max_temp"), convertTemp(vars.slave.dhw.maxTemp, UnitSystem::METRIC, settings.system.unitSystem), 2); - publishValue(mqttTopic("state/slave/fault/active"), vars.slave.fault.active); - publishValue(mqttTopic("state/slave/fault/code"), std::to_string(vars.slave.fault.code)); - publishValue(mqttTopic("state/slave/fault/available"), vars.slave.connected && vars.slave.fault.active); - publishValue(mqttTopic("state/slave/diag/active"), vars.slave.diag.active); - publishValue(mqttTopic("state/slave/diag/code"), std::to_string(vars.slave.diag.code)); - publishValue(mqttTopic("state/slave/diag/available"), (vars.slave.connected && vars.slave.fault.active) || vars.slave.diag.active); -} - -static void publishMasterState() { - publishValue(mqttTopic("state/master/heating/enabled"), vars.master.heating.enabled); - publishValue(mqttTopic("state/master/heating/blocking"), vars.master.heating.blocking); - publishValue(mqttTopic("state/master/heating/smart_control"), vars.master.heating.smartControl); - publishValue(mqttTopic("state/master/heating/overheat"), vars.master.heating.overheat); - publishValue(mqttTopic("state/master/heating/freezing"), vars.master.heating.freezing); - publishValue( - mqttTopic("state/master/heating/setpoint_temp"), - vars.master.heating.setpointTemp == 0 - ? 0 - : convertTemp(vars.master.heating.setpointTemp, UnitSystem::METRIC, settings.system.unitSystem), - 2 + published = publishField( + settingsSnapshot, &settings.heating.target, intervalMs, [](float value) { + return publishValue( + mqttTopic("settings/heating/target"), + convertTemp(value, UnitSystem::METRIC, settings.system.unitSystem), + 2 + ); + } ); - publishValue( - mqttTopic("state/master/heating/target_temp"), - vars.master.heating.targetTemp == 0 - ? 0 - : convertTemp(vars.master.heating.targetTemp, UnitSystem::METRIC, settings.system.unitSystem), - 2 + if (published) { + return true; + } + + published = publishField(settingsSnapshot, &settings.heating.hysteresis.enabled, intervalMs, "settings/heating/hysteresis/enabled"); + if (published) { + return true; + } + + published = publishField( + settingsSnapshot, &settings.heating.hysteresis.value, intervalMs, [](float value) { + return publishValue( + mqttTopic("settings/heating/hysteresis/value"), + value, + 3 + ); + } ); - publishValue(mqttTopic("state/master/heating/current_temp"), convertTemp(vars.master.heating.currentTemp, UnitSystem::METRIC, settings.system.unitSystem), 2); - publishValue(mqttTopic("state/master/heating/return_temp"), convertTemp(vars.master.heating.returnTemp, UnitSystem::METRIC, settings.system.unitSystem), 2); - publishValue(mqttTopic("state/master/heating/indoor_temp"), convertTemp(vars.master.heating.indoorTemp, UnitSystem::METRIC, settings.system.unitSystem), 2); - publishValue(mqttTopic("state/master/heating/outdoor_temp"), convertTemp(vars.master.heating.outdoorTemp, UnitSystem::METRIC, settings.system.unitSystem), 2); - publishValue(mqttTopic("state/master/heating/min_temp"), convertTemp(vars.master.heating.minTemp, UnitSystem::METRIC, settings.system.unitSystem), 2); - publishValue(mqttTopic("state/master/heating/max_temp"), convertTemp(vars.master.heating.maxTemp, UnitSystem::METRIC, settings.system.unitSystem), 2); - publishValue(mqttTopic("state/master/dhw/enabled"), vars.master.dhw.enabled); - publishValue(mqttTopic("state/master/dhw/overheat"), vars.master.dhw.overheat); - publishValue( - mqttTopic("state/master/dhw/target_temp"), - vars.master.dhw.targetTemp == 0 - ? 0 - : convertTemp(vars.master.dhw.targetTemp, UnitSystem::METRIC, settings.system.unitSystem), - 2 + if (published) { + return true; + } + + published = publishField( + settingsSnapshot, &settings.heating.turboFactor, intervalMs, [](float value) { + return publishValue(mqttTopic("settings/heating/turbo_factor"), value, 3); + } ); - publishValue(mqttTopic("state/master/dhw/current_temp"), convertTemp(vars.master.dhw.currentTemp, UnitSystem::METRIC, settings.system.unitSystem), 2); - publishValue(mqttTopic("state/master/dhw/return_temp"), convertTemp(vars.master.dhw.returnTemp, UnitSystem::METRIC, settings.system.unitSystem), 2); - publishValue(mqttTopic("state/master/dhw/min_temp"), convertTemp(settings.dhw.minTemp, UnitSystem::METRIC, settings.system.unitSystem), 2); - publishValue(mqttTopic("state/master/dhw/max_temp"), convertTemp(settings.dhw.maxTemp, UnitSystem::METRIC, settings.system.unitSystem), 2); - publishValue(mqttTopic("state/master/network/connected"), isNetworkConnected()); - publishValue(mqttTopic("state/master/network/rssi"), std::to_string(vars.network.rssi)); - publishValue(mqttTopic("state/master/network/connection_type"), getNetworkConnectionType()); - publishValue(mqttTopic("state/master/emergency/state"), vars.emergency.state); - publishValue(mqttTopic("state/master/ext_pump/state"), vars.externalPump.state); - publishValue(mqttTopic("state/master/cascade_control/input"), vars.cascadeControl.input); - publishValue(mqttTopic("state/master/cascade_control/output"), vars.cascadeControl.output); - publishValue(mqttTopic("state/master/uptime"), std::to_string(millis() / 1000u)); + if (published) { + return true; + } + + published = publishField( + settingsSnapshot, &settings.heating.minTemp, intervalMs, [](float value) { + return publishValue( + mqttTopic("settings/heating/min_temp"), + convertTemp(value, UnitSystem::METRIC, settings.system.unitSystem), + 2 + ); + } + ); + if (published) { + return true; + } + + published = publishField( + settingsSnapshot, &settings.heating.maxTemp, intervalMs, [](float value) { + return publishValue( + mqttTopic("settings/heating/max_temp"), + convertTemp(value, UnitSystem::METRIC, settings.system.unitSystem), + 2 + ); + } + ); + if (published) { + return true; + } + + published = publishField(settingsSnapshot, &settings.heating.maxModulation, intervalMs, "settings/heating/max_modulation"); + if (published) { + return true; + } + + published = publishField(settingsSnapshot, &settings.heating.indoorTempAvgType, intervalMs, "settings/heating/indoor_temp_avg_type"); + if (published) { + return true; + } + + published = publishField(settingsSnapshot, &settings.heating.outdoorTempAvgType, intervalMs, "settings/heating/outdoor_temp_avg_type"); + if (published) { + return true; + } + + published = publishField( + settingsSnapshot, &settings.heating.overheatProtection.highTemp, intervalMs, [](float value) { + return publishValue( + mqttTopic("settings/heating/overheat_protection/high_temp"), + value == 0 + ? 0 + : convertTemp(value, UnitSystem::METRIC, settings.system.unitSystem), + 2 + ); + } + ); + if (published) { + return true; + } + + published = publishField( + settingsSnapshot, &settings.heating.overheatProtection.lowTemp, intervalMs, [](float value) { + return publishValue( + mqttTopic("settings/heating/overheat_protection/low_temp"), + value == 0 + ? 0 + : convertTemp(value, UnitSystem::METRIC, settings.system.unitSystem), + 2 + ); + } + ); + if (published) { + return true; + } + + published = publishField( + settingsSnapshot, &settings.heating.freezeProtection.highTemp, intervalMs, [](float value) { + return publishValue( + mqttTopic("settings/heating/freeze_protection/high_temp"), + convertTemp(value, UnitSystem::METRIC, settings.system.unitSystem), + 2 + ); + } + ); + if (published) { + return true; + } + + published = publishField( + settingsSnapshot, &settings.heating.freezeProtection.lowTemp, intervalMs, [](float value) { + return publishValue( + mqttTopic("settings/heating/freeze_protection/low_temp"), + convertTemp(value, UnitSystem::METRIC, settings.system.unitSystem), + 2 + ); + } + ); + if (published) { + return true; + } + return false; } -static void publishState() { - publishValue(mqttTopic("status"), "online"); - publishSlaveState(); - publishMasterState(); +static bool publishDhwSettings(unsigned long intervalMs) { + bool published = publishField(settingsSnapshot, &settings.dhw.enabled, intervalMs, "settings/dhw/enabled"); + if (published) { + return true; + } + + published = publishField( + settingsSnapshot, &settings.dhw.target, intervalMs, [](float value) { + return publishValue( + mqttTopic("settings/dhw/target"), + convertTemp(value, UnitSystem::METRIC, settings.system.unitSystem), + 1 + ); + } + ); + if (published) { + return true; + } + + published = publishField( + settingsSnapshot, &settings.dhw.minTemp, intervalMs, [](float value) { + return publishValue( + mqttTopic("settings/dhw/min_temp"), + convertTemp(value, UnitSystem::METRIC, settings.system.unitSystem), + 2 + ); + } + ); + if (published) { + return true; + } + + published = publishField( + settingsSnapshot, &settings.dhw.maxTemp, intervalMs, [](float value) { + return publishValue( + mqttTopic("settings/dhw/max_temp"), + convertTemp(value, UnitSystem::METRIC, settings.system.unitSystem), + 2 + ); + } + ); + if (published) { + return true; + } + + published = publishField(settingsSnapshot, &settings.dhw.maxModulation, intervalMs, "settings/dhw/max_modulation"); + if (published) { + return true; + } + + published = publishField( + settingsSnapshot, &settings.dhw.overheatProtection.highTemp, intervalMs, [](float value) { + return publishValue( + mqttTopic("settings/dhw/overheat_protection/high_temp"), + value == 0 + ? 0 + : convertTemp(value, UnitSystem::METRIC, settings.system.unitSystem), + 2 + ); + } + ); + if (published) { + return true; + } + + published = publishField( + settingsSnapshot, &settings.dhw.overheatProtection.lowTemp, intervalMs, [](float value) { + return publishValue( + mqttTopic("settings/dhw/overheat_protection/low_temp"), + value == 0 + ? 0 + : convertTemp(value, UnitSystem::METRIC, settings.system.unitSystem), + 2 + ); + } + ); + if (published) { + return true; + } + return false; +} + +static bool publishPidSettings(unsigned long intervalMs) { + bool published = publishField(settingsSnapshot, &settings.pid.enabled, intervalMs, "settings/pid/enabled"); + if (published) { + return true; + } + + published = publishField( + settingsSnapshot, &settings.pid.p_factor, intervalMs, [](float value) { + return publishValue(mqttTopic("settings/pid/p_factor"), value, 3); + } + ); + if (published) { + return true; + } + + published = publishField( + settingsSnapshot, &settings.pid.i_factor, intervalMs, [](float value) { + return publishValue(mqttTopic("settings/pid/i_factor"), value, 4); + } + ); + if (published) { + return true; + } + + published = publishField( + settingsSnapshot, &settings.pid.d_factor, intervalMs, [](float value) { + return publishValue(mqttTopic("settings/pid/d_factor"), value, 1); + } + ); + if (published) { + return true; + } + + published = publishField(settingsSnapshot, &settings.pid.dt, intervalMs, "settings/pid/dt"); + if (published) { + return true; + } + + published = publishField( + settingsSnapshot, &settings.pid.minTemp, intervalMs, [](float value) { + return publishValue( + mqttTopic("settings/pid/min_temp"), + settings.equitherm.enabled + ? value + : convertTemp(value, UnitSystem::METRIC, settings.system.unitSystem), + 2 + ); + } + ); + if (published) { + return true; + } + + published = publishField( + settingsSnapshot, &settings.pid.maxTemp, intervalMs, [](float value) { + return publishValue( + mqttTopic("settings/pid/max_temp"), + settings.equitherm.enabled + ? value + : convertTemp(value, UnitSystem::METRIC, settings.system.unitSystem), + 2 + ); + } + ); + if (published) { + return true; + } + + published = publishField(settingsSnapshot, &settings.pid.deadband.enabled, intervalMs, "settings/pid/deadband/enabled"); + if (published) { + return true; + } + + published = publishField( + settingsSnapshot, &settings.pid.deadband.p_multiplier, intervalMs, [](float value) { + return publishValue( + mqttTopic("settings/pid/deadband/p_multiplier"), + value, + 3 + ); + } + ); + if (published) { + return true; + } + + published = publishField( + settingsSnapshot, &settings.pid.deadband.i_multiplier, intervalMs, [](float value) { + return publishValue( + mqttTopic("settings/pid/deadband/i_multiplier"), + value, + 3 + ); + } + ); + if (published) { + return true; + } + + published = publishField( + settingsSnapshot, &settings.pid.deadband.d_multiplier, intervalMs, [](float value) { + return publishValue( + mqttTopic("settings/pid/deadband/d_multiplier"), + value, + 3 + ); + } + ); + if (published) { + return true; + } + + published = publishField(settingsSnapshot, &settings.pid.deadband.thresholdHigh, intervalMs, "settings/pid/deadband/threshold_high"); + if (published) { + return true; + } + + published = publishField(settingsSnapshot, &settings.pid.deadband.thresholdLow, intervalMs, "settings/pid/deadband/threshold_low"); + if (published) { + return true; + } + return false; +} + +static bool publishEquithermSettings(unsigned long intervalMs) { + bool published = publishField(settingsSnapshot, &settings.equitherm.enabled, intervalMs, "settings/equitherm/enabled"); + if (published) { + return true; + } + + published = publishField( + settingsSnapshot, &settings.equitherm.slope, intervalMs, [](float value) { + return publishValue(mqttTopic("settings/equitherm/slope"), value, 3); + } + ); + if (published) { + return true; + } + + published = publishField( + settingsSnapshot, &settings.equitherm.exponent, intervalMs, [](float value) { + return publishValue(mqttTopic("settings/equitherm/exponent"), value, 3); + } + ); + if (published) { + return true; + } + + published = publishField(settingsSnapshot, &settings.equitherm.shift, intervalMs, "settings/equitherm/shift"); + if (published) { + return true; + } + + published = publishField( + settingsSnapshot, &settings.equitherm.targetDiffFactor, intervalMs, [](float value) { + return publishValue( + mqttTopic("settings/equitherm/target_diff_factor"), + value, + 3 + ); + } + ); + if (published) { + return true; + } + return false; +} + +static bool publishEmergencySettings(unsigned long intervalMs) { + bool published = publishField(settingsSnapshot, &settings.emergency.target, intervalMs, [](float value) { + return publishValue( + mqttTopic("settings/emergency/target"), + convertTemp(value, UnitSystem::METRIC, settings.system.unitSystem), + 2 + ); + }); + + return published; +} + +static bool publishSlaveState(unsigned long intervalMs) { + bool published = publishField(varsSnapshot, &vars.slave.memberId, intervalMs, "state/slave/member_id"); + if (published) { + return true; + } + + published = publishField(varsSnapshot, &vars.slave.flags, intervalMs, "state/slave/flags"); + if (published) { + return true; + } + + published = publishField(varsSnapshot, &vars.slave.type, intervalMs, "state/slave/type"); + if (published) { + return true; + } + + published = publishField(varsSnapshot, &vars.slave.appVersion, intervalMs, "state/slave/app_version"); + if (published) { + return true; + } + + published = publishField(varsSnapshot, &vars.slave.protocolVersion, intervalMs, "state/slave/protocol_version"); + if (published) { + return true; + } + + published = publishField(varsSnapshot, &vars.slave.connected, intervalMs, "state/slave/connected"); + if (published) { + return true; + } + + published = publishField(varsSnapshot, &vars.slave.flame, intervalMs, "state/slave/flame"); + if (published) { + return true; + } + + published = publishField(varsSnapshot, &vars.slave.cooling.active, intervalMs, "state/slave/cooling/active"); + if (published) { + return true; + } + + published = publishField(varsSnapshot, &vars.slave.cooling.setpoint, intervalMs, "state/slave/cooling/setpoint"); + if (published) { + return true; + } + + published = publishField(varsSnapshot, &vars.slave.modulation.min, intervalMs, "state/slave/modulation/min"); + if (published) { + return true; + } + + published = publishField(varsSnapshot, &vars.slave.modulation.max, intervalMs, "state/slave/modulation/max"); + if (published) { + return true; + } + + published = publishField(varsSnapshot, &vars.slave.power.min, intervalMs, "state/slave/power/min"); + if (published) { + return true; + } + + published = publishField(varsSnapshot, &vars.slave.power.max, intervalMs, "state/slave/power/max"); + if (published) { + return true; + } + + published = publishField(varsSnapshot, &vars.slave.heating.active, intervalMs, "state/slave/heating/active"); + if (published) { + return true; + } + + published = publishField( + varsSnapshot, &vars.slave.heating.minTemp, intervalMs, [](float value) { + return publishValue( + mqttTopic("state/slave/heating/min_temp"), + convertTemp(value, UnitSystem::METRIC, settings.system.unitSystem), + 2 + ); + } + ); + if (published) { + return true; + } + + published = publishField( + varsSnapshot, &vars.slave.heating.maxTemp, intervalMs, [](float value) { + return publishValue( + mqttTopic("state/slave/heating/max_temp"), + convertTemp(value, UnitSystem::METRIC, settings.system.unitSystem), + 2 + ); + } + ); + if (published) { + return true; + } + + published = publishField(varsSnapshot, &vars.slave.dhw.active, intervalMs, "state/slave/dhw/active"); + if (published) { + return true; + } + + published = publishField( + varsSnapshot, &vars.slave.dhw.minTemp, intervalMs, [](float value) { + return publishValue( + mqttTopic("state/slave/dhw/min_temp"), + convertTemp(value, UnitSystem::METRIC, settings.system.unitSystem), + 2 + ); + } + ); + if (published) { + return true; + } + + published = publishField( + varsSnapshot, &vars.slave.dhw.maxTemp, intervalMs, [](float value) { + return publishValue( + mqttTopic("state/slave/dhw/max_temp"), + convertTemp(value, UnitSystem::METRIC, settings.system.unitSystem), + 2 + ); + } + ); + if (published) { + return true; + } + + published = publishField(varsSnapshot, &vars.slave.fault.active, intervalMs, "state/slave/fault/active"); + if (published) { + return true; + } + + published = publishField(varsSnapshot, &vars.slave.fault.code, intervalMs, "state/slave/fault/code"); + if (published) { + return true; + } + + published = publishField(varsSnapshot, &vars.slave.diag.active, intervalMs, "state/slave/diag/active"); + if (published) { + return true; + } + + published = publishField(varsSnapshot, &vars.slave.diag.code, intervalMs, "state/slave/diag/code"); + if (published) { + return true; + } + return false; +} + +static bool publishMasterState(unsigned long intervalMs) { + const unsigned long now = millis(); + + { + static unsigned long lastPublishAt = 0; + if (now - lastPublishAt >= intervalMs) { + publishValue(mqttTopic("state/master/uptime"), std::to_string(now / 1000u)); + lastPublishAt = now; // force + return true; + } + } + + bool published = publishField(varsSnapshot, &vars.master.heating.enabled, intervalMs, "state/master/heating/enabled"); + if (published) { + return true; + } + + published = publishField(varsSnapshot, &vars.master.heating.blocking, intervalMs, "state/master/heating/blocking"); + if (published) { + return true; + } + + published = publishField(varsSnapshot, &vars.master.heating.smartControl, intervalMs, "state/master/heating/smart_control"); + if (published) { + return true; + } + + published = publishField(varsSnapshot, &vars.master.heating.overheat, intervalMs, "state/master/heating/overheat"); + if (published) { + return true; + } + + published = publishField(varsSnapshot, &vars.master.heating.freezing, intervalMs, "state/master/heating/freezing"); + if (published) { + return true; + } + + published = publishField( + varsSnapshot, &vars.master.heating.setpointTemp, intervalMs, [](float value) { + return publishValue( + mqttTopic("state/master/heating/setpoint_temp"), + value == 0 + ? 0 + : convertTemp(value, UnitSystem::METRIC, settings.system.unitSystem), + 2 + ); + } + ); + if (published) { + return true; + } + + published = publishField( + varsSnapshot, &vars.master.heating.targetTemp, intervalMs, [](float value) { + return publishValue( + mqttTopic("state/master/heating/target_temp"), + value == 0 + ? 0 + : convertTemp(value, UnitSystem::METRIC, settings.system.unitSystem), + 2 + ); + } + ); + if (published) { + return true; + } + + published = publishField( + varsSnapshot, &vars.master.heating.currentTemp, intervalMs, [](float value) { + return publishValue( + mqttTopic("state/master/heating/current_temp"), + convertTemp(value, UnitSystem::METRIC, settings.system.unitSystem), + 2 + ); + } + ); + if (published) { + return true; + } + + published = publishField( + varsSnapshot, &vars.master.heating.returnTemp, intervalMs, [](float value) { + return publishValue( + mqttTopic("state/master/heating/return_temp"), + convertTemp(value, UnitSystem::METRIC, settings.system.unitSystem), + 2 + ); + } + ); + if (published) { + return true; + } + + published = publishField( + varsSnapshot, &vars.master.heating.indoorTemp, intervalMs, [](float value) { + return publishValue( + mqttTopic("state/master/heating/indoor_temp"), + convertTemp(value, UnitSystem::METRIC, settings.system.unitSystem), + 2 + ); + } + ); + if (published) { + return true; + } + + published = publishField( + varsSnapshot, &vars.master.heating.outdoorTemp, intervalMs, [](float value) { + return publishValue( + mqttTopic("state/master/heating/outdoor_temp"), + convertTemp(value, UnitSystem::METRIC, settings.system.unitSystem), + 2 + ); + } + ); + if (published) { + return true; + } + + published = publishField( + varsSnapshot, &vars.master.heating.minTemp, intervalMs, [](float value) { + return publishValue( + mqttTopic("state/master/heating/min_temp"), + convertTemp(value, UnitSystem::METRIC, settings.system.unitSystem), + 2 + ); + } + ); + if (published) { + return true; + } + + published = publishField( + varsSnapshot, &vars.master.heating.maxTemp, intervalMs, [](float value) { + return publishValue( + mqttTopic("state/master/heating/max_temp"), + convertTemp(value, UnitSystem::METRIC, settings.system.unitSystem), + 2 + ); + } + ); + if (published) { + return true; + } + + published = publishField(varsSnapshot, &vars.master.dhw.enabled, intervalMs, "state/master/dhw/enabled"); + if (published) { + return true; + } + + published = publishField(varsSnapshot, &vars.master.dhw.overheat, intervalMs, "state/master/dhw/overheat"); + if (published) { + return true; + } + + published = publishField( + varsSnapshot, &vars.master.dhw.targetTemp, intervalMs, [](float value) { + return publishValue( + mqttTopic("state/master/dhw/target_temp"), + value == 0 + ? 0 + : convertTemp(value, UnitSystem::METRIC, settings.system.unitSystem), + 2 + ); + } + ); + if (published) { + return true; + } + + published = publishField( + varsSnapshot, &vars.master.dhw.currentTemp, intervalMs, [](float value) { + return publishValue( + mqttTopic("state/master/dhw/current_temp"), + convertTemp(value, UnitSystem::METRIC, settings.system.unitSystem), + 2 + ); + } + ); + if (published) { + return true; + } + + published = publishField( + varsSnapshot, &vars.master.dhw.returnTemp, intervalMs, [](float value) { + return publishValue( + mqttTopic("state/master/dhw/return_temp"), + convertTemp(value, UnitSystem::METRIC, settings.system.unitSystem), + 2 + ); + } + ); + if (published) { + return true; + } + + published = publishField( + varsSnapshot, &vars.master.dhw.minTemp, intervalMs, [](float value) { + return publishValue( + mqttTopic("state/master/dhw/min_temp"), + convertTemp(value, UnitSystem::METRIC, settings.system.unitSystem), + 2 + ); + } + ); + if (published) { + return true; + } + + published = publishField( + varsSnapshot, &vars.master.dhw.maxTemp, intervalMs, [](float value) { + return publishValue( + mqttTopic("state/master/dhw/max_temp"), + convertTemp(value, UnitSystem::METRIC, settings.system.unitSystem), + 2 + ); + } + ); + if (published) { + return true; + } + + published = publishField(varsSnapshot, &vars.network.rssi, intervalMs, "state/master/network/rssi"); + if (published) { + return true; + } + + published = publishField(varsSnapshot, &vars.emergency.state, intervalMs, "state/master/emergency/state"); + if (published) { + return true; + } + + published = publishField(varsSnapshot, &vars.externalPump.state, intervalMs, "state/master/ext_pump/state"); + if (published) { + return true; + } + + published = publishField(varsSnapshot, &vars.cascadeControl.input, intervalMs, "state/master/cascade_control/input"); + if (published) { + return true; + } + + published = publishField(varsSnapshot, &vars.cascadeControl.output, intervalMs, "state/master/cascade_control/output"); + if (published) { + return true; + } + return false; +} + +static void publishState(unsigned long intervalMs) { + const unsigned long now = millis(); + + { + static unsigned long lastPublishAt = 0; + if (now - lastPublishAt >= 5000UL) { + publishValue(mqttTopic("status"), "online"); + lastPublishAt = now; // force + return; + } + } + + publishSlaveState(intervalMs); + publishMasterState(intervalMs); } static bool isCommandValid(const std::string& topic, std::string_view payload) { @@ -663,6 +1394,7 @@ static void connect() { mqttClient->onConnect([] { mqttConnectedAt.store(millis()); + mqttRefreshRequested.store(true); mqttHaRefreshRequested.store(true); { std::lock_guard lock(mqttSensorsMutex); @@ -728,13 +1460,33 @@ Task mqttConfigureTask("Configure MQTT", Task::Type::ONCE, [](void*) { return; } - // Publish complete settings while MQTT is enabled. if (!mqttPublishSettingsTask) { auto* task = new Task("Publish MQTT settings", [](void*) { - publishSettings(); + const unsigned long intervalMs = settings.mqtt.interval * 1000UL; + collectMqttChanges(); + + if (publishEmergencySettings(intervalMs)) { + return; + } + + if (publishHeatingSettings(intervalMs)) { + return; + } + + if (publishDhwSettings(intervalMs)) { + return; + } + + if (publishEquithermSettings(intervalMs)) { + return; + } + + if (publishPidSettings(intervalMs)) { + return; + } }); - task->setEnabledWhen(isMqttReadyForPublish); + task->setInterval(50).setEnabledWhen(isMqttReadyForPublish); coreTaskManager.addTask(*task); std::lock_guard lock(mqttMutex); @@ -744,10 +1496,12 @@ Task mqttConfigureTask("Configure MQTT", Task::Type::ONCE, [](void*) { // Publish the current application and slave state. if (!mqttPublishStateTask) { auto* task = new Task("Publish MQTT state", [](void*) { - publishState(); + const unsigned long intervalMs = settings.mqtt.interval * 1000UL; + collectMqttChanges(); + publishState(intervalMs); }); - task->setEnabledWhen(isMqttReadyForPublish); + task->setInterval(50).setEnabledWhen(isMqttReadyForPublish); coreTaskManager.addTask(*task); std::lock_guard lock(mqttMutex); @@ -799,12 +1553,8 @@ Task mqttConfigureTask("Configure MQTT", Task::Type::ONCE, [](void*) { } if (interval != settings.mqtt.interval) { - const uint32_t previousExpiration = std::max(60u, 2u * interval); interval = settings.mqtt.interval; - const uint32_t expiration = std::max(60u, 2u * interval); - if (previousExpiration != expiration) { - mqttHaPendingEntities.set(); - } + mqttHaPendingEntities.set(); } if (heatingMinTemp != vars.slave.heating.minTemp) { @@ -1078,12 +1828,6 @@ Task mqttConfigureTask("Configure MQTT", Task::Type::ONCE, [](void*) { mqttPublishSensorsTask = task; } - // Refresh settings every ten configured intervals to reduce repeated configuration traffic. - mqttPublishSettingsTask->setInterval(settings.mqtt.interval * 10000u); - - // Publish state at the configured interval (seconds converted to milliseconds). - mqttPublishStateTask->setInterval(settings.mqtt.interval * 1000u); - // Call end() to reset the connection stopMqtt(false); diff --git a/src/opentherm.cpp b/src/opentherm.cpp index 1fb039e..370b5e7 100644 --- a/src/opentherm.cpp +++ b/src/opentherm.cpp @@ -342,6 +342,8 @@ static void updateMasterState() { settings.dhw.enabled && !vars.master.dhw.overheat; vars.master.dhw.targetTemp = settings.dhw.target; + vars.master.dhw.minTemp = settings.dhw.minTemp; + vars.master.dhw.maxTemp = settings.dhw.maxTemp; // CH2 settings vars.master.ch2.enabled = diff --git a/src_data/pages/settings.html b/src_data/pages/settings.html index 28a5c68..7c5f775 100644 --- a/src_data/pages/settings.html +++ b/src_data/pages/settings.html @@ -840,7 +840,7 @@