0

how to make logical functions javascript

Marco Tanis 7 years ago in iRidium Script updated 7 years ago 2

Hi,


I don't have a lot of knowledge in javascript and i want to use logical functions, but i don't get it to work.

i used an example of this forum and i want to make this with it:


if: input1=0 and input2=1

than set: output1->0 , output2->0, output3->0


if: input1=1 and input2=1

than set: output1->1 , output2->1, output3->1


here is the script i made, can anyone tell me what i did wrong?

IR.AddListener(IR.EVENT_START,0,function()
{
//Lsunch a listener of the basis if global tag work.
IR.SetGlobalListener(IR.EVENT_GLOBAL_TAG_CHANGE, function(name, value)
   {
   
      IR.Log("Active global: " + name + "\value: " + value);
      /*
      Write logics for required processing.
      */
      if ((name == "Global.input1" && value == 0) && (name == "Global.input2" && value == 1))
         {
             IR.Log("Light OFF")
             IR.SetVariable("Global.output1", 0)
             IR.SetVariable("Global.output2", 0)
             IR.SetVariable("Global.output3", 0)
         }else
      /*
      Write logics for required processing. 
      */
      if ((name == "Global.input1" && value == 1) && (name == "Global.input2" && value == 1))
         {
             IR.Log("Light ON")
             IR.SetVariable("Global.output1", 1)
             IR.SetVariable("Global.output2", 1)
             IR.SetVariable("Global.output3", 1)
         }                         
   });
//Signature block for required tags
IR.SubscribeTagChange("Global.input1");
IR.SubscribeTagChange("Global.input2")
IR.SubscribeTagChange("Global.output1")
IR.SubscribeTagChange("Global.output2")
IR.SubscribeTagChange("Global.output3")
});

I used this example from this forum, but is "IR.setGlobalListener" the best option to use?

If someone has a better suggestion please tell !


Thanks,

Marco

+1

Hi Marco

The global listener is fine and what you need.


From what I can see the problem is with

if ((name == "Global.input1" && value == 0) && (name == "Global.input2" && value == 1))

You are asking if the name is input1 AND input2 when the name can only be input1 OR input2


So you may want to try something like this


IR.SetGlobalListener(IR.EVENT_GLOBAL_TAG_CHANGE, function(name, value)
    //if the name is input1 or input2  
    if ((name == "Global.input1") ||  (name == "Global.input2")){
        //get the value of the other token
        var input;
        if (name == "Global.input1")
            input = IR.GetVariable("Global.input2")

        else if (name == "Global.input2")
            input = IR.GetVariable("Global.input1")
        
       //do some comparisons to decide which processing to do 
        if ( (value== 1) && (input == 1) ){
            IR.Log("Light ON")
             IR.SetVariable("Global.output1", 1)
             IR.SetVariable("Global.output2", 1)
             IR.SetVariable("Global.output3", 1)
        } else if ( (value== 0) && (input == 0) ){
            IR.Log("Light OFF")
             IR.SetVariable("Global.output1", 0)
             IR.SetVariable("Global.output2", 0)
             IR.SetVariable("Global.output3", 0)
        } else {
            //one is on and one off...
        }
    }//if
}//GlobalListener

I haven't tested this, but it should get you going in the right direction I hope :)






thanks for the reply!

I will try this out.