Is Rule-5.1 re-entrant?

What happens when a Rule-5.1trigger event occurs then another trigger event happens before the same rule completes?
There are many possibilities.

  1. Each trigger creates a separate Rule-5.1 instance, such that multiple Rule-5.1 with the same name execute.
  2. The new triggers are queued and only one instance of the Rule-5.1 runs at a time. The queued triggers would in turn be processed in order after each Rule-5.1 instance completes.
  3. While the Rule-5.1 is executing all new triggers are ignored and deleted.

This. And, in some circumstances this will be problematic. Depends on the rule. For embedded delays in conditional actions, it won't work and will throw an error.

Rules are not reentrant. There is a single state object that all instances write to, and for rules that depend on it, for example IF-THEN, that's a problem.

I am trying to understand how a Rule-5.1 handles the case where a trigger occurs while the Rule-5.1 is currently processing a previous trigger.

What exactly are the potential problems?
Can a Rule-5.1 require a programming semaphore?
If the trigger registers a single state object (system variable) for a defined Rule-5.1 trigger, when does that trigger state objects get cleared?

As I explained above, actions that involve state objects will have problems with simultaneous execution. Those include IF-THEN, delays, etc. Most actions don't have this issue.

But, you have to be very clear about what "currently processing a previous trigger" actually means. The execution of a rule happens in milliseconds, except for delays and waits. Of those two, delays are more likely to create a problem, as a new trigger cancels pending waits.

Not relevant, and not part of the implementation of Rule 5.1. A semaphore-like thing can be done using Private Boolean and a Required Expression, that can prevent a rule from running more than once (for most situations).

Events don't involve state objects per se. State objects used in a rule are 'cleared' when the execution of a rule instance completes. In some cases, specific state objects can be forced to their initial state. For example, the actions Cancel Delay and Cancel Rule Timers clear those elements. Again, it's difficult to explain this in the abstract.

General comment: avoid embedded delays in conditional actions if the rule can be triggered during the delay. Or, deal directly with the multiple triggering somehow in the rule itself. How to solve these issues depends on the specifics of what is causing the trigger and what the rule logic is.

1 Like

I have Aqare Vibration sensor as a Rule-5.1 motion trigger for washer Rule-5.1.

  1. Send notification ("Washer motion")
  2. Delay 5 minutes
  3. Exit if inactive
  4. Wait for Vibration sensor motion to go inactive for 3 minutes
  5. Close a virtual contact
  6. Send a notification ("Washer done.")
  7. Open the virtual contact
    I get several "Washer motion" ('s) and no "Washer done.".

Just get rid of the delay, you don't need it. Probably get rid of the first notification, as you'll get lots of those (to what end?). Or put it behind a test on Private Boolean, so you only get the first one. Test on PB true, and set it to false. Then set it to true again at the very end of the rule.

I live is a dynamic household in which people just do stupid stuff...
I have had cases where someone starts to do a load of wash, then decides to forget it, because of what ever. The 5 minute delay is to filter out this case (where it detects motion, but never really start). I just want peace and harmony (to avoid the complaining).
Waiting the 3 minutes gets past the washer's quiet periods.

I'm just trying understand how the Rule-5.1 works...

I use a zooz power monitoring switch this rule
No issues needing a delay and no false alarms.

You do need to tweak the rule for the cycle times and power usage of your washer however.


1 Like

Why would I get several "Washer motion"('s) and no "Washer done"?
The 5 minute wait is conservative.
I have the Rule-5.1 logging the Events, Triggers, and Action (no joy).
The washer takes about 100 minutes to finish.
It sounds like a Rule-5.1 is reentrant..

I'm doing more testing. I'll report any positive results.

Because the first thing you do is notify Washer motion, every time it is triggered. But it's never getting past what you described above as "Exit if inactive", I guess. You don't need that. It sounds like you just want to wait 5 minutes at first, when the washer first starts, and then wait until it stops moving for 3 minutes, and that means it's done, right?

Then put the delay in the IF-THEN described above, after IF-THEN Private Boolean true. That way, it only does the delay the first time the rule runs. After the END-IF comes the Wait for Event. Every time the rule is re-triggered the Wait will be cancelled, but only the notification and delay will happen the first time, when it first starts up. However, if the washer stops for 3 full minutes, the rule is going to interpret that as the washer being done. If that's not a desirable outcome, it will become more complicated (see below). The one thing you don't want to have happen is multiple simultaneous instances running with the same delay pending.

Another way this could be done (instead of above) is to make the Delay cancellable, and to Cancel Delay as the first action of the rule. So, each time the rule runs, a new 5 minute timer starts.

Reentrant would mean that each instance would have its own state, but all instances of a Rule share a single state. So it's not reentrant. However, it can run multiple times, and it can run while it is already 'running' (e.g., during the delay).

Reentrant (multi-instance) code is a reusable routine that multiple programs can invoke, interrupt, and reinvoke simultaneously . When you want to reuse code, but associate each instance of the shared code with unique, preserved data, use reentrant code.

Rule fails because it doesn't have "unique, preserved data" for each instance -- instead only a single copy.

@bravenel since we are talking about this topic, I often run into issues when I use local โ€˜workingโ€˜ variables getting messed up in the first instance when the rule is triggered again in quick succession. Is there something I can do to alleviate this?
In some cases, I would be okay with the first instance being cancelled by the second, but in most, I need both to complete their task.

I'd have to see the rule and an explanation for why it's triggered in quick succession. Is that intentional?

After several runs, the Washer Rule-5.1 (R5,1) implementation did not work properly.

I created a control R-5.1 test that used a simplify rule using virtual Motion device for the trigger. The results showed:

  1. A R-5.1 rule is reentrant in that it can be triggered while it was executing a previous trigger event.
  2. The rule simply executes at the beginning of the script every time a trigger occurs.

I am considering a statistical script that has no waits nor delays, except for the final no-motion wait at the very end of the washer cycle. This should work because there would be no more trigger events at the end of the cycle.

i reiterate i never got motion to work correct for this which is why i posted my rule for power monitoring, the rule i posted is designed to be re-entereed which actualls cancel the wait..

and it starts over.

The major reason I did not choose a power monitoring approach was because my dryer is electric, which makes it difficult to find and connect a power monitor to a 240VAC (3-wire) connection.

Works with 3 and 4 wire connections.

Here is another 220VAC power monitor sensor (I have not used it.):

My horizontal washer required the vibration monitor to be mount internally onto the washer drum (plastic). The drum is suspended by coil springs that did not provide enough vibration to the chassis.

Here is a R5.1 that works.
Trigger Event:
Washer motion active

set Seconds to now() in seconds
Set DeltaSeconds to (Seconds - LastSeconds)
Set LastSeconds to Seconds
Set TotalSeconds to (DeltaSeconds + TotalSeconds)
IF (Variable DeltaSeconds > MaxSeconds) THEN
Set TotalSeconds to 0
Set Private Boolean False
Exit Rule
ELSE-IF (Variable TotalSeconds > MaxSeconds) THEN
IF (Private Boolean is false) THEN
Set Private Boolean True
Notify MyPhone: '%device% active'
Wait for event: Washer motion inactive and stays that way for: 0:05:00
Set TotalSeconds to 0
Set Private Boolean False
Notify MyPhone: '%device% done'
active() on Alexa Washer
Wait for event: Washer motion inactive and stays that way for: 0:05:00
active() on Alexa Washer

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.