Function node comparison of 2 inputs

Hi, Im trying to take 2 door stats (bools) and output a bool of if they are both open or both closed.
I have been through the boolean logic node but it outputs on 0+1 and 1+0 and i only want 0+0=0 or 1+1=1.
I have a function node trying to do an && on 2 parts of a message but i get only get null.

[{"id":"14bf1400.441aac","type":"debug","z":"aead4504.f4cd68","name":"JoinDBG","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":620,"y":180,"wires":[]},{"id":"599345c7.a9091c","type":"join","z":"aead4504.f4cd68","name":"","mode":"custom","build":"object","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":true,"timeout":"","count":"2","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":383.5,"y":260,"wires":[["247d14d3.7e4bbc","14bf1400.441aac"]]},{"id":"247d14d3.7e4bbc","type":"function","z":"aead4504.f4cd68","name":"Test","func":"if (msg.payload.coop1 === \"true\"  &&  msg.payload.coop2 === \"true\" ) {\n    msg.payload = \"open\";\n} else {\n    msg.payload = \"null\";\n}\nif (msg.payload.coop1 === \"false\"  &&  msg.payload.coop2 === \"false\" ) {\n    msg.payload = \"closed\";\n} else {\n    msg.payload = \"null\";\n}\nreturn msg;","outputs":1,"noerr":4,"x":530,"y":300,"wires":[["62de7928.409798"]]},{"id":"99271dc9.f9bd2","type":"inject","z":"aead4504.f4cd68","name":"","topic":"Coop1","payload":"true","payloadType":"bool","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":200,"y":200,"wires":[["599345c7.a9091c"]]},{"id":"87f10675.ceb2b8","type":"inject","z":"aead4504.f4cd68","name":"","topic":"Coop1","payload":"false","payloadType":"bool","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":210,"y":240,"wires":[["599345c7.a9091c"]]},{"id":"275aaa73.f22256","type":"inject","z":"aead4504.f4cd68","name":"","topic":"Coop2","payload":"true","payloadType":"bool","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":200,"y":280,"wires":[["599345c7.a9091c"]]},{"id":"d65510ee.27845","type":"inject","z":"aead4504.f4cd68","name":"","topic":"Coop2","payload":"false","payloadType":"bool","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":210,"y":320,"wires":[["599345c7.a9091c"]]},{"id":"62de7928.409798","type":"debug","z":"aead4504.f4cd68","name":"OutputDBG","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":710,"y":300,"wires":[]}]

Any help much appreciated!

You are testing for string values "true" and "false" whereas the data values are booleans, not strings.
So you want tests like
if (msg.payload.coop1 && msg.payload.coop2 )
and
if (!msg.payload.coop1 && !msg.payload.coop2 )
Also you need to reorganise the logic, as if the the first test fails then msg.payload will be null and the test of msg.payload.coop1 will generate an error as msg.payload.coop1 does not exist.

If you need help reorganising the logic I suggest

If both true
  ...
else if both false
  ...
else
  ...

If what you really want to do is to send no message in the final case then if you set msg = null then it will not send a message at all.

Hi Colin, this is where i have got using a combination of things, i'm still not happy using the inject node to run the "test" but i think i should be able to trigger the test with the output of the set flow change nodes?

Anyway, i'm getting stuck on the bool vs string bit, when playing before i got an out put but the logic was wrong, i think this is the correct logic, and its nice to store them in flow vars but im still stuck getting null out.

the editor was complaining about confusing "!"
I have tried firing strings and bools in but get null out, i have put the !in front of the !"open", !flow., and both with out... any pointers? thanks for y our help already its pushed me on some way i think

[{"id":"281b3f7a.d9276","type":"debug","z":"9c68d5ed.a5e358","name":"JoinDBG","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":980,"y":3840,"wires":[]},{"id":"6eb31f41.8a154","type":"function","z":"9c68d5ed.a5e358","name":"Test","func":"\n\nif ( flow.coopdoor === \"open\" && flow.pentcoopdoor === \"open\" ) {\n    msg.payload = \"open\";\n      }\n//  else if (\n//      flow.coopdoor === \"closed\" && flow.pentcoopdoor === \"closed\" ) {\n//    msg.payload = \"closed\";\n//      }\n    else (msg.payload = null)\n      \n  \nreturn msg;","outputs":1,"noerr":0,"x":710,"y":3840,"wires":[["281b3f7a.d9276"]]},{"id":"8ea0749f.206268","type":"inject","z":"9c68d5ed.a5e358","name":"","topic":"Coop1","payload":"true","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":520,"y":3620,"wires":[["9daeb1bf.fffa1"]]},{"id":"6572e6f2.0063b8","type":"inject","z":"9c68d5ed.a5e358","name":"","topic":"Coop1","payload":"false","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":530,"y":3660,"wires":[["9ea1b6b8.78d5b8"]]},{"id":"a4bbb69f.2de648","type":"inject","z":"9c68d5ed.a5e358","name":"","topic":"Coop2","payload":"true","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":520,"y":3700,"wires":[["7e27e777.3b3278"]]},{"id":"93e294a4.c36568","type":"inject","z":"9c68d5ed.a5e358","name":"","topic":"Coop2","payload":"false","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":530,"y":3740,"wires":[["f51fbc5a.37ee6"]]},{"id":"5c66bb7d.7f84b4","type":"debug","z":"9c68d5ed.a5e358","name":"OutputDBG","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":990,"y":3800,"wires":[]},{"id":"9daeb1bf.fffa1","type":"change","z":"9c68d5ed.a5e358","name":"","rules":[{"t":"set","p":"pentcoopdoor","pt":"flow","to":"open","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":760,"y":3620,"wires":[[]]},{"id":"9ea1b6b8.78d5b8","type":"change","z":"9c68d5ed.a5e358","name":"","rules":[{"t":"set","p":"pentcoopdoor","pt":"flow","to":"closed","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":760,"y":3660,"wires":[[]]},{"id":"7e27e777.3b3278","type":"change","z":"9c68d5ed.a5e358","name":"","rules":[{"t":"set","p":"coopdoor","pt":"flow","to":"open","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":750,"y":3700,"wires":[[]]},{"id":"f51fbc5a.37ee6","type":"change","z":"9c68d5ed.a5e358","name":"","rules":[{"t":"set","p":"coopdoor","pt":"flow","to":"closed","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":750,"y":3740,"wires":[[]]},{"id":"d573e89b.562b08","type":"function","z":"9c68d5ed.a5e358","name":"Read door states","func":"var door1 = flow.get(\"pentcoopdoor\");\nvar door2 = flow.get(\"coopdoor\");\nmsg.payload = door1 + \" \" + door2;\nreturn msg;\n","outputs":1,"noerr":0,"x":750,"y":3800,"wires":[["5c66bb7d.7f84b4"]]},{"id":"3271f33e.a8094c","type":"inject","z":"9c68d5ed.a5e358","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":520,"y":3800,"wires":[["d573e89b.562b08","6eb31f41.8a154"]]}]

It isn't really, much better to pass the values in messages and only use flow or global context when there is a real benefit.
You seem to have changed the way everything works. Why? You seemed to be virtually there with the first try.

@tompeel

  1. Your topics are Coop1 and Coop2 but you are referencing coop1 and coop2. These properties are case sensitive.

  2. You can simplify your JS logic as follows:

if (msg.payload.Coop1 !== msg.payload.Coop2) {
    msg.payload = "null";
} else {
    // msg.payload.Coop1 === msg.payload.Coop2
    msg.payload = msg.payload.Coop1 ? "open" : "closed";
}
return msg;

Revised (working) flow:

[{"id":"3dd954a5.db457c","type":"debug","z":"e08ca7c1.c46a88","name":"JoinDBG","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":720,"y":1720,"wires":[]},{"id":"58015a64.39a1c4","type":"join","z":"e08ca7c1.c46a88","name":"","mode":"custom","build":"object","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":true,"timeout":"","count":"2","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":483.5,"y":1800,"wires":[["316d06aa.39081a","3dd954a5.db457c"]]},{"id":"316d06aa.39081a","type":"function","z":"e08ca7c1.c46a88","name":"Test","func":"if (msg.payload.Coop1 !== msg.payload.Coop2) {\n    msg.payload = \"null\";\n} else {\n    // msg.payload.Coop1 === msg.payload.Coop2\n    msg.payload = msg.payload.Coop1 ? \"open\" : \"closed\";\n}\nreturn msg;","outputs":1,"noerr":0,"x":630,"y":1840,"wires":[["f1bb1851.a9d678"]]},{"id":"a8813f84.322ea","type":"inject","z":"e08ca7c1.c46a88","name":"","topic":"Coop1","payload":"true","payloadType":"bool","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":300,"y":1740,"wires":[["58015a64.39a1c4"]]},{"id":"fc9042ca.fbde3","type":"inject","z":"e08ca7c1.c46a88","name":"","topic":"Coop1","payload":"false","payloadType":"bool","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":310,"y":1780,"wires":[["58015a64.39a1c4"]]},{"id":"75cba044.4022d","type":"inject","z":"e08ca7c1.c46a88","name":"","topic":"Coop2","payload":"true","payloadType":"bool","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":300,"y":1820,"wires":[["58015a64.39a1c4"]]},{"id":"acbd1c4c.20b7c","type":"inject","z":"e08ca7c1.c46a88","name":"","topic":"Coop2","payload":"false","payloadType":"bool","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":310,"y":1860,"wires":[["58015a64.39a1c4"]]},{"id":"f1bb1851.a9d678","type":"debug","z":"e08ca7c1.c46a88","name":"OutputDBG","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":810,"y":1840,"wires":[]}]

Hi Colin, i know, i went down a few rabbit holes... and round in a few circles.... seems some of my problem could have been case sensitivity and me not paying attention...

Thanks! this seems to do exactly what i need!
Case sensitivity is a bit of a schoolboy but i like how you simplified my problem... ill look into what you ended up doing!

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