MQTT, Incoming Value Transformation - OH3.1 text config

Dear, I’m kind of confused how to use Incoming Value Transformation as the documentation claims it is possible but there is no full example to follow.

I have a simple MQTT incoming message that is RAW 4bytes with 32bit float value for the temperature.
Can I Transform it with JS before I send it to the Item, still in the MQTT thing definition?
I cant find a way of right syntax for that.
I think it should be done directly here in the .things file (type is string since that is the only way to recieve 4 bytes of data over mqtt)

	Thing topic moxa "Hum/Temp Sensor 1" {
        Type string : temperature "Temperature" [ stateTopic="moxa/temp1"]	

What is well documented is the transformation in the .items files in that manner

String sensor_1_temp "Temperature [JS(hex2float.js):%s °C]" { channel="mqtt:topic:eui-oh3:moxa:temperature" }

But that is just for the display purposes and not really what I need as I want to convert it on the input phase. With such a transformation or examples still logs byte values.

2021-07-15 15:42:19.930 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item 'sensor_1_temp_string' changed from Aď\ to Aģ�

Is there a way to do it poperly in the text file?

A transformation is a transformation wherever it is used in openHAB.
There’s a lot of other resources about writing javascript, so openHAB won’t be giving you docs about that.

Key to understand transforms - “whatever you start with” is passed in as a single string. “whatever you produced” is returned as a single string.

Implications here, you may need to parse numbers or JSON etc. from the input before you process it in your javascript. You can’t return arrays, you must serialize to something. You can’t even return a number, it will get converted to string representation - that’s important if you are messing with character codes.

The OH framework will take care of parsing your output into something suitable for the real “target” - display, Number type Item, etc.

hmm, I understand the concept but the problem is tha docs are lacking the files examples as I see you are removing them for the sake of new users using GUI.
I solved the issue by switching the sensor to JSON output and going for JSONPATH transform that is more commonly documented here. So below works:

Type number : temperature_1 "Temperature Sensor 1" [ stateTopic="moxa/temp1", transformationPattern="JSONPATH:$.payload.temperature_1"]

But analogic one for the JS transform I tried before not so much as there is no example for it:

Type number : temperature_1 "Temperature Sensor 1" [ stateTopic="moxa/temp1", transformationPattern="JS(my_function.js)]

That does not work.

I would have thought analogous would be

1 Like

yeah your right, that is one syntax I have not tried, somehow got confused by this bit Transformations | openHAB
That is only use of the JS transformation in documentation and uses round brackets at the Item formatting.

Remember this is open source, user driven. There are hundreds of examples in this forum, though keep an eye on dates to be sure of relevant versions.

Syntax example inside {} of bridge

    Thing topic fan1 "Dining FAN" [ availabilityTopic="IFANdining/tele/LWT", payloadAvailable="Online", payloadNotAvailable="Offline"] {
        Type switch : Power1   "Dining Light "  [ stateTopic = "IFANdining/stat/POWER", commandTopic = "IFANdining/cmnd/POWER", on="ON", off="OFF" ]
        Type dimmer : fanspeed "Fan Speed"      [ stateTopic = "IFANdining/stat/FANSPEED", transformationPattern = "JSONPATH:$.FanSpeed", commandTopic ="IFANdining/cmnd/FANSPEED", 0="OFF", 1="LOW", 2="MED", 3="HIGH", 2=100  ]
        Type switch : reachable "Reachable"     [ stateTopic = "IFANdining/tele/LWT",	on="Online",	off="Offline" ]
1 Like