Event based on current time

I am trying to write a smart app that will turn off a switch x number of minutes after it turns on if it is past a specified time.

preferences {
	section("When it Turns on..."){
		input "switch1", "capability.switch"
	}

    
	section("For this amount of time...") {
		input name: "minutes", title: "Minutes?", type: "number", multiple: false
	}

	section("After What time..."){
		input "time1", "time", title: "When?"
	}
}

def installed() {
	log.debug "Installed with settings: ${settings}"
	subscribe(switch1, "switch.on", switchOnHandler)
}

def updated(settings) {
	log.debug "Updated with settings: ${settings}"
	unsubscribe()
	subscribe(switch1, "switch.on", switchOnHandler)
}

def switchOnHandler(evt) {
	if (now() > time1) {
        switch1.off(delay: minutes * 60000)
    	log.debug "Light tuned off"
    }
    if (now() < time1) {
    	log.debug "Time is before"
    }
}

I can get it to work without the time check.

thanks

  • jason

Jason,

I had a similar query about comparing the current time to the time a user inputed, you can check that topic here:

Compare Current Time to User Inputted Time.

I wanted to set a specific mode based on the time I entered in preferences.

Hope this helps.

-Dean

Peeps,

Here’s a little something fun that might help:

http://build.smartthings.com/projects/autooffswitchandtweet/

twack

Thanks, here is what I ended up with:

/**
 *  Turn off a switch a set number minutes after it turns on.
 *  If the current time is after a specifice time
 *
 *  Author: Jason
 */
preferences {
	section("When it Turns on..."){
		input "switch1", "capability.switch"
	}

    
	section("For this amount of time...") {
		input name: "minutes", title: "Minutes?", type: "number", multiple: false
	}

	section("After What time..."){
		input "timeOfDay", "time", title: "When?"
	}
}

def installed() {
	log.debug "Installed with settings: ${settings}"
	subscribe(switch1, "switch.on", switchOnHandler)
}

def updated(settings) {
	log.debug "Updated with settings: ${settings}"
	unsubscribe()
	subscribe(switch1, "switch.on", switchOnHandler)
}

def switchOnHandler(evt) {
	    def startTime = timeToday(timeOfDay)
        	if(now() >= startTime.time){
        	log.debug "After set time waiting delay"
        	switch1.off(delay: minutes * 60000)
    		log.debug "Light tuned off"
        }
        if(now() <= startTime.time){
        	log.debug "time is before set time"
    }
}