How to Make a rule to do this

But in that case what would be the trigger? If the door open is the trigger it is too late as the mode would already be home.

I don't know if this would be of help to anyone, but the following is the code I used in my own app that worked.

preferences {
section ("Night Enable") {
input "enable1", "capability.switch", required: true, title: "Switch To Enable Lights"
}
section("Entering Light Control") {
input "enteringlightson", "capability.switch", required: true, multiple: true, title: "Lights To Turn On When Entering"
input "enteringsensor", "capability.contactSensor", required: true, title: "Door Sensors To Control Lights"
}
}

def installed() {
log.debug "Installed with settings: ${settings}"
unsubscribe()
initialize()
}

def updated() {
log.debug "Updated with settings: ${settings}"

unsubscribe()
initialize()

}

def initialize() {
state.lightdelay = false

// This section for entering light control
subscribe(enteringsensor, "contact", enteringsensorHandler)
subscribe(location, "mode", modeChangeHandler)
state.currentMode = location.mode
state.modeStatus = false
}

//Control Routines

// This section for control of entering lights
def enteringsensorHandler(evt) {
if (evt.value == "open" && state.modeStatus == true ) {
enteringlightson.on()
state.modeStatus = false
}
log.debug "Previous Mode $state.lastMode, Current Mode $state.currentMode"
}

def modeChangeHandler(evt) {
state.lastMode = state.currentMode
state.currentMode = location.mode
if (state.lastMode == "Away" && location.mode == "Home" && enable1.currentswitch == "on") {
state.modeStatus = true // Mode changed from Away to Home to enable entering lights
}
}

You are going to need to replicate those state variables in RM, and use them pretty much the same way in the logic you set up.

Probably want a simple trigger that can be used to track mode. The trigger event would be the mode becomes any mode, and the action sets a String GV to %value%. %value% represents the last event value, and in this case it will be the mode.

So, then, with a rule that has Mode becomes Home as a Condition, it can have another Condition of Previous Mode = "Away", so the rule is true only when changing from Away to Home.

It looks like Bruce have solved the problem just at the moment I was going to suggest you don't use door as the trigger. Without it your light will come on when night is on and you arrive at your garage - a moment before you open the door :laughing:

I agree with that, but that has 2 rules that are triggered on mode change. So when the mode changes to home both rules will be looked at. The problem is how to I insure that the rule that sets the GV occurs before the rule that checks if the previous mode was away? It would seem to me if they occured in reverse of that it wouldn't work. I could be wrong, progably are wrong, but a concern.

EDIT: Maybe I understand. The 2nd rule is just a condition rule, no trigger. So when all the events are true then the actions take place. So it's not triggered by mode necessarily. Question here does it only perform the actions once when conditions are true until they go false again?

Actually, we would want the trigger that tracks the mode to be slow, so put a delay in it before it sets the GV that is "previous mode". Yes, both rules will fire at the same time, but it is indeterminate which fires first. But we want that GV to reflect the prior mode, not the new mode. So a delay will guarantee that the other rule runs before we change the GV.

A rule is evaluated any time any of its conditions change state. As a result of that evaluation it is either true or false, so it runs either Actions for True or Actions for False. However, it only runs the actions when its truth state changes. In other words, suppose it is false, and condition events keep happening that make it be evaluated, but it is still false. It would only have run the False Actions when it became false, not as it continues to be false. Then, when it becomes true, it will run the Actions for True (once).

Conditions:
Mode becomes Home
PreviousModeGV = "Away"

Rule:
Mode becomes Home AND
PreviousModeGV = "Away"

As long as that rule is evaluated before the other trigger sets PreviousModeGV to "Home", it will work as you want.

Here's what I ended up with. 3 rules that I think will do it. Haven't tested yet but will later today.

What you want to use is the new Wait feature @bravenel just implemented in RM. You have your trigger be mode going to Home, you have the whole rule restricted to Away and Home modes, the in your action you have a wait for door to open. One trigger....boom....done.

Is this what you meant?

After thinking about it, this is even better. Eliminates a variable and a rule. Thanks for all the help and suggestions.

Still not sure about one thing. To set the variable Previous Mode to the variable Current Mode I did this: Previous Mode = %Current Mode. Is that correct? I can't seem to find where I can see the current value of the variables to see if it worked or not.

Why do you need to have the variable at all? If the rule is restricted to modes Home and Away, that means it will only fire when the mode changes from Away to Home. It if it were to change from Night to Home, it shouldn't fire because of the restriction.

Something that comes to mind. What if the utility door never opens. What happens to the wait command? Is there maybe a way to cancel the wait after a few minutes? It would be rare, but could happen.

1 Like

I would put in a trigger of the light turning on or something else happening to stop the actions of this rule then. Otherwise it will just wait.

This should work???. If we haven't opened the door in 10 minutes it ain't gonna happen.

Just realized I probably need the stop actions ahead of the wait.

In case I forgot to say, thanks for the help. This went from a 3 rule with variables to a simple 1 rule.

That would work if you put the cancel above the wait.

And you're quite welcome. Hope it works! At least you have something else to fall back on.

One thing this experience has done for me. I learned an awful lot about RM and what it can do. Gonna go back and look at some of my other rules to see if I can streamline any.

2 Likes

RM 3 was a game changer eliminating some of my rules and allowing for consolidation. Can't wait to see what RM has in store with future iterations.

Okay, next question.

I want to turn a outlet on if the temperature is below say 37 degrees. I want it to stay on until the temperature goes above 40 degrees. I can do it with 2 rules, 1 on and 1 off, but can't figure out if anyway to do it with just 1 rule. Any suggestions? By the way the temperature is from a sensor.