Turn Off Lights after 2 Sequential Events

Does anybody know if there is any solution to this:

  1. Door sensor changes from Opened to Closed
  2. If this happens, checks for movement from Motion Sensor
  3. If no movement, turns off light

At the current state, when the door sensor is changed from Closed to Opened, the light turns on. However, with the motion detector in the room, it frequently sets off false negatives, turning the lights off when someone is in the room.

With the sequential event detection, I would position the motion sensor near the door. Should the door be closed and motion is detected (person in the room), no action is carried out (the lights has already turned on when door is opened). If motion is not detected when the door is closed (person out of room), the lights will be turned off.

Any help or ideas pointing in the general direction is greatly appreciated!

This can be done with core. :sunglasses:

It can be done with CoRE. I have a similar scenario where if I say OK Google “I’m leaving” followed by the back door opening, then open the garage but if I open the front door - close the garage.

There is another thread to get help with using CoRE. I would ask your question there, I’m sure folks will be quick to help.

1 Like

While it can be done in CoRE, I found it easier to write the SmartApp…

 *  Copyright 2015 SmartThings
 *  Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
 *  in compliance with the License. You may obtain a copy of the License at:
 *      http://www.apache.org/licenses/LICENSE-2.0
 *  Unless required by applicable law or agreed to in writing, software distributed under the License is distributed
 *  on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License
 *  for the specific language governing permissions and limitations under the License.
 *  Turn It On For 5 Minutes
 *  Turn on a switch when a contact sensor opens and then turn it back off 5 minutes later.
 *  Author: SmartThings
name: "Multi-Door Light Automation",
namespace: "SANdood",
author: "SmartThings",
description: "When any door opens, turn on switch, and then turn off N minutes after last door closes (and optionally, motion has stopped).",
category: "Convenience",
iconUrl: "https://s3.amazonaws.com/smartapp-icons/Meta/light_contact-outlet.png",
iconX2Url: "https://s3.amazonaws.com/smartapp-icons/Meta/light_contact-outlet@2x.png"

preferences {
	section("When any of these opens..."){
		input "doors", "capability.contactSensor", multiple: true, required: true
section("Or motion detected here..."){
	input "motion", "capability.motionSensor", multiple: true, required: false
	section("Turn on these switches..."){
		input "switches", "capability.switch", multiple: true, required: true
section("Turn off after this many minutes..."){
	input "minutes", "number", defaultValue: 5, descriptionText: "5", required: true

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

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

def initialize() {
	subscribe(doors, "contact.open", openHandler)
subscribe(doors, "contact.closed", closedHandler)
if (motion) {
	subscribe(motion, "motion.active", openHandler)
	subscribe(motion, "motion.inactive", closedHandler)

def openHandler(evt) {
	unschedule(turnOffSwitches)	// stop any pending "off"
	if (switches?.currentSwitch.contains("off")) switches?.on() // turn on any of the switches that are off


def closedHandler(evt) {
	if (doors?.currentContact.contains("open")) return
if (motion && motion?.currentMotion.contains('active')) return

// All the doors are now closed, start the timer
log.debug "Doors are all closed${motion?'and motion has stopped,':','} starting timer (${doors})"
	runIn(minutes.toInteger()*60, turnOffSwitches, [overwrite: true])

def turnOffSwitches() {
	log.debug "Turning off switch(es) ${switches}"
	switches?.off()	// turn off all the switches
1 Like

As stated already, CoRE for the win.