timeOfDayIsBetween Example


(Keith Croshaw) #1

Hello, I’m trying to get a SmartApp to execute between two times, as I fear that using timeToday does not reset at midnight, and found timeOfDayIsBetween

The syntax is: timeOfDayIsBetween(Date start, Date stop, Date value, TimeZone timeZone)

I tried to configure as follows:

timeOfDayIsBetween(TimeA, TimeB, now(), location.Timezone)

and I get:
java.lang.NullPointerException: Cannot get property ‘timeZone’ on null object @ line

I’m sure it’s something simple and I’ve tried to hardcode “GMT-4” or milliseconds between GMT, I’ve tried “GMT-04:00”, nothing seems to work.

Help would be appreciated, maybe a simple example. Thanks!


(Jim Anderson) #2

timeOfDayIsBetween signature is

timeOfDayIsBetween(Date start, Date stop, Date value, TimeZone timeZone)

now() returns a long, not a Date, and location.Timezone should be location.timeZone (case sensitivity strikes again! :slight_smile: )

Try this:

timeOfDayIsBetween(TimeA, TimeB, new Date(), location.timeZone)

where TimeA and TimeB are Date objects.

Note that location.timeZone requires that location services be enabled for the mobile app.

Relevant docs:
https://graph.api.smartthings.com/ide/doc/location (timeZone attribute)
https://graph.api.smartthings.com/ide/doc/smartApp (timeOfDayIsBetween and now method)


(Keith Croshaw) #3

@Jim Thanks!

I just rewrote my one app, I’ll give it a try if that doesn’t work.

Also having a hard time understanding timeToday, if that automatically resets at midnight to make way for logic in the morning or what.


(Keith Croshaw) #4

@Jim I’m still having a hard time with this, is there anyway you could write a quick hello world when the current time is between two user selected times? For some reason all of my Dates are null…

Thanks,
Keith


(Jim Anderson) #5

Hello World of timeOfDayIsBetween here: https://gist.github.com/jimmyjames/0939d142ea9702c33f8d


(Keith Croshaw) #6

Thanks for the quick response. I’m in trouble if I can’t figure it out now. :wink:


(Keith Croshaw) #7

Ok so my application seems to work if the trigger period is within the time of installation, but it never seems to execute if the time is after installation. This might be due to my newness to SmartApp writing or just a typo, I’m not sure. I’d appreciate some help even if the answer is there is a better way to accomplish my apps purpose.

The problem:
I have a device that is awesome but noisy at night so I want to turn it off after a specified time
Also this device executes cycles throughout the day that if interrupted will yield no place for my cats to go to the bathroom.

The solution:
Turn this device of between two times while it’s not drawing power.

The code so far:

Any assistance would be greatly appreciated.

PS I selected SmartThings because I was sure it could accomplish this, so I hope I was right. :wink: