Give this a try
This will protect the timer from changing until the timer runs.
/**
* Disable Sensors when sensor gets active
*
* Copyright 2015 Ryan Knopp
*
* 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.
*
*/
definition(
name: "Disable Sensors when sensor gets active",
namespace: "ryan@theknopps.com",
author: "Ryan Knopp",
description: "When a motion sensor gets triggered it will disable the rest for a set amount of time. ",
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 {
section("Motion sensor to disable others") {
input name: "motionSensors", title: "Motion here", type: "capability.motionSensor", multiple: true, required: true
}
section("Change to this mode") {
input "newMode", "mode", title: "Mode?"
}
section("For this amount of time") {
input name: "minutes", title: "Minutes?", type: "number", multiple: false
}
}
def installed() {
log.debug "Installed with settings: ${settings}"
initialize()
}
def updated() {
log.debug "Updated with settings: ${settings}"
unsubscribe()
initialize()
}
def initialize() {
subscribe(motionSensors, "motion.active", motionHandler)
state.running = null
}
def motionHandler(evt) {
if (state.running != "true"){
log.debug "Current mode = ${location.mode} TIMER START"
state.oldMode = location.mode
setLocationMode(newMode)
log.debug "Change mode = ${location.mode}"
def delay = minutes.toInteger() * 60
runIn(delay, executeTimer)
state.running = "true"
}
else{
log.debug("motion sensed but not running because i am within the execution timer")
}
}
def executeTimer(evt) {
state.running = "false"
log.debug "Current mode = ${location.mode} TIMER CHANGED"
setLocationMode(state.oldMode);
}
now if you wanted to have the timer be reset every time it senses motion you can do something like this…
/**
* Disable Sensors when sensor gets active
*
* Copyright 2015 Ryan Knopp
*
* 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.
*
*/
definition(
name: "Disable Sensors when sensor gets active",
namespace: "ryan@theknopps.com",
author: "Ryan Knopp",
description: "When a motion sensor gets triggered it will disable the rest for a set amount of time. ",
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 {
section("Motion sensor to disable others") {
input name: "motionSensors", title: "Motion here", type: "capability.motionSensor", multiple: true, required: true
}
section("Change to this mode") {
input "newMode", "mode", title: "Mode?"
}
section("For this amount of time") {
input name: "minutes", title: "Minutes?", type: "number", multiple: false
}
}
def installed() {
log.debug "Installed with settings: ${settings}"
initialize()
}
def updated() {
log.debug "Updated with settings: ${settings}"
unsubscribe()
initialize()
}
def initialize() {
subscribe(motionSensors, "motion.active", motionHandler)
state.running = null
}
def motionHandler(evt) {
if (state.running != "true"){
state.oldMode = location.mode
state.running = "true"
log.debug "Current mode = ${location.mode} TIMER START"
setLocationMode(newMode)
log.debug "Change mode = ${location.mode}"
}
def delay = minutes.toInteger() * 60
runIn(delay, executeTimer)
}
def executeTimer(evt) {
state.running = "false"
log.debug "Current mode = ${location.mode} TIMER CHANGED"
setLocationMode(state.oldMode);
}