[SOLVED] Javascript Transformation Error with MQTT

  • Platform information:

    • Hardware: RaspberryPi3
    • Java Runtime Environment: OpenJDK Runtime Environment (Zulu Embedded 8.25.0.76-linux-aarch32hf) (build 1.8.0_152-b76)
    • openHAB version:openHAB 2.3.0-1 (Release Build)
  • Issue of the topic:
    Hi, I have to problem that I get errors anytime I want to run a Javascript Transformation (yes, I installed the Javascript Transformation Add-In). I have defined the Transformation in the Item but independently from the Javascript Scripts I receive errors, that happens even with very simple scripts.
    I have set up zigbee2mqtt and want to use the mccgq11lm - Model as a Contact in OpenHab. All Transformations with the JSON-Addon work well. In this scenario I want to transform the contact-value in the JSON to “OPEN” or “CLOSED”, means into an contact conform format.

  • Here are the Configs, Files and Logs:
    mqtt.cfg (rest commented):

broker_test.url=tcp://localhost:1883
broker_test.clientId=OpenHAB

.items -File (the first one works well, the second one is where the error occurs)

Number WDS_LINKQUALITY "WDS Linkquality [%.1f]" <qualityofservice> (Bedroom, gLinkquality) {mqtt="<[broker_test:zigbee2mqtt/WDS:state:JSONPATH($.linkquality)]"}
Contact Bedroom_WDS_CONTACT "WDS OpenClosed [%s]" <door> (Bedroom, gDoor) {mqtt="<[broker_test:zigbee2mqtt/WDS:state:JS(test.js)"}

script-Files (test.js in /etc/openhab2/scripts)

(function(jsonString) {
	var data = JSON.parse(jsonString);
	var value = data.contact;
	var output = "NULL";
	if (value == true) {
     		output = "CLOSED";
	} else if (value == false) {
     		output = "OPEN";
	}
	return output;
})(input)

and even this one doesn’t work

(function(jsonString) {
    return "OPEN";
})(input)

this is how the mqtt msg looks like:
Aug 19 18:53:23 openHABianPi npm[551]: zigbee2mqtt:info 2018-8-19 18:53:23 MQTT publish, topic: 'zigbee2mqtt/WDS', payload: '{"contact":true,"linkquality":99,"battery":"100.00","voltage":3135}'

  • This is the Error Message I get all the time:
2018-08-19 19:26:12.102 [ERROR] [.mqtt.internal.MqttMessageSubscriber] - Error processing MQTT message.
org.openhab.core.transform.TransformationException: An error occurred while loading script.
	at org.openhab.core.transform.TransformationHelper$TransformationServiceDelegate.transform(TransformationHelper.java:67) [198:org.openhab.core.compat1x:2.3.0]
	at org.openhab.binding.mqtt.internal.MqttMessageSubscriber.processMessage(MqttMessageSubscriber.java:138) [197:org.openhab.binding.mqtt:1.12.0]
	at org.openhab.io.transport.mqtt.internal.MqttBrokerConnection.messageArrived(MqttBrokerConnection.java:574) [199:org.openhab.io.transport.mqtt:1.12.0]
	at org.eclipse.paho.client.mqttv3.internal.CommsCallback.deliverMessage(CommsCallback.java:475) [199:org.openhab.io.transport.mqtt:1.12.0]
	at org.eclipse.paho.client.mqttv3.internal.CommsCallback.handleMessage(CommsCallback.java:379) [199:org.openhab.io.transport.mqtt:1.12.0]
	at org.eclipse.paho.client.mqttv3.internal.CommsCallback.run(CommsCallback.java:183) [199:org.openhab.io.transport.mqtt:1.12.0]
	at java.lang.Thread.run(Thread.java:748) [?:?]

Thank you very much in advance and kind regards,
Sandmann

{ mqtt="<[pibroker:home/light/presence:state:MAP(ONOFF.map)]" }

You could use something similar and replace ONOFF.map with truefalse.map
Then in /etc/openhab2/transform en.map enter this:
true=CLOSED
false=OPEN

then save the file. No need for the js script.

Sadly, this is throwing and error aswell.

2018-08-19 20:32:35.757 [WARN ] [rm.AbstractFileTransformationService] - Could not transform '{"contact":false,"linkquality":107,"battery":"100.00","voltage":3125}' with the file 'bool_to_openclosed.map' : Target value not found in map for '{"contact":false,"linkquality":107,"battery":"100.00","voltage":3125}'

Furthermore with this approach wouldn’t I still have to specify via the JSON-Addon which value exactly is meant?

Do you get anything with default?

{mqtt="<[broker_test:zigbee2mqtt/WDS:state:default"}

With the default I get this warning msg:

2018-08-19 20:52:46.259 [WARN ] [b.core.events.EventPublisherDelegate] - given new state is NULL, couldn't post update for 'Bedroom_WDS_CONTACT'

If you change the state of the switch does the NULL change to the default value?

No, if I change the state of the switch (true to false ot other way round), I get this same warning log again and again.

The physical switch or window. Its NULL now but if you change the actual state if the switch you should see it in your log

Sorry, I misunderstood your question. If I change the item file to

{mqtt="<[broker_test:zigbee2mqtt/WDS:state:CLOSED"}

the state is actually changed.

2018-08-19 21:18:02.976 [vent.ItemStateChangedEvent] - Bedroom_WDS_CONTACT changed from NULL to CLOSED

Looking at your item this is a bedroom door switch (physical switch), this is what I was asking for you to change. If the door is open, close it and vise versa. The NULL is because OH does not know anything about the door yet. With you opening/ closing the door then NULL should change to a value of ???
Change the item back to default…change CLOSED to default as before then see what the log says after you open/close the door.

If I get you right, that is exactly what I did before with the same result.
I did change the item to

{mqtt="<[broker_test:zigbee2mqtt/WDS:state:default]"}

and closed/opened the door (so MQTT msg are sent with payload like the logs above), I get the exact same warning like before.

2018-08-19 21:46:22.487 [WARN ] [b.core.events.EventPublisherDelegate] - given new state is NULL, couldn't post update for 'Bedroom_WDS_CONTACT'

I found an alternative with

String WDS_CONTACT "WDS [MAP(bool_to_openclosed.map):%s]" <door> (Bedroom, gDoor) {mqtt="<[broker_test:zigbee2mqtt/WDS:state:JSONPATH($.contact)]"}

but in that case the state is still true or false but the OPEN/CLOSED is shown.

1 Like

Good to see you found the answer. :smiley:

1 Like

It’s work around but not a great one because you end up with a String item.
I have tested your JS transform and it works for me.

Did you try clearing the cache:

Short of that, I don’t know why it doesn’t work in your case

1 Like

Thanks for the hint! I just stopped the OH service and removed the specified folders and restarted the OH service and could see the following in the log:

2018-08-19 22:19:32.080 [ERROR] [core.karaf.internal.FeatureInstaller] - Failed installing 'openhab-transformation-js': Error restarting bundles:
	Unable to acquire the state change lock for the module: osgi.identity; type="osgi.bundle"; version:Version="0.10.0.oh230"; osgi.identity="org.eclipse.smarthome.io.transport.mdns" [id=124] STARTED [STOPPED]

which should be related to the occuring error. I will try to resolve the issue and provide and update!

I finally got the error! Hopefully this is going to help someone else to avoid this mistake.
Without thinking I have put the js-file into the /etc/openhab2/scripts folder.
The script has to remain in the /etc/openhab2/transform folder in order to work properly!
Sorry to bother you guys with such a mistake and thanks for your help!

1 Like