How to trigger with a global time variable with an offset

I feel like this exists but I looked for about 30 minutes here and in the Help and couldn't find anything.

I have a global variable SleepTime.

How do I set a trigger so that fires when the current time is an hour ahead of SleepTime? Or another rule that triggers when the current time is an hour past sleep time? I can find a trigger of a certain time, but not how to offset a global variable.

I can make lots of variables to use in the many rules I have around my sleep schedule, or I could just have my sleep time in one variable and offset it everywhere.

Thanks!

Triggering at SleepTime is straightforward, as you've probably figured out. Triggering before is tricky: as you've probably also figured out, Rule Machine does not let you use offsets with variable (or any) time as a trigger. But it's nothing you can't do yourself, maybe with a set of rules and variables like this:

Variables:

Rules to modify other variable(s) when "main" variable changes:

screenshot

Rules that effectively trigger 1 hour before:

screenshot

"Triggering" things one hour after is easy: don't trigger an hour after; just trigger at the normal time, wait one hour (or whatever your desired offset is), and then continue with your actions:

Rule 4.1 screenshot: trigger at variable time; wait for events: elapsed time for 1 hour, then do other actions

Depending on the rest of your rule actions, you could even combine lots of this kind of thing at various intervals into your "main" rule that triggers first, i.e., at the actual SleepTime.

(That being said, I could see this being cumbersome with a ton of offsets like this, so it might be a decent feature request, though I'm not sure I've seen anyone ask for this before. A custom app might also be easier if you're comfortable with Groovy.)

Alternatively, you could combine both of these ideas and take an even more streamlined approach: have a SleepTime variable and just one other variable for the most amount of time before SleepTime that you would want to start doing anything. If that's 90 minutes, then set that variable to SleepTime - 90 as in my first example (just with 90 instead of 60). Then have a rule that triggers off whatever this "SleepTime minus 90" variable is, and you can do actions like:

Trigger events: Time is SleepTimeMinus90Variable

Actions to run:

Log "It's 90 minutes before SleepTime!"
Wait for events: elapsed time --> 0:30:00
Log "It's 1 hour before sleep time!"
Wait for events: elapsed time --> 1:00:00
Log "It's sleep time!"
Wait for events: elapsed time --> 1:00:00
Log "It's an hour after sleep time!"

Do keep in mind that the rule retriggering will cancel any in-progress waits (and everything afterwards). This shouldn't really be an issue here unless you change SleepTime while an action is "running," including a pending wait, and the new trigger for this rule (offset from that) happens to be a time that will strike while the entire set of actions has not yet been completed. Probably unlikely, but I suppose it depends on when you plan on changing these variables. There's nothing wrong with keeping these split out into multiple rules either, though the same note applies to each of those rules, too. Multiple smaller rules may be easier to edit, too, though in this case at the expense of needing to make more variables.

Just a few ideas to consider!

4 Likes

Wow, @bertabcd1234, this might be the greatest response I've ever gotten online. Thanks so much for the elaborate answer! Hopefully this helps others find a solution.

So it looks like there is no true built in solution, but there are workarounds. I think time variables are relatively new by a couple of years, so its to be expected.

I like all three of your ideas for various rules I have, depending on the context. I'll give them a shot. Thanks again!

2 Likes

This is a great solution! I was trying to tackle something like this earlier tonight. I'm glad i took the time to search the forum.

I will say that this exact functionality is built in for the special cases of using sunrise and sunset as trigger times. There is an option to offset the trigger before or after the sunrise/set, and to use a hub variable rather than a fixed offset. This same option is not presented when using a "normal" time as a trigger. Perhaps @bravenel could consider adding this capability to a future RM release?

You can sort of already do that: use two hub variables and two rules. One would be like the above rule but triggered off of this second variable, and this second variable would be set in a rule that triggers when the first (only, above) variable changes and sets it to whatever time you want, e.g., plus or minus some time--effectively creating your own offset.

Presumably this feature doesn't exist for variable time because you have control over what the variable is set to and it's added complexity in the app (and interface), whereas we don't have any such control over sunrise and sunset. :smiley: Obviously, Bruce can make that call, but adding complexity to an already complex app usually needs a pretty strong case.

Maybe I'm missing something, but it is possible to trigger at a variable time with an offset:

Can the offset also be a variable?

My specific use is for a rule that runs through a series of actions, and the duration of the rule changes with the value of a variable. My goal is to have the rule finish at a specific time.
I was hoping to accomplish this by using a trigger time equal to the time I want the rule to finish, offset by the variable that establishes the duration of the rule.

As @bertabcd1234 noted, it can be done using a separate rule and variables. But it would be simpler (for me) if the time offset could be a variable much like what is available for the specific cases of sunrise and sunset. Everything contained within one rule and using variables that already exist within the rule.

No, only a constant.

And hence the origin of my earlier request to add this as a capability in a future release. As always, thanks for your consideration and no response is necessary.