Help with weird messages in the wrong place

I had the code and so decided to put it in a subflow.
Ok: It isn't perfect. (There: I said it.) :wink:

But I am not getting what is going on.

This is the crux of it, and here's how it is supposed to work:
Online - the button is active(ated) and usable. It should show OFF (red and up)
Offline - the button should be disabled and have a ban icon over it.
Instead it is disabled but showing OFF condition.

What I've done and why I am here:
Populating it with debug nodes I am seeing incorrect messages being sent.

Code:

[{"id":"94cf2ea396e5b6f6","type":"subflow","name":"MQTT Button Toggle","info":"# 2023 01 25\nControl MQTT devices (power points)\nwith this node.\n\n`Button` node MUST have `topic` set to include\nthe device's name.  eg: `A1/button`\nand the payload MUST be `X`\n\nSet the `Device Name` to (eg) `A1` to\ncontrol `A1` device.\n","category":"","in":[{"x":70,"y":160,"wires":[{"id":"7207fbe19cc07923"}]}],"out":[{"x":990,"y":200,"wires":[{"id":"66fb54e88f4acb8d","port":0}]},{"x":1190,"y":160,"wires":[{"id":"27d2dd9a875ab1a5","port":0}]},{"x":1020,"y":60,"wires":[{"id":"bdb2c46daf62b36f","port":0}]},{"x":500,"y":50,"wires":[{"id":"4788b5265af1d45a","port":0}]},{"x":470,"y":260,"wires":[{"id":"4788b5265af1d45a","port":1}]},{"x":350,"y":30,"wires":[{"id":"541720ad73579447","port":0}]}],"env":[{"name":"DN","type":"str","value":"","ui":{"label":{"en-US":"Device Name"},"type":"input","opts":{"types":["str"]}}}],"meta":{},"color":"#DDAA99","inputLabels":["MQTT (tele and LWT) and button input"],"outputLabels":["Back to `button` input","MQTT output","OFF icon","Online","Offline","Power message"],"status":{"x":1070,"y":270,"wires":[{"id":"45a85f7621f5c28f","port":0}]}},{"id":"541720ad73579447","type":"switch","z":"94cf2ea396e5b6f6","name":"what message?","property":"topic","propertyType":"msg","rules":[{"t":"cont","v":"/stat/POWER","vt":"env"},{"t":"cont","v":"/tele/LWT","vt":"str"},{"t":"cont","v":"button","vt":"str"}],"checkall":"true","repair":false,"outputs":3,"x":195,"y":160,"wires":[["61843ca1684f9f9d"],["4788b5265af1d45a"],["5b8cd9fe254ea8d1"]],"l":false},{"id":"61843ca1684f9f9d","type":"delay","z":"94cf2ea396e5b6f6","name":"Delay","pauseType":"delay","timeout":"200","timeoutUnits":"milliseconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"allowrate":false,"outputs":1,"x":320,"y":80,"wires":[["bdb2c46daf62b36f"]]},{"id":"6d4704f6fc9b98be","type":"change","z":"94cf2ea396e5b6f6","name":"RESET","rules":[{"t":"set","p":"payload","pt":"msg","to":"RESET","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":330,"y":120,"wires":[["bdb2c46daf62b36f"]]},{"id":"4788b5265af1d45a","type":"switch","z":"94cf2ea396e5b6f6","name":"LWT","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"Online","vt":"str"},{"t":"eq","v":"Offline","vt":"str"}],"checkall":"true","repair":false,"outputs":2,"x":320,"y":160,"wires":[["6d4704f6fc9b98be","bc2aabd04561d534"],["6a82c12953657965"]]},{"id":"6a82c12953657965","type":"function","z":"94cf2ea396e5b6f6","name":"BAN","func":"msg = {icon: '<font color = \"red\"><i class=\"fa fa-ban fa-3x\"></i></font>'};\nreturn msg;","outputs":1,"noerr":0,"x":320,"y":200,"wires":[["8dc8a0dd2b669ae3"]]},{"id":"bc2aabd04561d534","type":"change","z":"94cf2ea396e5b6f6","name":"Enable","rules":[{"t":"set","p":"enabled","pt":"msg","to":"true","tot":"bool"},{"t":"set","p":"ARLEC-1","pt":"flow","to":"ONLINE","tot":"str"},{"t":"delete","p":"payload","pt":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":520,"y":160,"wires":[["66fb54e88f4acb8d"]]},{"id":"bdb2c46daf62b36f","type":"function","z":"94cf2ea396e5b6f6","name":"Set state","func":"let device = msg.topic.split(\"/\");\n//node.warn(\"Device \" + device[0]);\n//node.warn(\"Payload \" + msg.payload);\n\nif (msg.payload == \"RESET\")\n{\n    let state = context.get(device[0]);\n    if (state === 0)\n    {\n        //\n        msg= {\"icon\": '<font color = \"red\"><i class=\"fa fa-toggle-on fa-rotate-270 fa-3x\"></i></font>',\"background\":\"black\"};\n        return msg;\n    }\n    //\n}\nif (msg.payload == \"ON\")\n{\n    msg = { \"icon\": '<font color = \"lime\"><i class=\"fa fa-toggle-on fa-rotate-90 fa-3x\"></i></font>', \"background\": \"black\" };\n    context.set(\"STATE\",1);\n    context.set(device[0],1);\n} else\n{\n    msg = { \"icon\": '<font color = \"red\"><i class=\"fa fa-toggle-on fa-rotate-270 fa-3x\"></i></font>', \"background\": \"black\" };\n    context.set(\"STATE\",0);\n    context.set(device[0], 1);\n}\nreturn msg;\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":520,"y":120,"wires":[["66fb54e88f4acb8d"]]},{"id":"8dc8a0dd2b669ae3","type":"change","z":"94cf2ea396e5b6f6","name":"Disable","rules":[{"t":"set","p":"enabled","pt":"msg","to":"false","tot":"bool"},{"t":"set","p":"ARLEC-1","pt":"flow","to":"OFFLINE","tot":"str"},{"t":"delete","p":"payload","pt":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":520,"y":200,"wires":[["66fb54e88f4acb8d"]]},{"id":"5b8cd9fe254ea8d1","type":"function","z":"94cf2ea396e5b6f6","name":"Button control","func":"let msg1 = {};\n\nlet x = msg.payload;\nlet counter = context.get(\"counter\") || 0;\nif (x == \"X\")\n{\n    counter = (counter + 1) % 2;\n    context.set(\"counter\",counter);\n    if (counter === 0)\n    {\n        //\n        msg.payload = \"OFF\";\n        msg1 = {\"icon\": '<font color = \"orangered\"><i class=\"fa fa-toggle-on fa-rotate-270 fa-3x\"></i></font>',\"background\":\"black\"};\n\n    } else\n    {\n        //\n        msg.payload = \"ON\";\n        msg1 = {\"icon\": '<font color = \"orangered\"><i class=\"fa fa-toggle-on fa-rotate-90 fa-3x\"></i></font>',\"backgound\":\"black\"};\n    }\n}\nif (x == \"ON\")\n{\n    //  Force power ON\n    msg.payload = \"ON\";\n    msg1 = { \"icon\": '<font color = \"orangered\"><i class=\"fa fa-toggle-on fa-rotate-90 fa-3x\"></i></font>', \"backgound\": \"black\" };\n    context.set(\"counter\",1);\n}\nif (x == \"OFF\")\n{\n    //  Force power OFF\n    msg.payload = \"OFF\";\n    msg1 = { \"icon\": '<font color = \"orangered\"><i class=\"fa fa-toggle-on fa-rotate-90 fa-3x\"></i></font>', \"backgound\": \"black\" };\n    context.set(\"counter\",0);\n}\n\nreturn [msg,msg1];","outputs":2,"noerr":0,"initialize":"","finalize":"","libs":[],"x":750,"y":120,"wires":[["4f29a481b6f0d0e3"],["66fb54e88f4acb8d"]]},{"id":"4f29a481b6f0d0e3","type":"delay","z":"94cf2ea396e5b6f6","name":"Delay","pauseType":"delay","timeout":"100","timeoutUnits":"milliseconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"outputs":1,"x":1040,"y":120,"wires":[["27d2dd9a875ab1a5"]]},{"id":"27d2dd9a875ab1a5","type":"change","z":"94cf2ea396e5b6f6","name":"#1","rules":[{"t":"set","p":"topic","pt":"msg","to":"DN","tot":"env"},{"t":"set","p":"topic","pt":"msg","to":"topic&'/cmnd/power1'","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":1040,"y":160,"wires":[[]]},{"id":"7207fbe19cc07923","type":"switch","z":"94cf2ea396e5b6f6","name":"Only for this device","property":"topic","propertyType":"msg","rules":[{"t":"cont","v":"DN","vt":"env"}],"checkall":"true","repair":false,"outputs":1,"x":135,"y":160,"wires":[["541720ad73579447"]],"l":false},{"id":"0ae8f43ccf9a1016","type":"function","z":"94cf2ea396e5b6f6","d":true,"name":"Identify","func":"return msg;","outputs":1,"noerr":0,"initialize":"// Code added here will be run once\n// whenever the node is started.\nnode.warn(env.get(\"DN\"));\n","finalize":"","libs":[],"x":720,"y":270,"wires":[[]]},{"id":"45a85f7621f5c28f","type":"status","z":"94cf2ea396e5b6f6","name":"","scope":["0ae8f43ccf9a1016"],"x":860,"y":270,"wires":[[]]},{"id":"66fb54e88f4acb8d","type":"junction","z":"94cf2ea396e5b6f6","x":680,"y":200,"wires":[[]]},{"id":"f29f3ae3ad90c065","type":"subflow:94cf2ea396e5b6f6","z":"26262ba1.62dcbc","name":"","env":[{"name":"DN","value":"A1","type":"str"},{"name":"Device name","value":"A1","type":"str"}],"x":490,"y":4450,"wires":[["9ba2764071e69ef2","09bbf34582385e2b"],["9dda7b6776cbfe6e"],["d3e03c085bc82f40"],["117134ce1596018c"],["c1ca608c59f2d129"],["1e958047459a73c3"]]},{"id":"a360d4423ef1ec6c","type":"junction","z":"26262ba1.62dcbc","x":330,"y":4450,"wires":[["f29f3ae3ad90c065","d03d156fa9522ca9"]]},{"id":"9ba2764071e69ef2","type":"ui_button","z":"26262ba1.62dcbc","name":"A1","group":"5dde4bf66eb38aa1","order":0,"width":"1","height":"1","passthru":false,"label":"{{msg.icon}}","tooltip":"","color":"","bgcolor":"{{msg.background}}","className":"","icon":"","payload":"X","payloadType":"str","topic":"A1/button","topicType":"str","x":760,"y":4420,"wires":[["c2f5a62f6628cf5f"]]},{"id":"09bbf34582385e2b","type":"debug","z":"26262ba1.62dcbc","name":"To button","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":770,"y":4340,"wires":[]},{"id":"d3e03c085bc82f40","type":"trigger","z":"26262ba1.62dcbc","name":"","op1":"1","op2":"0","op1type":"str","op2type":"str","duration":"2","extend":false,"overrideDelay":false,"units":"s","reset":"","bytopic":"all","topic":"topic","outputs":1,"x":770,"y":4530,"wires":[[]]},{"id":"117134ce1596018c","type":"debug","z":"26262ba1.62dcbc","name":"ONLINE","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":770,"y":4570,"wires":[]},{"id":"c1ca608c59f2d129","type":"debug","z":"26262ba1.62dcbc","name":"OFFLINE","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":770,"y":4610,"wires":[]},{"id":"1e958047459a73c3","type":"debug","z":"26262ba1.62dcbc","name":"Power message","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":790,"y":4650,"wires":[]},{"id":"61ee633777cbbe4d","type":"link in","z":"26262ba1.62dcbc","name":"Button","links":["c2f5a62f6628cf5f"],"x":265,"y":4490,"wires":[["a360d4423ef1ec6c"]]},{"id":"e56d8c944518c96b","type":"inject","z":"26262ba1.62dcbc","name":"Offline","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"A1/tele/LWT","payload":"Offline","payloadType":"str","x":230,"y":4530,"wires":[["a360d4423ef1ec6c"]]},{"id":"836617ea351143cb","type":"inject","z":"26262ba1.62dcbc","name":"Online","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"A1/tele/LWT","payload":"Online","payloadType":"str","x":230,"y":4570,"wires":[["a360d4423ef1ec6c"]]},{"id":"d03d156fa9522ca9","type":"debug","z":"26262ba1.62dcbc","name":"Going IN","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":430,"y":4350,"wires":[]},{"id":"c2f5a62f6628cf5f","type":"link out","z":"26262ba1.62dcbc","name":"Button","mode":"link","links":["61ee633777cbbe4d"],"x":855,"y":4420,"wires":[]},{"id":"c42bfcfaec6f1e40","type":"inject","z":"26262ba1.62dcbc","name":"Button","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"A1/button","payload":"X","payloadType":"str","x":760,"y":4380,"wires":[["c2f5a62f6628cf5f"]]},{"id":"5dde4bf66eb38aa1","type":"ui_group","name":"A(1-4) Sockets","tab":"aa487daa.33c1c","order":9,"disp":true,"width":"4","collapse":false,"className":""},{"id":"aa487daa.33c1c","type":"ui_tab","name":"Real_World_Control","icon":"dashboard","order":3,"disabled":false,"hidden":false}]

OH!

You may need to set the properties of the subflow properties to A1 for the Device Name.

I press the offline node.
You see the offline message (green) going in.
(Blue) you see what is being sent to the button. (Hang on before asking)
But the RED message should not happen/be sent.
Note here That red message I showed in the above picture is from OUTPUT 6!

Why?
(Inside the subflow)

The going in message has a topic of A1/tele/LWT
So in the switch node it should to go output #2 -- yes?

Let's look at it a bit more.

Screenshot from 2023-01-25 16-07-56

Output #6 is connected to the FIRST output of the switch node, and we've just looked and that output is ONLY if the topic contains /stat/POWER.
And you can see that the message being sent in did NOT have that in the topic.

So why/how does it happen?

I'm stumped here and this is where I am at.

Thanks in advance.

BTW

I have this in a flow and it works.
If I put it in the subflow: It fails how I described.
Ok, correction:
It is nearly the same.
In the main flow there aren't the two switch nodes.

You see the $ as type marker next to /stat/POWER - defining this as an $env variable? I'd need to check what the runtime does in case this doesn't exist - guessing that it doesn't.
To fix, I propose to change the type to <string> as in the next rows of the switch node config.

1 Like

Thanks.

I think that is because I was trying other things before I got to that and forgot to change it back to text.

I think I had Confirmation bias when looking at it.

THANKS again.

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