How does one prefill a date Input for a device?

Funny that I spent a lot of time working with date and times and can't figure it out!

First, a HUGE thank you to @csteele for providing me a massive jump start with his code model on my project! I am overwhelmingly grateful!

I want to add a 'feature' to a driver. I want to have the ability to use todays date as a default but give the ability to enter whatever date might be needed.
Here is a pic of my driver so far. As you can see I have a 'Date' input and I want it to default and prefill to todays date but have the ability to accept any date if entered. :

The code I have that isn't working is:

        preferences {
		input name: 'usedLatitude', type: 'text', title: 'Latitude', description: 'Override Latitude (default is hub value)', required:false, defaultValue: location.latitude
		input name: 'usedLongitude', type: 'text', title: 'Longitude', description: 'Override Longitude (default is hub value)', required:false, defaultValue: location.longitude
		input name: 'usedDate', type: 'date', title: 'Date', description: 'Override Date to be calculated (default is today)', required:false, defaultValue: location.getDate
		input name: 'usedTimeZone', type: 'text', title: 'Timezone', description: 'Override Timezone (default is hub value)', required:false, defaultValue: location.timeZone.ID
	

The result is 'Null'. I've tried iterations with today(), now() and getDate but I'm stymied and reading all the Hubitat docs hasn't shown me the way.

For a bonus point if interested - @csteele built a json routine to move inbound data to attributes. One attribute comes as an integer. I want to store it as 'hh:mm' but having trouble parsing. I've had to make a second api call to get the one value and its poor code. Is there an easy way to format that escapes me?
here's that called localDayLength formatted as I want (but requires a second API call)-
image

and my hack code is:
image

but I want it like @csteele s work:

    def sdf= new SimpleDateFormat("yyyy-MM-dd")  
    defDate = sdf.format(now())
   
   input name: 'usedDate', type: 'date', title: 'Date', description: 'Override Date to be calculated (default is today)', required:false,defaultValue: defDate
}

Didn't test but your code may work if you put the parens after getDate, i.e. location.getDate()

I'll give that a try - I just moved my home today. my tech world is a mess - will likely be 3-4 days before I can check it out.

Another route would be to check if the input setting is null, and if so, use driver.updateSetting.

if(settings['usedDate'] == null) driver.updateSetting('usedDate', [type: 'date', value: defDate])

1 Like