I want to use this code but need to add a timeframe for it to be active. for example between 5:00AM and 6:00AM Monday-Friday. Thanks
/**
* Light Follows Me If There Isn't Enought Light
*
* Author: SmartThings
*/
preferences {
section("Turn on when there's movement..."){
input "motion1", "capability.motionSensor", title: "Where?", multiple: true
}
section("And off when there's been no movement for..."){
input "minutes1", "number", title: "Minutes?"
}
section("If the light intensity on..."){
input "lightSensor1", "capability.illuminanceMeasurement"
}
section("is lest than..."){
input "lux1", "number", title: "Lux?"
}
section("Turn on/off light(s)..."){
input "switches", "capability.switch", multiple: true
}
}
def installed() {
subscribe(motion1, "motion", motionHandler)
subscribe(lightSensor1, "illuminance", illuminanceHandler)
}
def updated() {
unsubscribe()
subscribe(motion1, "motion", motionHandler)
subscribe(lightSensor1, "illuminance", illuminanceHandler)
}
def illuminanceHandler(evt) {
log.debug "$evt.name: $evt.value and lux: $lux1"
int illuminanceState = Integer.parseInt(evt.value);
if (illuminanceState >= lux1) {
def switchesState = "off";
def switchesValue = switches.currentState("switch")
for (String item : switchesValue.value) {
if (item == "on") {
switchesState = "on"
}
}
if (switchesState == "on"){
log.debug "there is enought light: turning lights off"
switches.off()
}
} else {
def motionState = "inactive";
def motionValue = motion1.currentState("motion")
for (String item : motionValue.value) {
if (item == "active") {
motionState = "active"
}
}
log.debug "motionState: $motionState"
if (motionState == "active") {
log.debug "Motion is active and the there isn't enought light: turning lights on"
switches.on()
}
}
}
def motionHandler(evt) {
log.debug "$evt.name: $evt.value"
if (evt.value == "active") {
def illuminanceState = lightSensor1.latestValue("illuminance")
log.debug "$illuminanceState < $lux1"
if (illuminanceState < lux1) {
log.debug "turning on lights"
switches.on()
}
} else if (evt.value == "inactive") {
runIn(minutes1 * 60, scheduleCheck)
}
}
def scheduleCheck() {
def motionState = "inactive";
def motionValue = motion1.currentState("motion")
log.debug "Schedule check... Sensor $motionValue.value"
for (String item : motionValue.value) {
if (item == "active") {
motionState = "active"
}
}
log.debug "motionState: $motionState"
if (motionState == "inactive") {
log.debug "Motion has stayed inactive long enough since last check: turning lights off"
switches.off()
} else {
log.debug "Motion is active, do nothing and wait for inactive"
}
}