Need help creating my first complex rule

Here's what I want to happen.

  1. Toddler gets out of bed. Motion detector under bed sees this. There are LED strips under his dresser. They should turn on warm white at level 3. The bathroom has a nightlight on a smart switch (technically it's the exhaust fan). It should turn on. This should only happen if the mode is night or sleeping.
  2. Once motion in the bedroom ends, a timer should start. I think maybe 1 minute (bathroom is literally right across the hall, it should take way less than 1 minute). It's possible the motion was triggered by us checking on him or one of the cats. If the motion sensor in the bathroom detects nothing after 1 minute, turn the lights back off and the rule should end.
  3. Motion is detected in the bathroom. The lights should stay on until motion is detected in the bedroom again. Once motion in the bedroom has been detected and then not detected for 30 seconds, all the lights should turn back off and the rule end.

With rules being event driven, I'm not really sure how to structure this in RM. I already have the logic to turn the lights on, and a local boolean called visitedBathroom set to false.

I've looked around a the various actions I can add after turning the lights on, and they seem close to what I want, but not quite. I see "wait for events" which I can use once motion in the bathroom has been detected. I

And somehow, just changing the different actions around broke the rule completely. I can't click "select actions to run" without this error coming up in the log, so time to delete and restart I guess.

app:5852020-07-30 04:59:19.507 pm errorjava.lang.NullPointerException: Cannot invoke method tokenize() on null object on line 2646 (selectActions)

First followup question:

How do I make a trigger be an AND and not OR?

image

Trigger events cant be and. Use the motion sensor as the trigger and use the mode as a conditional in the rule.

I removed the mode from the trigger, and it still shows as a condition when I select actions. Is creating a condition here good or should my first action be an If Then statement to check the mode?

image

It's fine. It just keeps it in there because you had it as a condition before. Select action, conditional etc etc...
The same could be done with motion lighting... Here's an example:

Actually this was really easy to test. I went ahead and just saved the rule and walked into his room. Here's what my rule looks like.

image

When I click Select Actions to Run, where it says Manage or Create Conditions I have Mode in Night, Sleeping, just like the screenshot above shows.

When I walked into his room, all the lights came on. So I'm not sure what that conditions area does, but it does not do what I expected.

RM4 is a powerful tool just remember its a takes more resources to process rules in RM4 than in some of the smaller purpose built apps.

Form your actions in the normal format

ff Something
then do this
else (optional)
do this (optional)
endif (optional but good practice)

You don't have that night condition spelled out anywhere in the rule itself.

There are 3 parts to a rule:

  • Triggers, which like the name implies are events that start the rule running.
  • Actions, which again like the name implies are things you want to happen.
  • Conditions are limitations on the actions, but they aren't automatically acted upon. You could have 50 conditions listed, and if you don't use them, they just sit there doing nothing. So you have to put them in the Actions section in some way or form.

So you need to edit that rule to say something like (not using exact RM syntax here)

If mode is night and motion is active (I suppose you could invert that too)

Then set color Joels Room 3
On Guest Bath Fan

Now my rule looks like

image

Conditions are limitations on the actions, but they aren't automatically acted upon. You could have 50 conditions listed, and if you don't use them, they just sit there doing nothing. So you have to put them in the Actions section in some way or form.

That doesn't make any sense to me. Then why have a separate place to manage or creation conditions if you still have to explicitly state the exact same thing in the rule?

Anyway, the next part is the part that I'm getting stumped at. I see I can wait for 30 seconds, but I'm assuming that basically just pauses the rule. How do I check for motion while it's waiting? The next part should basically be

while(waiting 30 seconds)
  if (motion in bathroom)
    visitedBathroom = true

if (!visitedBathroom)
  turn off lights

I can't figure out how to do anything like that.

Seems like your making it more difficult than it needs to be. You have a motion in the bedroom and in the bathroom. Use two rules and trigger events off each of the motions. First rule turns on the night lights in his room. Second rule turns on the nightlights in the bathroom.

1 Like

That's how I have it now. The problem is that I want the lights in the bedroom to still be on when he leaves the bathroom. So I have them set to turn off after 5 minutes of inactivity. But this means after he goes back to bed, they are on for 5 more minutes, and he doesn't like this. I want all the lights to turn on for him when he gets up, stay on until he's done, and then turn off shortly after he gets back in bed. I don't think that should be to hard to do.

For the first rule set the motion to turn on for 30 seconds, 45 seconds 1 minute or whatever. Could also have the bathroom motion trigger his bedroom lights.

I found using a text file to write everything out before hand is a great help. You are not bound by the "assembly structure" of RM4 and the logic is easy to read and modify.

And as stated before:

Trigger events are all OR's together.

If you wish to AND things you have to do it in conditional statements i.e. IF statements

Remember the Actions to run will only be run when there is a trigger so you must keep them in the Trigger portion of the code.

And trust me, trying to sort out the logic when you are entering the rule in RM is a path to the nut house.

You can choose a pre-built condition instead of creating it on the fly while creating the actions. This is often less clicking and may be less confusing, but either way certainly works. The conditions "bank" also allows you to re-use a condition multiple times and always point to the same condition, so if you need to change a device, you only have to do it in one place (for that particular conditional, at least).

Would this do what you want?

1 Like

I think I'm making progress towards what I want...

  1. Motion under the bed detected. Turn the lights on.
  2. Wait for event (motion in bathroom) with a timeout of 30 seconds
  3. IF (motion in bathroom) {
  4. Wait for event (motion in bedroom)
  5. Wait for event (no motion in bedroom)
  6. Turn off the lights -> delayed 30 seconds. }
  7. ELSE turn off the lights

I think that flow should do it. It may need some tweaks, but that's the gist of it.

This is what I ended up coming up with. I can't test it now because my son is sleeping. But maybe I'll get a chance tomorrow.

Hope it works for you. Not that I think it won't. Just some things need real life testing instead of armchair quarterbacking.

But my advice would be from here, to clone that rule and either keep the clone as a "clean" copy, or edit the clone and keep this as your clean known good copy. Pause the one so both rules aren't running. In fact, you can have multiple clones paused and waiting if you want to.

1 Like

In addition to what neonturbo said, I would save your screenshot so if needed you can reenter without going through the creation process again.

2 Likes

To put it in another way, the trigger is "omg something just happened". You might be hiding in the forest listening for the approach of either a tiger or a bear (OR). If you're only worried if they both come at once, that would be an (AND). But that would probably never happen - you might as well relax and go to sleep if you're not worried about a bear coming without a tiger or vice versa. That's why triggers are always OR.

Now for the condition. You may only need to listen for the tiger or the bear if you haven't got your trusty and armed safari guide with you. So that would go into the conditionals "(only) IF safari guide absent". You wouldn't use your safari guides absence as the trigger, because then you'd be getting alerts every minute even when there was no danger. The safari guide's status would therefore be in Conditions.