Need help creating my first complex rule

So this mostly works. It worked great if motion is detected in the bedroom but then no motion is detected in the bathroom.

However, when motion is detected in the bathroom, and then when motion is detected again in the bedroom, it starts the rule a second time, and seems to stop the currently running instance. Is there a way to prevent the rule from running again if it's already running? In this case, the functionality I want is actually still achieved, but it would still be good info to know.

Tested it out tonight when I woke him up to go potty before I went to bed. The rule definitely is trigger every time motion in the bedroom is detected. It does seem like only a single instance of the rule can be running though, otherwise I should have had a lot more log entries and more than a single line starting "End of rule."

So how can I make a rule not trigger if it's already running?

I'm might be wrong but I don't think it's possible. Which is why I said to split it into two rules. You could probably do it in a custom app but writing apps is over my head. A node-red flow is another option.

I was thinking the same as @at9. Motion Lighting App does most of the heavy lifting. I would use a Delay Off for 1 minute in the Lights-Off section, though.

1 Like

Wouldn't that require constant motion being detected in the bathroom or else the lights turn back off?

Within the 1 minute delay-off window, at least, yes.

With 0 delay it only depends on the reset on the sensors you have so not necessarily constant motion.

True but the 1 delay gives you a full minute to re-trigger the bathroom motion sensor to keep the lights on

Is there a way to do this in RM? I'd think there has to be.

Right now I can "wait for no motion" with a timeout of 30 seconds, but as soon as no motion is detected, it will stop waiting and move on. Is there way to say "wait until no motion has been detected for 30 seconds" and then move on to the next step in the rule?

EDIT: Also... I'm messing around with creating a motion zone but I'm a little confused by some of the wording. I think I want a "triggered activation" where the trigger is the under bed motion detector.

This zone will activate when any motion sensor becomes active within the Activation Window.
The Activation Window is enabled by the Trigger Devices(s).
The Activity Timeout is started after all motion sensors become inactive.
This zone will deactivate when the Activity Timeout expires, and no further motion was detected.

The default "activation window" is 15 seconds. I don't really understand what the activation window even is. I suspect that the motion zone app will make a virtual motion detector and then only report motion as true if the under bed sensor detects motion and then the bathroom detects motion within 15 seconds. If so, that's not what I want.

The "motion aggregation" zone type seems to fit best, but it's triggered by motion of any of the grouped sensors. I want motion aggregation, but the trigger to only be 1 of the motion sensors.

You could toggle the rule's private boolean upon motion in the bathroom. Initially i have it true until motion in the bathroom sets it false. Returning to bedroom resets to true.

1 Like

Do you need to always end the rule?

@joshuadrush Not sure I understand your question in regards to that quote.

@lewis.heidrick Sorry. I replied to something you put above. Do you need to put END-IF at the end of the rule if you simply have IF, Then, Else?

It's good practice but not required. It assumes an endif if it's missing.

So I think using the private boolean might work, but I've run into a slight problem. When I modified the rule to use the the PB, it seemed to initialize to True. I went to wake up my toddler to use the potty and when I came back and checked the rule, the PB is now False and I can't figure out how to get it back to True. I think I've fixed the logic in the rule and it should always reset to True when it completes. This is what I've got now.

Quick question... is using this special "private boolean" different than using a global boolean? I don't quite understand how the private boolean can be "private" when I can apparently change it from other rules. So it seems to be a global unnamed boolean for that specific rule. Is that correct?

In my logic the PB is being used as a flag/modifier as to what motion in the bedroom is-- leaving or returning. While True...he hasn't made it to the bathroom yet. So its OK to turn off lights. This keeps logic execution in the primary IF statement and its' nested ELSE statement. Only once he's triggered bathroom motion sensor would you set False. At that point the rule is done running. Returning to the bedroom fires the rule again but now the PB False causes the primary ELSE statement to begin watching for no motion and lights off.

Your rule appears to set the PB prematurely so it gets stuck. Also is there a need to wait for motion to end in bedroom before waiting for motion in the bathroom? I'd think the sensor inactive timing might mess with your rule if hes quick enough. (My 4 y.o. 'forgets' to flush/wash occasionally)

You might find some old info in the forums because RM has changed a lot over versions. PB can be good to de-bounce rule triggering but is can be a quick flag or modifier too. Its nice that the rule is tied to its own generic PB so if you clone this rule, less problems than if you created a named global variable and both point at the same one.

Just a suggestion, the light strips should turn on red (if capable). The white can interfere with night vision, and produces blue light which suppresses melatonin production and can make it difficult for anyone to get back to sleep. Just something to consider. other wise, cool idea.

1 Like

I was using the PB to basically determine if the rule is already running. Though, I suspect what happened is the rule ran again (and therefore stopping the already running rule) when we entered the bedroom, but the PB was false, so it literally skipped everything in the rule and since the first instance of the rule didn't get to complete, the PB never got set back to True. I'm not sure if my current revision will fix that; I'll be able to test it in a little bit.

It seems weird to me that there isn't an easy way to tell a rule "Don't trigger if this rule is already running".

He actually doesn't get out of bed on his own yet. If he wakes up and needs to pee, he'll just cry until one of us goes to help him. I wake him up to pee just before I go to bed, and sometimes he wants me to hold him for a minute before he walks to the bathroom. So the amount of time we spend in the bedroom before going to the bathroom is variable. Though.... I suppose if the rule is going to trigger every time motion is detected in the bedroom, that might not matter.

I tried red initially and was faced with 2 problems.

  1. If I turned it on a dim setting, it didn't light up the room enough to be able to see without waiting a few minutes for your eyes to adjust.
  2. If I turned it up a brighter red setting, it would be really bright to look at.

Plus, the night light that comes on in the bathroom is white and is brighter than the light strips so I don't think it'll matter anyway.

I don't understand what's happening here. I removed my custom log entries to make the rule easier to read. Here is the current rule.

However, the PB doesn't seem to work. You can see where the PB was set to false, but yet when motion was detected in the bedroom again, the rule triggered from the beginning and somehow the PB is now True?