Correct time zone of a group of datetime-items via script?

Hello,

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.

Well, DSL will stay…

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…) :wink:

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

I thought DateTimeType stores its value as a ZonedDateTime - why would it need to be readjusted if the Zone information in it is correct?

I think the real problem is, DateTime Items do always store, well a complete Date, whilst on some occasions one might only want to use the time part.

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

There is a 0 and not a -1, a bug in OH 3.4.2?

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
  }
]

Have you tried setting your time zone (in the system / regional setting?) to something like Europe/Berlin (or whatever is more suitable for you)

This is in jruby but it works the same with any other language

zdt = ZonedDateTime.parse("2023-03-24T22:50:00+01:00[Europe/Berlin]")
logger.info zdt # => 2023-03-24T22:50+01:00[Europe/Berlin]
logger.info zdt + 1.month # => 2023-04-24T22:50+02:00[Europe/Berlin]

Yes, I have. The timezone is Europe/Berlin in my settings.

This ist the output of these script

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
  }
]

There is no [Europe/Berlin] in the Log…

It depends on how the values were set.

I set the values via OH’s UI. With the DateTimePicker from the Marketplace. So this could be the error? But why isn’t the time zone saved correctly?
image
image

Is it possible to fix the “wrong” information with a script?

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.

Any ideas?

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.