Hi, I’ve tried a couple of different combinations as to cast the String variable “Arduino” to a Number variable “Weight”. What did I do wrong in this instance.
P.S. I’m trying to change it into a Number variable as I am trying to store it into a database (rrd4j) in order to create a chart/graph.
error:
2016-04-23 21:06:27.055 [ERROR] [o.o.c.s.ScriptExecutionThread ] - Error during the execution of rule ‘Arduino sends to Openhab’: Cannot cast org.openhab.core.library.types.StringType to org.openhab.core.library.types.DecimalType
rule "Arduino sends to Openhab"
when
Item Arduino received update
then
var Number newWeight = (Arduino.state as DecimalType).intValue
Weight.postUpdate(newWeight)
end
Not sure about the syntax here, however in Java it should look like this Float.parsefloat (Arduino. State)
Why didn’t you start of with a number variable for Arduino?
If Arduino is a String Item you can’t simply cast it to a DecimalType. You need to parse the value into a Number.
You have to reference static methods using “::” instead of “.”
Float::parseFloat(Arduino.state)
As long as the String the Arduino sends is parsable you can just do what @watou recommends and just send the String on to Weight as is without modification. OH will parse it for you.