Openhab2 write items to file - ERROR script execution: null

Hi,
I have a problem to store some items + values into export file.

rule "Export Sum values"
when
    Time cron "0 0/1 * * * ?"  // Run every 1 minutes, adjust as needed
then
    val Sum1A_value = Sum1A.state
    val Sum1B_value = Sum1B.state

    val KeySum1_value = KeySum1.state

    val String filePath = "/etc/openhab2/scripts/AGVolt-modbus/exported_sum.txt"  // exported file

    val file = new java.io.File(filePath)
    file.createNewFile()
    logInfo("Export Sum Values", "Export file created")

    val BufferedWriter writer = new BufferedWriter(new FileWriter(file, false))

    writer.write("Sum1A: " + Sum1A_value + "\n")
    writer.write("Sum1B: " + Sum1B_value + "\n")
    writer.write("KeySum1: " + KeySum1_value + "\n")

    writer.close()
end

Following ERROR message appears:

[ERROR] [ntime.internal.engine.ExecuteRuleJob] - Error during the execution of rule 'Export Sum values': An error occurred during the script execution: null

caused by line: val BufferedWriter writer = new BufferedWriter(new FileWriter(file, false))

Sorry but I can’t find a example for writing some items into external file.
Thanks for help.

  • Platform information:
    • Hardware: RPi4 8GB
    • OS: Raspbian Bullseye
    • Java Runtime Environment: OpenJDK Client VM (Zulu Embedded 8.25.0.76-linux-aarch32hf) (build 25.152-b76,
    • openHAB version: 2.5.12

What do you want to do with this file?

Did you import the Java classes you are trying to use (BufferedWriter, FileWriter, etc.)? You are using the full name of java.io.File but not for the others so you have to either use their full name or import them.

Often it can be much easier to use executeCommandLine.

   val toWrite = "Sum1A: " + Sum1A_value + "\n" +
                 "Sum1B: " + Sum1B_value + "\n" +
                  "KeySum1: " + KeySum1_value
   executeCommandLine(Duration.ofSeconds(5), "bash", "-c", "'echo \"' + toWrite + '\" >> ' + filePath)

But it’s even easier to use what OH has built in such as persistence, MainUI charts, logging, etc.

Hi Rich, thanks for reaction.
I can’t analyse this ERROR with no information. OpenAI too :0)

I need to export some items and those actual not historical values to a standard comma delimited csv file.

My file should have this form:

Header (Sum1A, Sum1B, … SumxA, SumxB) not necessary
Sum1A_value, Sum1B_value … SumxA_value, SumcB, value)

I have stored all items in Influxdb via OH persistence

Maybe a solution is to read last values from Influx and put it in external file.
But my problem isn’t read source. I can’t write values to file.

I will try bash as you recommended

val String filePath = "/etc/openhab2/scripts/AGVolt-modbus/exported_sum.txt" 
val toWrite = ("Sum1A_value + ", " + Sum1B_value + ", " + Sum2A_value + ", " + Sum2B_value)
executeCommandLine(Duration.ofSeconds(5), "bash", "-c", "'echo \"' + toWrite + '\" >> ' + filePath)
    val String filePath = "/etc/openhab2/scripts/AGVolt-modbus/exported_sum.txt"
    val toWrite = Sum1A_value + ", " + Sum1B_value + ", " + Sum2A_value + ", " + Sum2B_value
    executeCommandLine("bash -c 'echo \"" + toWrite + "\" >> " + filePath + "'")
    logInfo("Export values finished !", "Data written to file: " + toWrite)

no errors but file isn’t created/overwritten ???
Permissions to folder / file “777”

2023-10-25 19:52:11.845 [INFO ] [lipse.smarthome.io.net.exec.ExecUtil] - executed commandLine 'bash -c 'echo "12.08kWh / 0.71e, 71.28kWh / 4.17e, 135.41kWh / 7.92e, 0kWh / 0e" >> /etc/openhab2/scripts/AGVolt-modbus/exported_sum.txt''
2023-10-25 19:52:11.848 [INFO ] [odel.script.Export values finished !] - Data written to file: 12.08kWh / 0.71e, 71.28kWh / 4.17e, 135.41kWh / 7.92e, 0kWh / 0e

cmd line works

echo "Your test string" >> /etc/openhab2/scripts/AGVolt-modbus/exported_sum.txt

Because of OH2 version I’m trying to use @@ to substitute spaces
Rich article

Sorry I don’t have installed exec binding :0)

OH, if this is OH 2 the syntax is definitely going to be different. You’ll need to pass a delay to executeCommandLine (I can’t remember which argument it is) and capture the return value in a variable. Log out what’s being returned by the command.

Hi Rich.
I’m trying to solve problem with a shell command.
write_to_file.sh

#!/bin/bash
data_to_write="$1"
file_path="$2"

echo "$data_to_write" >> "$file_path"

rule

var String filePath = '/etc/openhab2/scripts/AGVolt-modbus/export.txt'
    logInfo("File path", filePath)
    var String toWrite = '"' + Sum1A.state + ", " + Sum1B.state + ", " + Sum2A.state + ", " + Sum2B.state + '"'
    logInfo("Export values", toWrite)
    // Call the Bash script with variables as arguments
    val results = executeCommandLine("bash@@-c@@/etc/openhab2/scripts/AGVolt-modbus/write_to_file.sh@@\"" + toWrite + "\"@@\"" + filePath, 5000)

But still not success,only if I define second parameter as

echo "$data_to_write" >> "/etc/openhab2/scripts/AGVolt-modbus/export.txt"

then space character is written. So problem is with transfer of values to par $1, $2
File export.txt - permission set 777

Again, log out the results so you can see what is returned from the command.

Also, since you have created a script, you can run it directly. You don’t need bash -c.

Hi Rich.
I found out whats going on in 2.5.12.

executeCommandLine("python3 /etc/openhab2/scripts/AGVolt-modbus/Transa_sql_final.py -date " + set_month.state.toString + "_" + set_year.state, 80000)

and

var String result = executeCommandLine("python3 /etc/openhab2/scripts/AGVolt-modbus/Transa_sql_final.py -date " + set_month.state.toString + "_" + set_year.state, 80000)
logInfo("Python", "Script execution: " + result)

are correct.

THE MOST IMPORTANT THING IS THAT:

All paths to files should be defined as complete paths, e.g. /etc/openhab2/scripts/AGVolt-modbus/Transa.csv

Thanks for help.