Lighting Off On 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