That can work but you can’t do it with reschedule. Since you have a new lambda to execute (stuff in ) you need create a new timer. You can call timer.reschedule(now.plusSeconds(3)) but you can’t change the code that executes when it runs again.
To do this the way I would recommend, you can’t implement it in a Script.
The two that I think are relevant here would be Looping Timers or Cascading Timers. But I think Cascading Timers may be overkill for this case as I suspect you are OK hard coding the times. So let’s look at what this would look like using Looping Timers.
The reason that you can’t do this in a Script is because we need to preserve the variable that the Timer is stored in and I’m not sure we can do that in a Script.
var Timer bedroomTimer = null
rule "Bedroom routine"
when
// some trigger
then
if(bedroomTimer !== null) return; // only let one routine run at a time, ignore repeated events
var step = 0
bedroomTimer = createTimer(now.plusMinutes(0), [ |
var rescheduleTime = -1
switch(step) {
case 0: {
// turn on the lights
step = 1
rescheduleTime = 3 * 60
}
case 1: {
// turn off the lights
// tell the Chromecast to say hello
step = 2
rescheduleTime = 3
}
case 2: {
// turn on the radio station
}
}
if(rescheduleTime != -1) bedroomTimer.reschedule(now.plusSeconds(rescheduleTime))
else bedroomTimer = null
])
end
Theory of operation. We keep track of what step we are on and perform the three steps based on that value. Depending on what step we are on we do the correct actions and reschedule the Timer for the right amount of time.
You could also implement this using three independent Timers. Dealing with avoiding having more than one set of these running at the same time is a little more complex (you have three Timers to check instead of just the one). But then the body of the Timers is WAY more straight forward.
var Timer step1Timer = null
var Timer step2Timer = null
rule "Bedroom routine"
when
// some trigger
then
if(step1Timer !== null || step2Timer !== null || step3Timer !== null) return; // only let one routine run at a time, ignore repeated events
// turn on the lights
step1Timer = createTimer(now.plusMinutes(3), [ |
// turn off lights
// say hello on Chromecast
step1Timer = null
])
step2Timer = createTimer(now.plusSeconds(3*60 + 3), [ |
// turn on radio station
step2Timer = null
])
end