First of all, there is a far easier way to do this. You are writing this rule through the UI. So in the “when” part of the rule click to add a new trigger to run the rule at around midnight. While you are at it, add yet another trigger to run the rule when openHAB starts up.
Done. That’s all you have to do. With those changes to the rule, and no changes to the actual code the timer will be recreated:
- when the time Item changes
- every day at midnight
- when openHAB restarts
Again, there is nothing you have to do in the Script Action. All you have to do is add a couple more events that cause the Script Action to run.
Now for your looping timer attempt which I feel the need to explain why it won’t work because it shows a fundamental misunderstanding of how Timers work.
No, that last line of code won’t work. Try to walk through the code in your head and you will see why.
- Initialize the this.timer if necessary
- Move the DateTime Item’s date/time to today
- Create the timer to execute the code in
runme at timerTime
- Reschedule this.timer to trigger one day from timerTime (note plusDays(1) or plusHours(24) is way easier for a person to understand).
Do you see the problem? By adding that last line of code you ensure that the timer will not execute
runme today. Assuming that the rule doesn’t get run again it will execute
runme tomorrow. Then it will never execute
To get the behavior you actually want, you need to put the call to
reschedule inside of
Another option is to advance your timer Item to tomorrow inside of
runme which will cause the rule to run again create a new Timer for tomorrow. But you have to be very careful with using
plusDays(1) (or any other way to add 24 hours to the time) because in places that use Daylight Savings Time there is one day of year that is only 23 hours long and another day of the year that is 25 hours long. Your timer will fail on those days if you just naively move the timer forward 24 hours.
This is why a better approach is to trigger the rule around midnight to reschedule the Timer. That way you can schedule it for the exact time desired and it will just work. You don’t have to care about DST change overs.
And of course, as rossko57 pointed out, you’ll need to reschedule the Timer when OH restarts so add a system runlevel reached trigger so it runs when OH starts up to create the Timer.
Finally, every time you change the rule, you’ll need to manually run it by pressing the play button to reschedule the Timer then too because when you change the rule your Timer gets orphaned (you’ll see an error in the logs when that orphaned timer finally does execute and the code in
runme won’t execute).