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"]
}
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.
Your temperature channel has two transformationPattern - you can only have one. However, you can chain transformations in one transformationPattern when using the MQTT binding.
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?
OK, now I rebooted the Pi and it works
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:
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” ]
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)
reboot raspberry / openhab ?!
anyway … If ther a solution to greate a log entry in the transformation JS file for a better debugging ?