Compare two values in an array

Hi - sorry to bother you with this - I'm new to Java and node red (and Object Oriented Code in general)

I have a join node that produces an array with 2 values

image

I'm just trying to write a function node that compares the two array values

Like this


So - just comparing the two values and coming up with an appropriate message.

I'm not sure if I have failed to grasp some fundamental issue or if it's just a syntax thing?

Can anyone help steer me right on this? Thanks in advance.

Your code should work.
the warning triangles are just telling you to use === instead of ==.
== equal values.
=== equal values and types.
https://www.w3schools.com/js/js_comparisons.asp

What if both [0] and [1] are 1 ?

And what if they are both zero (0)?

Thanks for your help - I've worked through this and have it working now - I'm posting the flow here in case anyone else runs into the same newbie question.

Thanks again

[{"id":"ab6ef931.883208","type":"tab","label":"Flow 2","disabled":false,"info":""},{"id":"ed5f8b48.7c3ed8","type":"join","z":"ab6ef931.883208","name":"","mode":"custom","build":"array","property":"payload","propertyType":"msg","key":"topic","joiner":"msg:topic","joinerType":"str","accumulate":false,"timeout":"","count":"2","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":330,"y":340,"wires":[["35fd9386.5c1dbc","ec4df093.156d4"]]},{"id":"e6a97bb1.dc6408","type":"debug","z":"ab6ef931.883208","name":"Compare","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":580,"y":340,"wires":[]},{"id":"4dcb46f1.9cd1c8","type":"delay","z":"ab6ef931.883208","name":"","pauseType":"delay","timeout":"2","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":180,"y":340,"wires":[["ed5f8b48.7c3ed8"]]},{"id":"ff7670a8.28ba2","type":"change","z":"ab6ef931.883208","name":"Cng_On2_1","rules":[{"t":"change","p":"payload","pt":"msg","from":"On","fromt":"str","to":"1","tot":"num"}],"action":"","property":"","from":"","to":"","reg":false,"x":370,"y":80,"wires":[["ed5f8b48.7c3ed8"]]},{"id":"72b87980.9774f8","type":"change","z":"ab6ef931.883208","name":"Chg_Off2_0","rules":[{"t":"change","p":"payload","pt":"msg","from":"Off","fromt":"str","to":"0","tot":"num"}],"action":"","property":"","from":"","to":"","reg":false,"x":370,"y":120,"wires":[["ed5f8b48.7c3ed8"]]},{"id":"f014bafb.c5d598","type":"inject","z":"ab6ef931.883208","name":"Switch ON","topic":"On","payload":"On","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":200,"y":80,"wires":[["ff7670a8.28ba2"]]},{"id":"7441c2ac.c5f60c","type":"inject","z":"ab6ef931.883208","name":"Switch OFF","topic":"Off","payload":"Off","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":210,"y":120,"wires":[["72b87980.9774f8"]]},{"id":"2ac3b51a.69e85a","type":"inject","z":"ab6ef931.883208","name":"Lamp On","topic":"1","payload":"1","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":200,"y":160,"wires":[["4dcb46f1.9cd1c8"]]},{"id":"af3a9129.66323","type":"inject","z":"ab6ef931.883208","name":"Lamp Off","topic":"0","payload":"0","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":200,"y":200,"wires":[["4dcb46f1.9cd1c8"]]},{"id":"35fd9386.5c1dbc","type":"debug","z":"ab6ef931.883208","name":"Joined","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":450,"y":300,"wires":[]},{"id":"ec4df093.156d4","type":"function","z":"ab6ef931.883208","name":"Comp","func":"if (msg.payload[0] === 0 && msg.payload[1] == 0) {\n    msg.payload = \"Output is OFF\";\n}\n\nif (msg.payload[0] === 1 && msg.payload[1] == 1) {\n    msg.payload = \"Output is ON\";\n}\n\nif (msg.payload[0] === 0 && msg.payload[1] == 1) {\n    msg.payload = \"Output On - should be OFF\";\n}\n\nif (msg.payload[0] === 1 && msg.payload[1] == 0) {\n    msg.payload = \"Output OFF - should be ON\";\n}\n\nreturn msg;\n","outputs":1,"noerr":0,"x":450,"y":340,"wires":[["e6a97bb1.dc6408"]]}]

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.