Need help with adding time range to Flasher II app


#1

I’m trying to add a time frame to when the Flasher II app works. For example, the app triggers a light switch to flash on and off when detecting motion. I want it to trigger only from 12AM to 5AM. Here’s what I’ve done. The time sequence doesn’t work and just activates at all times of the day with motion. I’m a newbie at this, so I’m looking for coding help! Thanks!

/**

preferences {
section(“When any of the following devices trigger…”){
input “motion”, “capability.motionSensor”, title: “Motion Sensor?”, required: false
input “contact”, “capability.contactSensor”, title: “Contact Sensor?”, required: false
input “acceleration”, “capability.accelerationSensor”, title: “Acceleration Sensor?”, required: false
input “mySwitch”, “capability.switch”, title: “Switch?”, required: false
input “myPresence”, “capability.presenceSensor”, title: “Presence Sensor?”, required: false
}
section(“Turn on between what times?”) {
input “fromTime”, “time”, title: “From”, required: true
input “toTime”, “time”, title: “To”, required: true
}
section(“Then flash…”){
input “switches”, “capability.switch”, title: “These lights”, multiple: true
input “numFlashes”, “number”, title: “This number of times (default 3)”, required: false
}
section(“Time settings in milliseconds (optional)…”){
input “onFor”, “number”, title: “On for (default 1000)”, required: false
input “offFor”, “number”, title: “Off for (default 1000)”, required: false
}
}

def installed() {
log.debug “Installed with settings: ${settings}”

subscribe()

}

def updated() {
log.debug “Updated with settings: ${settings}”

unschedule()
unsubscribe()
subscribe()

}

def subscribe() {
if (contact) {
subscribe(contact, “contact.open”, contactOpenHandler)
}
if (acceleration) {
subscribe(acceleration, “acceleration.active”, accelerationActiveHandler)
}
if (motion) {
subscribe(motion, “motion.active”, motionActiveHandler)
}
if (mySwitch) {
subscribe(mySwitch, “switch.on”, switchOnHandler)
}
if (myPresence) {
subscribe(myPresence, “presence”, presenceHandler)
}
if (time1) {
schedule(time1, scheduleHandler)
}
if (time2) {
schedule(time2, scheduleHandler)
}
}

def motionActiveHandler(evt) {
log.debug "motion $evt.value"
flashLights()
}

def contactOpenHandler(evt) {
log.debug "contact $evt.value"
flashLights()
}

def accelerationActiveHandler(evt) {
log.debug "acceleration $evt.value"
flashLights()
}

def switchOnHandler(evt) {
log.debug "switch $evt.value"
flashLights()
}

def presenceHandler(evt) {
log.debug "presence $evt.value"
if (evt.value == “present”) {
flashLights()
} else if (evt.value == “not present”) {
flashLights()
}
}

def scheduleHandler() {
log.debug "scheduled time to flash lights"
flashLights()
}

private flashLights() {
def doFlash = true
def onFor = onFor ?: 1000
def offFor = offFor ?: 1000
def numFlashes = numFlashes ?: 3

log.debug "LAST ACTIVATED IS: ${state.lastActivated}"
if (state.lastActivated) {
	def elapsed = now() - state.lastActivated
	def sequenceTime = (numFlashes + 1) * (onFor + offFor)
	doFlash = elapsed > sequenceTime
	log.debug "DO FLASH: $doFlash, ELAPSED: $elapsed, LAST ACTIVATED: ${state.lastActivated}"
}

if (doFlash) {
	log.debug "FLASHING $numFlashes times"
	state.lastActivated = now()
	log.debug "LAST ACTIVATED SET TO: ${state.lastActivated}"
	def initialActionOn = switches.collect{it.currentSwitch != "on"}
	def delay = 0L
	numFlashes.times {
		log.trace "Switch on after  $delay msec"
		switches.eachWithIndex {s, i ->
			if (initialActionOn[i]) {
				s.on(delay: delay)
			}
			else {
				s.off(delay:delay)
			}
		}
		delay += onFor
		log.trace "Switch off after $delay msec"
		switches.eachWithIndex {s, i ->
			if (initialActionOn[i]) {
				s.off(delay: delay)
			}
			else {
				s.on(delay:delay)
			}
		}
		delay += offFor
	}
}

}