Timer's and OH2 Restarts

I created a rule to control an outlet that turns On on my Xmas lights at 5pm each day, and then turns them off 7 hours later. The rule appears to work correctly with the exception of on OH2 restart. So if the lights are On, and I stop and restart OH2 manually (ie, for an upgrade), or a power failure, etc, it appears the timer is gone on the reboot.

So is a better/simpler approach to just have 2 separate rules to turn the light on at 5pm and Off 7 hours later at midnight or 12 am?

rule "Turn on Xmas Lights"
when
	Time cron "0 00 17 ? * * "
then
	sendCommand(FF_Light_GarageOutlet, ON)
	sendNotification("xxxxxx@gmail.com", "Xmas lights turned ON.")
	
	createTimer(now.plusHours(7)) [|
        	if (FF_Light_GarageOutlet.state==ON) {
				sendCommand(FF_Light_GarageOutlet, OFF)
				sendNotification("xxxxx@gmail.com", "Xmas lights turned OFF.")
			}
		]
end

Unfortunately, timers don’t survive a restart. That would be a cool feature that really should exist. I try to use cron triggers as much as possible for this reason.

That’s pretty much what I thought as well. I went and set up two separate rules and the 2nd will check if on at Midnight and then turn off it needed.

But what I wasn’t certain of, and perhaps I’ll experiment about Adding Start and End Time Variables and if capable of then storing/tracking those times with persist. That would likely keep it all in one rule?

Your cron expression can specify multiple hour values. This way a single rule will trigger at both 5pm and midnight:

when Time cron "0 0 0,17 * * ?"

That is correct.

For this use case where the on and off times are static I would say yes.

You can get that behavior with Design Pattern: Expire Binding Based Timers and persistence.

If you persist this Item:

Switch ChristmasTimer {expire="7h,command=OFF"}

Then you can use these rules:

rule "Reset timer"
when
    System started
then
    ChristmasTimer.sendCommand(ChristmasTimer.previousState)
end

rule "Turn on lights"
when
    Time cron "0 0 17 ? * *"
then
    // turn on the lights

    ChristmasTimer.sendCommand(ON)
end

rule "Turn off the Christmas Lights"
when
    Item ChristmasTimer received command OFF
then
    // turn off the lights
end

Little Copy & Paste Error
ON must be OFF

More than a little typo. It should read

when
    Item ChristmasTimer received command OFF
then
1 Like

Kind of unrelated but you might find it helpful, I have a ventfan/heater/light in my bathroom, I use the expire binding for the fan and heater, the fan runs for 30 minutes and the heater runs for 15 minutes before expiring. In then event of a reboot I have a rule that runs at startup, it simply checks to see if either is on and then turns them off.
I also have a rule that will turn off heat if you turn on the fan and heat is already on, and a rule that turns off the fan if you turn on heat while the fan is already on.

I am new to OH and just recently installed influxdb(+grafana) for persistence.

Doesn’t the persistence service automatically restores the item state to the value it was before the restart?

Yes. But - restore on startup does not “trigger” the expire binding to begin its countdown. The system started rule sends a command that resets expire, and so begins its timing.

Note that using expire in that way causes the full time (7 hours here) to begin from system startup.