JS transformation to encode JSON in HTTP binding

Happy (almost) New Year from the Southern Hemisphere!!

I am trying to use the HTTP binding to send this to a webserver:

where the target temperature is a variable and linked to an item. I know that I can have a MAP with individual lines such as:
and while this works, it seems inefficient.

On advice from a different post Help MAP transformation with HTTP Binding OH3
I came up with this bit of code (PS not a programmer at all) and put it in actron_tempTarget.js in the transformation folder, however after triggering the dial connected to the item, nothing happens. The JS transformation is installed.

(function transformValueToJson(i) { 
    var obj = { DA: {tempTarget: i }};
    return (JSON.stringify(obj));

The Command Transformation setting on the channel is coded as:

Just wondering what I am doing wrong? Can anyone help?

Presumably this could be something like:
var obj = "{ \"DA\": {\"tempTarget\": \"" +i+ "\" }}";

And then just return obj

Thanks for your very quick reply. Unfortunately it didn’t work. The response in the logs was:
{{"result":0,"error":"Unknown Request","id":400,"data":null}}

My version gets me

Yeah, you’re right - apologies, got my wires crossed. Not sure why yours isn’t working. What logs are you copying those responses from?

Sorry the “unknown request” response from the log happened because I didn’t use your entire suggestion. Once I changed to return obj; i get the same result of {{"result":1,"error":null,"id":0,"data":null}}

I am getting this from the log in karaf. should I look someplace else?

I have a hunch: remove the function name and trailing semicolon:

(function(i) { 
    var obj = { DA: {tempTarget: i }};
    return (JSON.stringify(obj));

No dice sadly. I removed the function name and semicolon from both versions (my original and your suggestion)

Then I’m out of ideas, sadly! As I understand it the JS transformation service looks for the ‘master’ function in a file, which is one without a name.

Perhaps try simplifying your rule so all it does is return “hello” and see what it does?

No worries. Thanks so much for the suggestions.

Ok I worked it out.

I tried the suggestion of returning “hello” but I couldn’t see this represented anywhere in the logs. It turns out I had changed the logging level of org.openhab.binding.http.internal.http to TRACE but it was only giving me part of the picture. On a hunch I set org.openhab.binding.http to TRACE and could then see the outgoing stuff.

In doing this, I noticed that the temperature value was being sent as a string Content = {{"DA":{"tempTarget":"31"}}} (I think that’s the correct interpretation). Anyway I changed the code in the transformation to convert the string value to a number first before compiling the response and it is now working. The complete code is below:

(function (i) { 
    var tempValue = parseFloat(i)
    var obj = { DA: {tempTarget: tempValue }};
    return (JSON.stringify(obj));

Not sure if this is how it “should be” done or whether the configuration in the UI should have been changed instead, but in any event it is working correctly now.

Thanks @hafniumzinc for your input on solving this.


Ah, crap, yes - the JS transformation input only deals in strings. I should have spotted that - apologies! Glad you’ve got it working!

No worries. Don’t be sorry. It was a good puzzle to solve and I’ve started the new year feeling like I’ve accomplished something already

Hey @Lucas I’m only just embarking on the migration of my Actron over to the new OH3. HTTP 3.x certainly is different.

Yeah it took a while and I needed some help but I got it to work. This thread documents my progress is getting it to work:

Shout out if you still have trouble and I’ll help where I can


Hi Lucas,
I am trying to get a JSON string also send via HTTP.
My challenge is, as shown here that the JSON string actually includes multiple components.
I know how to handle simple JSON strings and read values via GET, but the POST command with multiple components is still a black box for me.
Any advice on how this could work of it is even possible?

EDIT: I figured it out thanks to your post! map transformation to the rescue :slight_smile:

1 Like

Glad you figured it out. It took me ages to get it to work and I needed help in the end.