So… @AaronZON came through in a big way. His code is below and seems to work well in a 1 sensor / 1 dimmer environment:
/**
* Turn light on with motion for x minutes if it is dark based on light sensor
*
* Written by Aaron Herzon
*
* 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: "Turn light on with motion for x minutes if it is dark based on light sensor",
namespace: "AaronZON",
author: "Aaron Herzon",
description: "Turn on lights temporarily when there is motion but only if it is dark according to light sensor",
category: "Safety & Security",
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")
//todo: replace icon with something appropriate.
preferences {
section("Select Motion Sensor(s) you want to Use") {
input "motions", "capability.motionSensor", title: "Motion Detector", required: true, multiple: false
}
section("Select Dimmers you want to Use") {
input "switches", "capability.switchLevel", title: "Dimmer Switches", required: false, multiple: true
}
section ("Set Brightness for motion-triggered light and on time after motion stops") {
input "BrightLevelStr", "enum", title: "Motion-Sensed Level %", required: true,
options: ["100","75","50"], defaultValue: "100"
input "DelayMinStr", "enum", title: "Bright Delay After Motion Stops, minutes", required: true,
options: ["1","3","5","10","15","30","60"], defaultValue: "5"
}
section ("Ignore motion if light level is above this value") {
input "LightMeter", "capability.illuminanceMeasurement", title: "Light Meters", required: true, multiple: false
input "LuxSetPointStr", "enum", title: "Lux level", required: true,
options: ["50","100","200","400"], defaultValue: "50"
}
}
def installed() {
log.debug "Installed with settings: ${settings}"
initialize()
}
def updated() {
log.debug "Updated with settings: ${settings}"
unsubscribe()
initialize()
}
def initialize() {
log.debug "initialize()"
state.BrightLevel = BrightLevelStr as Integer
if (state.BrightLevel == 100) {
state.BrightLevel = 99
}
state.DelayMin = DelayMinStr as Integer
state.LuxSetPoint = LuxSetPointStr as Integer
subscribe(motions, "motion.active", handleMotionEvent)
subscribe(motions, "motion.inactive", handleEndMotionEvent)
}
def handleMotionEvent(evt) {
log.debug "handleMotionEvent() Motion detected . . . ."
unschedule(turnLightsOff)
if (state.lightIsOn) {
log.debug ". . . but light is already on so do nothing"
}
else {
def currentLux=LightMeter.currentIlluminance
if(currentLux < state.LuxSetPoint) {
switches?.setLevel(state.BrightLevel)
state.lightIsOn=true
log.debug ". . . . and it is dark so turn the lights on"
}
else {
log.debug ". . . .but it is light, so do nothing"
}
}
}
def handleEndMotionEvent(evt) {
log.debug "handleEndMotionEvent() Motion stopped . . . ."
if (state.lightIsOn) {
runIn((state.DelayMin*60), turnLightsOff)
}
}
def turnLightsOff() {
log.debug "turnLightsOff()"
switches?.setLevel(0)
state.lightIsOn=false
}
… here are the issues I am having though:
-
I would like to use multiple motion sensors. When changing “false” to “true” to pickup multiple motion sensors, the app isn’t able to fire on the lights…
input "motions", "capability.motionSensor", title: "Motion Detector", required: true, multiple: false
-
Same thing happens when changing the “enum” to “number” for more granular control of the various inputs - the app no long fires on the lights.
Anyone willing to take a stab at these? I’m stumped. This could be a killer app.
Thanks again to @AaronZON for starting this one!