DateTimeTrigger and timezones

Good morning, although somewhat earlier as planned.

I am working on some Date/Time based rules to trigger automation around the house. One of which being a sunrise routine in the bedroom.
This is where I noticed that the DateTimeTigger seems to be ignoring the timezone, and treats the time as local.

For example, using the Android app to set the AlarmClock item, which it does in UTC time. After which my rule set a second item a bit earlier.

2022-08-09 23:28:10.261 [INFO ] [openhab.event.ItemCommandEvent      ] - Item 'AlarmClock' received command 2022-08-10T06:30:00.000+0000
2022-08-09 23:28:10.272 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item 'AlarmClock' changed from 2022-08-10T05:00:00.000+0000 to 2022-08-10T06:30:00.000+0000
2022-08-09 23:28:10.312 [INFO ] [openhab.event.ItemCommandEvent      ] - Item 'Bedroom_VirtualSunrise' received command 2022-08-10T05:55:00.000+0000
2022-08-09 23:28:10.318 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item 'Bedroom_VirtualSunrise' changed from 2022-08-10T04:25:00.000+0000 to 2022-08-10T05:55:00.000+0000

That time is set by the following rule:

rule 'Set Virtual Sunrise time' do
  changed AlarmClock

  run do |event|
    if event.item.state?
      Bedroom_VirtualSunrise << event.item - 35.minutes
    else
      Bedroom_VirtualSunrise.update(NULL)
    end
  end
end

To create the virtual sunrise, I’m using the following rule, mostly a copy paste, straight from the documentation:

rule 'Virtual Sunrise' do
  description 'Create a virtual sunrise before the alarm clock sounds'

  trigger 'timer.DateTimeTrigger', itemName: Bedroom_VirtualSunrise.name

  run do
    BlindsBedroomWindow_Vane.ensure.command(50)
  end
end

However this morning, I had a early rise and shine

2022-08-10 05:55:00.389 [INFO ] [openhab.event.ItemCommandEvent      ] - Item 'BlindsBedroomWindow_Vane' received command 50
2022-08-10 05:55:00.391 [INFO ] [penhab.event.ItemStatePredictedEvent] - Item 'BlindsBedroomWindow_Vane' predicted to become 50
2022-08-10 05:55:00.404 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item 'BlindsBedroomWindow_Vane' changed from 0 to 50

Note: Timestamps in the logs are local time - Europe/Amsterdam, UTC +2 CEST

I have a different rule to run a Time Of Day routine, which is linked to the astro binding, which seems to set the time in local time, and this one works fine.

The item:

openhab> openhab:status ToD_Day
2022-08-10T06:21:00.000+0200

The rule:

ToD = %w[ Day Afternoon Evening ]

ToD.each do |t|
  rule "Set Time of Day to #{t}" do
    trigger 'timer.DateTimeTrigger', itemName: 'ToD_' + t
    run { items['TimeOfDay'].command(t) }
  end
end

The logs:

2022-08-10 06:21:00.729 [INFO ] [openhab.event.ItemCommandEvent      ] - Item 'TimeOfDay' received command Day
2022-08-10 06:21:00.732 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item 'TimeOfDay' changed from Evening to Day

As both the sunrise and the timeofday items clearly include a timezone/offset, I would expect that any triggers would compensate for this.

I’m not sure if this is a JRuby issue, a upstream issue at the DateTimeTrigger implementation, or a issue that sites between my keyboard and chair :slight_smile:

That’s good, that tells us your Java timezone is set correctly, which is the usual cause of time-shift issues.

I wouldn’t be surprised, it is a new feature.

It’s perfectly acceptable to set a DateTime Item to UTC or any other zone. It just represents some moment in time.

It does appear that the time-trigger setup is making a mess of it, extracting 05:50 from the Item, ignoring the UTC zone, and wrongly scheduling the trigger for 05:50 local time.

Ok, so that might be worth investigating then, and sure enough…

Test setup in Rule DSL

rule "DateTimeTrigger timezones"
when
    Time is MyTestDateTime
then
    logInfo("DateTimeTrigger", "The test DateTime item caused a trigger!")
end

Setting a timestamp with UTC timezone

openhab> openhab:send MyTestDateTime 2022-08-10T11:29:00.000+0000
Command has been sent successfully.

Log output

2022-08-10 11:28:26.844 [INFO ] [openhab.event.ItemCommandEvent      ] - Item 'MyTestDateTime' received command 2022-08-10T11:29:00.000+0000
2022-08-10 11:28:26.855 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item 'MyTestDateTime' changed from NULL to 2022-08-10T11:29:00.000+0000
2022-08-10 11:29:00.939 [INFO ] [ab.core.model.script.DateTimeTrigger] - The test DateTime item caused a trigger!

Now let’s retry with a CEST timezone.

Setting a timestamp with CEST timezone

openhab> openhab:send MyTestDateTime 2022-08-10T11:33:00.000+0200
Command has been sent successfully.

Log output

2022-08-10 11:32:31.064 [INFO ] [openhab.event.ItemCommandEvent      ] - Item 'MyTestDateTime' received command 2022-08-10T11:33:00.000+0200
2022-08-10 11:32:31.096 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item 'MyTestDateTime' changed from 2022-08-10T11:29:00.000+0000 to 2022-08-10T11:33:00.000+0200
2022-08-10 11:33:00.117 [INFO ] [ab.core.model.script.DateTimeTrigger] - The test DateTime item caused a trigger!

Looks like I am heading on out to upstream and opening a issue on Github :slight_smile:

Upstream bug report: DateTimeTrigger not taking timezones into account · Issue #3059 · openhab/openhab-core · GitHub

2 Likes

I posted a PR to fix this. A workaround would be to convert the zone in your rule

rule 'Set Virtual Sunrise time' do
  changed AlarmClock

  run do |event|
    if event.item.state?
      Bedroom_VirtualSunrise << (event.item - 35.minutes).with_zone_same_instant(ZoneId.system_default)
    else
      Bedroom_VirtualSunrise.update(NULL)
    end
  end
end
3 Likes

I am afraid I cannot test the jar of the PR as I am on openHAB 3.3.0. But I am able to confirm your workaround works!

2022-08-10 14:10:16.506 [INFO ] [openhab.event.ItemCommandEvent      ] - Item 'AlarmClock' received command 2022-08-11T05:00:00.000+0000
2022-08-10 14:10:16.514 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item 'AlarmClock' changed from UNDEF to 2022-08-11T05:00:00.000+0000
2022-08-10 14:10:16.625 [INFO ] [openhab.event.ItemCommandEvent      ] - Item 'Bedroom_VirtualSunrise' received command 2022-08-11T06:25:00.000+0200
2022-08-10 14:10:16.652 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item 'Bedroom_VirtualSunrise' changed from NULL to 2022-08-11T06:25:00.000+0200

Thanks!

I’ve uploaded a jar for 3.3 too.