JS transformation won't work, how to debug?

Hello,
I have a problem that some translation wont work. I have sonoff S20 with DHT11 and I like to reduce the temperature -3,8 °C. In other Threats I found this aussenTempCal.js:

(function(x){
     return x -3.8 ;
})(input)

My things file looks that:

Thing topic sonoff_s20_9 "Strahler" @ "Gartenhaus" {
Channels:
    Type string : reachable     "Reachable"             [ stateTopic="tele/sonoff_s20_9/LWT" ]
    Type switch : power         "Power"                 [ stateTopic="stat/sonoff_s20_9/POWER", commandTopic="cmnd/sonoff_s20_9/POWER" ]
    Type number : temperature   "Temperature"           [ stateTopic="tele/sonoff_s20_9/SENSOR", transformationPattern="JSONPATH:$.DHT11.Temperature", transformationPattern="JS:aussenTempCal.js" ]
    Type number : humidity      "Humidity"              [ stateTopic="tele/sonoff_s20_9/SENSOR", transformationPattern="JSONPATH:$.DHT11.Humidity"]
    Type number : dewpoint      "Taupunkt"              [ stateTopic="tele/sonoff_s20_9/SENSOR", transformationPattern="JSONPATH:$.DHT11.DewPoint"]
    Type number : rssi          "WiFi Signal Strength"  [ stateTopic="tele/sonoff_s20_9/STATE", transformationPattern="JSONPATH:$.Wifi.RSSI"]
}

The part of items:

Switch sonoff_s20_9_s “Strahler” (G_Gartenhaus) { channel=“mqtt:topic:mqtt2:sonoff_s20_9:power” }
Number sonoff_s20_9_temp “Außentemp [%.1f C°]” (G_Gartenhaus, influx) { channel=“mqtt:topic:mqtt2:sonoff_s20_9:temperature” }
Number sonoff_s20_9_humi “Rel.Luftfeute [%.1f %%]” (G_Gartenhaus, influx) { channel=“mqtt:topic:mqtt2:sonoff_s20_9:humidity” }
Number sonoff_s20_9_taupunkt “Taupunkt [%.1f C°]” (G_Gartenhaus, influx) { channel=“mqtt:topic:mqtt2:sonoff_s20_9:dewpoint” }

I try some other javascripts, but I always get the original temperature in the log file.
I have installed all updates and the translation binding “transformation = jsonpath,javascript” and “binding = astro,onewire1,harmonyhub,mqtt,systeminfo,expire1”
I have some js transformations for zigbee2mqtt with hue like this https://community.openhab.org/t/zigbee2mqtt-and-zigbee-bulbs-small-tutorial/72129 that works at the moment.

Thanks NetSnoopy

Your temperature channel has two transformationPattern - you can only have one. However, you can chain transformations in one transformationPattern when using the MQTT binding.

See here, where I asked a very similar question!

So you would have something like:

transformationPattern="JSONPATH:$.DHT11.Temperature∩JS:aussenTempCal.js"
1 Like

I find it easiest to debug and develop javascript transformations in a temporary rule, where we can feed in dummy data.

One thing to remember about the transformation service, it will always pass the data into your javascript as a string.
If you want to treat it as a number, you need to parse it, maybe using parseFloat()

Interesting. In the link I include my working example where I don’t do anything special to the input value of the JS transform - have I just got lucky?

Probably, javascript is pretty forgiving about strings and numbers, blurry boundaries there, but sometimes it’s ambiguous.

Hello,

tested both in all variations, but nothing helps.
I changed the JS to:

(function(x){

var result;

var temp = parseFloat(x);

result = temp - 3;

return result;

})(input)

Any other ideas?

Well, you haven’t shown us what your channel definition looks like now or what you see in your events.log
Anything in your openhab.log?

Edit - when you make small edits to things files, like your channel config, sometimes you need to restart the binding to get them picked up

No errors and it shows the same sensor data as tasmota webseite.

2020-07-02 15:13:18.786 [INFO ] [el.core.internal.ModelRepositoryImpl] - Refreshing model 'mqtt2.things'

2020-07-02 14:46:44.744 [vent.ItemStateChangedEvent] - sonoff_s20_9_temp changed from 28.0 to 27.0

2020-07-02 14:48:19.830 [vent.ItemStateChangedEvent] - sonoff_s20_9_taupunkt changed from -0.2 to 0.6

2020-07-02 14:48:19.843 [vent.ItemStateChangedEvent] - sonoff_s20_9_temp changed from 27.0 to 28.0

2020-07-02 14:53:25.017 [vent.ItemStateChangedEvent] - sonoff_s20_9_taupunkt changed from 0.6 to -0.2

2020-07-02 14:53:25.024 [vent.ItemStateChangedEvent] - sonoff_s20_9_temp changed from 28.0 to 27.0

2020-07-02 15:04:54.812 [vent.ItemStateChangedEvent] - sonoff_s20_9_temp changed from 27.0 to 26.0

2020-07-02 15:04:54.820 [vent.ItemStateChangedEvent] - sonoff_s20_9_humi changed from 17.0 to 18.0

OK, now I rebooted the Pi and it works :face_vomiting:
But I do this some times before, and also twice today and yesterday I cleared also the Cache too…

in the end maybe both helps:

  1. change the Thing transformation from:

Type number : temperature “Temperature” [ stateTopic=“tele/sonoff_s20_9/SENSOR”, transformationPattern=“JSONPATH:$.DHT11.Temperature”, transformationPattern=“JS:aussenTempCal.js” ]

to:

Type number : temperature “Temperature” [ stateTopic=“tele/sonoff_s20_9/SENSOR”, transformationPattern=“JSONPATH:$.DHT11.Temperature∩JS:aussenTempCal.js” ]

  1. I changed the transformation and add parseFloat(x) file to:

(function(x){
var result;
var temp = parseFloat(x);
result = temp - 3.8;
return result;
})(input)

  1. reboot raspberry / openhab ?!

anyway … If ther a solution to greate a log entry in the transformation JS file for a better debugging ?

1 Like

Sure, but your configuration was plain wrong.

No.