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.
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.
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.