Power Allowance producing odd effects

So I’m using the Power Allowance app for a number of things in my house. Unfortunately it seems to be producing some odd results and I’m not exactly sure where it’s coming from. Hopefully someone can help me figure it out.

The app is pretty straight forward. It simply subscribes to when a switch is turned on, and then turns it off X-number of minutes later. This part works fine. My kids like to listen to radios at night so I have them plugged into an outlet with two hours on power allowance. After two hours, they turn off.

The issue though is that there seems to be multiple off commands being sent to the switch somehow. If my son turns on his radio during the day, the switch automatically turns it off two hours later. Not a big issue as he rarely has it on for that long. But if he does when it turns off he needs to turn it back on. Again, not a problem, easy enough to do. But when he turns it back on, the outlet turns off again in like three minutes. He’ll turn it back on, then it clicks off again minutes later. Turn it on, then it clicks off.

I can’t figure out where these other “off” commands are coming from. There is only two other apps that touch this switch and both are designed to turn it on, not off.

Nevermind… I think I found the problem.

Apparently the “delay” process can stack. If I run this command whenever the switch turns on:

switch.off(delay: 3600000)

And then I turn it on at exactly 3:00pm, it will turn off the switch at 4:00pm. But let’s say I turn the switch off at 3:35, then back on at 3:45. Well, the program runs that delay command again so it’ll turn the switch off at 4:45. BUT… the previous delayed switch.off command is still waiting to fire and it will at 4:00pm. So at 4:01 I get annoyed cause my switch just turned off, and I turn it on again. Now I’ve got a delay turning it off at 5:01, but the 4:45 is still waiting to fire as well. As you can see you can quickly get these lumping up on top of each other.

I wrote a new app Improved Power Allowance that uses the runIn command to schedule a turn off event rather than the delay command. The because runIn schedules it will “unschedule” a previous instance if it needs to re-schedule the same command.