Logic for input value

I have 4 inputs, and Input payload can be one of them ("x", "y" or "z"). And if payload is "z" then it with show msg.payload "1" otherwise zero, and among 4 inputs if atleast one input is "z" then the msg.payload is "1" and ignore remainning inputs. For example, 1st input is "z" then msg.payload is "1", and it will again 0 after 2 seconds, then 2nd input is again "z" then msg.payload then msg.payload will no change (means show "0"), till 4 inputs and then it will start again (if 5 number input is "z" then it will again show msg.payload "1". 4 inputs, will generate only one time msg.payload "1" if any one of then are "z".

Hello and welcome to the forum.

That is a confusing bunch of inputs and Zs, but I can't see an actual question :slight_smile:

I want to know, is there any node what I can use between input and output node and I will get the require output.

I am still unclear of your logic flow and objective, but as for a "node to do it all"... I think you will need to try a few and see what you can do with them.

A go to for the programmer type might be the Function node - with your own if/else/then JS code to run the msg.payload logic

I think to make things clearer you might want to draw up a table of possible inputs and the required outputs for each combination. From that we can perhaps advise on the best way to do this. It may also help clarify things in your own mind in finding a solution.

Also of great importance is whether the inputs are all in one message or coming in separately. If they are in separate messages then first you have to get them into one message so that they are all available for testing at the same time. See this article in the cookbook for an example of how to join messages into one object.

Thanks for your suggestion. Please have a look the below table. while input are coming in separately.

Have a look at the Ultimate Boolean nodes if the messages are coming in seperately.

Craig

I'm a bit confused. In your first post you said you had 4 inputs, but your table shows 12 inputs (and outputs?)

Is this a sequence of messages, where you can have one of 3 values (or is there a fourth value?!)? You then produce a "1" output message whenever the first "z" is received, and then "0" for all inputs until something other than "z" is received. Does that sound correct?

As @Colin said, some of the Boolean logic nodes might be of use, or possibly a simple function using a context variable to track the status of "z"-ness. Here's a simple example :

[{"id":"8ed26de0.a9c2d","type":"tab","label":"Flow 1","disabled":false,"info":""},{"id":"d72ab946.f3f4f8","type":"inject","z":"8ed26de0.a9c2d","name":"Inject \"y\"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"wibble","payload":"y","payloadType":"str","x":160,"y":280,"wires":[["2bc08e7b.d8f6d2"]]},{"id":"7dbc2421.5a9f6c","type":"inject","z":"8ed26de0.a9c2d","name":"Inject \"x\"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"wibble","payload":"x","payloadType":"str","x":160,"y":240,"wires":[["2bc08e7b.d8f6d2"]]},{"id":"b8d22224.d102","type":"inject","z":"8ed26de0.a9c2d","name":"Inject \"z\"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"wibble","payload":"z","payloadType":"str","x":160,"y":320,"wires":[["2bc08e7b.d8f6d2"]]},{"id":"2bc08e7b.d8f6d2","type":"function","z":"8ed26de0.a9c2d","name":"Check for \"z\"","func":"// Contex variable remembers whether we've seen a \"z\"\nvar seenZ = flow.get(\"zTracker\");\n\n// Check for a \"z\" in the message\nif (msg.payload == \"z\")\n{\n    // If we haven't seen a \"z\" recently, send a \"1\"\n    if (!seenZ)\n    {\n        msg.payload = \"1\";\n        seenZ = true;\n    }\n    else    // This is a repeated \"z\", send a \"0\"\n    {\n        msg.payload = \"0\";\n    }\n}\nelse    // Something other than \"z\", reset the context var\n{\n    msg.payload = \"0\";\n    seenZ = false;\n}\n\n// And save back to context\nflow.set(\"zTracker\", seenZ);\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":370,"y":280,"wires":[["6602dd8b.7a49f4"]]},{"id":"6602dd8b.7a49f4","type":"debug","z":"8ed26de0.a9c2d","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":560,"y":280,"wires":[]}]

Ok if you want it to reset every 4 messages then you would need to store the count and whether "z" had been sent already, then reset on count of 4.
eg.

[{"id":"4f47ae18.694","type":"inject","z":"c74669a0.6a34f8","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"x","payloadType":"str","x":120,"y":700,"wires":[["f3f414cc.86ec7"]]},{"id":"f3f414cc.86ec7","type":"function","z":"c74669a0.6a34f8","name":"","func":"let count = context.get(\"count\") || 0; // get current count\n// check count and reset if count hit limit\nif(count >= 4){\n    context.set([\"zzz\",\"count\"], [0,0]);\n    count=0;\n}\nlet zzz = context.get(\"zzz\") || 0 // // get if z has already been sent as 1\n//check payload and for z and if it has not been sent already\nif(msg.payload === \"z\" && zzz === 0){\n    msg.payload = 1;\n    context.set(\"zzz\",1);\n}else{\n    msg.payload = 0;\n}\n//increment count\ncontext.set(\"count\", count+1)\n// uncomment to view count while testing\n//msg.topic=\"count \"+ (count+1);\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":320,"y":740,"wires":[["9e7a84a2.048568"]]},{"id":"ac43d04a.172da","type":"inject","z":"c74669a0.6a34f8","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"y","payloadType":"str","x":120,"y":760,"wires":[["f3f414cc.86ec7"]]},{"id":"54aee329.3e42a4","type":"inject","z":"c74669a0.6a34f8","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"z","payloadType":"str","x":130,"y":800,"wires":[["f3f414cc.86ec7"]]},{"id":"9e7a84a2.048568","type":"debug","z":"c74669a0.6a34f8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":600,"y":740,"wires":[]}]

[edit] removed bug

1 Like

Ah, @E1cid's function and mine are similar, but do slightly different things. This is why it's important to fully explain the requirements for the problem.

I didn't notice that there was a "z" -> "0" on line 4 of the table, so I'm now even more unsure of exactly what is expected...

From the OP's original explanation and the table he provided, i think the below is what he is after, i could be mistaken though.

When payload equals z it should return 1, then it should ignore the next z, until after count of 4, then the next z will produce 1, again till count of 4. rinse and repeat.

That makes sense now I see line 4 of the table. However I think @Stes needs to provide a more detailed explanation as there could be several other possible answers.

Thanks it is working. And I need 2nd output is "1" when all inputs (4 inputs) are "z" otherwise "0".

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