Rules: Reading from file

Hello, I am using OpenHAB (1.8, because I have some restriction with the version to use).
I would like to read from a file but I cannot do it with a rule (I need to set some global variables).

var String url = "C:\\Users\\ago\\Documents\\Ruscio\\OpenHAB\\configurations\\high_level_goals.conf"
var String electricity_goal = null
var Number threshold //if goal is of temperature
var Date date_beg = null
var Date date_end = null

rule Startup
when
	System started
then
	var String lastGoal = null
	logDebug( "mylogs", "Starting decision.rules...." )
	var List<String> lines = new FileReader(url).readLines.map [ s | 
		logDebug( "mylogs", s)
		if(s.equals("WINDOW") || s.contains("KEEP") || s.contains("ELEC")) {
			lastGoal = s
		}
		else {
			if(lastGoal.equals("WINDOW")) {
				if(date_beg == null) {
					date_beg = new SimpleDateFormat("HH:mm").parse(s)
				} else {
					date_end = new SimpleDateFormat("HH:mm").parse(s)
				}
			}
			else if(lastGoal.contains("KEEP")) {
				threshold = Float.parseFloat(s)
			}
		]
	}
	logDebug( "mylogs", lastGoal )
end

I am on Windows 10 and I am very confused on the syntax to be used, because it seems I cannot use Java to read files nor Xtend. In fact, this code does not work:

var String url = 'high_level_goals.conf'
var String electricity_goal = null
var Number threshold //if goal is of temperature
var Date date_beg = null
var Date date_end = null

var Timer windowsAlarm = null

rule Startup
when
	System started
then
	var String lastGoal = null
	logDebug( "mylogs", "Starting decision.rules...." )
	var List<String> lines = new FileReader(url).readLines.map [ s | 
		logDebug( "mylogs", s)
		if(s.equals("WINDOW") || s.contains("KEEP") || s.contains("ELEC")) {
			lastGoal = s
		}
		else {
			if(lastGoal.equals("WINDOW")) {
				if(date_beg == null) {
					date_beg = new SimpleDateFormat("HH:mm").parse(s)
				} else {
					date_end = new SimpleDateFormat("HH:mm").parse(s)
				}
			}
			else if(lastGoal.contains("KEEP")) {
				threshold = Float.parseFloat(s)
			}
		]
	}
	logDebug( "mylogs", lastGoal )
end

Could you help me reading a file from rules? Thank you a lot in advance!
P.S.: file is there and it seems OH can find it

Old postings (note use of java.io.FileReader)

https://groups.google.com/forum/#!topic/openhab/nX0jX1cN0Tc

1 Like

Thank you a lot! =)
Strange, I did something similar but I got nothing…in any case, that one is the solution!

1 Like