Repeat While Door Left Open Logic

I'm attempting to consolidate a two Rule solution into a single Rule.

If I'm understanding correctly, the issue with the logic below is that if the door is closed immediately after the Attention: Please close the pantry door announcement, it could be up to 0:05:00 until the while Repeat is exited and our receiver switches back to the correct volume and input - less than ideal if we're watching TV...

Before [END REP] (just after the speak action) add the following two actions:

  • wait for event: pantry contact closed
  • stop repeating actions

Try that, I think it'll fix the issue. The IF just places a condition on what starts the repeat action, but not what ends it. Adding those actions should immediately end the repeating section when the contact closes and allow the rule to finish immediately.

Alternately, you could simplify the whole thing a bit by eliminating the embedded IF condition altogether changing the type of repeat from a simple timer to a "Repeat while Expression: pantry contact open". In that case, you would not need the extra actions listed above, the repeat will end itself when the contact is no longer open.

** I just noticed it also looks like the embedded IF condition that starts your repeat is not closed. I think you might also need to add an [END-IF] just before the [ELSE]. But anyhow, I would go with the second option, using the "Repeat while Expression" action and removing that embedded IF completely.

*** Also you could simplify it even more by removing the repeat and all those actions out of that giant IF. Instead you could do something like:

Trigger: Unchanged

Actions:

  • IF (Mode is: Asleep) THEN
  • Log: your log entry
  • Exit Rule
  • END IF
  • now add all the AVR volume actions, turn on light, etc
  • While: Pantry Contact open - repeat every 5 min (stoppable)
  • Your repeating alert actions
  • END REP
  • reset volumes
  • turn off light

I believe the Wait for Event Action will pause execution and prevent repeating - "Wait for Events allows you to specify one or more events. The rule will effectively pause at this point and wait for one of the specified event(s) to happen."

The Repeat While is what I have implemented. See Repeat While Loop

Unless you're aware of an alternative Repeat While?

I believe the END-REP functions as the END-IF for the Repeat While conditional.

Nope, everything within the REPEAT <--> END REP will get repeated at the desired interval. If the wait takes longer than the repeat time, the wait is just cancelled and then restarted upon repeating. Then as soon as the wait is satisfied, it will proceed to the "Stop repeating" action, end the repeat and continue. I have several rules like this. Here's an example:

Huh, perhaps your rule was originally from an older version of Rule Machine?? When I use the "Repeat While Expression" action, mine shows up differently. Mine says "While" instead of "IF". I can't explain the discrepancy, but here's an example of a "Repeat While Expression" on one of my rules:

I don't understand how/why the Wait isn't blocking, but I tested it and you are correct!

Correct, I switched to Repeat Actions-Repeat While Expression.

All appears to be working as desired now. Thank you for the guidance.

For reference, here is the updated Rule:

1 Like

Glad it's all working now!

Since you switched the Repeat type now, I think you should be able to remove the -Wait and the -Stop Repeat actions. Try disabling them by checking the checkboxes on the right and test. You should only need to use that on a regular periodic repeat. Now that you have the "While (Pantry contact open)" part in the repeat, it should make the wait and stop redundant.

I tested and the Wait and Stop Repeating Actions are still required because the While is not immediately cancelled once the expression is no longer true.

1 Like

Awesome, yeah, I have definitely started overthinking it now.

I don't know if this helps you streamline things but here is my door open rule...

This works for multiple doors and allows for being turned off if you want the door(s) left open. Initial timer is 5 mins then repeats every 2 mins. Closing door resets the rule. Note, mine is very passive aggressive to annoy my kids and it's done in a female british accent.

1 Like

That's how I roll :crazy_face:

The logic of your Rule is interesting. There really are many ways to skin your kids for leaving a door open.

If I may challenge you, could rewrite your Rule so that it scales to N doors?

Just add all doors (or contact sensors) to the trigger and wait for events. The rest will be taken care of by the variables. Though be warned, every time any of the doors are open, the mute switch if on will turn off and the rule resets itself and you have to turn the mute switch back on.

1 Like

I'm skeptical, not because of your Rule writing ability but because I've found Hubitat to be more functional when separating rather than combining, but I'll give it a go.

What is the mute switch?

That's a virtual switch. If turned on (in my case it can be done with a programmed button on a pico on the wall or a shortcut on my phone) disables the repeating alarm allowing the door(s) to stay open without squawking... Note if one or more doors close the rule resets and the mute switch turns off. Just have to turn the mute switch back on and when it gets back to that section just stops.