Hi everyone,
I’m encountering an issue with updating a timer in my script. I use a global variable timerSunriseBedroom
and want to update this timer via a map based on the trigger. However, the following code doesn’t work as expected:
// Global variable
var timerSunriseBedroom = null;
triggers: [
triggers.ItemCommandTrigger("Switch_GF_Bedroom_SunriseSimulation", "ON") // Item to start the animation
],
const triggerItem = event.itemName;
const timerMap = {
Switch_GF_Bedroom_SunriseSimulation: timerSunriseBedroom
};
let timerItem = timerMap[triggerItem];
timerItem = actions.ScriptExecution.createTimer(time.toZDT().plusSeconds(0), () => {});
if (timerItem !== null) {
actions.Log.logInfo("Wort Ausgabe mit Timer", "Timer is already running!");
timerItem.cancel();
}
However, when I directly assign the new timer to the global variable, everything works fine:
triggers: [
triggers.ItemCommandTrigger("Switch_GF_Bedroom_SunriseSimulation", "ON") // Item to start the animation
],
const triggerItem = event.itemName;
const timerMap = {
Switch_GF_Bedroom_SunriseSimulation: timerSunriseBedroom
};
timerSunriseBedroom = actions.ScriptExecution.createTimer(time.toZDT().plusSeconds(0), () => {});
if (timerSunriseBedroom !== null) {
actions.Log.logInfo("Wort Ausgabe mit Timer", "Timer is already running!");
timerSunriseBedroom.cancel();
}
Question:
Why is the newly created timer not updating the global variable (or the map) in the first variant? How can I fix this so that I can properly manage the timer through the map?
I appreciate any hints or suggestions for improvement!
Best regards
edit:
I solved it by doing
timerSunriseBedroom = timerItem;
after calling the timer function… is there any better way to do it?