Run a command every X seconds using timer instead of cron?

Hello,
If I want to use the timer datatype to run a command every X seconds, how do I do that?

The reason I don’t want to use cron is because it has to start on an activity.

rule "start cycle"
  when
    Item cycle_start_PB received update
  then
    waitTimer = createTimer(now.plusSeconds(5))[|
      say("five seconds")
      waitTimer = createTimer(now.plusSeconds(5))  //this line does not work
    ]
end

I’d like the timer to restart after it runs. So I should here “five seconds”…“five seconds”…“five seconds”… forever, until I explicitely null out the timer somehow.

I know I can use cron with some if statements to kind of prevent the execution…

rule "repeat every 5 seconds kind of"
  when 
    Time cron "0 0/5 * * * ?"
  then
    if ( put flags here )...
end

But it doesn’t work well for some specific things I’m trying to do.

Thanks!

Instead of creating a new timer, try rescheduling it.

waitTimer.reschedule (now.plusSeconds (5))

You may need to make sure I got the function name right. I’m going from memory and it may be something like rescheduleTimer.

The problem is the createTimer inside the timer creates a timer that doesn’t do anything but to create a timer that does the same thing puts you into a situation where you get stuck in an infinite repetition. Kind of like facing two mirrors at each other.

Or have two timers that trigger each other

for example:

you kick off the timers by switching switch 1 on
switch 1 triggers a rule that starts timer 1 which, after 5 seconds, turns switch 1 off and switch 2 on
switch 2 triggers a rule that starts timer 2 which, after 5 seconds, turn switch 1 off and switch 2 on

that will merrily keep going forever