Item unit conversion (Temperature)

Hello Everyone,

I’m currently setting up mqtt with openhab and, in spite of trying quite a few things, am stuck now. The transfer of data/numbers into openhab via mqtt works fine, that’s not the issue, it’s the correct unit or the conversion from Celsius to Fahrenheit.

The MQTT binding does not support UoM, so the temperatures transmitted here are numbers (in Celsius, but I want Fahrenheit) without a unit. Accordingly, the channel is a number-channel, and not number:temperature. So using UoM and just defining the item as a number:temperature in Fahrenheit does not work. Neither does setting °C as “Unit of Measurement” in the mqtt-channel. I always get the untransformed celsius-value.

So I thought of the idea of a small conversion js-script. That works when used in a rule, so I know it is correct. Now I could use dummy-items and read the value from the mqtt-item and write it to the real item in a rule, but as I intend to do that with a number of items, I thought there must be a better way.

The “Javascript Transformation Services” and “Transformation Services” pages list two different formats for doing conversions within an item definition:

Number Livingroom_Temperature_F "Temperature [JS(convert-C-to-F.js):%.1f °F]"


String <itemName> { channel="<channelUID>"[profile="transform:JS", function="<filename>", sourceFormat="<valueFormat>"]}

although I’m not sure the latter one works with numbers.

Anyway, neither of those work, the corresponding item does not get updated even when a new mqtt-message for that topic arrives.

Any ideas on where I’m in the wrong direction here?


I do the transformation within the thing definition itself. Assuming your JavaScript is all correct and saved in the correct location, you’d just need to add the following to your temperature channel:


To fully understand what’s going on we’d need to see your things, items and JavaScript…

Edit: …and of course ensure that the JavaScript transformation service is actually installed

By way of explanation -

Number Livingroom_Temperature_F "Temperature [JS(convert-C-to-F.js):%.1f °F]"

This is not supposed to ‘work’ the way you want it. Use of [transformation] in this context is purely about display - the actual Item state is transformed for display in e.g. BasicUI but item state remains unaltered.

String <itemName> { channel="<channelUID>"[profile="transform:JS", function="<filename>", sourceFormat="<valueFormat>"]}

As you suspect, the transform profile does not work with Number or any non-String type Items.
I think that that is a deficiency that really limits its usefulness.

I’ve not tired it yet, but as of 2.5.4 I think the binding appears to support UoM.

It doesn’t. Profiles only work with String Items unfortunately.

Thanks, guys!

@hafniumzinc was spot on, that was the answer. I really don’t know why I didn’t realize that the transformation had to be done at thing-level. I use PaperUI for things (and files for items), so it’s a bit hard to post the thing, but I inserted the JS in the “incoming value transformations” field and that works. I read about people putting transformation scripts there to dissect a jsonpath topic message, but somehow failed to see that I could put my unit conversion script here.

@rlkoshak, I do have version 2.5.6, and tried putting “°C” in the “Unit of Measurement”, but I think that as in mqtt there are only “number” channels, but not “number:temperature” ones (like my zwave sensors supply with the zwave binding), I suppose that’s the reason why it does not work.

I currently don’t have shell access to the box as I’m on my office computer, but will post the js-script I use (just 3 lines, basically) tonight for completion’s sake.

1 Like

yes, please do as I just hooked a DHT22 to a Pi and sending OpenHAB the data via mqtt and it is Celsius and I’m in US of A and need Fahrenheit to make sense for me

which would be???

That depends on how you installed openHAB. It must live in the transform folder within the configuration folder. My configuration folder is at /etc/openhab2/…

Edit: fat fingers!

It’s called “transform” (without an “s”) but it should already be present in any openhab installation. But yes, it is in the configuration folder along with the folders for items, things, rules, etc. Any new file in there needs to made readable by the openhab user.

1 Like

I do exactly the same, but do the conversion earlier in the shell script that reads out the DHT and calls mosquitto_pub to publish the value to the broker. But you can certainly do the conversion in openhab, no difference, really.

Here’s the js-script. Pretty simple. One could probably condense it into less lines, but whatever.

(function(i) {
var f = i * 9/5 + 32;
return f;

1 Like