Parse Multiple Attributers

Hello.
I want to pass 3 attributes from my SmartApp to DHT.

Now, I’m able to sent an attribute using:

settings.target.parse(“attr1: ${value1}”) command on SmartApp.

On DHT site the code is the:

def parse(String message) {
    Map msg = stringToMap(message)
    Float temp = msg.attr1.toFloat()
    def event = [
        name  : "attr1",
        value : temp.round(2),
        unit  : “unit", //scale,
    ]
}

Can I pass these tree attributes by issuing the command:
settings.target.parse(“attr1: ${value1}”,“attr2: ${value2}”,“attr3: ${value3}”)

I tried but I got this error:

groovy.lang.MissingMethodException: No signature of method: script_dth_ccef3addb0cab668417f9cb6e1fea649e10faa190aa4df8a52db1da0e45a8eae.parse() is applicable for argument types: (org.codehaus.groovy.runtime.GStringImpl, org.codehaus.groovy.runtime.GStringImpl, org.codehaus.groovy.runtime.GStringImpl) values: [eq: 0.25, c: 38, ac: 23.44] Possible solutions: pages(), page(), parse(java.lang.String), tiles(), wait(), run() @line 67 (doCall)

any suggestions?

Either create a second parse() method that takes more parameters, or add extra parameters to your existing one that have default values.

@orangebucket
Can you please give me a sample code?

The error you are getting is because parse() is being called with three arguments but the definition parse() says it accepts just one. You need to define a version that can accept the three arguments. Groovy is perfectly happy for you to define parse() more than once.

However to start with I would try:

def parse(String message1, String message2 = null, String message3 = null)

By setting default values for the extra parameters there shouldn’t be a problem if it only gets called with one sometimes.

1 Like

@orangebucket
Thanks!!
I will give it a try and I let you know!:blush:

-Edit -
It works like a charm!! Thanks!!!

1 Like