diff --git a/src/ESP32React/NTPSettingsService.cpp b/src/ESP32React/NTPSettingsService.cpp index 5f229d347..1945bc00b 100644 --- a/src/ESP32React/NTPSettingsService.cpp +++ b/src/ESP32React/NTPSettingsService.cpp @@ -101,7 +101,7 @@ StateUpdateResult NTPSettings::update(JsonObject root, NTPSettings & settings) { settings.tzLabel = root["tz_label"] | FACTORY_NTP_TIME_ZONE_LABEL; settings.tzFormat = root["tz_format"] | FACTORY_NTP_TIME_ZONE_FORMAT; settings.thermostat_sync = root["thermostat_sync"] | FACTORY_NTP_THERMOSTAT_SYNC; - settings.tzLabelT = root["tz_label_t"] | settings.tzLabel; - settings.tzFormatT = root["tz_format_t"] | settings.tzFormat; + settings.tzLabelT = root["tz_label_t"] | FACTORY_NTP_TIME_ZONE_LABEL; + settings.tzFormatT = root["tz_format_t"] | FACTORY_NTP_TIME_ZONE_FORMAT; return StateUpdateResult::CHANGED; } diff --git a/src/ESP32React/NTPSettingsService.h b/src/ESP32React/NTPSettingsService.h index 413023279..2a7b5129d 100644 --- a/src/ESP32React/NTPSettingsService.h +++ b/src/ESP32React/NTPSettingsService.h @@ -19,6 +19,10 @@ #define FACTORY_NTP_TIME_ZONE_FORMAT "GMT0BST,M3.5.0/1,M10.5.0" #endif +#ifndef FACTORY_NTP_THERMOSTAT_SYNC +#define FACTORY_NTP_THERMOSTAT_SYNC 0 +#endif + #ifndef FACTORY_NTP_SERVER #define FACTORY_NTP_SERVER "time.google.com" #endif diff --git a/src/devices/thermostat.cpp b/src/devices/thermostat.cpp index 47a130d15..e429a4738 100644 --- a/src/devices/thermostat.cpp +++ b/src/devices/thermostat.cpp @@ -1737,26 +1737,17 @@ void Thermostat::process_RCTime(std::shared_ptr telegram) { } // check clock - time_t now = time(nullptr); - tm * tm_; - bool sync = true; + time_t now = time(nullptr); + bool sync = false; + // change to thermostat timezone if different EMSESP::esp32React.getNTPSettingsService()->read([&](const NTPSettings & settings) { - switch (settings.thermostat_sync) { - default: - sync = false; - break; - case 1: - tm_ = localtime(&now); - break; - case 2: + sync = settings.thermostat_sync != 0; + if (settings.thermostat_sync == 2) { setenv("TZ", settings.tzFormatT.c_str(), 1); tzset(); - tm_ = localtime(&now); - setenv("TZ", settings.tzFormat.c_str(), 1); - tzset(); - break; } }); + tm * tm_ = localtime(&now); bool tset_ = tm_->tm_year > 110; // year 2010 and up, time is valid tm_->tm_year = (telegram->message_data[0] & 0x7F) + 100; // IVT tm_->tm_mon = telegram->message_data[1] - 1; @@ -1775,19 +1766,25 @@ void Thermostat::process_RCTime(std::shared_ptr telegram) { bool ivtclock = (telegram->message_data[0] & 0x80) == 0x80; // dont sync ivt-clock, #439 // bool junkersclock = model() == EMSdevice::EMS_DEVICE_FLAG_JUNKERS; - time_t ttime = mktime(tm_); // thermostat time + time_t ttime = mktime(tm_); // make thermostat time + // set back emsesp timezone + EMSESP::esp32React.getNTPSettingsService()->read([&](const NTPSettings & settings) { + if (settings.thermostat_sync == 2) { + setenv("TZ", settings.tzFormat.c_str(), 1); + tzset(); + } + }); // correct thermostat clock if we have valid ntp time, and could write the command - // if (sync && !ivtclock && !junkersclock && tset_ && EMSESP::system_.ntp_connected() && !EMSESP::system_.readonly_mode() && has_command(&dateTime_)) { if (sync && !ivtclock && tset_ && EMSESP::system_.ntp_connected() && !EMSESP::system_.readonly_mode() && has_command(&dateTime_)) { int difference = difftime(now, ttime); if (difference > 15 || difference < -15) { if (setTimeRetry < 3) { if (!use_dst) { set_datetime("ntp", 0); // set from NTP without dst - LOG_INFO("thermostat time correction from ntp, ignoring dst"); + LOG_INFO("thermostat time correction %d seconds from ntp, ignoring dst", difference); } else { set_datetime("ntp", -1); // set from NTP - LOG_INFO("thermostat time correction from ntp"); + LOG_INFO("thermostat time correction %d seconds from ntp", difference); } setTimeRetry++; } @@ -3083,19 +3080,14 @@ bool Thermostat::set_datetime(const char * value, const int8_t id) { uint8_t data[9]; if (dt == "ntp") { time_t now = time(nullptr); - tm * tm_; + tm * tm_ = localtime(&now); EMSESP::esp32React.getNTPSettingsService()->read([&](const NTPSettings & settings) { - switch (settings.thermostat_sync) { - default: - tm_ = localtime(&now); - break; - case 2: + if (settings.thermostat_sync == 2) { setenv("TZ", settings.tzFormatT.c_str(), 1); tzset(); tm_ = localtime(&now); setenv("TZ", settings.tzFormat.c_str(), 1); tzset(); - break; } }); if (tm_->tm_year < 110) { // no valid time