Error in rule with transform JSONPATH

Could someone please check the following code please. I got an Error with the transform and cannot decide if it is a bug or an error in front of the screen :slight_smile:

		val Station01_ID="3d8c2dfb-4612-4aff-8ae7-96e3f88eafe6" //Nordoel Uetersen
		val Station02_ID="1ceea95b-d46e-4159-91f6-71f57d612bdb"	// Nordoel Elmshorn
		val Station03_ID="e1a15081-24e2-9107-e040-0b0a3dfe563c"	// HEM Elmshorn
		val Station04_ID="005056ba-7cb6-1ed2-bceb-bc577a5e6d4e"	// Star Elmshorn
		val Station05_ID="e1a15081-254f-9107-e040-0b0a3dfe563c"	// HEM Wedel
		val API_Key="xxxxxx"
		
		var String URL
		var String JSON
		var String Diesel
		var ArrayList<StringItem> FuelPriceItems =  new ArrayList<StringItem>()
		var Map <String, Double> FuelPricesMap = new HashMap<String, Double>()
		var i = 0
		
		// First read the fule prices for defines stations
		URL= "https://creativecommons.tankerkoenig.de/json/prices.php?ids=" + Station01_ID + "," + Station02_ID + "," + Station03_ID  + "," + Station04_ID  + "," + Station05_ID + "&apikey=" + API_Key
		logInfo ("Benzinpreise", "URL = {}", URL)
    	JSON = sendHttpGetRequest(URL)
		logInfo ("Response", "JSON = {}", JSON)

		// Assign display items to array
		FuelPriceItems.add(0, OhVl_FuelStation1_Value)
		FuelPriceItems.add(1, OhVl_FuelStation2_Value)
		FuelPriceItems.add(2, OhVl_FuelStation3_Value)
		FuelPriceItems.add(3, OhVl_FuelStation4_Value)
		FuelPriceItems.add(4, OhVl_FuelStation5_Value)

 		// Assign actual prices and station name to map	
 		Diesel = transform("JSONPATH",  "$.prices." + Station01_ID + ".diesel", JSON)
		FuelPricesMap.put("Nordoel (Uetersen)",	(Double.parseDouble(Diesel)))
 		Diesel = transform("JSONPATH",  "$.prices." + Station02_ID + ".diesel", JSON)
		FuelPricesMap.put("Nordoel (Elmshorn)",	(Double.parseDouble(Diesel)))
 		Diesel = transform("JSONPATH",  "$.prices." + Station03_ID + ".diesel", JSON)
		FuelPricesMap.put("HEM (Elmshorn)",		(Double.parseDouble(Diesel)))
 		Diesel = transform("JSONPATH",  "$.prices." + Station04_ID + ".diesel", JSON)
		FuelPricesMap.put("Star (Elmshorn)",	(Double.parseDouble(Diesel)))
 		Diesel = transform("JSONPATH",  "$.prices." + Station05_ID + ".diesel", JSON)
		FuelPricesMap.put("HEM (Wedel)",		(Double.parseDouble(Diesel)))

The Error ist the following:

2016-06-29 23:15:02.700 [ERROR] [ntime.internal.engine.ExecuteRuleJob] - Error during the execution of rule New fuel price display
java.lang.RuntimeException: The name 'transform(<XStringLiteralImpl>,<XBinaryOperationImplCustom>,<XFeatureCallImplCustom>)' cannot be resolved to an item or type.
        at org.eclipse.smarthome.model.script.interpreter.ScriptInterpreter.invokeFeature(ScriptInterpreter.java:112)[122:org.eclipse.smarthome.model.script:0.9.0.201606280608]
        at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter._doEvaluate(XbaseInterpreter.java:901)[138:org.eclipse.xtext.xbase:2.9.2.v20160428-1452]
        at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter._doEvaluate(XbaseInterpreter.java:864)[138:org.eclipse.xtext.xbase:2.9.2.v20160428-1452]
        at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter.doEvaluate(XbaseInterpreter.java:223)[138:org.eclipse.xtext.xbase:2.9.2.v20160428-1452]
        at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter.internalEvaluate(XbaseInterpreter.java:203)[138:org.eclipse.xtext.xbase:2.9.2.v20160428-1452]
        at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter._doEvaluate(XbaseInterpreter.java:1211)[138:org.eclipse.xtext.xbase:2.9.2.v20160428-1452]
        at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter.doEvaluate(XbaseInterpreter.java:215)[138:org.eclipse.xtext.xbase:2.9.2.v20160428-1452]
        at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter.internalEvaluate(XbaseInterpreter.java:203)[138:org.eclipse.xtext.xbase:2.9.2.v20160428-1452]
        at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter._doEvaluate(XbaseInterpreter.java:446)[138:org.eclipse.xtext.xbase:2.9.2.v20160428-1452]
        at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter.doEvaluate(XbaseInterpreter.java:227)[138:org.eclipse.xtext.xbase:2.9.2.v20160428-1452]
        at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter.internalEvaluate(XbaseInterpreter.java:203)[138:org.eclipse.xtext.xbase:2.9.2.v20160428-1452]
        at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter._doEvaluate(XbaseInterpreter.java:459)[138:org.eclipse.xtext.xbase:2.9.2.v20160428-1452]
        at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter.doEvaluate(XbaseInterpreter.java:243)[138:org.eclipse.xtext.xbase:2.9.2.v20160428-1452]
        at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter.internalEvaluate(XbaseInterpreter.java:203)[138:org.eclipse.xtext.xbase:2.9.2.v20160428-1452]
        at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter._doEvaluate(XbaseInterpreter.java:446)[138:org.eclipse.xtext.xbase:2.9.2.v20160428-1452]
        at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter.doEvaluate(XbaseInterpreter.java:227)[138:org.eclipse.xtext.xbase:2.9.2.v20160428-1452]
        at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter.internalEvaluate(XbaseInterpreter.java:203)[138:org.eclipse.xtext.xbase:2.9.2.v20160428-1452]
        at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter.evaluate(XbaseInterpreter.java:189)[138:org.eclipse.xtext.xbase:2.9.2.v20160428-1452]
        at org.eclipse.smarthome.model.script.runtime.internal.engine.ScriptImpl.execute(ScriptImpl.java:77)[123:org.eclipse.smarthome.model.script.runtime:0.9.0.201606280608]
        at org.eclipse.smarthome.model.rule.runtime.internal.engine.ExecuteRuleJob.execute(ExecuteRuleJob.java:60)[121:org.eclipse.smarthome.model.rule.runtime:0.9.0.201606280608]
        at org.quartz.core.JobRunShell.run(JobRunShell.java:202)[101:org.eclipse.smarthome.core.scheduler:0.9.0.201606280608]
        at org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:573)[101:org.eclipse.smarthome.core.scheduler:0.9.0.201606280608]
201

Thomas

The error implies a missing import. I’ve no experience with transforms within rules nor the ins and outs of OH 2 yet so don’t see anything else obviously wrong.

@rlkoshak Thanks for you view. I will check the imports. My problem is that i currently have no idea what i am doiing (sort of)

Thomas

You copied nearly my complete rule as posted, xmt the first line!

import org.openhab.core.library.types.*

Sorry for giving you that hint in the other thread, I was relating your question to the OH setup not the rule itself.

Added it, did not work :frowning:
Next step is moving home and search a solution. Maybe i should first fild out which imports are done automatically.

Thanks
thomas

The problem is not with the import. If I do

		logError("TEST", transform("MAP",  "de.map", "CLOSED"))

it all works as expected without any additional import.

This means that you are trying to call the method with something different than 3 string parameters.
To analyze what might be wrong, maybe better prepare your strings outside of the method call and then just pass 3 string variables. Maybe this helps identifying your problem.

Looked at it again,you’re declaring the IDs and the API KEY as “val”.
Don’t know what would come of that, I declared them as strings!

  • var - denotes a variable that can be reassigned
  • val - denotes a variable that cannot be reassigned

When creating a variable you always must use one or the other.

Next is the optional indication of what type of variable it is. This is optional because much of the time the Rule’s DSL can figure it out on its own. So this

val String text = "foo"

is equivalent to

val text = “foo”

Therefore, in this case @Dibbler42’s API_Key and Station IDs should all be Strings. It might make sense to add in the val String to make sure but it shouldn’t be necessary. But it isn’t working as is so …

Try adding the String to force the Rule’s DSL to make the IDs and API a String. Then make sure that you are getting a String back in your sendHttpGetRequest(URL) call. You do not provide whether and what the logInfo prints right after that. But if JSON is not being set to a String (e.g. null) that could account for the error.

1 Like

@rlkoshak Thank you for the coding lessons. I had a lot of them in the last days, could you suggest any site from which I could get some more insigths without having to rely on you?

@Dibbler42
I just copied your code into a test-rule on my systen, using my Api-Key (you should delete yours in the post!) and omitting the chapter “Assign display items to array”.
I get past the transform without an error!

For specifics on the basics of the language see the Xtend Documentation. For how to structure your rules and specifics about the Rule’s DSL see the Rule’s Wiki page.

Also look at the examples on the wiki and posted to this forum. I personally learned the most from these examples than I did from the other documents.

1 Like

@rlkoshak

Thanks for the Link to Xtend docu.
Yes, the examples give some good hints. However, the docu will help to learn the basic syntax differences compared to other languages ( like “val” instead of “const”) …
Personally, I do a lot of learning by reading in such a forum and trying to help others. Done that for more then a decade in another language.

Currently i have no internet (Vodafone broke it). But i will check for all the hints an come back later

Thomas

Ok guys, i do not get these json tranformation up an running.
Reduced it to the minimum.
Used the exampleon the wiki
Restarted openHAB
Installed the latest snapshot
Checked in Karaf if bundle is running

The rule is now like that

rule "New fuel price display"
when
	Time cron "0 0/5 * * * ?"
then
	
		var String test
		
		test = transform("JSONPATH","$.store.book[0].author", '{"store":{"book":[{"category":"reference","author":"Nigel Rees","title": "Sayings of the Century", "price": 8.95  } ],  "bicycle": { "color": "red",  "price": 19.95} }}')
		logInfo("Transform Test","{}", test)
end

and what do i get

java.lang.RuntimeException: The name 'transform(<XStringLiteralImpl>,<XStringLiteralImpl>,<XStringLiteralImpl>)' cannot be resolved to an item or type.

Any ideas? Should i sell my car?

Is my example with the MAP transform from above working for you?

Which transformations do you have installed?
…and for your car: You can keep your VW😉

That should not matter at all - the “transform” method must be there in any case and if you are missing a certain addon, this will be complained about only later (within the transform method, but you are not even getting there).

@Kai Unfortunately the MAP Transformation is not working. Got the same error

@opus


:slight_smile: ok i will keep it

Thomas

@Dibbler42 Don’t take my answers for granted, especially if @Kai is answering as well.

I’m a newbie in here, just trying to help. And I 'm out of ideas.

@opus Any help is apprciated. compared to Kai we are all newbies

Ok, I have to stand corrected. I only tested it from within the IDE and everything works there.
In the distro, it is currently broken due to this issue coming back.