Ultimately everything you need to solve this has been said on this post so far but I want to summarize and consolidate.
-
Your call to execute command line is returning a newline after the output. So instead of
1
, you are getting1\n
. Obviously,1\n
is not a number and you cannot parse it into one. But you can usetrim
to remove the newline (I recommend that over multiplying by 1 as suggested by @Oliver2, though that should work too.val echoResult2 = executeCommandLine(Duration.ofSeconds(5),“/bin/cat”, “/etc/openhab/variables/valve.txt”).trim()
Also I suspect the carriage return is being added by cat
so even if you managed to remove it from the file, you’ll still have to deal with it. So don’t bother, just trim the result in your rule.
-
The
sendCommand()
action indeed expects twoStrings
as arguments, doesn’t matter what type the Item is. Often it can be smart enough to calltoString()
on what’s passed to it but not always. This is why the docs and it’s always been recommended to useItem.sendCommand(newState)
oversendCommand(ItemName, newState)
as theItem
method can accept more different types and convert as needed.
But given that it expects a String in the first place, you don’t need to parse it into an Integer in the first place. Just pass the String fromexecuteCommandLine
directly to the Item in the update. The error “for input string: 1” is telling you that it expects a String for the second argument but it’s not getting a String because you are parsing it into an Integer. -
The line above avoids the assignment to final
val
by doing thetrim
on the same line. If you do two lines like @Baschtlwaschtl desmonstrates, you must usevar
instead ofval
so you can change the value of the variable.