Issue of the topic: I have a device in my network which I can launch a CURL command against and it will return a JSON file which I want to pull apart and show in OpenHAB. I prefer to do this with the HTTP binding.
The --data-binary option is mandatory, if I delete it, the server returns a 404 error.
The JSON is quite big and via JSON-path statements I succeeded in getting the right values (tested this with http://jsonpath.com/, it returns the value I want between square brackets
2018-03-15 13:39:36.017 [ERROR] [el.item.internal.GenericItemProvider] - Binding configuration of type 'channel' of item 'SMAJSON' could not be parsed correctly.
org.eclipse.smarthome.model.item.BindingConfigParseException: UID must have at least 4 segments.
at org.eclipse.smarthome.model.thing.internal.GenericItemChannelLinkProvider.createItemChannelLink(GenericItemChannelLinkProvider.java:82)[133:org.eclipse.smarthome.model.thing:0.9.0.b4]
at org.eclipse.smarthome.model.thing.internal.GenericItemChannelLinkProvider.processBindingConfiguration(GenericItemChannelLinkProvider.java:72)[133:org.eclipse.smarthome.model.thing:0.9.0.b4]
at org.eclipse.smarthome.model.item.internal.GenericItemProvider.internalDispatchBindings(GenericItemProvider.java:312)[123:org.eclipse.smarthome.model.item:0.9.0.b4]
at org.eclipse.smarthome.model.item.internal.GenericItemProvider.internalDispatchBindings(GenericItemProvider.java:284)[123:org.eclipse.smarthome.model.item:0.9.0.b4]
at org.eclipse.smarthome.model.item.internal.GenericItemProvider.processBindingConfigsFromModel(GenericItemProvider.java:167)[123:org.eclipse.smarthome.model.item:0.9.0.b4]
at org.eclipse.smarthome.model.item.internal.GenericItemProvider.modelChanged(GenericItemProvider.java:347)[123:org.eclipse.smarthome.model.item:0.9.0.b4]
at org.eclipse.smarthome.model.core.internal.ModelRepositoryImpl.notifyListeners(ModelRepositoryImpl.java:207)[122:org.eclipse.smarthome.model.core:0.9.0.b4]
at org.eclipse.smarthome.model.core.internal.ModelRepositoryImpl.addOrRefreshModel(ModelRepositoryImpl.java:120)[122:org.eclipse.smarthome.model.core:0.9.0.b4]
at org.eclipse.smarthome.model.core.internal.folder.FolderObserver.checkFile(FolderObserver.java:272)[122:org.eclipse.smarthome.model.core:0.9.0.b4]
at org.eclipse.smarthome.model.core.internal.folder.FolderObserver.access$1(FolderObserver.java:265)[122:org.eclipse.smarthome.model.core:0.9.0.b4]
at org.eclipse.smarthome.model.core.internal.folder.FolderObserver$WatchQueueReader.processWatchEvent(FolderObserver.java:146)[122:org.eclipse.smarthome.model.core:0.9.0.b4]
at org.eclipse.smarthome.core.service.AbstractWatchQueueReader.run(AbstractWatchQueueReader.java:122)[98:org.eclipse.smarthome.core:0.9.0.b4]
at java.lang.Thread.run(Thread.java:745)[:1.8.0_121]
Yes, that works too. But doesn’t matter because afaics (?) the regular --data option isn’t supported either in the HTTP binding, so that’s why I diverted to exec binding.
ah, just learned about this 2.0 way of using the Exec binding. You miss the Channel name in the Item. {channel="exec:command:smajson:output"} (append :output)
In this case, you can see just by looking at the way the forum did syntax highlighting that this is the problem. The command needs to be a single String.
The fact that destDev is highlighted in black instead of red like the rest of the String shows you that it isn’t being seen as a single string. Escape the double quotes and it will not treat the " as the end of the string but as part of the string.
'{\"destDev\":[]}'"
This would have been a problem in exec 1.x, HTTP, in Rules or anywhere else.
2018-03-15 15:45:04.524 [INFO ] [el.core.internal.ModelRepositoryImpl] - Loading model 'exec.things'
2018-03-15 15:45:04.543 [INFO ] [el.core.internal.ModelRepositoryImpl] - Refreshing model 'exec.things'
2018-03-15 15:45:04.557 [ERROR] [hab.binding.exec.handler.ExecHandler] - An exception occurred while waiting for the process ('curl 'http://192.168.4.129/dyn/getAllOnlValues.json?sid=yBak0jS7r0F7TjSX' --data '{"destDev":[]}'') to finish : 'null'
2018-03-15 15:45:04.558 [WARN ] [hab.binding.exec.handler.ExecHandler] - Forcibly termininating the process ('curl 'http://192.168.4.129/dyn/getAllOnlValues.json?sid=yBak0jS7r0F7TjSX' --data '{"destDev":[]}'') after a timeout of 15000 ms
After deleting the single quotes around the URL I’m getting a 404 now, which means the data option is not getting passed through or something is going wrong there.
After doing some TCP dumps I saw the difference and the solution was deleting the single qutoes around the curly brackets.
The item gets populated with the JSON now but that brings up a new question:
Can you process an item and split it in different new items without using rules? I was thinking of creating a JSONSMA-item and make “sub-items” which point to the “main” item and process that main item with JSONPATH so the sub-items get populated…?
You could use the JSONPATH transform in the thing but that gives you only one item
unfortunately you can’t do that without a rule
see:
rule "jsonpath"
when
Item SMAJSON changed
then
val jsonString = SMAJSON.state.toString
Item1.postUpdate(transform("JSONPATH",".$.path.path1", jsonString))
Item2.postUpdate(transform("JSONPATH",".$.path.path2", jsonString))
Item3.postUpdate(transform("JSONPATH",".$.path.path3", jsonString))
end
Thanks for the quick reply, it confirmed my fears I should start writing a rule again.
Continuing with this story, I implemented your rule (changed the variables names to match my naming convention)
rule "Process JSON from SMA Inverter"
when
Item JSON_SMA changed
then
logInfo("RULE", "JSON from SMA inverter changed!")
logInfo("RULE", "---------------------------------------------------------------------------------")
logInfo("RULE", "-----------------------------WRITING THE ITEM TO LOG-----------------------------")
logInfo("RULE", "---------------------------------------------------------------------------------")
logInfo("RULE", JSON_SMA.state.toString)
val jsonString = JSON_SMA.state.toString
logInfo("RULE", "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-")
logInfo("RULE", "++++++++++++++++++++++++++++WRITING THE STRING TO LOG++++++++++++++++++++++++++++")
logInfo("RULE", "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-")
logInfo("RULE", jsonString)
number_SMA_DCVermogenA.postUpdate(transform("JSONPATH","$.result.XXXX-XXXXXXXX.XXXX_XXXXXXXX.1.0.val", jsonString))
However, the logging shows:
2018-03-18 00:37:24.078 [ERROR] [.script.engine.ScriptExecutionThread] - Rule ‘Process JSON from SMA Inverter’: The argument ‘state’ must not be null or empty
So I checked if it really was empty or null, which it isn’t. I’m outputting both the item defined in the items file to the log and the jsonString val object and they both return the JSON in the log file…
If I comment out the number_SMA… transform rule, the error disappears in the logging. Additional strange thing is that I have multiple of these code-lines (vermogen means power in Dutch, so I have power,amperage,voltage etc) but I’m seeing the error only once though I would expect to see it the amount of times I have defined a statement to process a value…