[Help] Issue Updating/Syncing Timer in Global Variable and local variable

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?

This is JS in a JS file? It’s missing stuff so it’s hard to tell.

Because you never assign anything to they global variable in your rule. You assign the timer to timerItem instead of timerSunriseBedroom so timerSunriseBedroom remains null forever and when the rule exits timerItem goes away and you lose your reference to the timer.

But it doesn’t really even get that far because you immediately test to see if the timerItem in not null, which it isn’t because you just saved your timer to it, and then you cancel the timer you just created.

It’s not clear to me exactly what you are trying to do with the map. I think you are trying to have a separate timer per triggering item.

I’m that case, your global variable needs to be the map. When you create a new timer, you need to put that rubber into the map using the triggering Item name as the key.

Or you can use the cache as your map. Since these are not shared across rules you should use cache.private. This is already a map stopped outside your rule with the added bonus that you’re times stored there will get cancelled when your rule is unloaded instead of becoming orphaned.

Or you can use TimerMGR from OHRT which does all the management stuff for you.