Formatting Numbers in ValueTiles


(Ron) #1

Since the last Android Update 1.7.2 my value tiles have changed to show too many decimal places in the values making the text tiny and unreadable.

How can I set the precision I want the tile to write ?

Can I configure the tile to format the value or do I have to alter the number to the precision I want. Should I convert to a formatted string, round to the precision I want etc.

My value is an integer/1000 which results in BigDecimal

I am trying to use the round method of BigDecimal but the ide will not let me create a MathContext object to pass in. It is frustrating to not know what ST includes and what they do not. If BigDecimal is returned from the divide I should have to it’s methods :frowning:

I am sure this is simple but I find it hard to figure out in the limited environment. I would have sworn the documentation pages used to have a reference of methods available but I can’t find it anymore.


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

As usual, I’ll toss out at one possible solution.

You may have to add another Attribute of type “string”, and use any Groovy / Java number to string conversion method to get the desired precision. The Value Tile(s) would have to, of course, refer to these new string Attribute(s), instead of the raw numeric Attribute.

http://docs.oracle.com/javase/tutorial/java/data/converting.html


(Barry) #3

in my HEMv2+ device, I format the number before I send its value to the tile. Here’s an excerpt from that device showing how I round the value to 2 decimals of precision, format it as a string and send the formatted value to the display tile and the raw (numerical) value as the Attribute’s value (which has no destination tile, but is available programmatically to other SmartApps). In addition, I calculate the effective cost (in dollars and cents), and format that value for yet another tile.

        newValue = Math.round(cmd.scaledMeterValue * 100) / 100
        if (newValue != state.energyValue) {
            formattedValue = String.format("%5.2f", newValue)
            dispValue = "${formattedValue}\nkWh"
            sendEvent(name: "energyDisp", value: dispValue as String, unit: "", descriptionText: "Display Energy: ${newValue} kVh")
            state.energyValue = newValue
            BigDecimal costDecimal = newValue * ( kWhCost as BigDecimal )
            def costDisplay = String.format("%5.2f",costDecimal)
            state.costDisp = "Cost\n\$"+costDisplay
            if (state.display == 1) { sendEvent(name: "energyTwo", value: state.costDisp, unit: "", descriptionText: "Display Cost: \$${costDisp}") }
            [name: "energy", value: newValue, unit: "kWh", descriptionText: "Total Energy: ${formattedValue} kWh"]
        }

(Ron) #4

This is part of my issue. I did that and every one I tried made the app crash or resulted in Class not found errors. That is why I get frustrated that you can’t just import what you need. It be OK if ST documented what they do provide well but they do not so you are left with trial and error. You also never know if you just made a typo or they really don’t have the class imported. So I may skip something that should have worked because I missed my mistake. Basically I wind up looking for sample code and until we get the github access they keep promising that is also tricky. Anyway just frustrated by something that in a normal development environment I would have absolutely no issue solving.

@storageanarchy Thanks I swear I tried that first but my app kept crashing when I tried to open the tiles. With nothing in the logs I could not solve it so I abandoned this attempt. Just as I mentioned above. You try something that should work, when it fails you don’t know if another limitation of the environment or if you just made a typo. Very cool that it works for you so now I know for sure I can hack at that until I make it work. Cheers :beer: this drinks on me :slight_smile:


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

Improvement of the IDE (debugging, github,…?) is high on the SmartThings priority list.

Well… Higher than some other things, I think.

In the meantime, we gotta guess and use lots of log.debug.

I also do most of my editing on free Cloud9 (built in unlimited undo and git integration), and copy past back to IDE.


(Ron) #6

debug doesn’t work when the app just crashes :smile:
I used git, eclipse, cut/paste …


(Barry) #7

Are you testing/debugging usinh an Android device?

With numerous display issues with colors and embedded newlines (\n), Irecommend not using Android/ While the iPhone is not 100% stable either, it is more reliable for this sort of formatting.


(Ron) #8

Yes I am. I am very sorry to hear that. I do not own an iphone as a developer I prefer the openness of android. Besides if everyone takes the apporach of avoiding the android issues SmartThings will never fix the app.

That said I reported issues with the Android slider tiles during release 1.7.2 which was recently released and it was VERY difficult to reach someone in support working on the android app. I was originally working with two developers on the issue then suddenly they realized I was using android and they said “We don’t work on the android app and we don’t know who does” that really surprised me. If the developers don’t know each other how do they coordinate releases. Apparently they do not which leads to these bugs. I now have no idea if anyone is really working on these slider issues, I will find out in the next release. I was assured the issue was passed to the correct people however I found it weird that they never contacted me like the other developers had.


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

Just growing pains.

But if you develop for shared use, I recommend you test on all 3 Mobile Platforms.

This will probably be a part of the certification process, even if platform inconsistency is SmartThings’s “fault”. Specs should be documented to the lowest common denominator, and hopefully Developers will be given fair notice of deprecated syntax, and Beta pre-releases for testing.

We’ve been told that the platform will eventually facilitate and require you to submit an automated test suite with your code, so that regression failures can be detected automatically by ST.


(Ron) #10

@storageanarchy Your code was VERY helpful I learned a lot from how you are doing things. I didn’t realize for example that sendEvent can have descriptionText added. I havn’t tried it yet but that can be handy.

I modified my code easily to just convert when I send the events so very little code change

delayBetween([
sendEvent(name: 'energy_today', value: (String.format("%5.2f", energyToday)))
,sendEvent(name: 'energy_life', value: (String.format("%5.2f",energyLife)))
,sendEvent(name: 'power', value: (currentPower))
,sendEvent(name: 'production_level', value: (String.format("%5.2f",productionLevel)))
,sendEvent(name: 'today_max_prod', value: (String.format("%5.2f",todayMaxProd)))
,sendEvent(name: 'reported_id', value: (systemId))
])

Also seems /n is working now in Android.
valueTile(“power”, “device.power”) {
state(“Power”, label: ‘${currentValue}W\nPower’, unit:“W”, backgroundColor: “#000000”)
}

Nice result for my Solar Panel Monitor Device


(Barry) #11

That’s very nice - glad to be able to help!


(Ben Wolodko) #12

Hi I have

Samsung Galaxy S III
Model: SGH1747M
Android Version: 4.42

Smart Things version 2.1.4
Build number 204485

When playing with valueTiles, \n doesnot work

If a Background color is specified, I get a solid color dot, with the the label, and current value all on one line

I have seen authors using \r for the same purpose, a new line

The following groovy code gives the same results, everything on one line

valueTile(“roomTemp_F1”, “device.roomTemp_F1”, width: 4, height: 3, decoration: “flat”){
state (“default”, label:‘FLWR1\n ${currentValue} °C’, unit:"", backgroundColor:"#153591")}

valueTile("humidity_F1", "device.humidity_F1", decoration: "flat", wordWrap: true, width: 4, height: 3, ) {
    state ("humidity", label:'F1_Room\n${currentValue}% humidity', unit:"", backgroundColor:"#bc2323")   
}

both of these give the same results

The Simulator, shows the proper display with the “F1_Room” on one line and say “25.6 °C” on the next line.

Any advice to get the smart app display to have multiple lines of text?


(George) #13

Screenshot please


(Ben Wolodko) #14

Hi George, here is what I Get

Thanks

Ben


(George) #15

Hi. Yeah, it should have worked.

Some things to try (some of this is voodoo - maybe you didn’t make enough sacrifices to the ST gods):

  • Try 2x2 or 3x3 tiles
  • Try having just one “main” tile
  • Try having the entire label in the currentValue, so sendEvent(name: “humidity_F1”, value: “F1_Room\n${humidityVariable}% humidity”) and the label is just ‘${currentValue}’

(Ben Wolodko) #16

Hi George, 3x3 tiles worked better for arrangement of tiles, two side by side, one below.
but no joy with more than one line of text.

I donot know what you mean by just having one “Main” tile

and I assume the last suggestion would be to modify the Arduino code to
send the Text as well as the value.

I added some text and the \n to the string that is received from the Arduino, but the \n still didnot create a second line.

Can anyone indicate an Android device ( tablet or phone ) that can actually support two or more lines of text on a value tile?

Thanks again for your suggestions

Ben


(George) #17

So strange. One last thought, promise :wink: – try tile size of width 4 height 1 and width 6 height 1. Basically, see if forcing it to use a different font/font size will make a difference.


(Ben Wolodko) #18

Hi George, here are some shots

This is 4x1 size

This is 6x1 size


This is 3x3 size no Text

This is 3x3 with Text, but no multiline

This is 6x2 size no multiline

This 6x3 no Multiline

This is 6x4 size, no multiline

If I add the \n for groovy newline or \r for groovy carriage return
on the 6x4 size, the F1-Room stays and the number results disappear

from
http://docs.groovy-lang.org/latest/html/documentation/index.html#_introduction

I would suspect that each manufacture of phones and tablets, have their own flavor of Android, so inconsistency would be the norm. You would think that Samsung owning SmartThings would be compatible with Samsung phones…

Hey SmartThings gurus, how about a list of devices that actually will work with Groovy \n and \r?


(Barry) #19

This is a long-standing bug in the Android version, which for reasons unknown has not been addressed since it was first reported 2+ years ago. This bug continues to this day to stop people from using my HEMv2+ whole-house power monitor DTH on Android (works fine on iOS).