[OBSOLETE] Timer After No Motion Detected - regardless of how it was turned on or whether there was any motion

There are multiple ways to turn lighting on and off depending on motion.

But I wanted something that would turn off the lights after the motion stopped, but was not dependent on how the light was turned on. And including the situation where the light was turned but there was never any motion.

So here’s my app that only turns switches off. It assumes the switch is turned on manually or by a different smartapp

/**

  • Lighting Off On Timer After No Motion Detected - regardless of how it was turned on or whether there was any motion
  • States are
  • 0 - light is off - wait for light to be turned on by anything, set state = 1
  • 1 - light is on waiting for time to expire (set timer, but if motion, set state = 2)
  • 2 - light is on with motion; when motion stops, set state = 1 and start timer
  • When time is up, if state is still 1, then turn off switch, and set state = 0

*/
definition(
name: “Lighting Off On Timer After No Motion Detected”,
author: “King”,
description: “Lighting Off On Timer After No Motion Detected - regardless of how it was turned on or whether there was any motion”,
category: “Convenience”,
iconUrl: “https://s3.amazonaws.com/smartapp-icons/Convenience/Cat-Convenience.png”,
iconX2Url: “https://s3.amazonaws.com/smartapp-icons/Convenience/Cat-Convenience@2x.png”,
iconX3Url: “https://s3.amazonaws.com/smartapp-icons/Convenience/Cat-Convenience@2x.png”)
preferences {
input(name: “motion1”, type: “capability.motionSensor”, title: “When there’s no motion on…”, required: true, multiple: false, description: “this motion sensor”)
input(name: “delay1”, type: “number”, title: “For at least…”, required: true, description: “this number of minutes”)
input(name: “switch1”, type: “capability.switch”, title: “Turn these off …”, required: true, multiple: true, description: “these switches”)
}

def installed() {
initialize()
}

def updated() {
unsubscribe()
initialize()
}

def initialize() {
subscribe(switch1, “switch.on”, switchOnHandler)
subscribe(switch1, “switch.off”, switchOffHandler)
subscribe(motion1, “motion.active”, motionOnHandler)
subscribe(motion1, “motion.inactive”, motionOffHandler)
state.phasenumber = ‘0’
log.info(“Lighting Off On Timer After No Motion Detected V21.9.20.1039”)
}

def switchOnHandler(evt) {
state.phasenumber = ‘1’
def MinutesDelay = 60 * delay1 as int
runIn(MinutesDelay, timerDone)
log.debug "Light is on - no motion: - state is $state.phasenumber; wait for $delay1 minutes "
}

def switchOffHandler(evt) {
state.phasenumber = ‘0’
log.debug “Light switched off: - state is $state.phasenumber”
}

def motionOnHandler(evt) {
state.phasenumber = ‘2’
log.debug “Motion detected: - state is $state.phasenumber wait for Motion to stop”
}

def motionOffHandler(evt) {
state.phasenumber = ‘1’
def MinutesDelay = 60 * delay1 as int
runIn(MinutesDelay, timerDone)
log.debug "Light is on - no motion: - state is $state.phasenumber; wait for $delay1 minutes "
}

def timerDone() {
if (state.phasenumber == ‘1’) {
state.phasenumber = ‘0’
switch1.off()
log.debug “Switching light off: - state is $state.phasenumber”
}
}

AND
– indentation is gone - sorry about that
– to use this app – you will need to change the quotation marks to plan quote marks not curly
(Tips on how to post directly and avoid those pitfalls appreciated)

I’m not sure, but can’t you just use the code tags?

type or paste code here

Thank you JDR

/**
 *  Lighting Off On Timer After No Motion Detected - regardless of how it was turned on or whether there was any motion
 *  
 *  States are
 *  0 - light is off - wait for light to be turned on by anything, set state = 1
 *  1 - light is on waiting for time to expire (set timer, but if motion, set state = 2)
 *  2 - light is on with motion; when motion stops, set state = 1 and start timer
 *  When time is up, if state is still 1, then turn off switch, and set state = 0 
 *
 */
definition(
    name: "Lighting Off On Timer After No Motion Detected",
    author: "King",
    description: "Lighting Off On Timer After No Motion Detected - regardless of how it was turned on or whether there was any motion",
    category: "Convenience",
    iconUrl: "https://s3.amazonaws.com/smartapp-icons/Convenience/Cat-Convenience.png",
    iconX2Url: "https://s3.amazonaws.com/smartapp-icons/Convenience/Cat-Convenience@2x.png",
    iconX3Url: "https://s3.amazonaws.com/smartapp-icons/Convenience/Cat-Convenience@2x.png")
preferences {
	input(name: "motion1", type: "capability.motionSensor", title: "When there's no motion on...", required: true, multiple: false, description: "this motion sensor")									
	input(name: "delay1", type: "number", title: "For at least...", required: true, description: "this number of minutes")
	input(name: "switch1", type: "capability.switch", title: "Turn these off ...", required: true, multiple: true, description: "these switches")
}
	
def installed() {
	initialize()
}

def updated() {
	unsubscribe()
	initialize()
}

def initialize() {
	subscribe(switch1, "switch.on", switchOnHandler)
	subscribe(switch1, "switch.off", switchOffHandler)
	subscribe(motion1, "motion.active", motionOnHandler)
	subscribe(motion1, "motion.inactive", motionOffHandler)
	state.phasenumber = '0'
	log.info("Lighting Off On Timer After No Motion Detected V21.9.20.1039")
}

def switchOnHandler(evt) {
	state.phasenumber = '1'
	def MinutesDelay = 60 * delay1 as int
	runIn(MinutesDelay, timerDone)
	log.debug "Light is on - no motion: - state is $state.phasenumber; wait for $delay1 minutes "
}

def switchOffHandler(evt) {
	state.phasenumber = '0'
	log.debug "Light switched off: - state is $state.phasenumber"
}

def motionOnHandler(evt) {
	state.phasenumber = '2'
	log.debug "Motion detected: - state is $state.phasenumber wait for Motion to stop"
}

def motionOffHandler(evt) {
	state.phasenumber = '1'
	def MinutesDelay = 60 * delay1 as int
	runIn(MinutesDelay, timerDone)
	log.debug "Light is on - no motion: - state is $state.phasenumber; wait for $delay1 minutes "
}

def timerDone() {					
	if (state.phasenumber == '1') {
		state.phasenumber = '0'
		switch1.off()
		log.debug "Switching light off: - state is $state.phasenumber"
	}				
}
1 Like

How about simple automation:
If Light is On for X minutes
Than Turn it Off


It doesn’t matter how the light is turned on, after X minutes goes off.
And of course if you need to turn that ligt On, when motion is detected, create another automation for that.

2 Likes

I was looking for an app that keeps the light on while there’s motion, and then turns it off, after an appropriate delay.

The options that I could find that do this, also turned the light on with motion which I didn’t want

Simple automation to do that.

Screenshot_20211001-185739|250x500

Does this is also trigger if there never was any motion, or does it look only for the “no motion” event?

I do want to avoid this light being left on, but if so this would be much simpler.

My light is generally activated by motion, so motion may be needed.

My Automation that i have posted earlier, turns light off regardless of how is turned on, after X time that you specify.
This one turns Light on when motion is detected.

@Paul_Oliver automation turns light off, X minutes after motion stops.
It is up to you how you want to have lights.

For my Porch Lights I have my in mix of all of this:

  • Lights come on at sunset, @60%
  • When Motion starts lights go to 100%
  • When there is no motion for 3 minutes light go back to 60%
  • Lights go Off at sunrise

I also change white temperature at the same time with dimming, @ 60% - 3000K, and 100% - 4500K

1 Like

I have seen the options which tie the switch to motion for both on and off.

I wanted something slightly different
– never turn the light on (it’s switched on manually or by another automation)
– don’t turn the light off if there’s continued motion
– but if it’s left on accidentally then switch off (including if there never was any motion)

You can have this with just another automation


Anyway try a couple of this and see which one works best for you. You might need 2 or 3 automations.

That works and is much easier. Thanks

1 Like

To make sure that the record is accurate, I wanted to update my previous answer.

This automation is close but doesn’t solve the situation where the switch is turned on after a long gap, but before there’s any motion. It still thinks that the light should be off, and keeps turning it off. (My switch is outside the room)

I am sure that there is a combination of automations that will meet the exact scenario that I described, but since I have a smart app that works that I posted at top, I don’t want to test it any more.

Wanted to drop in and say thank you for this smartapp. So far in testing it is exactly what I was trying to get to happen but couldn’t accomplish with the app alone.

I have a motion sensor in the hallway that turns on a light in one of the rooms off the highway at night. I have another motion sensor in the room with the light. I want the light to turn off even if I never go into the room. Your smartapp makes that happen. Thanks.

1 Like

So unless I’m mistaken this used to be easily possible with the built in “Smart Lighting” app in SmartThings. I had a Night Light mode that only ran from 11pm until sunrise if the mode was set to “night”. Certain lights would turn on and dim if any of the three motion sensors were activated and lights turned off after 2 minutes of inactivity. Now it seems like it will take at least 2 or three separate routines to accomplish this since there is no “off” option. Turning the lights off after activity has ended is more difficult now. There used to be an option for “and turn off” anytime a light was turned on. Other conditions are also more limited than before like being able to choose a specific start time but then use sunrise as the end time. It seems to be either time based OR sun based but not both.

For sun based routines you need to use 2 separate routines.

For example, 1 to turn a light on at sunset and a 2nd one to turn it off at a set time like midnight.

You are allowed 1000 automations, so you should have plenty to work with.

1 Like

Can still be done is Smartlighting. Here is an example.

I do it with Routines by using a virtual switch as a variable to represent “Nighttime” and then use “Nighttime is on” as a precondition in my Routines. I then have the virtual device turn on at a specific time and turn off at sunrise.

I’m trying to accomplish the same thing as OP. I successfully created an automation to turn off a light after 10 minutes of no motion, but this requires the motion sensor to be triggered first in order for it to work. If the light is turned on and no one ever entered the room, I still want it to turn off after 10 minutes. Can this be done?