For a history, please check previous topic:
In the end I got the JSON working, values are coming through and I can display them in OpenHAB, however I need to do some data manipulation to show it in an appropriate way.
For example:
-
the JSON returns a value for the operating hours counter but it is presented in seconds, so to get hours I need to divide by 3600
-
the JSON returns a value for voltage but the decimal is positioned wrong. (29281 is 292.81 Volts)
So I thought, “well that can’t be hard”… Boy was I wrong:
rule "Process JSON from SMA Inverter"
when
Item JSON_SMA changed
then
logInfo("RULE", "JSON from SMA inverter changed!")
val jsonString = JSON_SMA.state.toString
if (jsonString != null) {
val valTester = transform("JSONPATH","$.result.0156-76BC5229.6400_00462F00.1[0].val", jsonString)
logInfo("RULE", "Test Value " + valTester)
var Number valTesterCorrected = (valTester.state as DecimalType).intValue() / 3600
logInfo("RULE", "Testwaarde correctie " + valTesterCorrected)
VSCode does not point out any errors, syntax wise everything apparently is okay?
But in the logging we’re seeing:
2018-03-19 20:57:38.084 [INFO ] [.eclipse.smarthome.model.script.RULE] - JSON from SMA inverter changed!
2018-03-19 20:57:38.089 [INFO ] [.eclipse.smarthome.model.script.RULE] - Test Value 292819
2018-03-19 20:57:38.090 [ERROR] [.script.engine.ScriptExecutionThread] - Rule ‘Process JSON from SMA Inverter’: An error occured during the script execution: The name ‘.state’ cannot be resolved to an item or type.
Without a .state it doesn’t work either by the way:
2018-03-19 21:21:48.571 [INFO ] [.eclipse.smarthome.model.script.RULE] - JSON from SMA inverter changed!
2018-03-19 21:21:48.572 [INFO ] [.eclipse.smarthome.model.script.RULE] - Test Value 292819
2018-03-19 21:21:48.573 [ERROR] [.script.engine.ScriptExecutionThread] - Rule 'Process JSON from SMA Inverter': org.eclipse.smarthome.core.library.types.DecimalType
What’s going wrong then?
A question that will come up eventually too is how to evaluate if the value extracted from a JSON with JSONPATH is null, because with this kind of “weird” things I can perfectly imagine that the vscode syntax will mark my code as perfectly valid while the rule engine chokes on it because I’m doing my checks wrong “null” vs null due to some weird string conversion…
Sorry to express my frustration here but getting something to work in a rule is never a walk in the park, weird conversions, working with .state (state??? it’s a value…), having to need to convert values while it works fine in items, there’s no consistency here… I do love OpenHAB and everything I eventually got working, works rock solid, but the hell you have to go through to GET something working is very frustrating…