I don't understand why this is *failing* - subflow output. I've tried to understand it, but failed

Sorry folks, I've just spent a lot of time (some by mistake) chasing shadows to what is going on.

I found some problems but not all.

Now I've found the crux of the matter.

To help I've supplied 3 versions. 1 working; 2 not.

First off the code:

[{"id":"1bc21d7f6ceb5285","type":"subflow","name":"Blinker O","info":"2023 08 05\nHad to change `delay` to not allow text.\n\n2021 12 7  \nA subflow to send pulsing messages at a given interval to make something *flash*.\n\nConfigurable by setting `environment` variables in the node's settings.\n\n\n# **Output toggle node to send alternating messages at a specified frequency.**\n# \n\n## 3 things need to be set to configure this subflow\n1 - `delay`:  milliseconds between toggles (override with msg.delay)\n\n2 - `msg1`:   text message for state 1  \n\n3 - `msg2`:   text message for state 2 \n\n`msg.topic` is output and alternates between `msg1` and `msg2` to indicate which message is being sent.\n\n## Optional:\n`offmsg` - sent when it is reset.\n\nMay be handy to make sure things are *off*.\n\n## Input message\nTo start send `msg` (anything)\n\nTo stop, send `msg.reset`","category":"","in":[{"x":60,"y":180,"wires":[{"id":"3a95f6976aee6fd0"}]}],"out":[{"x":920,"y":170,"wires":[{"id":"a655faf0bc379507","port":0},{"id":"f3b2b2634e2662cd","port":0}]},{"x":920,"y":220,"wires":[{"id":"f3b2b2634e2662cd","port":1}]}],"env":[{"name":"delay","type":"num","value":"","ui":{"type":"input","opts":{"types":["num","bin","env"]}}},{"name":"msg1","type":"str","value":""},{"name":"msg2","type":"str","value":""},{"name":"offmsg","type":"str","value":""}],"meta":{"module":"Toggler","version":"0.1.2","desc":"Toggles output between `msg1` and `msg2` set in node config"},"color":"#A6BBCF","icon":"node-red-contrib-bool-gate/switch.png","status":{"x":920,"y":270,"wires":[{"id":"502c279ae9a6f385","port":0}]}},{"id":"c53ebcc9e62a7552","type":"trigger","z":"1bc21d7f6ceb5285","name":"resend every x","op1":"1","op2":"0","op1type":"str","op2type":"str","duration":"-1","extend":false,"overrideDelay":true,"units":"s","reset":"","bytopic":"all","topic":"topic","outputs":1,"x":400,"y":180,"wires":[["f3b2b2634e2662cd"]]},{"id":"f3b2b2634e2662cd","type":"function","z":"1bc21d7f6ceb5285","name":"2022 02 26 *","func":"delete msg.delay;\nconst msgnull = null;\nconst msgS = {};\nlet c = context.get(\"counter\") ||0;\ncontext.set(\"counter\",((c + 1) % 2));\n\nif (c == 0) {\n    //  0\n    msg.payload = env.get(\"msg1\");\n    msg.topic = \"msg1\";\n    //\n    msgS.payload =c;\n    return[msg,msgnull,msgS];\n    //\n} else if (c == 1) {\n    //  1\n    msg.payload = env.get(\"msg2\");\n    msg.topic = \"msg2\";\n    //\n    msgS.payload =c;\n    return[msgnull,msg,msgS];\n    //\n};\n\n//msgS.payload = c;\n//return [msg,msgS];\n","outputs":3,"noerr":0,"initialize":"","finalize":"","libs":[],"x":590,"y":180,"wires":[[],[],["502c279ae9a6f385"]],"outputLabels":["msgA","msgB","which message"],"info":"This has 2 outputs to allow message modification straight out of subflowl\n`msg.stop` is sent to only the first output"},{"id":"c183245c1a6a7fac","type":"switch","z":"1bc21d7f6ceb5285","name":"is reset?","property":"reset","propertyType":"msg","rules":[{"t":"nnull"}],"checkall":"true","repair":false,"outputs":1,"x":380,"y":270,"wires":[["a9be61803cd67654","6e3a0cdb44f6a994"]]},{"id":"a9be61803cd67654","type":"switch","z":"1bc21d7f6ceb5285","name":"off msg?","property":"offmsg","propertyType":"env","rules":[{"t":"nempty"}],"checkall":"true","repair":false,"outputs":1,"x":580,"y":230,"wires":[["a655faf0bc379507"]]},{"id":"a655faf0bc379507","type":"change","z":"1bc21d7f6ceb5285","name":"set off message","rules":[{"t":"set","p":"payload","pt":"msg","to":"offmsg","tot":"env"},{"t":"delete","p":"reset","pt":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":780,"y":230,"wires":[[]]},{"id":"502c279ae9a6f385","type":"function","z":"1bc21d7f6ceb5285","name":"status","func":"const c = msg.payload;\n\nif (c == 0) {\n    msg.payload = ({text: \"msg1\"});\n} else if (c == 1) {\n    msg.payload = ({text: \"msg2\"});\n} else {\n    msg.payload = ({text:\"\"});\n};\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":750,"y":270,"wires":[[]]},{"id":"6e3a0cdb44f6a994","type":"change","z":"1bc21d7f6ceb5285","name":"reset","rules":[{"t":"set","p":"payload","pt":"msg","to":"2","tot":"num"}],"action":"","property":"","from":"","to":"","reg":false,"x":570,"y":270,"wires":[["502c279ae9a6f385"]]},{"id":"3a95f6976aee6fd0","type":"function","z":"1bc21d7f6ceb5285","name":"set msg.delay","func":"msg.delay = msg.delay || env.get(\"delay\") || 1000;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":200,"y":180,"wires":[["c53ebcc9e62a7552","c183245c1a6a7fac"]]},{"id":"bb19774fe916fd83","type":"inject","z":"d188b95f33e5f7e4","name":"Stop","props":[{"p":"reset","v":"reset","vt":"str"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":1200,"y":1570,"wires":[["15fad6aa8214bb83"]]},{"id":"15fad6aa8214bb83","type":"subflow:1bc21d7f6ceb5285","z":"d188b95f33e5f7e4","name":"text","env":[{"name":"delay","value":"2000","type":"num"},{"name":"msg1","value":"A","type":"str"},{"name":"msg2","value":"B","type":"str"},{"name":"offmsg","value":"C","type":"str"}],"x":1360,"y":1610,"wires":[["c55ae669e80e8b81"],["c55ae669e80e8b81"]]},{"id":"10ed964b959ead0c","type":"inject","z":"d188b95f33e5f7e4","name":"Start","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":1200,"y":1640,"wires":[["15fad6aa8214bb83"]]},{"id":"c55ae669e80e8b81","type":"debug","z":"d188b95f33e5f7e4","name":"Brightness","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","statusVal":"payload","statusType":"auto","x":1550,"y":1610,"wires":[]},{"id":"a5b78cc276b374f8","type":"subflow:1bc21d7f6ceb5285","z":"d188b95f33e5f7e4","name":"numbers (Problem)","env":[{"name":"delay","value":"2000","type":"num"},{"name":"msg1","value":"1","type":"num"},{"name":"msg2","value":"2","type":"num"},{"name":"offmsg","value":"3","type":"num"}],"x":1410,"y":1740,"wires":[["241b22025a53edf7"],["241b22025a53edf7"]]},{"id":"931662c3c3cf7d68","type":"inject","z":"d188b95f33e5f7e4","name":"Stop","props":[{"p":"reset","v":"reset","vt":"str"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":1200,"y":1700,"wires":[["a5b78cc276b374f8"]]},{"id":"35c4eaebf2a3faad","type":"inject","z":"d188b95f33e5f7e4","name":"Start","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":1200,"y":1770,"wires":[["a5b78cc276b374f8"]]},{"id":"241b22025a53edf7","type":"debug","z":"d188b95f33e5f7e4","name":"Brightness","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","statusVal":"payload","statusType":"auto","x":1610,"y":1740,"wires":[]},{"id":"5a7936c4e4196f41","type":"subflow:1bc21d7f6ceb5285","z":"d188b95f33e5f7e4","name":"problem","env":[{"name":"delay","value":"2000","type":"num"},{"name":"msg1","value":"A","type":"str"},{"name":"msg2","value":"B","type":"str"},{"name":"offmsg","value":"1","type":"num"}],"x":1370,"y":1860,"wires":[["c89c04b382f4ae32"],["c89c04b382f4ae32"]]},{"id":"a04b9d72ba8815ca","type":"inject","z":"d188b95f33e5f7e4","name":"Stop","props":[{"p":"reset","v":"reset","vt":"str"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":1200,"y":1820,"wires":[["5a7936c4e4196f41"]]},{"id":"5a0e73c1f6b10e93","type":"inject","z":"d188b95f33e5f7e4","name":"Start","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":1200,"y":1890,"wires":[["5a7936c4e4196f41"]]},{"id":"c89c04b382f4ae32","type":"debug","z":"d188b95f33e5f7e4","name":"Brightness","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","statusVal":"payload","statusType":"auto","x":1550,"y":1860,"wires":[]}]

No foreign nodes.

The idea is to have a toggling output at a given frequency and also be able to stop it.
With an option to have an off message to reset things - if needed.

I was and AM using it a fair bit but only out of luck I had never done it in a way that makes it fail.

What makes it fail?
If the offmsg is a NUMBER
If it is TEXT it works.

See example code.

If you press the START button it will start sending A then B and repeat every 2 seconds for ever.
Pressing the STOP button you get C - the stop message

But with the lower two, the stop message is a number and it isn't sent.

WHY?

I shall again state: I spent a lot of time chasing shadows but luckily resolved them before posting here.

This is (I hope) the best cut down version to show the problem as I understand it.

Someone - please?

Your offmsg test is incorrect.

You need
image

not null instead of not empty.

Thanks so much.

Forgive me for asking for help in getting my head around this.

I have this MAP Truth table (?) that shows me what is what WRT null etc.

Node-Red truth table

I am not getting how if offmsg is set to a it passes the not empty but if it is 1 (number) it fails.

No worries. To be honest, I don't worry too much about those details. I simply guessed (from experience) that it would be something like this so I just tried the other setting :slight_smile:

In this case, what does "empty" mean? Not really clear but probably relates to an "empty string" which is an immediate red-flag when moving from string to number.

1 Like

And I understand what you mean.

Alas I SEEM to get bogged down in that stuff sometimes, and after the ride I've been through working out the cause.... I am .... Yeah, ok. Empty string.

Noted.

I now just hope I remember it for next time.

Again: Thanks so much.

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