Rules: String concatenation

Hello

I have a problem with string concatenation in a for loop.

for .....{
     .....
      ScheduleTadoJSON=ScheduleTadoJSON+("{\"dayType\":\""+DayName+"\",\"start\":\""+ScheduleStart+"\",\"end\":\""+ScheduleEnd+"\",\"geolocationOverride\":false,\"setting\":{\"type\":\"HEATING\",\"power\":\"ON\",\"temperature\":{\"celsius\":"+ScheduleTemp+"}}}"+JSONLineEnd)
    .....
}

2018-10-21 11:24:40.840 [ERROR] [ntime.internal.engine.ExecuteRuleJob] - Error during the execution of rule ‘test’: Unknown variable or command ‘+’; line 46, column 20, length 251

I also tried using the += operator :

2018-10-21 11:27:55.039 [ERROR] [ntime.internal.engine.ExecuteRuleJob] - Error during the execution of rule ‘test’: Unknown variable or command ‘+=’; line 46, column 3, length 250

Here is the context :
I am currently making rules to manage my Tado thermostat scheduling based on the type of day (Home, Homeworking, Work…).
The idea in the following script is to test the ability of transforming the scheduling in a user friendly format (ScheduleLightJSON) into the Tado API format.

rule "test"
when
	Time cron   "0/5 1 1 1/1 * ? *"
then
	var string ScheduleStart
	var string ScheduleEnd
	var string  ScheduleTemp
	var string JSONLineEnd
	var string ScheduleTadoJSON="["
	var DayName = ""
		switch now.getDayOfWeek{
        case 1: DayName = "MONDAY"
        case 2: DayName = "TUESDAY"
        case 3: DayName= "WEDNESDAY"
        case 4: DayName= "THURSDAY"
        case 5: DayName= "FRIDAY"
        case 6: DayName= "SATURDAY"
        case 7: DayName= "SUNDAY"
    }
		
	var string ScheduleLightJSON='[
	{"start":"00:00","temperature":17},
	{"start":"07:00","temperature":20},
	{"start":"23:00","temperature":17}]'

       //Browse ScheduleLightJSON in order to generate the ScheduleTadoJSON that will be sent to the Tado API
	for(var i=0; transform("JSONPATH","$.["+i+"].start",ScheduleLightJSON) != ScheduleLightJSON; i++) {
		ScheduleStart=(transform("JSONPATH","$.["+i+"].start",ScheduleLightJSON))
		ScheduleTemp=(transform("JSONPATH","$.["+i+"].temperature",ScheduleLightJSON))
		//Is it the last Schedule block?
		if(transform("JSONPATH","$.["+(i+1)+"].start",ScheduleLightJSON)!=ScheduleLightJSON){
			ScheduleEnd=(transform("JSONPATH","$.["+(i+1)+"].start",ScheduleLightJSON))
			JSONLineEnd=','
		}else{
			ScheduleEnd="00:00"
			JSONLineEnd=']'
	
		}
		ScheduleTadoJSON=ScheduleTadoJSON+("{\"dayType\":\""+DayName+"\",\"start\":\""+ScheduleStart+"\",\"end\":\""+ScheduleEnd+"\",\"geolocationOverride\":false,\"setting\":{\"type\":\"HEATING\",\"power\":\"ON\",\"temperature\":{\"celsius\":"+ScheduleTemp+"}}}"+JSONLineEnd)
	}
	logInfo("myLog", "JSONtr:"+ScheduleTadoJSON)	
end

I’m not sure what the issue is myself, but having had a number of similar problems over and over, I’ve started using Python for scripting - it’s still a bit rough and leading edge, but I think it would make this a whole lot easier (for example, you can “import json” and have a library for working with JSON easily). There is some further info here about it: https://github.com/OH-Jython-Scripters/openhab2-jython

I think the problem is the ( after the first +. Also don’t forget to remove the closing parenthesis.

Thank you @sillyfrog .
I will give it a try.

@pacive
I tried without the ‘(’, same problem

In fact I even tried these :

ScheduleTadoJSON=ScheduleTadoJSON +  "TEST"
ScheduleTadoJSON=ScheduleTadoJSON+"TEST"

got the same error messages :

2018-10-21 13:03:35.027 [ERROR] [ntime.internal.engine.ExecuteRuleJob] - Error during the execution of rule ‘test’: Unknown variable or command ‘+’; line 46, column 20, length 26

Back to basics; try the simple test outside of your for-loop.

@rossko57, same error :wink:

Maybe this works

ScheduleTadoJSON.append("TEST ...

Sadly not

ScheduleTadoJSON.append("Result")

Error during the execution of rule ‘test’: ‘append’ is not a member of ‘java.lang.String’; line 48, column 2, length 33

You are declaring a string literal, as opposed to a String object, try capitalizing the type declaration:

	var String ScheduleStart
	var String ScheduleEnd
	var String  ScheduleTemp
	var String JSONLineEnd
	var String ScheduleTadoJSON="["

My bad it would be concat not append.

@pacive
Thank you for the solution :wink:

 var String ScheduleTadoJSON="["

I can even use the “+= operator” :

ScheduleTadoJSON+="{\"dayType\":\""+DayName+"\",\"start\":\""+ScheduleStart+"\",\"end\":\""+ScheduleEnd+"\",\"geolocationOverride\":false,\"setting\":{\"type\":\"HEATING\",\"power\":\"ON\",\"temperature\":{\"celsius\":"+ScheduleTemp+"}}}"+JSONLineEnd

Here is the result :

2018-10-21 14:03:15.100 [INFO ] [eclipse.smarthome.model.script.myLog] - JSONtr:[{“dayType”:“SUNDAY”,“start”:“00:00”,“end”:“07:00”,“geolocationOverride”:false,“setting”:{“type”:“HEATING”,“power”:“ON”,“temperature”:{“celsius”:17}}},{“dayType”:“SUNDAY”,“start”:“07:00”,“end”:“23:00”,“geolocationOverride”:false,“setting”:{“type”:“HEATING”,“power”:“ON”,“temperature”:{“celsius”:20}}},{“dayType”:“SUNDAY”,“start”:“23:00”,“end”:“00:00”,“geolocationOverride”:false,“setting”:{“type”:“HEATING”,“power”:“ON”,“temperature”:{“celsius”:17}}}]

@Josar

ScheduleTadoJSON.concat("{\"dayType\":\""+DayName+"\",\"start\":\""+ScheduleStart+"\",\"end\":\""+ScheduleEnd+"\",\"geolocationOverride\":false,\"setting\":{\"type\":\"HEATING\",\"power\":\"ON\",\"temperature\":{\"celsius\":"+ScheduleTemp+"}}}"+JSONLineEnd)

is accepted with the String declaration. However it doesn’t give the expected result

2018-10-21 14:39:05.116 [INFO ] [eclipse.smarthome.model.script.myLog] - JSONtr:[