Cannot get variable value of 1 to display "on" in HTML instead of binary 0001 icon thingy

I’m baffled! I’m playing with Remote-Spark, and I cannot get the read variable value 1 from the Spark to show “On” in the HTML.

Showing this thing for on/1 value…

instead of “on”
Originally it was:

       
        var varKey2 = "relaysEnable";
        var var2onLabel = "On.";
        var var2offLabel = "Off";
        var var2onState = 1; // Set to null to allow raw value to be displayed.
        var var2offState = 0; // Set to null to allow raw value to be displayed.
        var refresh2 = 0; // variable 2 refresh rate in milliseconds (set to 0 to disable)

          function getVariable2() {
          getVariable(varKey2, function (res) {
            if(res === var2onState)
              $("#var-val-2").val(var2onLabel);
            else if(res === var2offState)
              $("#var-val-2").val(var2offLabel);
            else
              $("#var-val-2").val(res);
          });
        } 

which either showed
(binary 1 icon) or blank
I then tried if(res == var2onState) instead of if(res === var2onState)
and it worked for Off but not on.

I tried


          function getVariable1() {
          getVariable(varKey1, function (res) {
            if(res == 1 || res == '1' || res == true)
              $("#var-val-1").val(var1onLabel);
            else if(res == var1offState)
              $("#var-val-1").val(var1offLabel);
           else
              $("#var-val-1").val(res); 

Which worked… once!!! WTF?? then went back to the binary icon 1

Any assistance appreciated. :slight_smile:

Update - Figure out that I had the variable defined as STRING instead of INT in the Spark firmware. Perhaps someone can explain why I couldn’t parse the STRING value of 1 in the HTML??
Thank you!

1 Like

Yeah, getting the variable from the cloud as a string, then doing a compare to a numeric variable in JavaScript, “strvar === numvar” will be false (because the ‘===’ operator is strict about type, whereas ‘==’ will coerce types to do a looser check).

But I’d want to see your Spark code to know more about what’s going on. I suspect that maybe your string was actually holding a binary value rather than a string representation of the number?

1 Like