Http binding - Turn JSON value from '1.0' to 'ON' to update Switch Item


I’m trying to update a Switch Item from a json field via the http binding.

I can receive the value however this is numeric either 1.0 which would represent ON and 0.0 which represents OFF which the Switch Item does not understand.

I have created a second Number Item to receive the number and then a rule to update the Switch Item by sending an ON / OFF update but I’m sure there must be a better way??? I need to duplicate this a number of times.


Switch Mute         "Mixer Mute"                  
Number MuteN         "Mixer Mute N"       { http="<[$.value)]" }


rule "MuteN"
	Item MuteN received update
//    logInfo("Mute State", (MuteN.state).toString)
    if ((MuteN.state) == 1) {


JSON is a bit of a mystery to me…

Thanks for any help!!!

Maybe you could use a transform map?

Can that be applied to the binding? Ideally I don’t want the extra Number Item.

So I think I need something along the lines of;

Switch Mute       "Mute"       { http="<[]" }


(function(i) {

    var status = 0 ; //don't know what goes here?????
    if (status == 1) {
        return 'ON';
    } else {
        return 'OFF';


Changing the var from 1 to 0 etc. mutes and updates the Switch Item correctly, but I cant get it all to talk!

1 Like

You are not using the input value at all, that script will return OFF in any case!
The script should read:

(function(i) {
if (i == 1) {
        return 'ON';
    } else {
        return 'OFF';

Thanks, I’ve tried exactly that but it still always returns OFF.

If I reverse it to != it always returns ON so its still not ‘seeing’ the value.

Is there a way to log / display what is coming into the script?

What do you get when posting

into the browser?
I suspect you need to decode the JSON in the script in order to get the suspected integer.


(function(i) {
var myvalue=JSON.parse(i).value;
if (myvalue == 1) {
        return 'ON';
    } else {
        return 'OFF';
1 Like

Perfect… Thank you very much for keeping me sane!!!

I think learning some JS is next on my list…

So is ‘i’ always taken as the input from where the script is referenced in the http binding for example… so a different function would need to be in a different .js file?

It is the variable-name behind the “function” that is taken, changing the name in this position would change the variable name in the function badly.
And yes a different function would be in a different file.

Ok, that makes sense. Thanks again.

I’ll have a play then, if I go up a few levels to for example then there are more variables; so in this case I presume the whole lot would come through as ‘i’ and could be filtered (probably not the right word)…