fix different timezones

This commit is contained in:
MichaelDvP
2026-07-06 16:11:14 +02:00
parent dd0c8a5ca8
commit f16b10f544
3 changed files with 24 additions and 28 deletions

View File

@@ -101,7 +101,7 @@ StateUpdateResult NTPSettings::update(JsonObject root, NTPSettings & settings) {
settings.tzLabel = root["tz_label"] | FACTORY_NTP_TIME_ZONE_LABEL; settings.tzLabel = root["tz_label"] | FACTORY_NTP_TIME_ZONE_LABEL;
settings.tzFormat = root["tz_format"] | FACTORY_NTP_TIME_ZONE_FORMAT; settings.tzFormat = root["tz_format"] | FACTORY_NTP_TIME_ZONE_FORMAT;
settings.thermostat_sync = root["thermostat_sync"] | FACTORY_NTP_THERMOSTAT_SYNC; settings.thermostat_sync = root["thermostat_sync"] | FACTORY_NTP_THERMOSTAT_SYNC;
settings.tzLabelT = root["tz_label_t"] | settings.tzLabel; settings.tzLabelT = root["tz_label_t"] | FACTORY_NTP_TIME_ZONE_LABEL;
settings.tzFormatT = root["tz_format_t"] | settings.tzFormat; settings.tzFormatT = root["tz_format_t"] | FACTORY_NTP_TIME_ZONE_FORMAT;
return StateUpdateResult::CHANGED; return StateUpdateResult::CHANGED;
} }

View File

@@ -19,6 +19,10 @@
#define FACTORY_NTP_TIME_ZONE_FORMAT "GMT0BST,M3.5.0/1,M10.5.0" #define FACTORY_NTP_TIME_ZONE_FORMAT "GMT0BST,M3.5.0/1,M10.5.0"
#endif #endif
#ifndef FACTORY_NTP_THERMOSTAT_SYNC
#define FACTORY_NTP_THERMOSTAT_SYNC 0
#endif
#ifndef FACTORY_NTP_SERVER #ifndef FACTORY_NTP_SERVER
#define FACTORY_NTP_SERVER "time.google.com" #define FACTORY_NTP_SERVER "time.google.com"
#endif #endif

View File

@@ -1738,25 +1738,16 @@ void Thermostat::process_RCTime(std::shared_ptr<const Telegram> telegram) {
// check clock // check clock
time_t now = time(nullptr); time_t now = time(nullptr);
tm * tm_; bool sync = false;
bool sync = true; // change to thermostat timezone if different
EMSESP::esp32React.getNTPSettingsService()->read([&](const NTPSettings & settings) { EMSESP::esp32React.getNTPSettingsService()->read([&](const NTPSettings & settings) {
switch (settings.thermostat_sync) { sync = settings.thermostat_sync != 0;
default: if (settings.thermostat_sync == 2) {
sync = false;
break;
case 1:
tm_ = localtime(&now);
break;
case 2:
setenv("TZ", settings.tzFormatT.c_str(), 1); setenv("TZ", settings.tzFormatT.c_str(), 1);
tzset(); 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 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_year = (telegram->message_data[0] & 0x7F) + 100; // IVT
tm_->tm_mon = telegram->message_data[1] - 1; tm_->tm_mon = telegram->message_data[1] - 1;
@@ -1775,19 +1766,25 @@ void Thermostat::process_RCTime(std::shared_ptr<const Telegram> telegram) {
bool ivtclock = (telegram->message_data[0] & 0x80) == 0x80; // dont sync ivt-clock, #439 bool ivtclock = (telegram->message_data[0] & 0x80) == 0x80; // dont sync ivt-clock, #439
// bool junkersclock = model() == EMSdevice::EMS_DEVICE_FLAG_JUNKERS; // 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 // 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_)) { if (sync && !ivtclock && tset_ && EMSESP::system_.ntp_connected() && !EMSESP::system_.readonly_mode() && has_command(&dateTime_)) {
int difference = difftime(now, ttime); int difference = difftime(now, ttime);
if (difference > 15 || difference < -15) { if (difference > 15 || difference < -15) {
if (setTimeRetry < 3) { if (setTimeRetry < 3) {
if (!use_dst) { if (!use_dst) {
set_datetime("ntp", 0); // set from NTP without 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 { } else {
set_datetime("ntp", -1); // set from NTP 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++; setTimeRetry++;
} }
@@ -3083,19 +3080,14 @@ bool Thermostat::set_datetime(const char * value, const int8_t id) {
uint8_t data[9]; uint8_t data[9];
if (dt == "ntp") { if (dt == "ntp") {
time_t now = time(nullptr); time_t now = time(nullptr);
tm * tm_; tm * tm_ = localtime(&now);
EMSESP::esp32React.getNTPSettingsService()->read([&](const NTPSettings & settings) { EMSESP::esp32React.getNTPSettingsService()->read([&](const NTPSettings & settings) {
switch (settings.thermostat_sync) { if (settings.thermostat_sync == 2) {
default:
tm_ = localtime(&now);
break;
case 2:
setenv("TZ", settings.tzFormatT.c_str(), 1); setenv("TZ", settings.tzFormatT.c_str(), 1);
tzset(); tzset();
tm_ = localtime(&now); tm_ = localtime(&now);
setenv("TZ", settings.tzFormat.c_str(), 1); setenv("TZ", settings.tzFormat.c_str(), 1);
tzset(); tzset();
break;
} }
}); });
if (tm_->tm_year < 110) { // no valid time if (tm_->tm_year < 110) { // no valid time