defaultValue for time type

I have a ‘time’ type setting and I would like to set a default value of 1pm (13:00). I’ve tried various things and haven’t figured this out. Thank you.

Here is a code snippet to show what I’m trying to modify. I think what I meant to say is how to I define a “time” type constant?

input(name: 'theTimeToDoThings', type: 'time', title: 'Time when all the magic happens', required: true, refreshAfterSelection: true)

I didn’t try that before, but my understanding on how ST localize things is… they don’t localize anything. For instance, you want to use a unit (C vs F degrees), well you have to manage it by yourself in your code.

I believe it will be the same for the time.

I found this code in routine-director.groovy on the ST public master which could help you.

private getTimeOk() {
def result = true
if (starting && ending) {
def currTime = now()
def start = timeToday(starting, location?.timeZone).time
def stop = timeToday(ending, location?.timeZone).time
result = start < stop ? currTime >= start && currTime <= stop : currTime <= stop || currTime >= start
}
log.trace "timeOk = $result"
result
}

private hhmm(time, fmt = “h:mm a”) {
def t = timeToday(time, location.timeZone)
def f = new java.text.SimpleDateFormat(fmt)
f.setTimeZone(location.timeZone?:timeZone(time))
f.format(t)
}

1 Like

Here are a couple things I’ve tried:

def defaultTime = timeToday("13:00", location.timeZone).time
def defaultTime = timeToday("13:00", location.timeZone)

But neither of these seem to work when placed as a default value for a time type input:

input(name: 'theTimeToDoThings', type: 'time', title: 'Time when all the magic happens', required: true, refreshAfterSelection: true, defaultValue: defaultTime)

This will work if you want to hardcode the value. It has to follow the full date time format from JSScript something (sorry I read that yesterday but I cannot find the reference again in the ST documentation. Should be somewhere in a chapter dealing with Time).

    input(name: 'theTimeToDoThings', type: 'time', title: 'Time when all the magic happens', required: true, refreshAfterSelection: true, defaultValue: "2017-11-05T13:00:00.000-0800")

But remember this is the defaultValue so it is used only the first time you enter your device settings before you set another value.

BTW, I believe that you cannot pass a defaultvalue through a variable, just like the rest of a DTH sections used for the GUI, these are parsed and not interpreted so your setting value is either a string that the parser reads and set if no value is stored in the ‘theTimeToDoThing’. Another way would be to send an event to “theTimeToDoThing” with a value following the string format I put above and do that from the Installed function so that it happens only once in the DTH life.

Hope it helps

Can you clarify what you mean by this and maybe give a short example?

I was thinking of

sendEvent(name:“theTimeToDoThings2”, value:“2017-11-05T08:54:00.000-0800”)

But I didn’t manage to make it working.
So you are back to my first proposal. This one works:

input(name: 'theTimeToDoThings', type: 'time', title: 'Time when all the magic happens', required: true, refreshAfterSelection: true, defaultValue: "2017-11-05T13:00:00.000-0800")

That does work, however it is time zone specific. If this is just for me than that’s fine, I’ll just set it to my local time zone (-6 or -7) but I was hoping to not do that.

Hey look at what I just found… I didn’t try it but seems solving your issue…

That worked exactly as I wanted. In case you need a code example here is the code from my setup page:

def setupPage() {
  dynamicPage(name: 'setupPage', title: 'Setup Lock', nextPage: 'mainPage', uninstall: true) {
    section('Choose devices for this lock') {
      def defaultTime = timeToday("13:00", location.timeZone).format("yyyy-MM-dd'T'HH:mm:ss.SSSZ", location.timeZone)
      input(name: 'ical', type: 'text', title: 'Airbnb Calendar Link', required: true, refreshAfterSelection: true, image: 'https://images.lockmanager.io/app/v1/images/calendar.png', submitOnChange: true)
      input(name: 'userSlot', type: 'enum', options: parent.availableSlots(settings.userSlot), title: 'Select slot', required: true, refreshAfterSelection: true, submitOnChange: true)
      input(name: 'checkoutTime', type: 'time', title: 'Checkout time (when to change codes)', required: true, refreshAfterSelection: true, submitOnChange: true, defaultValue: defaultTime)
      input(name: 'userName', title: 'Name for User', required: true, image: 'https://images.lockmanager.io/app/v1/images/user.png', defaultValue: 'Airbnb', submitOnChange: true)
    }
  }
} 

I am using this in my fork of lock-manager I wrote that has a new user type that automates setting entry codes to the last 4 digits of a guest’s phone number. This should be in the smartthings documentation as being able to use a variable for a defaultValue makes life much easier.

My repo: lock-manager fork on Github

1 Like