Code to not turn lights off that are already on

One of my biggest issues with my smartthings is that if I program a light to come on when a door opens, then go off in an amount of time, if the light was on to begin with then it shuts off when it shouldn’t.

Is there a way to have the program check to see if the light is already on before it runs it’s event program of turning the light on then shut off in x minutes?

Thanks!

Even though I follow the posts very regularly I would be interested in knowing this too. Perhaps the veternes can chime in. My simple case is my hues are all on after sunset and I open the door and close it, my lights go off as they are also tied to open/close event of the ST Multi on my front door!
I know I can write some code but I am sure this already has been done in a much better way. Guys?

I posted a similar question about turning off lights that were turned on manually.

Basically, there needs to be a few additions to the core software so it is an option in all apps automatically:

  • a “previous device state” variable so it understands the state of the device prior to execution of the action which can be held by the app to complete that task later, such as the timer to turn off a light
  • the ability to turn off devices as either “all selected no matter their previous state” or “only devices that were not in a differnt state than expected when action began” (and the opposite of this one, as in “only the devices that were in a differnt previous state” (I can’t think of a scenario for the last one, but it seems that having the opposite of something is always a good option to have)
1 Like

This isn’t that hard to do. You use a virtual switch, so that the “light” that gets turned on when the door opens is the virtual one. That in turn fires an app that looks to see if the real light is already on, and remembers that in its state. Then, when the light wants to shut off in x minutes, again it’s the virtual switch that “turns off”, and the app looks at its saved state to decide whether or not to turn off the real light. The only way the real light gets turned off is manually (or some other logic that might apply, like a mode change or Hello Home phrase).

I just wrote an app to test this for my closet light, which ordinarily turns on with motion, and off in a couple of minutes after no motion. Now, if I have turned it on manually, it stays on until I turn it off manually. However, if it’s off, motion turns it on and it turns off after a couple of minutes.

No need for any additions to the core software of ST. Just a virtual switch and a simple app does it.

1 Like

Bruce,
Mind sharing a hat code with me? Sounds like a good way to do it.

Thanks!

Here you go:

preferences {
    section("Real Switch...") { 
        input "realswitch", "capability.switch", 
	title: "Real Switch...", 
        required: true
    }
    section("Virtual Stand-in...") {
    	input "standin", "capability.switch",
        title: "Stand In Virtual Switch...",
        required: true
    }
}

def installed() {
    subscribe(standin, "switch.on", switchOnHandler)
    subscribe(standin, "switch.off", switchOffHandler)
}

def updated() {
    unsubscribe()
    subscribe(standin, "switch.on", switchOnHandler)
    subscribe(standin, "switch.off", switchOffHandler)
}

def switchOnHandler(evt) {
    state.wasOff = realswitch.currentValue("switch") == "off"
    if(state.wasOff)realswitch.on()
}

def switchOffHandler(evt) {
    if(state.wasOff)realswitch.off()
}
4 Likes

Not hard, but a simple and clever way to handle it. Thanks for sharing, Bruce!

realswitch.currentValue(“switch”) == “off”

I think this is not or no longer correct.

When the switch is “off” the value is null. When the switch is on the value is “on”.

Nope, that’s still the way you access a switch’s current state.

http://docs.smartthings.com/en/latest/capabilities-reference.html#switch

One can also use device.currentSwitch == "off"

@bravenel, Thank you for providing your code. I’ve tried to adapt it to dimmers, but ran out of intelligence. If you could please coach me on how to add your code to Dawn to Dusk Dimming it would be much appreciated.

Hey, that was like a year ago. What is exactly that you want to do?

Wow, quick response. Thanks Bruce.

The app that I have linked brightens and dims a light based on motion. I’m wanting the app to reference the physical switch so that if my wife turns on the physical switch that the app no longer dims the lights even if there is no motion. She doesn’t like the lights dimming on her when we’re having a dinner party (for example).

Is the physical switch that you want to use within the area covered by the motion sensor?

yes. The switch is in the same room

Then this approach probably will not work. What would work is some disable logic that prevents motion-off from turning the lights off. That would typically be done with a virtual switch. The next question becomes how to turn the virtual disable switch on and off.

If you have an Echo, that would be one way to do it. Another way would be to use a Minimote. Another way would be to use the mobile app, or SmartTiles. Do any of those make sense for you?

If you use this app linked below for Motion Lighting, it has the disable logic in it. The issue to solve is the one above.

Thanks Bruce. I’ll explore the app you’ve linked.