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

Hi,

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.

Items:

Switch Mute         "Mixer Mute"                  
Number MuteN         "Mixer Mute N"       { http="<[http://10.10.1.52/datastore/mix/chan/6/matrix/mute:5000:JSONPATH($.value)]" }

Rule:

rule "MuteN"
when
	Item MuteN received update
then
//    logInfo("Mute State", (MuteN.state).toString)
    if ((MuteN.state) == 1) {
        Mute.postUpdate(ON) 
    }
    else
        Mute.postUpdate(OFF) 

end

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="<[http://10.10.1.52/datastore/mix/main/0/matrix/mute:1000:JS(mute.js)]" }

/transform/mute.js

(function(i) {

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

})(input)

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';
    }  
})(input)

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

http://10.10.1.52/datastore/mix/main/0/matrix/mute

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

{“value”:0}

(function(i) {
var myvalue=JSON.parse(i).value;
if (myvalue == 1) {
        return 'ON';
    } else {
        return 'OFF';
    }  
})(input)
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 http://10.10.1.52/datastore/mix/main/0 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)…

{"eq/highshelf/bw":1.000000,
"eq/highshelf/mode":0.000000,
"eq/lowshelf/bw":1.000000,
"eq/lowshelf/mode":0.000000,
"leveler/makeup":20.000000,
"leveler/reduction":20.000000,
"leveler/enable":0.000000,
"leveler/limit":0.000000,
"tb/enable":0.000000,
"eq/highshelf/enable":1.000000,
"eq/highshelf/freq":13301.371094,
"eq/highshelf/gain":7.898000,
"eq/lowshelf/enable":1.000000,
"eq/lowshelf/freq":40.023994,
"eq/lowshelf/gain":9.328000,
"eq/mid1/bw":1.570593,
"eq/mid1/enable":1.000000,
"eq/mid1/freq":2342.475342,
"eq/mid1/gain":4.818000,
"eq/mid2/bw":2.368354,
"eq/mid2/enable":1.000000,
"eq/mid2/freq":270.139893,
"eq/mid2/gain":5.478000,
"matrix/fader":0.21114257281035398,
"matrix/mute":0.000000}