[SOLVED] MQTT Outbound transformation using JS questions

I am replacing some self written ESP8266 dimmers sketches with Sonoff-Tasmota sketches. These sketches provide LED dimming with 0-1023 while a slider in OH 2.1 is using 0-100.

To prevent all kind of rules and stuff I tried to go down the Transform way:

javascript Multi100.js:

(function(multi100) {
  return multi100 * 100;
})(input);

item:

Dimmer sonoff_D542CF "Keukentafel strip" (gLights_VH_Keuken)  {mqtt=">[mosquitto:home/esp8266/tasmota/sonoff_D542CF/cmnd/Pwm1:command:*:JS(multi100.js)],
	<[mosquitto:home/esp8266/tasmota/sonoff_BD6C7F/POWER:state:default]"} 

when moving the slider however I get an error in the logfile:

2017-10-11 11:33:25.826 [ERROR] [g.mqtt.internal.MqttMessagePublisher] - Error publishing...
org.openhab.core.transform.TransformationException: An error occurred while loading script.
	at org.openhab.core.transform.TransformationHelper$TransformationServiceDelegate.transform(TransformationHelper.java:62)[176:org.openhab.core.compat1x:2.1.0]
	at org.openhab.binding.mqtt.internal.MqttMessagePublisher.createMessage(MqttMessagePublisher.java:149)[185:org.openhab.binding.mqtt:1.10.0]
	at org.openhab.binding.mqtt.internal.MqttMessagePublisher.publish(MqttMessagePublisher.java:175)[185:org.openhab.binding.mqtt:1.10.0]
	at org.openhab.binding.mqtt.internal.MqttItemBinding.internalReceiveCommand(MqttItemBinding.java:45)[185:org.openhab.binding.mqtt:1.10.0]
	at org.openhab.core.binding.AbstractBinding.receiveCommand(AbstractBinding.java:97)[176:org.openhab.core.compat1x:2.1.0]
	at org.openhab.core.events.AbstractEventSubscriber.handleEvent(AbstractEventSubscriber.java:45)[176:org.openhab.core.compat1x:2.1.0]
	at org.apache.felix.eventadmin.impl.handler.EventHandlerProxy.sendEvent(EventHandlerProxy.java:415)[6:org.apache.karaf.services.eventadmin:4.0.8]
	at org.apache.felix.eventadmin.impl.tasks.HandlerTask.run(HandlerTask.java:90)[6:org.apache.karaf.services.eventadmin:4.0.8]
	at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)[:1.8.0_144]
	at java.util.concurrent.FutureTask.run(FutureTask.java:266)[:1.8.0_144]
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)[:1.8.0_144]
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)[:1.8.0_144]
	at java.lang.Thread.run(Thread.java:748)[:1.8.0_144]

would love to know what I am doing wrong…

kr

Han

Hmmmm. It is having a problem loading your script.

Is Multi100.js in your conf folder (/etc/openhab2/transform for apt-get/rpm installations)?

Is it truly named Multi100.js? If so you are using multi100.js. Case matters.

The example function on the JS Transform README does not put a ; at the end. I’m not super proficient with JavaScript so can’t say if that could cause a problem.

(function(multi100) {
  return multi100 * 100;
})(input)

And while this is unrelated to your problem, with your current implementation you will never get to to top value of the range of 1023. If you want to utilize the full range of values you would want to do something like:

(function(multi100) {
  var percent = multi100 / 100.0;
  var value = 1023 * percent;
  return Math.round(value);
})(input)
2 Likes

thx Rich,

JS file is called multi100.js

server@OpenHAB2:/etc/openhab2/transform$ ls
aanuit.map  de.map  en.map  motion.map  multi100.js  opendicht.map  otg.map  reachable.map  readme.txt  scenes.map  unifi_eq.map  unifi.map
server@OpenHAB2:/etc/openhab2/transform$

I have put your code in my script as it takes in account the last 23! Guess my script now should be called multi100andabit :wink:

But in the end YOU DID IT! Its working now, thank you very much.

cheers,

Han

1 Like