I have a group (gTimeZone) with DateTime items, which I can set via the UI (time of day).
However, the time zones are not adjusted for these items during the summer/winter time changeover, and the times are then wrong. I would now like to correct the zones of the items in this group via script shortly after the time change so that the switching times on this day are correct again.
What type of script should I use to be prepared for OH4? How could such a script look like? (Unfortunately I can’t get any further with Blockly.)
Thank you very much.
rule "correct DateTime"
when
Time cron "1 0 3 * 3,10 0L" // last Sunday of March and October at 03:00:01
then
val iHour = if(now.month == 3) 1 else -1 // at March + 1, at October -1
gTimeZone.members.forEach[d| // for each member of gTimeZone (use d as proxy
if(d.state instanceof DateTimeType) { // is d.state a valid date?
val myDateTime = (d.state as DateTimeType).getZonedDateTime // get the date and change to zonedDateTime
val myNewDateTime = now.plusHours(iHour).toLocalDateTime // add or subtract one hour
d.postUpdate(DateTimeType.valueOf(myNewDateTime)) // set d to new dateTime
}
]
end
should do the job (did not test though, but in theory…)
If you want to code through UI, please use Time cron for the when part of the rule and use the code between then and end (without then and end) as DSL rule script
Thank you for the script, Udo. I will try him.
The real problem is that OH saves the time zone.
I always only enter the time for the items, i.e. around 10:50 p.m. The item then says something like 2023-03-24 22:50:00+0100. After the time change, this will remain so, but should be 2023-03-24 22:50:00+0200. This is exactly the problem I want to fix with the script so that I don’t have to save every item again to take over the new zone. The same game, only the other way around, I then have when switching to winter time.
Funny, he’s bothered by the cron. I get this error message:
Cannot inform the listener "org.openhab.core.automation.internal.RuleEngineImpl$2@6638bd24" about the "UPDATED" event: Day of week in cron expression '5 0 3 * 3,10 0L' in field 'DayOfWeek': value -1 is outside range
This rule seems to work now. Then we’ll wait for the winter. ;(
val iHour = if(now.month == 3) 1 else -1 // at March + 1, at October -1
gTimeZone.members.forEach[d| // for each member of gTimeZone (use d as proxy
if(d.state instanceof DateTimeType) { // is d.state a valid date?
val myDateTime = (d.state as DateTimeType).getZonedDateTime // get the date and change to zonedDateTime
val myNewDateTime = myDateTime.plusHours(iHour).toLocalDateTime // add or subtract one hour
logInfo("Zeitzone",d.name + " - alt: " + myDateTime + " - neu: " + myNewDateTime)
d.postUpdate(DateTimeType.valueOf(myNewDateTime.toString)) // set d to new dateTime
}
]
val iHour = if(now.month == 3) 1 else -1 // at March + 1, at October -1
gTimeZone.members.forEach[d| // for each member of gTimeZone (use d as proxy
if(d.state instanceof DateTimeType) { // is d.state a valid date?
val myDateTime = (d.state as DateTimeType).getZonedDateTime // get the date and change to zonedDateTime
val myNewDateTime = myDateTime.plusHours(iHour).toLocalDateTime // add or subtract one hour
logInfo("Zeitzone",d.name + " : " + myDateTime)
//logInfo("Zeitzone",d.name + " - alt: " + myDateTime + " - neu: " + myNewDateTime)
//d.postUpdate(DateTimeType.valueOf(myNewDateTime.toString)) // set d to new dateTime
}
]
Hmm, when I try to assign a zdt date/time to a DateTime item using a rule, an info message comes up and the value is not accepted. What now? Somehow there is a worm in OH.