Give a default value for a optional preference?

Is there a easy way to check whether a preference is set and based on that either set the preference to a default value or at least pass a default value when it’s needed? It seems “defaultValue” on a preference is broke, there are multiple threads about it not working and also SmartThings documentation says it’s not recommended.

So with that said I have a preference that takes a string. I want to check if that preference has a value or is null/empty. If its null or empty I want to put in a default value and if possible update the preference. Like:

if (str = null || str.isEmpty()) {
// is either null or blank so assign prference
str: “default”
// and call routine but pass default value
updateRoutine(“default”)
} else {
// string was not null or blank so pass it to routine
updateRoutine(str)
}

Is something like this possible? I tried the above and I couldn’t get it to work although it looks like the correct java syntax.

change to str==null to test
str=null sets it to null

This works for me in the app but not in the simulator
input “stayDefault”, “mode”, required: true, defaultValue: “Night”,
title: “Default Mode for Armed Home”

I’ve gotten a little further but still not working. I tried with both = an ==. Tried based on a java example I found also (https://stackoverflow.com/questions/14721397/checking-if-a-string-is-empty-or-null-in-java) and typed it verbatim but it didn’t seem to work either. I think the issue is it’s having trouble getting the preference value. I’m throwing a exception now on the preference:

groovy.lang.MissingPropertyException: No such property: myPreferenceItem1 for class: script150396243419840848066 @ line 57

But the value in the preference is used later and works fine. I’ll keep plugging away.

It would be helpful if you posted the input definition causing the problem. For the PC environment I know PHP and Javascript, and I keep getting my butt kicked by Java/Groovy. My code looks (to me) like PHP, but works well.

I find the online documentation tedious, but it has a lot of good information for using the SmartThings version of Groovy. If you have not already done so, try “Write Your first Smartapp” at http://docs.smartthings.com/en/latest/.

Editing data in this environment is in my opinion more difficult than it should be. If you have install true on a page where there is a data error not caught by the ST input handler, and the user presses done, I have not found a way to stop the install no matter what I do, including going to a differant page. Perhaps someone in the community has an answer for that issue.

I come from a VB background (VB 6 all the way through VB.Net 2015). I feel the same way…the code looks perfect but SmartThings doesn’t like it.

I think I’ve given up on trying to get this to work. I’ve tried str.equals("") and different variations (which work better) but the end result is I’m trying to build on my Change labels of child device programatically? code and I think I’m going to just give the user xx number of options for the labels instead of letting them willy nilly enter them. There just isn’t a way to programmatically update the tiles value (easy) and the icon/background (hard) at runtime.

If (!myInput) updateSetting(“myInput”,“defaultValue”)

1 Like

I would code that as
if (!myinput)
{myinput=“string value”}
where string value is whatever you want as the default

You can’t assign input values directly

This is how I set the Label on a child app from my recently released Smarthome Delay app. The Label after it is set by the code, can still be changed by the user. This sets the child profile name to the name of the real contact sensor. Not sure if this is what you are trying to do.
(sorry the forum editor killed most of the code indentation)

def pageOne(error_data)
{
dynamicPage(name: “pageOne”, title: “The Contact Sensors”, uninstall: true)
{
section
{
if (error_data instanceof String )
{
paragraph error_data
}
input “thecontact”, “capability.contactSensor”, required: true,
title: “Real Contact Sensor (Remove from SmartHome Monitoring)”, submitOnChange: true
}
section
{
input “thesimcontact”, “capability.contactSensor”, required: true,
title: “Simulated Contact Sensor (Must Monitor in SmartHome)”
}
if (thecontact)
{
section([mobileOnly:true])
{
label title: “Profile name”, defaultValue: “Profile: ${thecontact.displayName}”, required: false
}

		}	
	else	
		{
		section([mobileOnly:true]) 
			{
			label title: "Profile name", required: false
			}
		}	
	}
}

Did not know that, thanks for the information. So far defaultValue has worked for me despite the warning that it was a problem, but it does not work in the simulator.

1 Like

Don’t use the simulator…, there’s really not much value to it.
Also default value is a one time shot, it only works when the input is empty.

I find the simulator useful for initial testing of standalone modules, but always use the IDE Live Logging with the phone app for the real testing.

I discovered that empty field only issue a while back when testing with a Label statement in my SmartApp. Just one of many ST eccentricities.(sigh)

Yeah I am stumped on this completely.
Mike, I am initially setting the network id from a parent smartapp - how can I edit the value for a child device?
will I have to create a separate settings variable for each child device?
Also if I wanted someone to click on the gear in a child device and edit its network id how could I provide that?
Is the only choice I have is to delete the device and have them add it again?

In child use parent.settingname
In parent it is child.childname.settingname

I’m on my phone and doing this from memory. Suggest looking it up in the documentation.