Workaround for the non-working "defaultValue"

I have 3 inputs:

preferences {
    input name: "wakeUpInterval", type: "number", title: "Wake Up Interval (0-24h)", description: "24", displayDuringSetup: true, range:"0..24"
    input name: "report", type: "enum", title: "Report Type", description: "Alert Report", displayDuringSetup: true, options: ["Alert Report","Sensor Binary Report","Basic Report"], defaultValue: "Alert Report"
    input name: "led", type: "bool", title: "LED", description: "LED", required: true, displayDuringSetup: true, defaultValue: true
}

But since the attribute “defaultValue” does not work, I need a workaround.

I have the method:

def resultConfiguration() {
		def result = []
    	def timeString = new Date().format("MMM d yyyy, hh:mm:ss", location.timeZone)
        result << createEvent(name: "tamper", value: timeString , descriptionText: "$device.displayName was tampered")
        if ( wakeUpInterval != null) {
                result << response(zwave.wakeUpV1.wakeUpIntervalSet(seconds:3600*wakeUpInterval, nodeid:zwaveHubNodeId).format())
        }
        if ( led != null ) {
        	if (led) {
        		result << response(zwave.configurationV1.configurationSet(parameterNumber: 2, size: 1, scaledConfigurationValue: 1).format())
        	} else {
        		result << response(zwave.configurationV1.configurationSet(parameterNumber: 2, size: 1, scaledConfigurationValue: 0).format())
       		}   
        }
}  

First , I tried to define the variables’ values in configure(), but it didn’t work - I guess they are defined only locally.
Then I tried the above: using “if” statements to catch when the variables are null and not to execute their respective “result”.
But the above method is not working when “led” and “wakeupInterval” are null - the “tamper” event is not sent and I don’t know why - is there something wrong with the syntax? It looks, as the “if” condition is not working properly - the values are null, but still it tries to execute the commands.
Is there a way to set the variables somehow during the configuration to avoid them being null?

I also tried without any success different “if” statements::

    if (led == null) {def led = true}
    if (wakeUpInterval == null) {def wakeUpInterval=24}
    if (report == null) {def report = "Alert Report"}

and

    if (led == null) {led = true}
    if (wakeUpInterval == null) {wakeUpInterval=24}
    if (report == null) {report = "Alert Report"}

It seems that the variables are not null, but totally non-existent. And I don’t know how to proceed.

you can’t set the value of input elements, bummer I know…
defaultValue works, but only from the context of opening the the preference settings…
without opening the page, well you see what happens…
try this in your result config:
def wakeUpIntervalLocal = wakeUpInterval ?: yourDefaultValueHere
def ledLocal = led ?: yourDefaultValueHere
Then use the locals for the zwave commands…

Result only gets returned if it’s the last line of code so to get the tamper event to work in that situation you need to add “return result” right before the closing bracket. To avoid these types of scenarios, I recommend always explicitly returning result as the last line.

1 Like

If I define variables in configure(), I think think they are accessible in another function, resultConfiguration() in my case.

So if I define the variables “wakeUpIntervalLocal” and “ledLocal” in configure(), how can I access them in resultConfiguration() ?

Nevertheless I tried and it didn’t work.

Is there a way to access a variable defined in one method in another method?

Does “return result” returns anythings for a void method?

The method that you want is updated, configure is only called when the device is joined. Updated is called after the preferences page is closed.
Variables are local to the method, their values can be passed to other methods as parameters.
Global variables as they exist in st are referenced using state.somethingHere, these are written to the back end, so they persist through app invocations.

1 Like

Groovy has a “lazy” function - it returns the last thing evaluated unless you specify otherwise. That’s why you’ll see a lot of code that looks really cool and magic happens. It’s also why @krlaframboise explicitly uses the return - otherwise you can get unexpected results if you evaluated something and didn’t pay attention or realize before the end of the function.

As far as variables go, they’re basically all local variables except for the preferences variables. If you want a variable to be available across multiple functions throughout your code - and from execution to execution - I use state variables. Not sure if that’s what you’re supposed to do, but it works for me.