Script with delay sometimes does not run to completion (race condition?)

I have a button which triggers a macro with a collection of actions. The actions are

  1. set visibility of a “wait for shutdown…” overlay to true
  2. send all commands to shutdown system (one of them beeing a philips TV which takes approx 20 seconds to completely shutdown)
  3. wait for predefined time delay of $HomeRemote.PowerOff_Delay.Value milliseconds
  4. hide overlay

The delay before hiding the popup (point 3) is dynamic through a script which is assigned to the macro:

var powerstate = App.GetDeviceAttribute("OLED754_POWER.State");
if (powerstate == "ON") {
  App.SetDeviceAttribute("PowerOff_Delay","22000");
}
else {
  App.SetDeviceAttribute("PowerOff_Delay","0");
}
//App.ShowToast("Delay: " + App.GetDeviceAttribute("PowerOff_Delay"));

The point is if powerstate is “ON” and PowerOff_Delay is set to 22000 it sometimes works that pop is hidden after those 22s and sometimes not (popup stay visible forever). If Delay is set to 0 it always works. So if it gets stuck I usually run the macro again (this time Delay is 0) and pop-up is hidden.

But there’s an interesting thing which I noticed if I uncomment the “App.ShowToast” which shows the PowerOff in case OLED754_POWER.State is “ON”.

First the toast message is displayed with value 22000, then the series of actions in the ActionCollection are processed and when it reaches the delay and waits for 22s the OLED754_POWER.State is set to “OFF” (by a plugin) after ~15s or so, so far so good.

Now comes the weird part: The Script assigned to the Macro (or the whole Macro maybe) is triggered again cause I suddenly see a new toast message from the script with value 0. So now the script or maybe the whole macro is run in parallel a second time. I guess this could cause some race conditions or other problems.

Any Idea at all what could re-trigger this Macro? Cause I’m definitly no pressing the play/pause button a second time?

The interval on a DelayAction is not bindable. You cannot bind it to your PowerOff_Delay variable.

I don’t know if that’s causing your issue though. You should really try to refrain from using Scripts outside of a Plugin. With all of the recently added features, there really isn’t a need for that anymore.

Rather than spending time troubleshooting your issue. It may be more beneficial to spend a little time converting these scripts to Actions & Scenes. The snippet you’ve shown can be handled with the new Conditions feature on Actions.

As always, make sure you are running the latest Designer & apps version which is currently 3.17.0.