Detecting if I am between two different times


#1

I am trying to adjust a smartapp to be able to ignore triggers within certain times.

currently I am just playing around with the code to get what I want

def startTime = timeToday(starting, location.timeZone)
def endTime = timeToday(ending, location.timeZone)


 if (now() < endTime.time && now() > startTime.time) {
        log.info "NOW BETWEEN TIMES"
    }
    else {
        log.info "NOW IS OUTSIDE OF TIMES"
    }

The issue I have of course, is if the start time is before midnight and the end time is after midnight… since they all basically fall into Today.

How should I adjust the code to add a day to date if the end time falls after midnight and the start time is before midnight?


(Tracy) #2

Try this, I use it to know if I need to go back to night mode when the bedroom door is shut

def checkTime() {
log.debug "Checking time and performing actions"

// if between the start and end time, send the correct Hello Home Phrase
if(timeOfDayIsBetween(timeStart, timeEnd, (new Date()), location.timeZone))
{
    // after the time and the trigger went; talk to house
    location.helloHome.execute(settings.HHPhrase)
    log.debug "sent $HHPhrase"
}

}


#3

That seems to work! Thank you!

I was also just now able to get this to work:

if (startTime > endTime) {
endTime.date = endTime.date + 1
}

But your method is cleaner, thanks!