Hi Folks,
my OpenHAB Version is 3.4.0.1 Stable (Debian)
I am converting most of my DSL rules to JSRule (ECMA2021) in the last few weeks and try to learn a bit more JS while doing so…
Right now I’m dealing with timers (timeout & interval) and try to combine them with functions.
But I have various questions about getting this to act the way I think it should - I attach the code for better understanding what I’m talking about…
generally I’m not sure to use the right logic in the syntax…
- is this the right way to use multiple cascaded timers in functions
- can I clear timers/intervals in that way
- what happens to timers/intervals which are called in a function which is cleared after the call
- are there any things that I missed or done wrong
- does it make a difference in which order I’m defining the functions
To explain this a little bit: I’m trying to call a timer in a new function out of the main function, and after the time is over, this function should call a new timeout/interval in another function and quit itself.
(function (event){
if (some condition) return;
setTimeout(timer1, 60000);
})(event);
function timer1(){
do something;
setTimeout(timer2, 300000);
clearTimeout(timer1);
}
function timer2(){
do something;
setInterval(timer3, 50);
clearTimeout(timer2);
}
function timer3(){
if(condition is met){
do something;
clearInterval(timer3);
}
}
Here’s the actual JSRule file I’m working on:
rules.JSRule({
name: "Actions - Execute Daystart",
description: "execute daystart when sleepstate changed to awake",
triggers: [triggers.ItemStateChangeTrigger("SleepState", "ASLEEP", "AWAKE")],
execute: (data) => {
const presence = items.getItem("PresenceSwitch");
const overridelock = items.getItem("OverrideLock");
const bedtime = items.getItem("Bedtime");
const player = items.getItem("SqueezePlayer");
const bedroomaudio = items.getItem("Smartplug03Switch");
const kitchenaudio = items.getItem("Smartplug07Switch");
const daystartpl = items.getItem("DaystartPlaylist");
const power = items.getItem("SqueezeBedroomPower");
const volume = items.getItem("SqueezeBedroomVolume");
const shuffle = items.getItem("SqueezeBedroomShuffle");
const favorite = items.getItem("SqueezeBedroomPlayFavorite");
const play = items.getItem("SqueezeBedroomPlaypause");
let event;
function volumeIncrease() {
if (volume.state < "20") {
vol = volume.state;
increase = vol + 1;
volume.sendCommand(increase);
} else clearInterval(volumeIncrease);
}
function bootupTimer() {
console.log("Good Morning!");
if (overridelock.state !== "OFF") {
manualoverride.sendCommandIfDifferent("OFF");
power.sendCommandIfDifferent("ON");
volume.sendCommandIfDifferent("5");
shuffle.sendCommandIfDifferent("1");
favorite.sendCommand(daystartpl);
play.sendCommandIfDifferent("ON");
console.log("Squeeze - Start playing Music in Bedroom");
setInterval(volumeIncrease, 60000);
clearTimeout(bootupTimer);
}
}
(function (event) {
if (presence.state === "OFF" || overridelock.state === "ON") {
return;
}
if (bedtime.state !== "OFF") {
bedtime.sendCommand("OFF");
}
player.sendCommandIfDifferent("Bedroom");
bedroomaudio.sendCommandIfDifferent("ON");
kitchenaudio.sendCommandIfDifferent("ON");
setTimeout(bootupTimer, 120000);
let wakeTimer;
wakeTimer?.clearTimeout(wakeTimer);
const sleepstate = items.getItem("SleepState");
console.log("changing Sleepstate");
wakeTimer = setTimeout(() => {
if (sleepstate.state !== "AWAKE") {
sleepstate.sendCommand("AWAKE");
console.log("Sleepstate is now", sleepstate.state);
}
clearTimeout(wakeTimer);
}, 60000);
})(event);
},
tags: [],
});
I’m fairly new to the matter of coding JS and would love to hear where I can make some improvements to my code and how to avoid bad habits, and learn more about the logic and syntax of ECMA2021 based scripting…
Thanks and I hope you have a nice day…