Trigger Basic Rule when Mode Changes

I have a basic rule which will turn on the Porch Light if the Front Door Sensor is opened when the mode is Evening or Night, it will then turn it off if the contact is closed for 5 mins. What I would like it to do is if the mode goes from Day to Evening, and the Front Door contact is open, to turn the light on. Is this possible? I am thinking about this as 2 triggers, but reading up that is not possible in Hubitat? Or am I thinking of this wrong. Apologizes if this has been posted, did do a bit of googling and I couldn't figure it out. Thank you.

Right out of the gate, I'd suggest creating an entirely new rule for the 2nd automation instead of trying to modify the 1st one. The question you're then faced with is, Can the Basic Rules app accommodate all of the required elements?

The trigger based on Mode changing to "Evening"? No problem. Turning on the Porch Light in response? Sure. Doing so only when the Front Door contact is open? Not so much, since Basic Rules' optional restrictions are based exclusively on Time, Switches and/or Illuminance.

I'm afraid there is no provision for "Contacts" here, which suggests you might want to try creating your new rule in Rule Machine, whose restrictions (principally "Required Expressions" although it allows conditional branching in certain other contexts) can derive from pretty much anything you can conceive.

Something tells me you already sensed that coming, since you posted this in the RM category? If it would help you see a working example in RM, I can post a screenshot.

On the other hand, if you prefer working in Basic Rules, considering creating a Virtual Switch to mimic the Open/Closed status of your contact sensor, then use the Mirror App to have the switch follow the contact's status. This way, the switch can serve the purpose of restricting when your new Basic Rule executes.

Yes sorry I should have been more clear, I couldn't find a way to do it in Basic Rules, so I figured Rule machine was the way to go. I also think I was thinking about it the wrong way, correct me if I'm wrong, but I want 1 Trigger (moving from Day to evening) and 1 Condition (Front door contact is open) and 1 action with a follow up (turn off when front door sensor has been closed for 5 mins). Would it still make sense to have 2 rules, since my other rule is based on the Front Door sensor opening Trigger. I have tried to modify my Basic Rule by importing it into Rule Machine, i'm not sure if I have it correct. Any input or screenshots would be awesome, thank you.

1 Like

Great, I think RM really is the right place (it's like you've graduated from Basic Rules!). As a graduation present, I've built you a sample rule taking into account (hopefully) everything you said you wanted it to do:

I think it's largely self-explanatory, but you might have to ponder a bit on what constitutes a Trigger (which is an spontaneous event), versus a Condition (the ongoing state of something), and how the latter can be used within an Expression (evaluating one or more conditions), along with how and there each one is employed throughout the rule.

I tried to name everything more or less in line with your naming scheme, to help make the rule as familiar as possible. Let me know if you have questions!

In reviewing your rule's first iteration (thanks for the screenshot), I see you made an inspired choice in selecting:


meaning the rule cancels pending actions when the R.E. becomes false. But I invite you to compare yours with mine, as far as how we're detecting the change in Mode. On mine, the R.E. of "Mode is Day" means "Refrain from running this rule unless Mode is currently set to Day." This gives meaning to the Trigger of "Mode becomes Evening" (which is how I interpreted your OP). In my rule, if Mode were anything else but Day, or changed to anything else but Evening, nothing happens. In yours, with "Mode changed" as your Trigger, it will fire under a lot more scenarios than you intended.

Another thing to consider: While you've correctly defined a Condition (under Conditions), notice how it is "(unused)", thus serving no purpose yet. Compare where I've placed it in my rule.

Your rule nicely waits for the Front Door Contact to close and remain closed for 5 minutes, using "Wait for Event", whereas I elected a slightly different approach with "Wait for Expression". I believe both are valid in this circumstance.

Again, hoping all this helps shape your understanding of RM and its various parts. We've all gone down dead ends as we learn, so good luck!

1 Like

harmony7, Thank you so much for your help! I was thinking a Condition was something which should be true when a trigger is set. The Cancel pending action when Required Expression becomes false is something which was defaulted? when I imported a basic rule into Rule Machine. I have followed your guide and implemented a rule. I noticed the rule has a (Required Expression false) next to the name. Is this because it won't trigger right now because it's not Day and the Front Door contact is closed?

Yes, the R.E. reports "(False)" (and is reflected in the rule name in the UI) because that's how it evaluated on my hub at that instant. You've got the right idea...

More generally speaking, a Required Expression must evaluate to True in order for the rule to be eligible to run (by being triggered).

Now that we've both had a good night's sleep, it's worth turning our attention to what you really intend this rule to accomplish. Before, you mentioned two triggers, with a specific Mode thrown in. Is it possible you meant for the rule to do this instead?

  • Only run during Mode "Evening" (Req Exp)
  • When the door opens (Trigger)...
  • ...Turn ON the light (Act1)
  • Turn OFF the light 5 mins. after door closes (Act2)

I suspect this is what you intended, rather than triggering just on the "Day" to "Evening" transition. If so, this greatly simplifies the rule!


I hope this modification helps illustrate some of the nuances encountered in Rule Machine, and better suits your needs.

1 Like

I actually want it to do both, so I after tinkering with it I think I need 2 rules. One to turn on the light in the evening if the door contact is opened. Also one to turn on the light in the transition from Day to Evening if the door contact is open. Would the rule you posted trigger on the transition? Basically I'm out in the front yard, the sun sets, would the light automatically turn on if the front door is open?

You do not need two rules as this can be done in one rule using conditional triggers instead of required expressions. See this sample:

I've used my own stuff, but it would work for you. In the triggers, my the rule would run if:

1.) Mode becomes Ambiance (your Evening), but only if the front door was open
or
2.) The front door opens, but only if in the Mode of Ambiance or Overnight (your evening and night modes)

From there, the actions are the same. Turn on the light, wait for the front door to close and stay that way for 5 minutes, and finally turn off the light. Reading this thread, this should accomplish everything you want in a single rule.

2 Likes

That worked great, thank you.

2 Likes

Yes, this is an excellent way to make something happen only when two conditions apply — trigger on each with a condition on the other.

If you need more than two, you have to phrase it differently: trigger on all of them, then wrap the whole thing in an IF(A AND B AND...) The rule will trigger a lot, but do nothing if they haven't all been met yet.

It's useful to understand what Rule Machine is doing for you. Required Expressions are essentially an implied helper rule that enables and disables your actual rule. Conditional triggers basically wrap your rule in an IF, except that you can define a separate condition for each trigger if you want.

2 Likes