Lights on Motion SmartApp?

The code for more options is in two parts: The first part is the preferences, and the second part are the tests to see if the app should do anything when there is motion.

Here is the first part:

section(title: "Motion options", hidden: hideOptionsSection(), hideable: true) {

	def timeLabel = timeIntervalLabel()

	href "timeIntervalInput", title: "Only during a certain time", description: timeLabel ?: "Tap to set", state: timeLabel ? "complete" : null

	input "days", "enum", title: "Only on certain days of the week", multiple: true, required: false,
		options: ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]

	input "modes", "mode", title: "Only when mode is", multiple: true, required: false
}    

And below is the part that does the tests. You would use if(allOk) {.... in the motion handler.

// execution filter methods
private getAllOk() {
	modeOk && daysOk && timeOk
}

private getModeOk() {
	def result = !modes || modes.contains(location.mode)
//	log.trace "modeOk = $result"
	result
}

private getDaysOk() {
	def result = true
	if (days) {
		def df = new java.text.SimpleDateFormat("EEEE")
		if (location.timeZone) {
			df.setTimeZone(location.timeZone)
		}
		else {
			df.setTimeZone(TimeZone.getTimeZone("America/New_York"))
		}
		def day = df.format(new Date())
		result = days.contains(day)
	}
//	log.trace "daysOk = $result"
	result
}

private getTimeOk() {
	def result = true
	if (starting && ending) {
		def currTime = now()
//		def start = timeToday(starting).time
        def start = timeToday(starting,location.timeZone).time
//		def stop = timeToday(ending).time
        def stop = timeToday(ending,location.timeZone).time
		result = start < stop ? currTime >= start && currTime <= stop : currTime <= stop || currTime >= start
	}
//	log.trace "timeOk = $result"
	result
}

private hhmm(time, fmt = "h:mm a")
{
	def t = timeToday(time, location.timeZone)
	def f = new java.text.SimpleDateFormat(fmt)
	f.setTimeZone(location.timeZone ?: timeZone(time))
	f.format(t)
}

private hideOptionsSection() {
	(starting || ending || days || modes) ? false : true
}

private timeIntervalLabel() {
	(starting && ending) ? hhmm(starting) + "-" + hhmm(ending, "h:mm a z") : ""
}
1 Like

@nico89s , just search for Light Director , copy and paste code in the IDE from here. New App: Lighting Director

1 Like

Thank you very much @bravenel , i’ll try it tomorrow
The link is not available @daven

Strange, I click on the link in my post or the one on the right labelled New App: and it jumps right there.

i mean the link to github, but it’s working now, thanks