Hint needed: easiest way to read item values from file

I read about reading values from a file with rules.

But my question is: what would be the easiest way with a thing (channels) or item definition?

Structure of the file I will do like

item-name, value
Item2-name, value2

If only rules possible, I found enough to do so,

So just need a hint :wink:

Only possible with rules.

Thanks!

If you are able to place the file in /conf/html/ or somewhere where the built-in HTTP server can use it, you might be able to use the HTTP binding to localhost to read it, and select vales with transformations. It’s a terribly complicated way to avoid using a rule.

While rules would be the most convenient and probably the most efficient.

However, I can think of some other alternatives.

  • Using the Exec binding to call something like cat myfile.txt and use a REGEX transformation to extract the value for a given Item. The problem with this approach is you’ll need a separate Thing for each Item, or you need a rule to parse the result in which case you may as well use the rule in the first place with executeCommandLine or the Java file reading classes.

  • Using an external script to read in the file and publish the states to the Items using the REST API. This probably isn’t going to any easier or straight forward than using the rule but could be useful if the file isn’t on the same machine as OH.

  • Serve the file up using some HTTP server (maybe even OH itself) and use the HTTP binding to query for and parse out the Items and their states. (I see @rossko57 beat me to this idea)

Though I gotta say this sounds a whole lot like an XY Problem.

For everyone searching an easy version for value import from a file:

import java.io.File
import java.util.Scanner

// Import Regel für Datei-Import
rule "Ablesung Importieren aus Datei"
when Time cron "5 /15 * ? * *"
then
    logInfo("98_HeizkostenVerordnung.rules", "Importlauf gestartet")
    val File ImportFile = new File("/etc/openhab/import/Zaehler.import")
    try {
        var String line = ""
        val Scanner scanner = new Scanner(ImportFile)
        while (scanner.hasNextLine()) {
            line = scanner.nextLine();
            val String[] fields = line.split("=")
            postUpdate(fields.get(0), fields.get(1))
//            logInfo("Import", fields.get(0) + " to " + fields.get(1))
        }
    } catch (Exception e) {
        logError("Ablesung Importieren aus Datei ", e.message)
    }
end

Syntax for Import file (in my example /etc/openhab/import/Zaehler.import) is very easy:

itemname=value
Itemname2=value2
...

The rule imports values for all items found in file.

1 Like

Thanks for posting. But please use code fences so the white spaces are preserved. There are a bunch of icons on the editor or

```
code goes here
```

Another simple approach could be to use executeCommandLine.

then
    val f = executeCommandLine(java.time.Duration.ofSeconds(5), "cat", "/etc/openhab/import/Zaehler.import")
    val lines = f.split("\n")
    lines.forEach[ line |
        val fields = line.split("=")
        postUpdate(fields.get(0), fields(1))
    ]
end