At a high level this seems reasonable. Often the devil is in the details though.
Sometimes it’s easier to just always cancel and recreate the timer. One gotcha is if the new time is in the past. You have to decide what’s appropriate in that case (e.g. skip it for today or run it now).
You shouldn’t have to cancel anything. At midnight there shouldn’t be any more timers for that day. But at midnight it is the perfect time to create new timers for the new day so look at the Items and figure out if it’s a day where it needs to run.
Trigger the same rule that runs at midnight at system runlevel 100 (or what ever startup level you feel is appropriate).
Over all, this is kind of how the Time State Machine works: Time Based State Machine [4.0.0.0;4.9.9.9]. It could be used for inspiration (note because it’s a rule template roughly half of the code is there strictly for error checking so don’t be scared away by it’s length and complexity). You might even be able to use that rule template directly. Let the rule template handle the creation of the timers for you.
I’ve also a bunch of timer management utilities in openHAB Rules Tools which could be used for this as well. LoopingTimer (once created the timer will reschedule itself based on the value returned by the timer function) and Deferred (simple way to schedule a command to an Item).
But over all, I still think using a Time is <Item>
trigger would be the easiest way to handle this. All you’d really need to do is manipulate the DateTime Items used to trigger the rule and won’t have to mess with Timers at all. You’d still need to do some work though.
- Create a proxy DateTime Item to trigger the rule.
- Use the trigger that takes into account the date as well as the time on the rule that commands the device using the Item created in 1.
- Create a rule that:
a. use the Items to determine if today is a day that it should run.
b. If so, copy the value in the “SetTime” Item to the proxy Item.
c. If not set the proxy Item to NULL (prevents the rule from triggering).
e.Trigger this rule at midnight and when the “SetTime” Item changes. You don’t need to trigger it at system startup if you have restoreOnStartup on the Items, but it doesn’t hurt either.
There are no timers to manage with this approach. You just have to manage the state of an Item.