Multiple button up presses adds delay, down cancels

So I got this working I'm pretty sure but I'm wondering if there's a better way to do what I want with a custom app I haven't found or even consolidating it into one rule. I'm a programmer so I understand the logic, but I'm still wrapping my head around using the functionality of RM.

Basically I have a switch that controls a fan and I want to turn it on and have it turn off after 15 mins but if I press it up again it will be 30...again would be 45, etc and if I manually turn the switch off then it turns off and resets everything. I'm currently using 2 rules due to the multiple triggers (button 1 vs 2). The private boolean is basically my "is this the first time it was pressed up/initially turned on" variable.

First rule:

Second rule:

Any tips for making this better or did I accidentally do it right on my own?

Only suggestions I have and really just my preference, nothing wrong with how you did it.

I would get rid of the private Boolean and just set delay to 0 instead of setting the Boolean true and use that.

I would probably also make it one rule. You can look at %text% after the trigger and determine which button was pressed.

For extra credit you could do a time difference calculation and have it add the delay time to whatever delay time is left.

OK...I set button 1 or button 2 as trigger and used conditional action to see which button was pushed and if 1 do the on actions and if 2 do the off actions. That got me down to 1 rule. I'm still thinking about working on the "adding delay to whatever delay is left" but since I don't need that it's not at the top of my list. However, I'm not quite understanding the

part. Could you point me in the right direction of what you mean?

The Private Boolean is only used to determine if a delay is already set.

Instead of looking at a private Boolean you can just look at the value of delay. If it is > 0, then a delay has already been set.

When the delay is over just set delay to 0. I would also use a wait for event Elapsed time using the delay variable, instead of delays. Then you don't need to add the cancel delay. With the wait for event it will automatically get canceled on a new trigger. Your method works fine it's just the way I would do it.

IF delay = 0 then
    Set Delay
ELSE
    Add delay
ENDIF

set
wait for event elapsed time delay min
fan off
set delay = 0

Thanks...that makes sense (figured it would be obvious after you explained). I'll take a look at the elapsed time event...

Now I see the efficiency of using the wait for event vs delay and the automatic cancellation. Just need to attempt the "extra credit" but I like this much better so thank you very much.

1 Like