Periodic Schedule trigger scheduled even when required exp. is false

Just wanted to check an assumption I had regarding required expression. I have a rule that uses a periodic schedule trigger of "every two seconds", with a required expression that is quite restrictive such that the rule doesn't get actually triggered very often and for short periods of time. This is on a C7 running 2.3.6.146.

It turns out that the Periodic Schedule is in fact scheduled at all times, even though the required expression is false. I noticed only because app stats revealed the rule is consuming a surprising amount of CPU, all things relative, due to its frequency.

Am I wrong to assume that the periodicHandler should not be scheduled unless an event makes the required expression true and until another event makes it false?

Here's a mockup of the rule I created to test it out:

Think of it this way, HE is an event based system so something has to trigger the execution of your rule. As written the event is every 2 seconds which will then execute the rule to check your required expression condition. Without the 2 second trigger the rule wouldn’t know to check the virtual switch defined in your expression.

Hope this makes sense.

Yes and no.

HE being event-driven, I expect the rule to listen only to the events the required expression depends upon (in this case, the switch). And once the required expression becomes true, then listen to the events the triggers depend on.

For example, if I add another trigger, e.g. for another switch state change, that trigger doesn't get a subscription in the app settings (gear icon)

Once the required expression becomes true, you can see the switch event handler appear in the list:

I am expecting the same behaviour for periodicHandler.

(the updated test rule for reference)

Required expressions are only evaluated when the trigger defined happens. So in your original example it is evaluated every 2 seconds. Think of it as an overarching condition for the rule to continue processing the actions or stop.

In your example if you want the rule to only fire based on the state of the switch then that should be the only trigger, not the periodic schedule of 2 seconds. Now if you want the rule to continue processing for 2 seconds after the switch turns on, then in the actions setup a repeat and go from there.

I could be wrong, but I believe it’s the other way around. Otherwise switch 2 events would be subscribed at all times. Also the “evaluate required expression at startup” option would make no sense…

My understanding is that only required expression events should be subscribed by the rule until I the required expression evaluates to true. Therefore applies to all triggers simultaneously.

I think conditional triggers work as you describe except they are per trigger.

From the documentation

@bravenel is this a bug or is there a reason that the trigger's periodicHandler must remain scheduled when the required expression is false ?

I don't recall why that wasn't done, and is maybe the source of the word 'typically' in what you quoted. Irrespective of the scheduled job still being there, the trigger is not run when the Required Expression is false (the rule wakes up on the schedule, discovers Required Expression false, and exits. I'll take a look at it...

1 Like