Problem with syntax in function node for comparison

Once more, I think i have a priblem with my syntax.
I want to compare to things :slight_smile:The msg.payload sent previously by an Inject node (boolean true or false) and a global variable xNR_I1 wich can have the values "Active' or 'Inactive".

Then, according to the result, I set the msg.payload to "Entrée I activée" or "Entrée 1 désactivée".
But the msg.payload always returns the input msg.payload.

I suppose my test in not written correctly, but even after several syntax tests, that still don't work.
Here is my node :slight_smile:

[{"id":"360cf844.a58228","type":"function","z":"987ca6e6.534d28","name":"Test if Input 1 value has changed, and tweet it","func":"if\n((msg.payload === true) && (global.get(\"xNR_I1\") === \"Inactive\"))\n{msg.payload = \"Entree 1 Activée\"}\nelse\nif\n((msg.payload === false) && (global.get(\"xNR_I1\") === \"Active\"))\n{msg.payload = \"Entree 1 Désactivée\"}\nreturn msg;\n","outputs":1,"noerr":0,"x":1273.018798828125,"y":135.02500915527344,"wires":[["eda47117.c99aa","42fd53e8.195cbc"]]}]

The problem is that the conditional statement is not covering all the possible combinations.

What do you want to assign to msg.payload when:

1- msg.payload = true and global variable = Active ?
2- msg.payload = false and global variable = Inactive ?


In both cases you write, I just need nothing.

Those cases are not important for me, and so, I would like to exit the node without any result.

Envoyé de mon iPhone

Understood. There are a number of ways to code this algorithm. I would do like below.

Sometimes using the logical operator && can be confusing because of JavaScript short-circuit evaluation feature.

[{"id":"9e7fa9cb.268018","type":"tab","label":"Flow 6","disabled":false,"info":""},{"id":"46f941b9.06b7f","type":"function","z":"9e7fa9cb.268018","name":"Test if Input 1 value has changed, and tweet it","func":"let pay;\n\nlet gv = global.get(\"xNR_I1\");\nnode.status({text: gv});\n\n\nif (msg.payload === true) {\n    if (gv === \"Inactive\") pay = \"Entree 1 Activée\"\n    else pay = \"pas important\";\n}\n\nif (msg.payload === false) {\n    if (gv === \"Active\") pay = \"Entree 1 Désactivée\"\n    else pay = \"pas important\";\n}\n\nmsg.payload = pay;\n\nreturn msg;","outputs":1,"noerr":0,"x":460,"y":160,"wires":[["bb168d38.c13f8"]]},{"id":"621c8375.d1f21c","type":"inject","z":"9e7fa9cb.268018","name":"","topic":"","payload":"true","payloadType":"bool","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":190,"y":160,"wires":[["46f941b9.06b7f"]]},{"id":"453436c4.5e9888","type":"inject","z":"9e7fa9cb.268018","name":"","topic":"","payload":"false","payloadType":"bool","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":190,"y":220,"wires":[["46f941b9.06b7f"]]},{"id":"bb168d38.c13f8","type":"debug","z":"9e7fa9cb.268018","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":760,"y":160,"wires":[]}]

Thanks a lot.
Your code works well. I thought I wrote the same thing, but it seems that the script syntax doens't accept my version. I have understood your code, but I haven't understood why mine doesn't work.
Anyway, thanks a lot for your help.

Testing your code I verified that it will assign the expected strings to msg.payload in the following conditions:

You will get "Entree 1 Activee" when msg.payload === true AND global variable === Inactive. On the other hand, you will get "Entree 1 Desactivee" when msg.payload === false AND global variable === Active

In a couple of cases msg.payload will not receive a string from the code inside the function. Those are the cases that are not important.

Your code can behave like the one I proposed (as a matter of fact it will be even more robust) with a simple change depicted below. See line number 9.

Thanks you for your great help.