Why does my valueTile() display NULL?


(Audi) #1

So, I created a valueTile() like this. . .

tiles {
  valueTile("power", "device.power", decoration: "flat") {
    state "power", label:"${currentValue} W"
  }
}

Notice that all strings are double quoted, this is significant. Here is a page that discusses the Groovy programming language syntax with a lot to say about strings.

http://www.groovy-lang.org/syntax.html#all-strings

And here is a digest of section 4 on that page.

Groovy lets you instantiate two kinds of strings: java.lang.String objects and groovy.lang.GString objects (a.k.a. “interpolated strings”).

Single quoted and triple quoted strings don’t support interpolation (I infer from this statement that single quoted and triple quoted strings can only be java.lang.String objects). Only double quoted strings can be groovy.lang.GString objects.

If there’s no interpolated expression then a double quoted string is a plain java.lang.String object. But, if interpolation is present then it is a groovy.lang.GString object.

Any Groovy expression can be interpolated in double quoted string literals. Interpolation is the act of replacing a placeholder in the string with its value upon evaluation of the string. Placeholders are expressions surrounded by ${} or prefixed with $ (for dotted expressions).

Based on the material above, using double quotes in label string (as shown in my example above) must be the correct syntax. But it’s not. Using double quotes here will cause the valueTile() to display “NULL.”

Here’s a working example given at
http://docs.smartthings.com/en/latest/ref-docs/device-handler-ref.html#valuetile

tiles {
  valueTile("power", "device.power", decoration: "flat") {
    state "power", label:'${currentValue} W'
  }
}

Notice that double quotes are used everywhere except in the label string, which contains a placeholder for the current value, which is what we want to interpolate and display.

So what gives? Why do I have to use this “incorrect” syntax to make my tile work? What am I missing?


(Johnathan Fullman) #2

I found that the values don’t display on tiles if there isn’t a corresponding attribute on the device.
You either need to add the powerMeter capability or add a power attribute.


(Audi) #3

No, no, no. You missed the point.

Simply by changing the double quotes to single quotes fixes it. Writing “illegal” code makes it work.

But, thanks for the reply.


(ActionTiles.com co-founder Terry @ActionTiles; GitHub: @cosmicpuppy) #4

The entire preferences section is not really Groovy?

It is a part of the Device Type Object, but I think there is a preprocessor?


(Jim Anderson) #5

I agree this doesn’t seem to make sense on first look. I’ll take a look and/or reach out to engineering. You’re correct to point out how Groovy handles interpolated strings.


(Audi) #6

Thank you, I appreciate that.

I spent the better part of a day trying to figure out what I was doing wrong before I noticed that one little detail.

It would be nice if we could use double quotes there. It would keep the code uniform, and it might save someone else a lot of grief.


(Jim Anderson) #7

I agree that it would make more sense to use double quotes to be consistent with Groovy. I verified that in this case single quotes must be used for the label because we are doing manual substitution for label values.

The reason is that when the device type handler is initialized by the platform, we process the metadata to store for the mobile client. But at this time, we don’t know the currentValue of the event. So, the platform needs to manually substitute this later.

We’ll need to clearly document this exception to the standard groovy string interpolation rules. Also I know we really need a clear lifecycle explanation/diagram for both SmartApps and device type handlers, to help general understanding as well as give context for situations like this. This is also in our documentation backlog.

Sorry for your confusion - I would be confused myself! :slight_smile:


(ActionTiles.com co-founder Terry @ActionTiles; GitHub: @cosmicpuppy) #8

That’s what I said in Post #4. :stuck_out_tongue_winking_eye:


(Audi) #9

Thanks for looking into this. Your explanation really helps. I am (just) starting to understand how all of these pieces fit together.

I would be very interested in reading that!

Thanks again,

  • Audi