valueTile precision to 3 decimal places

(Glenn Smith) #1

I want to be able to display decimal places on some data in a tile.

The title is:

	valueTile("energy", "", decoration: "flat", width: 3, height: 2) {
		state "default", label:'${currentValue} kWh (total)'

I’ve tried as per other posts to use the String.format, which I did when the z wave event fired:

def zwaveEvent(physicalgraph.zwave.commands.meterv3.MeterReport cmd) {
	if (cmd.scale == 0) {
		[name: "energy", value: (String.format("%6.3f", cmd.scaledMeterValue)), unit: "kWh"]

But I still do not get anything other than 0 displayed even through the event log shows it as 0.032. So I’m either doing something wrong with the format, or the value doesn’t allow formatting, or something else I’ve not considered.

Any thoughts?

(Andreas A.) #2

You have no control over the formatting for valueTiles - I submitted a request to add a format parameter for a valueTile() to allow specifying the number format. I suggest that you do the same.

(Glenn Smith) #3

Thats annoying then, but at least I know I’ve not missed the obvious. I’ll defiantly do that, as I’m now seeing rounding that I’d change if I had control over it.

(Andreas A.) #4

There is a way around this issue - though it is far from perfect:

  1. Use “string” rather than “number” for your energy attribute
  2. Include the unit in the event you send:
[name: "energy", value: (String.format("%6.3f", cmd.scaledMeterValue)) + "kWh"]

(and remove the unit: part). While this works for display in the app, the energy is now a string including units instead of a raw numeric value - using this in another smart app might causes problems as most likely it won’t expect a string with units…

( co-founder Terry @ActionTiles; GitHub: @cosmicpuppy) #5

To avoid this rather serious implication, you can instead add a new ad hoc / custom Attribute of the String datatype and also update it whenever / wherever the existing numeric attribute is set.