I was looking for a way to turn on ceiling spot lights in sequence. My first approach was to use setTimeout with a 500ms delay between each sendCommand statement. That resulted in a “Multithreaded Access Error.” After some searching in the forums it seems multiple setTimout statements aren’t allowed.
I googled around and found another solution that worked. A sleep-like function that uses javascript’s Promise object and setTimeout. You can find it here: https://www.sitepoint.com/delay-sleep-pause-wait/
Here is what my ECMA-11 script looks like:
var trigger = event.itemName.toString();
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
if(trigger == "LivingroomSensor_MotionAlarm"){
items.getItem("HuespotHallway2_Color").sendCommand("5,100,30");
sleep(600)
.then(() => items.getItem("HuespotHallway1_Color").sendCommand("5,100,30"))
.then(() => sleep(600))
.then(() => items.getItem("HuespotDR4_Color").sendCommand("5,100,30"))
setTimeout(()=>{if(items.getItem("HueRoomLR_Power").state == "OFF"){
items.getItem("HuespotHallway2_Color").sendCommand("30,65,0");
sleep(600)
.then(() => items.getItem("HuespotHallway1_Color").sendCommand("5,100,30"))
.then(() => sleep(600))
.then(() => items.getItem("HuespotDR4_Color").sendCommand("5,100,30"));}},120000)
}
else {
items.getItem("HuespotDR4_Color").sendCommand("5,100,30");
sleep(600)
.then(() => items.getItem("HuespotHallway1_Color").sendCommand("5,100,30"))
.then(() => sleep(600))
.then(()=> items.getItem("HuespotHallway2_Color").sendCommand("5,100,30"))
setTimeout(()=>{if(items.getItem("HueRoomLR_Power").state == "OFF"){
items.getItem("HuespotDR4_Color").sendCommand("30,65,0");
sleep(600)
.then(() => items.getItem("HuespotHallway1_Color").sendCommand("30,65,0"))
.then(() => sleep(600))
.then(() => items.getItem("HuespotHallway2_Color").sendCommand("30,65,0"));}},120000)
}
Please note: I am not a programmer. I was just luckily able to find a solution on the internet and cobble it into a rule. I can’t promise to answer any technical questions on this technique. Hopefully the link will be able to answer any questions you may have. I just wanted to share my solution that worked.