Configuration parameters - retrieval from xls / xml / json/ mysql

hi,

I am creating a horticulture controller.

In this I need to change the value of temperature , humidity , lux etc every hour based on a set of pre-calculated data which I maintain in an xls file.

the file / data format is something like

  • Date - dd/mm
  • time - hh:mm
  • Temperature
  • humidity
  • lux

Each day will have 24 entries corresponding to each hour of the day

Q: What is a simple way to maintain and read the data in a rule to update 3 items in OH based on corresponding date / time entry every hour?
I was thinking csv or xml maybe better vs mysql - but unsure of how complex the parsing script would be and which language to use in OH3

please advise …

it is possible to e.g. use exeuteCommandLine . Using this action you have the choice to use any scripting/programming language that is supported by the OS. That means you are free to use shell scripting, python, perl, c program, … To control / set values you would use the REST API from inside the script/program then.

yes , I can certainly use executeCommandLine. However, was thinking if there is a way to do this from within OH. Are jsontools or JSONPath good options ?

JSON stands for JavaScript Object Notation so JSON and JavaScript go very well together. That would probably be the simplest. But you’ll have to figure out how to get the data into OH in the first place. The simplest way to read in a file, regardless of the rules language, is in fact to use executeCommandLine with cat. That will return the contents of the file as a String which can then be parsed in the rule.

I load external data into several of my UI managed JSScripting rules. As Rich says JSON is a native format in this case so you can use require to bring in the data with no further parsing needed. Just one line that looks something like this is all you need:

var myScriptVar = require('/path/to/data/file.json');

and myScriptVar is now a full JS data object.

If your data is reasonably dynamic, then working out how to format it into a proper json file may be more hassle than it’s worth and using the suggested command line options may be easier.

3 Likes

I didn’t know require could do that. It make total sense.

Thanks for the tips. Am able to process from within OH rule with approach -

var file = executeCommandLine(Duration.ofSeconds(5), "cat", "/etc/openhab/data/parameters.json");						

val newValue = transformRaw("JSONPATH", "$.parameters.[?((@.Temp==25)&&(@.Humidity==75)].DayTime", file)

File being used for JSON is

{
"parameters":[
 {
  "DayTime": "7\/28\/23 0:00",
  "Temp": 26,
  "humidity": 85,
  "Lux": 300,
  "LightOn": 1
 },
 {
  "DayTime": "7\/28\/23 1:00",
  "Temp": 25,
  "humidity": 75,
  "Lux": 400,
  "LightOn": 1
 },
 {
  "DayTime": "7\/28\/23 2:00",
  "Temp": 24,
  "humidity": 65,
  "Lux": 500,
  "LightOn": 0
 },
 {
  "DayTime": "7\/28\/23 3:00",
  "Temp": 23,
  "humidity": 55,
  "Lux": 600,
  "LightOn": 0
 }]
}

Able to filter on simple values, struggling with the date format comparison.
Also, need to make the JSON better, currently its just an array based on export from xls

Think I have a solution now …

        var currentDateTime 	= now()
		val int nowHour 	= currentDateTime.getHour()
		val int nowDay 		= currentDateTime.getDayOfMonth()
		val int nowMonth 	= currentDateTime.getMonthValue()
		
		
		var file = executeCommandLine(Duration.ofSeconds(5), "cat", "/etc/openhab/data/parameters.json");	
		
		
		val double todayMaxTemp 		= Double.parseDouble(transformRaw("JSONPATH", "$.[?(@.Int_date=='" +nowDay +"."+ nowMonth+ "')].Max", file))
		val double todayMinTemp 		= Double.parseDouble(transformRaw("JSONPATH", "$.[?(@.Int_date=='" +nowDay +"."+ nowMonth+ "')].Min", file))



JSON file modified , changed the format slightly to make filtering easier

[
 {
  "Date": "28-Jul-23",
  "Int_date": "28.7",
  "Max": 26,
  "Min": 26,
  "humidity": 65,
  "Day": "Friday",
  "lux": 0,
  "Photo period": 0
 },
 {
  "Date": "29-Jul-23",
  "Int_date": "29.7",
  "Max": 26,
  "Min": 26,
  "humidity": 65,
  "Day": "Saturday",
  "lux": 0,
  "Photo period": 0
 }]
```