Read item value from text file

Hi,

Seem to be a few articles on this but they are perhaps a little out of date and I am struggling to make this work, and help would be appreciated.

I am trying to read from a flat text file and update an item based on the contents, I see there are other ways of doing this with MQTT etc, but in my situation they are not viable.

My rule is thus:

rule "Read in file"
when
 Time cron "0 01 08 ? * * *"
then
    val f = executeCommandLine(Duration.ofSeconds(5), "cat", "/openhab/conf/misc/test.import")
    val lines = f.split("\n")
    lines.forEach[ line |
        val fields = line.split("=")
        postUpdate(fields.get(0), fields(1))
    ]
end

The file contains:

vswSchoolHolidayMode=OFF

The error:

Script execution of rule with UID 'action-33' failed: The name 'fields' cannot be resolved to an item or type; line 460, column 35, length 9 in action

My Java Fu is weak, so I am stumped…

I added some logging:

rule "Read in file"
when
 Time cron "0 45 08 ? * * *"
then
    val filedata = executeCommandLine(Duration.ofSeconds(5), "cat", "/openhab/conf/misc/test.import")
       logger.info("FILEDATA " + filedata);
    val lines = filedata.split("\n")
       logger.info("FILELINES " + lines);
    lines.forEach[ line |
       logger.info("FILEFIELD " + fields);
        var fields = line.split("=")
        postUpdate(fields.get(0), fields(1))
    ]
end

Logs:

2022-08-17 08:45:00.449 [INFO ] [enhab.core.model.script.actions.HTTP] - FILEDATA vswSchoolHolidayMode=OFF
2022-08-17 08:45:00.450 [INFO ] [enhab.core.model.script.actions.HTTP] - FILELINES [Ljava.lang.String;@73d4326f
2022-08-17 08:45:00.453 [ERROR] [internal.handler.ScriptActionHandler] - Script execution of rule with UID 'action-33' failed: The name 'fields' cannot be resolved to an item or type; line 464, column 35, length 6 in action

I assume the content of FILELINES is a pointer to location in memory rather than string, I am none the wiser.

Its not using the value in post update

I think our posts crossed, and I would agree, I just don’t have the first clue how to fix it :slight_smile:

You have started the the right place Logging.

Can you explain a little more what you are trying to achieve with this rule. Explain your situation as there may be an easier way to intergrade the system together

I am not sure what else I can provide that might help, it’s just this.

An external program will update that flat file.

Do you have control over this External program or the only output of this program is to a file?

Because if you do have control you can just plonk the data into the rest api

I can probably make that happen, but as much as anything it’s a matter of principal now. It would also be smoother for me in the long run if I could make this work.

    val filedata = executeCommandLine(Duration.ofSeconds(5), "cat", "/openhab/conf/misc/test.import")
       logger.info("DATA " + filedata);
    val lines = filedata.split("\n")
       logger.info("LINES " + lines);
    lines.forEach[ line |
       logger.info("LINE " + line);
        val fields = line.split("=")
          logger.info("FIELDS " + fields);
        postUpdate(fields.get(0), fields(1))

Improved logging:

2022-08-17 09:03:00.111 [INFO ] [enhab.core.model.script.actions.HTTP] - DATA vswSchoolHolidayMode=OFF
2022-08-17 09:03:00.112 [INFO ] [enhab.core.model.script.actions.HTTP] - LINES [Ljava.lang.String;@15aa9347
2022-08-17 09:03:00.118 [INFO ] [enhab.core.model.script.actions.HTTP] - LINE vswSchoolHolidayMode=OFF
2022-08-17 09:03:00.118 [INFO ] [enhab.core.model.script.actions.HTTP] - FIELDS [Ljava.lang.String;@6ac1ed48
2022-08-17 09:03:00.119 [ERROR] [internal.handler.ScriptActionHandler] - Script execution of rule with UID 'action-33' failed: The name 'fields' cannot be resolved to an item or type; line 467, column 35, length 9 in action

Suggests to me that this is not working as expected:

val fields = line.split("=")

Just to make sure you know the rules DSL language is Xtend

You are on the right path

For the next person who comes by, I made it work like this:

rule "Read in file"
when
 Time cron "0 33 09 ? * * *"
then
    val filedata = executeCommandLine(Duration.ofSeconds(5), "cat", "/openhab/conf/misc/test.import")
       logger.info("DATA " + filedata);
    val lines = filedata.split("\n")
       logger.info("LINES " + lines);
    lines.forEach [ line |
       logger.info("LINE " + line);
        val field1 = line.toString.split('=').get(0)
        val field2 = line.toString.split('=').get(1)
          logger.info("FIELDS " + field1);
          logger.info("FIELDS " + field2);
        postUpdate(field1, field2)
    ]
end

Log for ref:

2022-08-17 09:33:00.110 [INFO ] [enhab.core.model.script.actions.HTTP] - DATA vswSchoolHolidayMode=OFF
2022-08-17 09:33:00.112 [INFO ] [enhab.core.model.script.actions.HTTP] - LINES [Ljava.lang.String;@10fdd742
2022-08-17 09:33:00.114 [INFO ] [enhab.core.model.script.actions.HTTP] - LINE vswSchoolHolidayMode=OFF
2022-08-17 09:33:00.115 [INFO ] [enhab.core.model.script.actions.HTTP] - FIELDS vswSchoolHolidayMode
2022-08-17 09:33:00.115 [INFO ] [enhab.core.model.script.actions.HTTP] - FIELDS OFF
2 Likes

That was the pointer I needed, thanks!