timeOfDayIsBetween Help

I moved my app from ST to my new Hubitat. I’m having trouble getting timeOfDayIsBetween to work (like it did in ST)

I tried this code:

def initialize() {

state.mytime_start = "1:00" //1am

state.mytime_end = "23:55" //11:55pm

subscribe(TriggerSwitch, "switch.off";, BillsSwitchoffHandlerON)

}

def BillsSwitchoffHandlerON(evt){

def run_time = timeOfDayIsBetween(toDateTime(state.mytime_start), toDateTime(state.mytime_end), new Date(), location.timeZone)

if (run_time) then …..

I get the below error. I didn’t have the “toDateTime” in there originally but saw that suggestion posted by someone. I also tried inputting the start and end times and that didn’t work. What am I doing wrong? Help please.

2019-05-07 09:12:24.755 am error groovy.lang.MissingMethodException: No signature of method: user_app_wrj54_MBR_Lights_Control_Hv1_98.timeOfDayIsBetween() is applicable for argument types: (java.lang.String, java.lang.String, java.util.Date, sun.util.calendar.ZoneInfo) values: [21:00, 23:55, Tue May 07 09:12:24 CDT 2019, sun.util.calendar.ZoneInfo[id="US/Central",offset=-21600000,dstSavings=3600000,useDaylight=true,transitions=235,lastRule=java.util.SimpleTimeZone[id=US/Central,offset=-21600000,dstSavings=3600000,useDaylight=true,startYear=0,startMode=3,startMonth=2,startDay=8,startDayOfWeek=1,startTime=7200000,startTimeMode=0,endMode=3,endMonth=10,endDay=1,endDayOfWeek=1,endTime=7200000,endTimeMode=0]]] on line 74 (BillsSwitchoffHandler)

I'd suggest seeing what you actually get when you call toDateTime() with "1:00" as a value, it may be expecting more than just the time, ie today's date with a time.

Something like...

def startTime = toDateTime(state.mytime_start)
def endTime =toDateTime(state.mytime_end)
log.debug "Start time is $startTime, end time is $endTime"
def run_time = timeOfDayIsBetween(startTime, endTime, new Date(), location.timeZone)

if (run_time) then …..

At least you'll know then whether you have an appropriate format for the datetime string.

Ah, I've just seen that the expected format for toDateTime is yyyy-MM-dd'T'HH:mm:ss.SSSZ

So, I would guess you may have to do something like (off the top of my head)

def now = new Date()
def startTime = now.copyWith([hourOfDay: 1, minute: 0])
1 Like

Rob

That did it. Thank you

1 Like

Noticed that if you define the variables as global, this doesn't work. Not sure if I have something wrong.

This is fine....
def now = new Date()
def startTime = now.copyWith([hourOfDay: 1, minute: 0])

This didn't work...
def now = new Date()
state.startTime = now.copyWith([hourOfDay: 1, minute: 0])

I'd need to see more of the code I think, but state variables aren't actually saved until execution terminates.

1 Like

However if you use atomicState variables then they are saved immediately. You are not supposed to mix state and atomicState variables in an app/driver.

1 Like

I made a simple test program. atomicstate or state doesn't seemed to work.
When I turn off TriggerSwitch, testlight doesn't turn on. However, if I don't use atomicState or State and just have the time band within the BillsSwitchHandler, then it works.

def initialize() {
def now = new Date()
atomicState.startTime = now.copyWith([hourOfDay: 7, minute: 0])
atomicState.endTime = now.copyWith([hourOfDay: 23, minute: 0])
subscribe(TriggerSwitch, "switch.off", BillsSwitchHandler)}

def BillsSwitchHandler(evt){
def run_time = timeOfDayIsBetween(atomicState.startTime,
atomicState.endTime, new Date(), location.timeZone)

if (run_time) 
	{testlight.on()}

}

I seem to remember that there are limits on what can be stored in state and atomicState - collections are fine, but possibly not other objects.