I’m fighting with parsing JSON using JSONPATH.
Scenario: There is a MQTT button which publish state while button is clicked. I have a rule to handle such event. Rule is triggered but during execution I’m getting error:
Script execution of rule with UID ‘ff95ce3b6e’ failed: org.graalvm.polyglot.PolyglotException: ReferenceError: “transform” is not defined
Transformation service is installed and started:
openhab-transformation-jsonpath x 3.4.4 x x x Started x openhab-addons-3.4.4 x JSONPath Transformation
Rule:
var logger = Java.type('org.slf4j.LoggerFactory').getLogger('org.openhab.rule.' + ctx.ruleUID);
logger.info("Button handler");
var buttonStatus = event.getPayload();
var eventSource = transform("JSONPath", "$.event", buttonStatus);
...... do some stuff, but doesn't work as previous line fail.
What I’m doing wrong? What else should I check/install?
Thanks. But this is n’th version of this call. I tried different “notations”:
JSONPATH
JSONPath
JsonPath
jsonpath
None of above works. Always I get same error. Seems that “transform” function is not available in scope.
It looks like you are writing the rule using JSScripting. In that case you call transforms using actions.Transformation.transform not just transform.
From the help docs:
Transformation Actions
openHAB provides various data transformation services which can translate between technical and human-readable values. Usually, they are used directly on Items, but it is also possible to access them from scripts.
console.log(actions.Transformation.transform(‘MAP’, ‘en.map’, ‘OPEN’)); // open
console.log(actions.Transformation.transform(‘MAP’, ‘de.map’, ‘OPEN’)); // offen
See openhab-js : actions.Transformation for complete documentation.
But, if you are using JSScripting, then there is no need for the JSONPath transform, json strings can be handled natively by just parsing the string into a json object.
var buttonJSON = JSON.parse(buttonStatus);
var eventSource = buttonJSON.event;
thanks a lot. “actions.Transformation.transform” does the job.
Is there a good JSScripting guide for openHAB? Notation is not a case, maily I would like to know names of objects native for OH.