Happy (almost) New Year from the Southern Hemisphere!!
I am trying to use the HTTP binding to send this to a webserver: {"DA":{"tempTarget":25.5}
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: 22={"DA":{"tempTarget":22}}
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));
})(input);
The Command Transformation setting on the channel is coded as: JS:actron_tempTarget.js
Just wondering what I am doing wrong? Can anyone help?
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 {{"result":1,"error":null,"id":0,"data":null}}
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?
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?
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));
})(input)
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.
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?
Thanks!
EDIT: I figured it out thanks to your post! map transformation to the rescue