Date Range Compare

I’m trying to turn on a virtual switch based on a date range. Originally, I had a rule that turned the switch on from the 4th Friday in November, then another rule that turned the switch off starting with the first Saturday in January. The issue is that my Pi rebooted and the switch then turns off, so my rule won’t trigger because the switch is now off.

Is there a way to test if a date is between x and y, turn on a switch?

I’ve searched for “between time cron” to see if I could find examples and couldn’t find anything that fit my scenario.

Thanks in advance!

How about persistence and “restoreOnStartup”?

Other way would be to make a rule which is executed on openhab start.

val currMonth = now.getMonthOfYear
val currDay = now.getDayOfYear
if (currMonth >= 11 || currMonth <= 1) {       
...do something...
}

also possible:
if (currMonth == 11 || currDay == 1) {          // or
if (currMonth >= 4 && currMonth <= 10) {        // and

To get the exact date or timerange you have to make more if´s

1 Like

Here is another way to do this: You have to make a new item-switch, which is powered on with a cron job on the day your timerange starts and powered of at the end of your timerange.

This is what I have today. It turns on after Thanksgiving and then turns off the switch after the first Saturday in January. The problem is that we had a power outage and it rebooted my Pi, so the virtual switch turned off.

I manually had to turn the switch back on, but I was trying to find a more permanent way to set the switch.

Then set persistence with restoreOnStartup on this virtual switch-item and it works even after a power outage.

The only way, when you can get an error in this case is, when the power outage is exactly at this moment the cron-job rule should fire. In this case your switch will stay with the old value, because the cron-job rule only fires at exactly the defined time.

Thanks! I didn’t know about that!

Would I put the below in the Items file? (My virtual switch is called “Christmas”.)

Strategies {
	everyMinute	: "0 * * * * ?"
        everyDay        : "0 0 0 * * ?"
}

Items {
	Christmas : strategy = everyChange, everyDay, restoreOnStartup
}

You have to install a persistence first.

For your case i would use mapDB, it only stores one (the last) value and this is enough for restoreOnStratup.

If you want to do other things (e.g. generate charts) i would use another persistence, maybe rrd4j.

My mapdb.persist looks like this: (in persistence-folder)

Strategies {
	default = everyChange
}
Items {
	gAstro*				: strategy = everyChange, restoreOnStartup
	Licht*				: strategy = everyChange, restoreOnStartup
	Rollos*				: strategy = everyChange, restoreOnStartup
	Jalousien*			: strategy = everyChange, restoreOnStartup
	Steckdosen*			: strategy = everyChange, restoreOnStartup
	gPersist*			: strategy = everyChange, restoreOnStartup
	gMapDB*				: strategy = everyChange, restoreOnStartup
	gLichtZeit*			: strategy = everyChange, restoreOnStartup
	...
        and so on
}

Don´t forget the * on the end of each group-name.