Syntax: using custom function does not work

Ok, a very stupid question:

When I use the code:

else if (cmd.event == 0x04) {
        def timeString = new Date().format("MMM d yyyy, hh:mm:ss", location.timeZone)
        result << createEvent(name: "tamper", value: timeString , descriptionText: "$device.displayName was tampered")
        result << response(zwave.wakeUpV1.wakeUpIntervalSet(seconds:3600*wakeUpInterval, nodeid:zwaveHubNodeId).format())
}

everything is fine. But since the amount of code is getting more, I would like to use a custom function where to put all the code:

def resultConfiguration() {
    	def timeString = new Date().format("MMM d yyyy, hh:mm:ss", location.timeZone)
        result << createEvent(name: "tamper", value: timeString , descriptionText: "$device.displayName was tampered")
        result << response(zwave.wakeUpV1.wakeUpIntervalSet(seconds:3600*wakeUpInterval, nodeid:zwaveHubNodeId).format())
}

And then only to call the custion function:

    else if (cmd.event == 0x04) {
           resultConfiguration()
    }

But it does not work when I do so and I have no idea what is wrong with the Groovy syntax.

What can be the problem?

In your resultConfiguration() function, result variable is not defined within the scope of the function. Therefore it won’t compile.

2 Likes

To elaborate on what @geko said, your second code block needs to have

def result = []

Your third code bock should then have:

result += resultConfiguration()

1 Like

Of course! Thank you, guys.

And if the function contains the code:

   	cmds << command(zwave.wakeUpV1.wakeUpIntervalSet(seconds:3600*wakeUpInterval, nodeid:zwaveHubNodeId))
    if (led) {
        cmds << command(zwave.configurationV1.configurationSet(parameterNumber: 2, size: 1, scaledConfigurationValue: 1))
    } else {
        cmds << command(zwave.configurationV1.configurationSet(parameterNumber: 2, size: 1, scaledConfigurationValue: 0))
    }

Should I use:
cmds += resultConfiguration()

You need to post a larger code segment, I can’t tell what you’re trying to do.

The function is:

def cmdsConfiguration() {
 def cmds = []
 cmds << command(zwave.wakeUpV1.wakeUpIntervalSet(seconds:3600*wakeUpInterval, nodeid:zwaveHubNodeId))
 if (led) {
    cmds << command(zwave.configurationV1.configurationSet(parameterNumber: 2, size: 1, scaledConfigurationValue: 1))
 } else {
    cmds << command(zwave.configurationV1.configurationSet(parameterNumber: 2, size: 1, scaledConfigurationValue: 0))
 }

}

Based on your input, I tried using:
cmds += cmdsConfiguration()
and it worked.

Thanks.