I have an Item of type “Number:Length”, an example value is:
logInfo("Snow", fc24hSnowVolume.state.toString)
0.0075000000000001 mm
(mm snow in forecast, as example)
How can I round this state to 0 or 1 decimal points, and keep the unit intact? When I try to apply Math.round(), I see an error message like:
Could not invoke method: org.eclipse.smarthome.model.script.actions.LogAction.logInfo(java.lang.String,java.lang.String,java.lang.Object[]) on instance: null
Any combination of “as DecimalType” or “as Number”, together with “floatValue” also doesn’t work.
Error during the execution of rule 'Weather': An error occurred during the script execution: Could not invoke method: org.eclipse.smarthome.model.script.actions.LogAction.logInfo(java.lang.String,java.lang.String,java.lang.Object[]) on instance: null
To follow up on this, I went down the brute force way, split the string up, and then handle the parts separately:
var snow_value = fc24hSnowVolume.state.toString.split(' ').get(0)
var snow_value_rounded = Math::round(Float::parseFloat(snow_value))
var snow_unit = fc24hSnowVolume.state.toString.split(' ').get(1)
“snow_value_rounded” can be rounded to more decimal points, but in my case it’s enough to know if there will be snow at all. Same for rain, no one cares about “0.0038 mm” rain tomorrow …