Help with a time window issue


(Pj Hollenbeck) #1

Hi all, I’ve been struggling with this problem for days, and not being a programmer isn’t helping the cause. The app I’m trying to write is supposed to alert the user to feed their dogs if a cabinet door hasn’t been opened within a window of time. The app is based off of the “Medicine Reminder” app, in which a text message reminder would be sent to the user’s device if the medicine cabinet hasn’t been opened by a certain time. However, I am trying to rewrite it to have a window of time in which the cabinet should be opened, as that would be when I feed the dogs and would differentiate breakfast from lunch etc. The problem is that the reminder is coming at the beginning of the time window, not the end as would be more useful (if the cabinet had been opened during the window of time, no need to send a text.) Can anyone shed some light as to what may be the problem?

/**

  • Medicine Reminder
  • Author: SmartThings
    */

preferences {
section(“Choose the dog food cabinet…”){
input “cabinet1”, “capability.contactSensor”, title: “Where?”
}
section(“Feed the dogs between…”){
input “tweenStart”, “time”, title: "Start Time"
input “tweenEnd”, “time”, title: “End Time”
}
section(“Text me if I forget…”){
input “phone1”, “phone”, title: “Phone number?”
}
}

def installed()
{
// schedule(tweenStart, “scheduleCheck”)
schedule(tweenEnd, “scheduleCheck”)

}

def updated()
{
unsubscribe() //TODO no longer subscribe like we used to - clean this up after all apps updated
unschedule()
// schedule(tweenStart, “scheduleCheck”)
schedule(tweenEnd, “scheduleCheck”)
}

def scheduleCheck(){
log.trace “medicineCheck: $settings”

def midnight = (new Date()).clearTime()
def t0 = now()
log.trace "t0: ${new Date(t0)}"

def startTime = timeToday(tweenStart)
log.trace "startTime: $startTime"

def endTime = timeToday(tweenEnd)
def cabinetEvents = cabinet1.eventsBetween(startTime, endTime)
 log.debug "t0: ${new Date(t0)}, startTime: $startTime, endTime: $endTime"
if (t0 <= endTime.time && t0 >= startTime.time) {
log.trace "Found ${cabinetEvents?.size() ?: 0} cabinet events since $startTime"
def cabinetOpened = cabinetEvents.count { it.value && it.value == "open" } > 0
if (cabinetOpened) {
		log.debug "Medicine cabinet was opened by $endTime, no SMS required"
log.debug "cabinetOpened: $cabinetOpened"
} else {
	log.debug "Medicine cabinet was not opened by $endTime, texting $phone1"
		sendSms(phone1, "Please remember to feed the dogs!")
	}

}
}