String to Integer conversion in OH4 rules

Ultimately everything you need to solve this has been said on this post so far but I want to summarize and consolidate.

  1. Your call to execute command line is returning a newline after the output. So instead of 1, you are getting 1\n. Obviously, 1\n is not a number and you cannot parse it into one. But you can use trim 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.

  1. The sendCommand() action indeed expects two Strings as arguments, doesn’t matter what type the Item is. Often it can be smart enough to call toString() on what’s passed to it but not always. This is why the docs and it’s always been recommended to use Item.sendCommand(newState) over sendCommand(ItemName, newState) as the Item 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 from executeCommandLine 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.

  2. The line above avoids the assignment to final val by doing the trim on the same line. If you do two lines like @Baschtlwaschtl desmonstrates, you must use var instead of val so you can change the value of the variable.