How to implement a fixed received value in node-red to output two different values in a loop?

I want to implement a fixed received value in node-red to loop through two different values. Suppose the value is the string “a”, I want the first time msg.payload receives the string “a” the output value is the string “b”, when msg.payload receives the string “a” again the output value is the string “c”, the third time msg.payload receives the string “a” the output value is the string “b”, and the fourth time msg.payload receives the string “a” the output value is the string “b”. payload receives the string “a” and the output value is the string “c”. … repeats the above loop, how do I set up a function to achieve this? Any help would be greatly appreciated!

In the iteration switch you can bind whatever output you want for every iteration (1,2,3,4,etc). In the last iteration it resets the counter so it loops.

[{"id":"d1f64219.aca37","type":"inject","z":"23a6237c.cc175c","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"a","payloadType":"str","x":190,"y":240,"wires":[["58d816b8.3f26f8"]]},{"id":"58d816b8.3f26f8","type":"counter","z":"23a6237c.cc175c","name":"","init":"0","step":"1","lower":"","upper":"","mode":"increment","outputs":1,"x":370,"y":240,"wires":[["eb3583b1.0a3a8"]]},{"id":"eb3583b1.0a3a8","type":"switch","z":"23a6237c.cc175c","name":"iteration","property":"count","propertyType":"msg","rules":[{"t":"eq","v":"1","vt":"num"},{"t":"eq","v":"2","vt":"num"},{"t":"eq","v":"3","vt":"num"},{"t":"eq","v":"4","vt":"num"},{"t":"eq","v":"5","vt":"str"}],"checkall":"true","repair":false,"outputs":5,"x":520,"y":240,"wires":[["a0701bed.f99bd8"],["100574f7.d40eeb"],["a0701bed.f99bd8"],["a0701bed.f99bd8"],["c63f0340.f2ce7","100574f7.d40eeb"]]},{"id":"c63f0340.f2ce7","type":"change","z":"23a6237c.cc175c","name":"Reset","rules":[{"t":"set","p":"reset","pt":"msg","to":"true","tot":"bool"}],"action":"","property":"","from":"","to":"","reg":false,"x":450,"y":340,"wires":[["58d816b8.3f26f8"]]},{"id":"a0701bed.f99bd8","type":"debug","z":"23a6237c.cc175c","name":"b","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":730,"y":220,"wires":[]},{"id":"100574f7.d40eeb","type":"debug","z":"23a6237c.cc175c","name":"c","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":730,"y":260,"wires":[]}]

Thank you very much for your help!

1 Like