Item.postUpdate from result of executeCommand

  • Platform information:
    • Hardware: amd64/8G/130G
    • OS: Debian 11
    • Java Runtime Environment: openJdk 17.0.7
    • openHAB version: 4.0.1
  • Issue of the topic: Number Item state shall be set from the result of a file

Hi all,

I would like to set the state of a number item in a rule by executeCommand.
The command reads a value from a file (which is 0 or 1) and shall post it to the item.

I already rewrite the executeCommand to be runnable on OH4 (I came from OH2 where all works fine).

This is the code:

val VdfSt = (executeCommandLine(Duration.ofSeconds(10),“//bin/cat”,“//run/openhab/net.vdf”))
VDFONL.postUpdate(VdfSt)

That produces the following log:
“2023-08-12 03:35:05.493 [WARN ] [b.core.model.script.actions.BusEvent] - Cannot convert '0
’ to a command type which item ‘VDFONL’ accepts: [DecimalType, QuantityType, RefreshType].”

When I do:

VDFONL.postUpdate(VdfSt as DecimalType)

I get:
“2023-08-12 03:38:45.337 [ERROR] [internal.handler.ScriptActionHandler] - Script execution of rule with UID ‘net-1’ failed: Could not cast 0
to org.openhab.core.library.types.DecimalType; line 7, column 20, length 20 in net”

Could someone help me what the correct coding would be?

You probably have a newline character in there.

Why not just read the file directly using java

val VdfSt = java.nio.file.Files.readAllLines(java.nio.file.Paths.get("/run/openhab/net.vdf")).get(0)
VDFONL.postUpdate(VdfSt)

Thank you very much JimT!
Reading with Java worked.