From a6295891220d222e3f2a291bffe7c2138f4a95c9 Mon Sep 17 00:00:00 2001 From: Yurii Date: Sun, 29 Nov 2026 05:43:52 +0300 Subject: [PATCH] fix: normalize setting changes and reject non-finite control values Round settings to their stored precision before detecting changes and applying relevant range checks. Accept the minimum sensor factor of 0.09. Handle matching NaNs and infinities consistently in float comparisons. Reject non-finite OpenTherm setpoints and PID results before they affect heating control. --- include/utils.h | 12 ++++++++ src/config.cpp | 72 ++++++++++++++++++++++++++++------------------- src/opentherm.cpp | 20 +++++++++++++ src/regulator.cpp | 2 +- 4 files changed, 76 insertions(+), 30 deletions(-) diff --git a/include/utils.h b/include/utils.h index fa3736f..56580c2 100644 --- a/include/utils.h +++ b/include/utils.h @@ -159,6 +159,18 @@ inline bool isValidTemp(const float value, UnitSystem unit, const float min = 0. } inline bool isFloatDifferent(float first, float second, float tolerance) { + if (first == second) { + return false; + } + + if (std::isnan(first) && std::isnan(second)) { + return false; + } + + if (!std::isfinite(first) || !std::isfinite(second)) { + return true; + } + return fabsf(first - second) > tolerance; } diff --git a/src/config.cpp b/src/config.cpp index 7fcdec5..35082a9 100644 --- a/src/config.cpp +++ b/src/config.cpp @@ -977,24 +977,24 @@ SettingResult fromJson(JsonVariantConst src, OpenThermSettings& dst) { result.accepted = ch2DisablingActionResult.accepted || result.accepted; if (src[S_MIN_POWER].is()) { - const auto value = src[S_MIN_POWER].as(); + const auto value = roundf(src[S_MIN_POWER].as(), 2); if (value >= 0 && value <= 1000) { result.accepted = true; if (isFloatDifferent(value, dst.minPower, 0.0001f)) { - dst.minPower = roundf(value, 2); + dst.minPower = value; result.changed = true; } } } if (src[S_MAX_POWER].is()) { - const auto value = src[S_MAX_POWER].as(); + const auto value = roundf(src[S_MAX_POWER].as(), 2); if (value >= 0 && value <= 1000) { result.accepted = true; if (isFloatDifferent(value, dst.maxPower, 0.0001f)) { - dst.maxPower = roundf(value, 2); + dst.maxPower = value; result.changed = true; } } @@ -1364,10 +1364,10 @@ static SettingResult setEmergencyTarget(EmergencySettings& dst, float value) { value = noRegulators ? DEFAULT_HEATING_TARGET_TEMP : THERMOSTAT_INDOOR_DEFAULT_TEMP; } - value = constrain(value, minTemp, maxTemp); + value = roundf(constrain(value, minTemp, maxTemp), 2); if (isFloatDifferent(dst.target, value, 0.0001f)) { - dst.target = roundf(value, 2); + dst.target = value; changed = true; } @@ -1475,10 +1475,10 @@ static SettingResult setHeatingTarget(HeatingSettings& dst, float value) { value = smartControl ? THERMOSTAT_INDOOR_DEFAULT_TEMP : DEFAULT_HEATING_TARGET_TEMP; } - value = constrain(value, minTemp, maxTemp); + value = roundf(constrain(value, minTemp, maxTemp), 2); if (isFloatDifferent(dst.target, value, 0.0001f)) { - dst.target = roundf(value, 2); + dst.target = value; changed = true; } @@ -1536,8 +1536,9 @@ static SettingResult applySetting(HeatingSettings& dst, std::string_view path, f return {}; } + value = roundf(value, 2); if (isFloatDifferent(value, dst.hysteresis.value, 0.0001f)) { - dst.hysteresis.value = roundf(value, 2); + dst.hysteresis.value = value; changed = true; } @@ -1555,8 +1556,9 @@ static SettingResult applySetting(HeatingSettings& dst, std::string_view path, f return {}; } + value = roundf(value, 3); if (isFloatDifferent(value, dst.turboFactor, 0.0001f)) { - dst.turboFactor = roundf(value, 3); + dst.turboFactor = value; changed = true; } @@ -2242,8 +2244,9 @@ static SettingResult applySetting(PidSettings& dst, std::string_view path, float return {}; } + value = roundf(value, 3); if (isFloatDifferent(value, dst.p_factor, 0.0001f)) { - dst.p_factor = roundf(value, 3); + dst.p_factor = value; changed = true; } @@ -2261,8 +2264,9 @@ static SettingResult applySetting(PidSettings& dst, std::string_view path, float return {}; } + value = roundf(value, 4); if (isFloatDifferent(value, dst.i_factor, 0.0001f)) { - dst.i_factor = roundf(value, 4); + dst.i_factor = value; changed = true; } @@ -2280,8 +2284,9 @@ static SettingResult applySetting(PidSettings& dst, std::string_view path, float return {}; } + value = roundf(value, 1); if (isFloatDifferent(value, dst.d_factor, 0.0001f)) { - dst.d_factor = roundf(value, 1); + dst.d_factor = value; changed = true; } @@ -2374,8 +2379,9 @@ static SettingResult applySetting(PidSettings& dst, std::string_view path, float return {}; } + value = roundf(value, 3); if (isFloatDifferent(value, dst.deadband.p_multiplier, 0.0001f)) { - dst.deadband.p_multiplier = roundf(value, 3); + dst.deadband.p_multiplier = value; changed = true; } @@ -2393,8 +2399,9 @@ static SettingResult applySetting(PidSettings& dst, std::string_view path, float return {}; } + value = roundf(value, 3); if (isFloatDifferent(value, dst.deadband.i_multiplier, 0.0001f)) { - dst.deadband.i_multiplier = roundf(value, 3); + dst.deadband.i_multiplier = value; changed = true; } @@ -2412,8 +2419,9 @@ static SettingResult applySetting(PidSettings& dst, std::string_view path, float return {}; } + value = roundf(value, 3); if (isFloatDifferent(value, dst.deadband.d_multiplier, 0.0001f)) { - dst.deadband.d_multiplier = roundf(value, 3); + dst.deadband.d_multiplier = value; changed = true; } @@ -2431,8 +2439,9 @@ static SettingResult applySetting(PidSettings& dst, std::string_view path, float return {}; } + value = roundf(value, 2); if (isFloatDifferent(value, dst.deadband.thresholdHigh, 0.0001f)) { - dst.deadband.thresholdHigh = roundf(value, 2); + dst.deadband.thresholdHigh = value; changed = true; } @@ -2450,8 +2459,9 @@ static SettingResult applySetting(PidSettings& dst, std::string_view path, float return {}; } + value = roundf(value, 2); if (isFloatDifferent(value, dst.deadband.thresholdLow, 0.0001f)) { - dst.deadband.thresholdLow = roundf(value, 2); + dst.deadband.thresholdLow = value; changed = true; } @@ -2630,8 +2640,9 @@ static SettingResult applySetting(EquithermSettings& dst, std::string_view path, return {}; } + value = roundf(value, 3); if (isFloatDifferent(value, dst.slope, 0.0001f)) { - dst.slope = roundf(value, 3); + dst.slope = value; changed = true; } @@ -2649,8 +2660,9 @@ static SettingResult applySetting(EquithermSettings& dst, std::string_view path, return {}; } + value = roundf(value, 3); if (isFloatDifferent(value, dst.exponent, 0.0001f)) { - dst.exponent = roundf(value, 3); + dst.exponent = value; changed = true; } @@ -2668,8 +2680,9 @@ static SettingResult applySetting(EquithermSettings& dst, std::string_view path, return {}; } + value = roundf(value, 2); if (isFloatDifferent(value, dst.shift, 0.0001f)) { - dst.shift = roundf(value, 2); + dst.shift = value; changed = true; } @@ -2687,8 +2700,9 @@ static SettingResult applySetting(EquithermSettings& dst, std::string_view path, return {}; } + value = roundf(value, 3); if (isFloatDifferent(value, dst.targetDiffFactor, 0.0001f)) { - dst.targetDiffFactor = roundf(value, 3); + dst.targetDiffFactor = value; changed = true; } @@ -3407,12 +3421,12 @@ SettingResult fromJson(const JsonVariantConst src, Sensors::Settings& dst) { // offset if (src[S_OFFSET].is()) { - const auto value = src[S_OFFSET].as(); + const auto value = roundf(src[S_OFFSET].as(), 2); if (value >= -20.0f && value <= 20.0f) { result.accepted = true; if (isFloatDifferent(value, dst.offset, 0.0001f)) { - dst.offset = roundf(value, 2); + dst.offset = value; result.changed = true; } } @@ -3420,12 +3434,12 @@ SettingResult fromJson(const JsonVariantConst src, Sensors::Settings& dst) { // factor if (src[S_FACTOR].is()) { - const auto value = src[S_FACTOR].as(); + const auto value = roundf(src[S_FACTOR].as(), 3); - if (value > 0.09f && value <= 100.0f) { + if (value >= 0.09f && value <= 100.0f) { result.accepted = true; if (isFloatDifferent(value, dst.factor, 0.0001f)) { - dst.factor = roundf(value, 3); + dst.factor = value; result.changed = true; } } @@ -3444,12 +3458,12 @@ SettingResult fromJson(const JsonVariantConst src, Sensors::Settings& dst) { // filtering factor if (src[S_FILTERING_FACTOR].is()) { - const auto value = src[S_FILTERING_FACTOR].as(); + const auto value = roundf(src[S_FILTERING_FACTOR].as(), 3); if (value >= 0.001f && value <= 1.0f) { result.accepted = true; if (isFloatDifferent(value, dst.filteringFactor, 0.0001f)) { - dst.filteringFactor = roundf(value, 3); + dst.filteringFactor = value; result.changed = true; } } diff --git a/src/opentherm.cpp b/src/opentherm.cpp index 58069a5..1fb039e 100644 --- a/src/opentherm.cpp +++ b/src/opentherm.cpp @@ -85,30 +85,50 @@ static float getCh2ChannelSetpoint() { } static bool needWriteRoomSetpoint(const float target) { + if (!std::isfinite(target)) { + return false; + } + return !roomSetpointWriteTime || millis() - roomSetpointWriteTime > roomSetpointWriteInterval || isFloatDifferent(target, vars.slave.heating.roomTargetTemp, 0.05f); } static bool needWriteRoomSetpointCh2(const float target) { + if (!std::isfinite(target)) { + return false; + } + return !ch2RoomSetpointWriteTime || millis() - ch2RoomSetpointWriteTime > roomSetpointWriteInterval || isFloatDifferent(target, vars.slave.ch2.roomTargetTemp, 0.05f); } static bool needWriteDhwSetpoint(const float target) { + if (!std::isfinite(target)) { + return false; + } + return !dhwSetpointWriteTime || millis() - dhwSetpointWriteTime > dhwSetpointWriteInterval || isFloatDifferent(target, vars.slave.dhw.targetTemp, 0.05f); } static bool needWriteHeatingSetpoint(const float target) { + if (!std::isfinite(target)) { + return false; + } + return !heatingSetpointWriteTime || millis() - heatingSetpointWriteTime > heatingSetpointWriteInterval || isFloatDifferent(target, vars.slave.heating.targetTemp, 0.05f); } static bool needWriteCh2Setpoint(const float target) { + if (!std::isfinite(target)) { + return false; + } + return !ch2SetpointWriteTime || millis() - ch2SetpointWriteTime > ch2SetpointWriteInterval || isFloatDifferent(target, vars.slave.ch2.targetTemp, 0.05f); diff --git a/src/regulator.cpp b/src/regulator.cpp index aa3afaa..3905e15 100644 --- a/src/regulator.cpp +++ b/src/regulator.cpp @@ -75,7 +75,7 @@ static Task pidTask("PID", Task::Type::FOREVER, [](void*) { } float result = pidRegulator.getResult(); - if (isFloatDifferent(pidResult, result, 0.09f)) { + if (std::isfinite(result) && isFloatDifferent(pidResult, result, 0.09f)) { pidResult = result; ESP_LOGI(L_REGULATOR_PID, "New result: %.2f", result); ESP_LOGD(L_REGULATOR_PID, "Integral: %.2f", pidRegulator.integral);