I have recently upgraded to openHAB 2.5.0 Milestone 3 from M2. I am not really sure whether this has something to do with that upgrade but my previously working code has an issue at a certain point. I have found out that a String::format statement silently fails. Here is my setup:
Number Taupunkt_Differenz "Taupunktdifferenz Δtd [%.1f °C]" <humidity>
Taupunkt_Differenz is a calculated item out of 2 doubleValues.
rule "My rule"
Item Taupunkt_Differenz changed
val Number taupunkt_delta = Taupunkt_Differenz.state as DecimalType
logInfo("myrule.rules", "td=" + td + " | taupunkt_delta=" + taupunkt_delta)
var String test1 = String::format("%.1f °C", td)
logInfo("myrule.rules", "test1=" + test1)
var String test2 = String::format("%.1f °C", taupunkt_delta)
logInfo("myrule.rules", "test2=" + test2) // this line won't be reached!
// (more code)
and that’s it. The last logInfo is missing. So my impression is that the second String::format statement fails and the rule is exited without any error message because the following code is not executed.
I am quite sure that this code worked before. Does anybody have an idea what is going on here?
Yes! For whatever reason, this did the trick! Now this approach works:
val Number taupunkt_delta = Taupunkt_Differenz.state as DecimalType + 0 // needed to avoid non-working String::format
var String test = String::format("%.1f °C", taupunkt_delta)
logInfo("myrule.rules", "test=" + test) // now it works!
I was worrying about the leading zero maybe followed by comma actually, amazed it works with + 0
Something to do with hidden baggage here. QuantityType issues shouldn’t be coming into play with a plain Number type Item, but it has that feel.
I had a play with this in OH2.4
The good news is an error message appears - f != org.eclipse.smarthome.core.library.types.DecimalType
As we all probably expected, it’s to do with Java formatter not liking object type passed in.
But first, why did you not get the error message? Something odd about your install, like logging problems or locale, or is it a general OH2.5 issue? Need an OH2.5 M3 user to try the following out please.
Okay, so a demo rule to show the issue
rule "format state"
Item nm_test_temp changed
// Item nm_test_state is defined: Number nm_test_temp "temp [%.1f °C]"
val teststateA = (nm_test_temp.state as DecimalType).floatValue
val teststateB = nm_test_temp.state as DecimalType
logInfo("myrule.rules", "teststateA=" + teststateA.getClass)
logInfo("myrule.rules", "teststateB=" + teststateB.getClass)
var String test2 = String::format("%.1f °C", teststateA)
logInfo("myrule.rules", "A formatted=" + test2)
var String test3 = String::format("%.1f °C", teststateB)
logInfo("myrule.rules", "B formatted=" + test3) // this line won't be reached!
So, a java float type (A) satisfies the java String formatter
An openHAB/Eclipse DecimalType does not.
I think the error message is a bit backwards ; at first it seems like it is expecting a org.eclipse.smarthome.core.library.types.DecimalType, but I think the closer meaning is that “f” (as in %.1f) is complaining that it cannot work with DecimalType.
Remembering that String::format is “raw java” and not openHAB-ified, this is perhaps not that surprising after all.