End goal is that for a few of my lights, the time to turn off in evening should be no motion for 15 minutes, but in night, it should turn off after no motion for 2 minutes. These are Rule machines trigger lights and cannot be controlled via Lights/Motion app.
I am thinking of a setup, where I create a Rule, which sets a "light_timout_delay" variable.'s value as 15 or 2 depending on the Mode being Evening or Night.
I next plan to update my existing 1 or 2 other rules, which actually do the light triggering based on motion, and set the wait duration based on the above variable. These lights are also triggered on based on Mode.
My question is, if there is a way to define the order of rules? How do I ensure that the rule to turn on light based on Mode, is only run AFTER my earlier rule is run, which actually sets the variable value?
You could turn the lights off using switches per mode.
Here is a rule I have for a set of lights that has a delay of 30secs turn off unless mode is night when it is 5 secs.
This may give you some ideas.
Here's some code which may help. I lock doors based on MODE (time of day) and based on a switch (auto-locks ON). Time of day determines the lock timer (30 minutes day / 10 minutes at night/evening or MODE away).
There are two rule sets - with predicate conditions determining which rule set to run. The AUTO LOCK switch is the 2nd predicate condition which determines whether or not to run the rule. You can also move the predicate into the rule itself as a conditional if you want to add other conditions that don't work well as a predicate.
The trigger is simply the device action you want to timeout (in my case the lock 'unlocked'). It triggers the reverse action with the desired timer delay.
If you want to trigger on a sensor rather than the device itself, then simply add the action (turn light ON), followed by the opposite action with the delay (turn light OFF delayed xx).
In my case I set hub variables to store the delay time so that I could change the delays outside the RM code itself. The delay variable is in seconds, so for 10 minutes I set the variable to 600, etc.
The way this 'should' work is each time there is action (motion) at the sensor, the rule will be triggered which resets the OFF delay. The reverse action (turn off the light) will only occur once the timer has timed out with no motion to re-trigger the rule.
If that's not the case, may need to tweak how/where you put the delay. There are also WAIT options within RM 5.0 with CANCEL/etc that you can use to help refine the rule.
See this post (which I now realized is ALSO your post!!) for more detailed discussion to do this with RM5.0 WAIT events: