Error with Timezone
Hi,
I have got an problem with handling on time zones.
I have this test code in a UI rule:
from openhab.services import getService
from java.time import ZoneId
from openhab import Registry, logger
from datetime import datetime, timedelta
now = datetime.now()
today = str(now)[0:19]
print("------------------")
print(now)
print(today)
testdatetime = "2025-09-26 10:00:00"
#start_time = datetime.strptime(testdatetime, "%Y-%m-%d %H:%M:%S")
print("User Timezone (from -Duser.timezone) " + ZoneId.systemDefault().getId())
timeZoneProvider = getService('org.openhab.core.i18n.TimeZoneProvider');
print("Regional Timezone (regional settings from webui) " + timeZoneProvider.getTimeZone().getId())
print("Datetime (test if timezone is used) " + str(datetime.now().astimezone()))
print("------------------")
I can run and save the rule as many times I want, no problem.
Logging is:
2025-09-26 17:50:15.774 [INFO ] [utomation.pythonscripting.74bb0877b4] - ------------------
2025-09-26 17:50:15.775 [INFO ] [utomation.pythonscripting.74bb0877b4] - 2025-09-26 17:50:15.772000
2025-09-26 17:50:15.775 [INFO ] [utomation.pythonscripting.74bb0877b4] - 2025-09-26 17:50:15
2025-09-26 17:50:15.776 [INFO ] [utomation.pythonscripting.74bb0877b4] - User Timezone (from -Duser.timezone) Europe/Stockholm
2025-09-26 17:50:15.777 [INFO ] [utomation.pythonscripting.74bb0877b4] - Regional Timezone (regional settings from webui) Europe/Stockholm
2025-09-26 17:50:15.781 [INFO ] [utomation.pythonscripting.74bb0877b4] - Datetime (test if timezone is used) 2025-09-26 17:50:15.777000+02:00
2025-09-26 17:50:15.781 [INFO ] [utomation.pythonscripting.74bb0877b4] - ------------------
The problem comes when I UNCOMMENT this line:
start_time = datetime.strptime(testdatetime, "%Y-%m-%d %H:%M:%S")
After save and run once the Time zone has changed:
2025-09-26 17:50:26.775 [INFO ] [utomation.pythonscripting.74bb0877b4] - ------------------
2025-09-26 17:50:26.776 [INFO ] [utomation.pythonscripting.74bb0877b4] - 2025-09-26 17:50:26.774000
2025-09-26 17:50:26.776 [INFO ] [utomation.pythonscripting.74bb0877b4] - 2025-09-26 17:50:26
2025-09-26 17:50:26.838 [INFO ] [utomation.pythonscripting.74bb0877b4] - User Timezone (from -Duser.timezone) GMT
2025-09-26 17:50:26.839 [INFO ] [utomation.pythonscripting.74bb0877b4] - Regional Timezone (regional settings from webui) Europe/Stockholm
2025-09-26 17:50:26.844 [INFO ] [utomation.pythonscripting.74bb0877b4] - Datetime (test if timezone is used) 2025-09-26 17:50:26.840000+02:00
2025-09-26 17:50:26.844 [INFO ] [utomation.pythonscripting.74bb0877b4] - ------------------
After one more save and run the time starts to get wrong:
2025-09-26 17:50:32.553 [INFO ] [utomation.pythonscripting.74bb0877b4] - ------------------
2025-09-26 17:50:32.553 [INFO ] [utomation.pythonscripting.74bb0877b4] - 2025-09-26 15:50:32.551000
2025-09-26 17:50:32.554 [INFO ] [utomation.pythonscripting.74bb0877b4] - 2025-09-26 15:50:32
2025-09-26 17:50:32.603 [INFO ] [utomation.pythonscripting.74bb0877b4] - User Timezone (from -Duser.timezone) GMT
2025-09-26 17:50:32.605 [INFO ] [utomation.pythonscripting.74bb0877b4] - Regional Timezone (regional settings from webui) Europe/Stockholm
2025-09-26 17:50:32.609 [INFO ] [utomation.pythonscripting.74bb0877b4] - Datetime (test if timezone is used) 2025-09-26 15:50:32.605000+00:00
2025-09-26 17:50:32.609 [INFO ] [utomation.pythonscripting.74bb0877b4] - ------------------
There must be som problem with âdatetime.strptimeâ and it should be possible to recreate.
I run Ubuntu 24.04.3 LTS, openhab 5.0.1.
As You can see the time differs between the log-time and the datetime time.
Restart of openhab removes the issue until next time.
The following setting is still correct:
-Duser.timezone=Europe/Stockholm
All other rules continue to run correctly if I donât save them.
Best Regards, Valter