[SOLVED] Rule should fire at the date of a DateTime item

I have a DateTime-Item, which gets populated externally:

.items

DateTime Event_StartTime

now I want to perfom an action at the exact time given in that item. and only once… :wink:
Is there a simple way in a rule for this? Or is it only possible with a cron based rule and the help of a proxy-item, which sets “event fired” on TRUE, if the event was already fired?

1 Like

Use a timer

you mean, if I catch the update on the DateTime-item, I should setup a timer counting down for the event? How much memory does a timer use? I know, it uses much less than Thread::sleep… But is it OK to let it run for a few hours?

No, create a Timer with the time from the item (you may have to convert it) the timer lambda will execute at that time

oh. I didn’t get that all that years!?
If I use

createTimer(Event_Starttime.state, [ |
		// my Commands here
    ])

the timer is created and executed not within the rule anymore, but in the system? That’s cool.

Close. You will need to convert the DateTimeType to a Joda DateTime to pass it to createTimer.

myTimer = createTimer(new DateTime(Event_Starttime.state.toString), [ |
    // my commands here
])

You will probably want to save that Timer to a global variable so you can cancel it, reschedule it, and otherwise manage it. You will probably also want to make sure that Event_Starttime gets persisted with restoreOnStartup and have a System started Rule run to recreate the Timer when OH restarts.

3 Likes

Hi, i am currently trying to solve the same problem.
Do i have to run the rule with the timer every 1 min and than activate a switch item (to trigger the main rule) or how to do that?
BR Simon

No, you misunderstand the timer element.
createTimer() allows you to define a block of code to be executed at to some future date and time.
So you set the timer up once when your target datetime changes, like setting an alarm clock.

Note, the Alarm Clock Rule rule template which can be installed from the Marketplace (available in recent versions of OH snapshots) implements just this. You give it a DateTime Item and a rule to run and everything else is handled for you.