[SOLVED] Use jsonPath in JavaScript transformation?

Is it possible to use jsonPath in a JavaScript transformation rule?

That’s what I tried:

transform/myTransform.js

(function(dataString) {
    var data = JSON.parse(dataString);
    var switchState = jsonPath(data, "$[?(@.id == '42')].value").toJSONString();
    if(switchState) return "ON";
    if(!switchState) return "OFF";
    return reply;
})(input)

Result - an exception:

logs/openhab.log
org.openhab.core.transform.TransformationException: An error occurred while executing script.
	at org.openhab.core.transform.TransformationHelper$TransformationServiceDelegate.transform(TransformationHelper.java:67) [210:org.openhab.core.compat1x:2.3.0]
	at org.openhab.binding.http.internal.HttpBinding.execute(HttpBinding.java:194) [208:org.openhab.binding.http:1.12.0]
	at org.openhab.core.binding.AbstractActiveBinding$BindingActiveService.execute(AbstractActiveBinding.java:144) [210:org.openhab.core.compat1x:2.3.0]
	at org.openhab.core.service.AbstractActiveService$RefreshThread.run(AbstractActiveService.java:166) [210:org.openhab.core.compat1x:2.3.0]

2018-12-18 16:08:19.518 [WARN ] [ab.binding.http.internal.HttpBinding] - Transformation 'JS(myTransform.js)' threw an exception.

I couldn’t find any example here - so is it possible or am I searching for something that doesn’t work anyways?

Can you post the JSON please so we can debug. Thanks

Sure :slight_smile:
I have a Item configured with a similar jsonPath, this works:

Number ALL4176_Testvalue "Testvalue [%s]" { http="<[ALL4176_StatusCache:10000:JSONPATH($[?(@.id == '19')].value)]" }

I got the exception above with id == ‘6’, replace 42 with 6 in above example.

Here’s my JSON:

[{"id":"7","name":"Kontakt Eingang 1","description":null,"fe_view":"0","sort":"1:1","fe_cvs_show_typ":"1","actor_analogValue":null,"digitalToText":"0::","tileColors":"1e7eac:990000:990000","tileFormats":"55:","lang_port_identifier":"_999066_ 1","fading":null,"device_type":"4","value":"0.00","error":0,"config":{"icon":null,"display":{"min":"0","max":"0"},"limit":{"min":"0","max":"0"}},"info":{"activ":"1","enabled":"1","unit":"","type":"1","view":"0","chipid":"100678","chipnumber":"6","chipaddress":"1","helperchipnumber":"0","helperchipaddress":"0","bitaddress":"1"},"minmax":{"today":{"min":"0.00","max":"0.00"},"absolute":{"min":"0.00","max":"0.00"}},"connection":{"port":"0","bus":"90","group":"0"}},{"id":"8","name":"Kontakt Eingang 2","description":null,"fe_view":"0","sort":"1:1","fe_cvs_show_typ":"1","actor_analogValue":null,"digitalToText":"0::","tileColors":"1e7eac:990000:990000","tileFormats":"55:","lang_port_identifier":"_999066_ 2","fading":null,"device_type":"4","value":"0.00","error":0,"config":{"icon":null,"display":{"min":"0","max":"0"},"limit":{"min":"0","max":"0"}},"info":{"activ":"1","enabled":"1","unit":"","type":"1","view":"0","chipid":"100678","chipnumber":"6","chipaddress":"1","helperchipnumber":"0","helperchipaddress":"0","bitaddress":"2"},"minmax":{"today":{"min":"0.00","max":"0.00"},"absolute":{"min":"0.00","max":"0.00"}},"connection":{"port":"0","bus":"90","group":"0"}},{"id":"9","name":"Kontakt Eingang 3","description":null,"fe_view":"0","sort":"1:1","fe_cvs_show_typ":"1","actor_analogValue":null,"digitalToText":"0::","tileColors":"1e7eac:990000:990000","tileFormats":"55:","lang_port_identifier":"_999066_ 3","fading":null,"device_type":"4","value":"0.00","error":0,"config":{"icon":null,"display":{"min":"0","max":"0"},"limit":{"min":"0","max":"0"}},"info":{"activ":"1","enabled":"1","unit":"","type":"1","view":"0","chipid":"100678","chipnumber":"6","chipaddress":"1","helperchipnumber":"0","helperchipaddress":"0","bitaddress":"3"},"minmax":{"today":{"min":"0.00","max":"0.00"},"absolute":{"min":"0.00","max":"0.00"}},"connection":{"port":"0","bus":"90","group":"0"}},{"id":"10","name":"Kontakt Eingang 4","description":null,"fe_view":"0","sort":"1:1","fe_cvs_show_typ":"1","actor_analogValue":null,"digitalToText":"0::","tileColors":"1e7eac:990000:990000","tileFormats":"55:","lang_port_identifier":"_999066_ 4","fading":null,"device_type":"4","value":"0.00","error":0,"config":{"icon":null,"display":{"min":"0","max":"0"},"limit":{"min":"0","max":"0"}},"info":{"activ":"1","enabled":"1","unit":"","type":"1","view":"0","chipid":"100678","chipnumber":"6","chipaddress":"1","helperchipnumber":"0","helperchipaddress":"0","bitaddress":"4"},"minmax":{"today":{"min":"0.00","max":"0.00"},"absolute":{"min":"0.00","max":"0.00"}},"connection":{"port":"0","bus":"90","group":"0"}},{"id":"11","name":"Kontakt Eingang 5","description":null,"fe_view":"0","sort":"1:1","fe_cvs_show_typ":"1","actor_analogValue":null,"digitalToText":"0::","tileColors":"1e7eac:990000:990000","tileFormats":"55:","lang_port_identifier":"_999066_ 5","fading":null,"device_type":"4","value":"0.00","error":0,"config":{"icon":null,"display":{"min":"0","max":"0"},"limit":{"min":"0","max":"0"}},"info":{"activ":"1","enabled":"1","unit":"","type":"1","view":"0","chipid":"100678","chipnumber":"6","chipaddress":"1","helperchipnumber":"0","helperchipaddress":"0","bitaddress":"5"},"minmax":{"today":{"min":"0.00","max":"0.00"},"absolute":{"min":"0.00","max":"0.00"}},"connection":{"port":"0","bus":"90","group":"0"}},{"id":"12","name":"Kontakt Eingang 6","description":null,"fe_view":"0","sort":"1:1","fe_cvs_show_typ":"1","actor_analogValue":null,"digitalToText":"0::","tileColors":"1e7eac:990000:990000","tileFormats":"55:","lang_port_identifier":"_999066_ 6","fading":null,"device_type":"4","value":"0.00","error":0,"config":{"icon":null,"display":{"min":"0","max":"0"},"limit":{"min":"0","max":"0"}},"info":{"activ":"1","enabled":"1","unit":"","type":"1","view":"0","chipid":"100678","chipnumber":"6","chipaddress":"1","helperchipnumber":"0","helperchipaddress":"0","bitaddress":"6"},"minmax":{"today":{"min":"0.00","max":"0.00"},"absolute":{"min":"0.00","max":"0.00"}},"connection":{"port":"0","bus":"90","group":"0"}},{"id":"13","name":"USB TYP-B \u00dcberwachung","description":null,"fe_view":"0","sort":"2:1","fe_cvs_show_typ":"1","actor_analogValue":null,"digitalToText":"0::","tileColors":"1e7eac:990000:990000","tileFormats":"55:","lang_port_identifier":"_999077_","fading":null,"device_type":"4","value":"0.00","error":0,"config":{"icon":null,"display":{"min":"0","max":"0"},"limit":{"min":"0","max":"0"}},"info":{"activ":"1","enabled":"1","unit":"","type":"1","view":"0","chipid":"3997","chipnumber":"-1","chipaddress":"0","helperchipnumber":"37","helperchipaddress":"0","bitaddress":"0"},"minmax":{"today":{"min":"0.00","max":"0.00"},"absolute":{"min":"0.00","max":"0.00"}},"connection":{"port":"0","bus":"0","group":"0"}},{"id":"14","name":"Wechselspannung","description":null,"fe_view":"2","sort":"2:1","fe_cvs_show_typ":"1","actor_analogValue":null,"digitalToText":"0::","tileColors":"1e7eac:990000:990000","tileFormats":"55:","lang_port_identifier":"_820504_","fading":null,"device_type":"0","value":"228.56","error":0,"config":{"icon":null,"display":{"min":"210","max":"250"},"limit":{"min":"220","max":"240"}},"info":{"activ":"1","enabled":"1","unit":"V","type":"1","view":"2","chipid":"0","chipnumber":"0","chipaddress":"","helperchipnumber":"0","helperchipaddress":"0","bitaddress":"0"},"minmax":{"today":{"min":"224.35","max":"232.96"},"absolute":{"min":"222.91","max":"240.75"}},"connection":{"port":"0","bus":"0","group":"0"}},{"id":"15","name":"Wechselstrom","description":"","fe_view":"2","sort":"2:1","fe_cvs_show_typ":"1","actor_analogValue":null,"digitalToText":"0::","tileColors":"1e7eac:990000:990000","tileFormats":"55:","lang_port_identifier":"_820506_","fading":null,"device_type":"0","value":"0.15","error":0,"config":{"icon":null,"display":{"min":"-0.1","max":"1"},"limit":{"min":"-0.05","max":"16"}},"info":{"activ":"1","enabled":"1","unit":"A","type":"1","view":"2","chipid":"0","chipnumber":"0","chipaddress":"","helperchipnumber":"0","helperchipaddress":"0","bitaddress":"0"},"minmax":{"today":{"min":"-0.00","max":"0.18"},"absolute":{"min":"-0.01","max":"7.55"}},"connection":{"port":"0","bus":"0","group":"0"}},{"id":"16","name":"Leistung","description":null,"fe_view":"1","sort":"2:2","fe_cvs_show_typ":"2","actor_analogValue":null,"digitalToText":"0::","tileColors":"1e7eac:990000:990000","tileFormats":"55:","lang_port_identifier":"_820532_","fading":null,"device_type":"0","value":"35.0","error":0,"config":{"icon":null,"display":{"min":"-10","max":"3800"},"limit":{"min":"-1","max":"3500"}},"info":{"activ":"1","enabled":"1","unit":"W","type":"1","view":"1","chipid":"0","chipnumber":"0","chipaddress":"","helperchipnumber":"0","helperchipaddress":"0","bitaddress":"0"},"minmax":{"today":{"min":"-0.20","max":"42.80"},"absolute":{"min":"-0.20","max":"1738.30"}},"connection":{"port":"0","bus":"0","group":"0"}},{"id":"17","name":"Leistungsfaktor","description":null,"fe_view":"2","sort":"2:1","fe_cvs_show_typ":"1","actor_analogValue":null,"digitalToText":"0::","tileColors":"1e7eac:990000:990000","tileFormats":"55:","lang_port_identifier":"_820528_","fading":null,"device_type":"0","value":"1.00","error":0,"config":{"icon":null,"display":{"min":"0","max":"1"},"limit":{"min":"0","max":"1"}},"info":{"activ":"1","enabled":"1","unit":"cos \u03c6","type":"1","view":"2","chipid":"0","chipnumber":"0","chipaddress":"","helperchipnumber":"0","helperchipaddress":"0","bitaddress":"0"},"minmax":{"today":{"min":"0.00","max":"1.00"},"absolute":{"min":"0.00","max":"1.00"}},"connection":{"port":"0","bus":"0","group":"0"}},{"id":"18","name":"Frequenz","description":null,"fe_view":"2","sort":"2:1","fe_cvs_show_typ":"1","actor_analogValue":null,"digitalToText":"0::","tileColors":"1e7eac:990000:990000","tileFormats":"55:","lang_port_identifier":"_820529_","fading":null,"device_type":"0","value":"49.53","error":0,"config":{"icon":null,"display":{"min":"48","max":"52"},"limit":{"min":"49","max":"51"}},"info":{"activ":"1","enabled":"1","unit":"Hz","type":"1","view":"2","chipid":"0","chipnumber":"0","chipaddress":"","helperchipnumber":"0","helperchipaddress":"0","bitaddress":"0"},"minmax":{"today":{"min":"49.49","max":"49.68"},"absolute":{"min":"49.37","max":"50.54"}},"connection":{"port":"0","bus":"0","group":"0"}},{"id":"19","name":"Intern","description":null,"fe_view":"0","sort":"2:1","fe_cvs_show_typ":"1","actor_analogValue":null,"digitalToText":"0::","tileColors":"1e7eac:990000:990000","tileFormats":"55:","lang_port_identifier":"_002014_","fading":null,"device_type":"0","value":"15.96","error":0,"config":{"icon":null,"display":{"min":"46","max":"54"},"limit":{"min":"48","max":"52"}},"info":{"activ":"1","enabled":"1","unit":"\u00b0C","type":"1","view":"0","chipid":"0","chipnumber":"0","chipaddress":"0","helperchipnumber":"0","helperchipaddress":"0","bitaddress":"0"},"minmax":{"today":{"min":"6.64","max":"16.31"},"absolute":{"min":"0.00","max":"255.98"}},"connection":{"port":"0","bus":"0","group":"0"}},{"id":"1","name":"Schaltrelais rot","description":"","fe_view":"50","sort":"1:1","fe_cvs_show_typ":"1","actor_analogValue":null,"digitalToText":"0::","tileColors":"1e7eac:990000:","tileFormats":"55:","lang_port_identifier":"_999064_ 1","fading":null,"device_type":"10","value":0,"error":0,"config":{"icon":null,"display":{"min":"0","max":"0"},"limit":{"min":"0","max":"0"}},"info":{"activ":"1","enabled":"1","unit":"","type":"2","view":"50","chipid":"100668","chipnumber":"6","chipaddress":"0","helperchipnumber":"0","helperchipaddress":"0","bitaddress":"1"},"minmax":{"today":{"min":"20490000","max":"-20490000"},"absolute":{"min":"20490000","max":"-20490000"}},"connection":{"port":"0","bus":"90","group":"0"}},{"id":"2","name":"Schaltrelais gr\u00fcn","description":"","fe_view":"50","sort":"1:1","fe_cvs_show_typ":"1","actor_analogValue":null,"digitalToText":"0::","tileColors":"1e7eac:990000:","tileFormats":"55:","lang_port_identifier":"_999064_ 2","fading":null,"device_type":"10","value":"1","error":0,"config":{"icon":null,"display":{"min":"0","max":"0"},"limit":{"min":"0","max":"0"}},"info":{"activ":"1","enabled":"1","unit":"","type":"2","view":"50","chipid":"100668","chipnumber":"6","chipaddress":"0","helperchipnumber":"0","helperchipaddress":"0","bitaddress":"2"},"minmax":{"today":{"min":"20490000","max":"-20490000"},"absolute":{"min":"20490000","max":"-20490000"}},"connection":{"port":"0","bus":"90","group":"0"}},{"id":"3","name":"Schaltrelais 3","description":null,"fe_view":"50","sort":"1:1","fe_cvs_show_typ":"1","actor_analogValue":null,"digitalToText":"0::","tileColors":"1e7eac:990000:990000","tileFormats":"55:","lang_port_identifier":"_999064_ 3","fading":null,"device_type":"10","value":0,"error":0,"config":{"icon":null,"display":{"min":"0","max":"0"},"limit":{"min":"0","max":"0"}},"info":{"activ":"1","enabled":"1","unit":"","type":"2","view":"50","chipid":"100668","chipnumber":"6","chipaddress":"0","helperchipnumber":"0","helperchipaddress":"0","bitaddress":"3"},"minmax":{"today":{"min":"20490000","max":"-20490000"},"absolute":{"min":"20490000","max":"-20490000"}},"connection":{"port":"0","bus":"90","group":"0"}},{"id":"4","name":"Schaltrelais 4","description":null,"fe_view":"50","sort":"1:1","fe_cvs_show_typ":"1","actor_analogValue":null,"digitalToText":"0::","tileColors":"1e7eac:990000:990000","tileFormats":"55:","lang_port_identifier":"_999064_ 4","fading":null,"device_type":"10","value":0,"error":0,"config":{"icon":null,"display":{"min":"0","max":"0"},"limit":{"min":"0","max":"0"}},"info":{"activ":"1","enabled":"1","unit":"","type":"2","view":"50","chipid":"100668","chipnumber":"6","chipaddress":"0","helperchipnumber":"0","helperchipaddress":"0","bitaddress":"4"},"minmax":{"today":{"min":"20490000","max":"-20490000"},"absolute":{"min":"20490000","max":"-20490000"}},"connection":{"port":"0","bus":"90","group":"0"}},{"id":"5","name":"Schaltrelais 5","description":null,"fe_view":"50","sort":"1:1","fe_cvs_show_typ":"1","actor_analogValue":null,"digitalToText":"0::","tileColors":"1e7eac:990000:990000","tileFormats":"55:","lang_port_identifier":"_999064_ 5","fading":null,"device_type":"10","value":0,"error":0,"config":{"icon":null,"display":{"min":"0","max":"0"},"limit":{"min":"0","max":"0"}},"info":{"activ":"1","enabled":"1","unit":"","type":"2","view":"50","chipid":"100668","chipnumber":"6","chipaddress":"0","helperchipnumber":"0","helperchipaddress":"0","bitaddress":"5"},"minmax":{"today":{"min":"20490000","max":"-20490000"},"absolute":{"min":"20490000","max":"-20490000"}},"connection":{"port":"0","bus":"90","group":"0"}},{"id":"6","name":"Schaltrelais 6","description":null,"fe_view":"50","sort":"1:1","fe_cvs_show_typ":"1","actor_analogValue":null,"digitalToText":"0::","tileColors":"1e7eac:990000:990000","tileFormats":"55:","lang_port_identifier":"_999064_ 6","fading":null,"device_type":"10","value":0,"error":0,"config":{"icon":null,"display":{"min":"0","max":"0"},"limit":{"min":"0","max":"0"}},"info":{"activ":"1","enabled":"1","unit":"","type":"2","view":"50","chipid":"100668","chipnumber":"6","chipaddress":"0","helperchipnumber":"0","helperchipaddress":"0","bitaddress":"6"},"minmax":{"today":{"min":"20490000","max":"-20490000"},"absolute":{"min":"20490000","max":"-20490000"}},"connection":{"port":"0","bus":"90","group":"0"}}]

@vzorglub Did debugging get some news on this?

There is no ID 42 in the JSON

Exactly. That’s why I wrote:

You can use any other existing ID, I tried it with more than just 6, I also used 19 as in the working item example above.

OK, I get on it.
You want the transform to return ON when value is != 0
Correct?

Thanks!
Exactly :slight_smile:

Every part of the transformation works - JSON path in Item (see above), transformation in JavaScript (see [SOLVED] How to do double transformation for HTTP sensors?)
Just putting that together in a JavaScript doesn’t work.

Try this one:

(function(dataString) {
    var data = JSON.parse(dataString);
    var i;
    var value = 'UNDEF';
    var switchState = '';
    for (i = 0; i < data.length; i++) {
        if (data[i].id == '6') {
            value = data[i].value;
        }
    }
    if (value != 'UNDEF') {
        if (value == '0.00') {
            value = 'OFF';
        } else {
            value = 'ON';
        }
    }
    switchState = value;
    return switchState;
})(input)

Tested for:
id=6 - OFF
id=14 - ON
id=42 - UNDEF

1 Like

So the answer is: jsonPath isn’t possible there :slight_smile:

Thanks for writing the code, it works for me too!

I stripped it down a little bit, this is the version I now use:

(function(dataString) {
    var data = JSON.parse(dataString);
    var value = 'UNDEF';
    for (var i = 0; i < data.length; i++) {
        if (data[i].id == '6') {
            value = data[i].value;
            break;
        }
    }
    if (value != 'UNDEF') {
        if (value == '0.00') {
            value = 'OFF';
        } else {
            value = 'ON';
        }
    }
    return value;
})(input)