I have created a script that contacts my solarlog device and returns a json formated string:
{“801”:{“170”:{“100”:“25.11.15 16:59:45”,“101”:0,“102”:20,“103”:232,“104”:138,“105”:16098,“106”:2261,“107”:291949,“108”:8751601,“109”:35466782,“110”:0,“111”:0,“112”:0,“113”:0,“114”:0,“115”:0,“116”:8640}}}
I have defined an item to retrieve value 100 (update time) of the json as follows:
String Update “Update [%s]” { exec="<[/bin/sh@@-c@@//get.sh:60000:JS(getSL100.js)]" }
transform getSL100.js has following content
JSON.parse(input).100;
However, this does not seem to be correct. What would be the correct syntax in the getSL100.js
file to retrieve the value corresponding with title “100”?
Thanks!
(function(i) {
var json = JSON.parse(i);
return json['801']['170']['100'];
})(input)
I think it will return 25.11.15 16:59:45. If you further massaged that string into 2015-11-25T16:59:45 using Javascript, you ought to be able to use it as the state of a DateTime item.
Great! Also, an alternative could be to use the JSONPATH transformation service so that you don’t need a separate .js file for each bit you want to pick out of the JSON string.
I’ve tried this but it did not work.
I installed 1.7.1 using debian apt-get and the JSONPATH jar files do not seam to be integrated or installed.
It have errors.
I looked through the source code that provides the JSONPATH transformation service and I can’t see a reason why the code would not be available to you. If you could provide more specific details about the errors you received, I could give you my opinion of what might make this work correctly.
2015-11-27 12:47:24.582 [WARN ] [transform.TransformationHelper] - Cannot get service reference for transformation service of type JASONPATH
2015-11-27 12:47:24.584 [WARN ] [.o.b.exec.internal.ExecBinding] - couldn’t transform response because transformationService of type ‘JASONPATH’ is unavailable
the item is defined as
String SolarLog_Test “Updated [%s]” { exec="<[/bin/sh@@-c@@//etc/openhab/configurations/solarget.sh:60000:JASONPATH($.801.170.100)]" }
which proved to be correct in an online JSONPATH validator