This was working.
Not too old version of NR.
[{"id":"5dcb15fb.24d17c","type":"inject","z":"39ea4ac5.b46c1e","name":"Inject","repeat":"","crontab":"","once":true,"onceDelay":"3","topic":"","payload":"","payloadType":"str","x":420,"y":860,"wires":[["6272e4a0.8faa44"]]},{"id":"6272e4a0.8faa44","type":"function","z":"39ea4ac5.b46c1e","name":"Setup","func":"msg = {\n \"topic\":\"SETUP\",\n \"disabledColour\":\"brown\",\n \"colourA\": \"lime\",\n \"colourB\": \"green\",\n \"txtA\": \"Log\",\n \"txtB\": \"Log Stop\",\n \"txtclrA\": \"black\",\n \"txtclrB\": \"black\",\n \"payloadA\": \"GO\",\n \"payloadB\": \"STOP\",\n \"topicSET\": \"CONTROL\"\n}\nreturn msg;","outputs":1,"noerr":0,"x":420,"y":900,"wires":[["93f710a2.69e118"]]},{"id":"a999cdd7.d7fdc","type":"ui_button","z":"39ea4ac5.b46c1e","name":"Error Logging","group":"d057989d.1f3368","order":3,"width":"2","height":"1","passthru":false,"label":"{{msg.txt}}","tooltip":"","color":"{{msg.fontclr}}","bgcolor":"{{msg.colour}}","icon":"","payload":"X","payloadType":"str","topic":"","x":630,"y":860,"wires":[["93f710a2.69e118","763a7526.22a8f4"]]},{"id":"93f710a2.69e118","type":"function","z":"39ea4ac5.b46c1e","name":"Push Button","func":"// working\nvar state = context.get(\"STATE\")||0;\nvar enabled = context.get(\"ENABLED\")||0;\n\n// Look for a message with topic set to SETUP to set values.\nif (msg.topic == \"SETUP\") //Do this if the message is NOT \"X\"\n{\n //\n // Background colours first.\n //\n context.set(\"ABGC\", msg.colourA);\n context.set(\"BBGC\", msg.colourB);\n //\n // Disabled button background colour.\n //\n context.set(\"DISABLEDCLR\",msg.disabledColour);\n //\n // Now do text.\n //\n context.set(\"Atxt\", msg.txtA);\n context.set(\"Btxt\", msg.txtB);\n //\n // Font colours.\n //\n context.set(\"AFC\",msg.txtclrA);\n context.set(\"BFC\",msg.txtclrB);\n //\n // Payloads.\n //\n context.set(\"PayloadA\", msg.payloadA);\n context.set(\"PayloadB\", msg.payloadB);\n //\n // Topic.\n //\n if (msg.topicSET !== null)\n {\n context.set(\"Topic\",msg.topicSET);\n } else\n {\n context.set(\"Topic\",\"~\");\n }\n msg.txt = msg.txtA;\n msg.colour = msg.disabledColour;\n msg.fontclr = msg.txtclrA;\n return msg;\n}\n\n// Now on to the real stuff.\nif (msg.payload == \"X\")\n{\n //node.warn(\"X detected\");\n // Insert here code to enable other stuff.\n// if (context.get(\"ENABLED\") === 0)\n if (enabled === 0)\n {\n //\n // Old code.\n //\n //node.warn(\"Setting enable\");\n// context.set(\"ENABLED\",1);\n// msg.txt = context.get(\"Atxt\");\n// msg.colour = context.get(\"ABGC\");\n// msg.fontclr = context.get(\"AFC\");\n\n // New code.\n \n //node.warn(\"State = \" + state);\n\n if (state === 0)\n {\n //\n // Set things for state 0\n //\n //node.warn(\"State 0 detected\");\n msg.payload = context.get(\"PayloadA\");\n msg.colour = context.get(\"ABGC\");\n msg.txt = context.get(\"Atxt\");\n msg.fontclr = context.get(\"AFC\");\n }\n else if (state === 1)\n {\n //\n // Set things for state 1\n //\n //node.warn(\"State 1 detected\");\n msg.payload = context.get(\"PayloadB\");\n msg.colour = context.get(\"BBGC\");\n msg.txt = context.get(\"Btxt\");\n msg.fontclr = context.get(\"BFC\");\n }\n context.set(\"ENABLED\",1);\n return msg;\n }\n\n// if (context.get(\"ENABLED\") === 1)\n state = (state + 1)% 2;\n //node.warn(state);\n context.set(\"STATE\",state);\n if (enabled === 1)\n {\n if (state === 0)\n {\n // Condition A\n msg.payload = context.get(\"PayloadA\");\n msg.colour = context.get(\"ABGC\");\n msg.txt = context.get(\"Atxt\");\n msg.fontclr = context.get(\"AFC\");\n } else\n if (state === 1)\n {\n // Condition B\n msg.payload = context.get(\"PayloadB\");\n msg.colour = context.get(\"BBGC\");\n msg.txt = context.get(\"Btxt\");\n msg.fontclr = context.get(\"BFC\");\n }\n }\n \n if (context.get(\"Topic\") == \"~\")\n {\n msg.topic = \"\";\n } else\n {\n msg.topic = context.get(\"Topic\");\n }\n return msg;\n}\nif (msg.payload == \"Z\")\n{\n //\n ////node.warn(\"Timed out\");\n //\n context.set(\"ENABLED\",0);\n if (state === 0)\n {\n //\n // Set things for state 1\n //\n msg.payload = context.get(\"PayloadA\");\n msg.colour = context.get(\"DISABLEDCLR\");\n msg.txt = context.get(\"Atxt\");\n msg.fontclr = context.get(\"AFC\");\n }\n else if (state === 1)\n {\n //\n // Set things for state 2\n //\n msg.payload = context.get(\"PayloadB\");\n msg.colour = context.get(\"DISABLEDCLR\");\n msg.txt = context.get(\"Btxt\");\n msg.fontclr = context.get(\"BFC\");\n }\n return msg;\n}\n","outputs":1,"noerr":0,"initialize":"","finalize":"","x":620,"y":900,"wires":[["a999cdd7.d7fdc","1ddaae04.be6602"]]},{"id":"763a7526.22a8f4","type":"trigger","z":"39ea4ac5.b46c1e","name":"","op1":"","op2":"Z","op1type":"nul","op2type":"str","duration":"3","extend":false,"units":"s","reset":"","bytopic":"all","outputs":1,"x":610,"y":940,"wires":[["93f710a2.69e118"]]},{"id":"d057989d.1f3368","type":"ui_group","name":"Errors","tab":"41ad2f9a.ed0df","order":2,"disp":true,"width":"6","collapse":false},{"id":"41ad2f9a.ed0df","type":"ui_tab","name":"Logging","icon":"list","order":5,"disabled":false,"hidden":false}]
I updated the dashboard recently and things went awry.
Button versions: 2.26.2 and 2.28.1
Walk through of the code:
There is a button and it toggles between two messages indicating the mode.
The inject
node is run at the start to set up the context of the function
node (push button).
Break down of what I suspect:
The msg.topic
is some how kept at setup
when the button is pressed.
Though it is set to NOT pass through the message.
This code gets around the problem:
[{"id":"c5ccf203.8fff9","type":"inject","z":"39ea4ac5.b46c1e","name":"Inject","repeat":"","crontab":"","once":true,"onceDelay":"3","topic":"","payload":"","payloadType":"str","x":290,"y":440,"wires":[["a95c95ac.a0ab6"]]},{"id":"a95c95ac.a0ab6","type":"function","z":"39ea4ac5.b46c1e","name":"Setup","func":"msg = {\n \"topic\":\"SETUP\",\n \"disabledColour\":\"brown\",\n \"colourA\": \"lime\",\n \"colourB\": \"green\",\n \"txtA\": \"Display\",\n \"txtB\": \"Display Stop\",\n \"txtclrA\": \"black\",\n \"txtclrB\": \"black\",\n \"payloadA\": \"GO\",\n \"payloadB\": \"STOP\",\n \"topicSET\": \"CONTROL\"\n}\nreturn msg;","outputs":1,"noerr":0,"x":290,"y":480,"wires":[["e0d476a7.3e5e7"]]},{"id":"1d77a37e.a76b95","type":"ui_button","z":"39ea4ac5.b46c1e","name":"Commands RX List","group":"1baf0833.3ae918","order":1,"width":"2","height":"1","passthru":false,"label":"{{msg.txt}}","tooltip":"","color":"{{msg.fontclr}}","bgcolor":"{{msg.colour}}","icon":"","payload":"X","payloadType":"str","topic":"blah","topicType":"str","x":510,"y":440,"wires":[["e0d476a7.3e5e7","aa338cfc.ca1fd"]]},{"id":"e0d476a7.3e5e7","type":"function","z":"39ea4ac5.b46c1e","name":"Push Button","func":"var state = context.get(\"STATE\")||0;\nvar enabled = context.get(\"ENABLED\")||0;\n\n// Look for a message with topic set to SETUP to set values.\nif (msg.topic == \"SETUP\") //Do this if the message is NOT \"X\"\n{\n //\n // Background colours first.\n //\n context.set(\"ABGC\", msg.colourA);\n context.set(\"BBGC\", msg.colourB);\n //\n // Disabled button background colour.\n //\n context.set(\"DISABLEDCLR\",msg.disabledColour);\n //\n // Now do text.\n //\n context.set(\"Atxt\", msg.txtA);\n context.set(\"Btxt\", msg.txtB);\n //\n // Font colours.\n //\n context.set(\"AFC\",msg.txtclrA);\n context.set(\"BFC\",msg.txtclrB);\n //\n // Payloads.\n //\n context.set(\"PayloadA\", msg.payloadA);\n context.set(\"PayloadB\", msg.payloadB);\n //\n // Topic.\n //\n if (msg.topicSET !== null)\n {\n context.set(\"Topic\",msg.topicSET);\n } else\n {\n context.set(\"Topic\",\"~\");\n }\n msg.txt = msg.txtA;\n msg.colour = msg.disabledColour;\n msg.fontclr = msg.txtclrA;\n return msg;\n}\n\n// Now on to the real stuff.\nif (msg.payload == \"X\")\n{\n //node.warn(\"X detected\");\n // Insert here code to enable other stuff.\n// if (context.get(\"ENABLED\") === 0)\n if (enabled === 0)\n {\n //\n // Old code.\n //\n //node.warn(\"Setting enable\");\n// context.set(\"ENABLED\",1);\n// msg.txt = context.get(\"Atxt\");\n// msg.colour = context.get(\"ABGC\");\n// msg.fontclr = context.get(\"AFC\");\n\n // New code.\n \n //node.warn(\"State = \" + state);\n\n if (state === 0)\n {\n //\n // Set things for state 0\n //\n //node.warn(\"State 0 detected\");\n msg.payload = context.get(\"PayloadA\");\n msg.colour = context.get(\"ABGC\");\n msg.txt = context.get(\"Atxt\");\n msg.fontclr = context.get(\"AFC\");\n }\n else if (state === 1)\n {\n //\n // Set things for state 1\n //\n //node.warn(\"State 1 detected\");\n msg.payload = context.get(\"PayloadB\");\n msg.colour = context.get(\"BBGC\");\n msg.txt = context.get(\"Btxt\");\n msg.fontclr = context.get(\"BFC\");\n }\n context.set(\"ENABLED\",1);\n return msg;\n }\n\n// if (context.get(\"ENABLED\") === 1)\n state = (state + 1)% 2;\n //node.warn(state);\n context.set(\"STATE\",state);\n if (enabled === 1)\n {\n if (state === 0)\n {\n // Condition A\n msg.payload = context.get(\"PayloadA\");\n msg.colour = context.get(\"ABGC\");\n msg.txt = context.get(\"Atxt\");\n msg.fontclr = context.get(\"AFC\");\n } else\n if (state === 1)\n {\n // Condition B\n msg.payload = context.get(\"PayloadB\");\n msg.colour = context.get(\"BBGC\");\n msg.txt = context.get(\"Btxt\");\n msg.fontclr = context.get(\"BFC\");\n }\n }\n \n if (context.get(\"Topic\") == \"~\")\n {\n msg.topic = \"\";\n } else\n {\n msg.topic = context.get(\"Topic\");\n }\n return msg;\n}\nif (msg.payload == \"Z\")\n{\n //\n ////node.warn(\"Timed out\");\n //\n context.set(\"ENABLED\",0);\n if (state === 0)\n {\n //\n // Set things for state 1\n //\n msg.payload = context.get(\"PayloadA\");\n msg.colour = context.get(\"DISABLEDCLR\");\n msg.txt = context.get(\"Atxt\");\n msg.fontclr = context.get(\"AFC\");\n }\n else if (state === 1)\n {\n //\n // Set things for state 2\n //\n msg.payload = context.get(\"PayloadB\");\n msg.colour = context.get(\"DISABLEDCLR\");\n msg.txt = context.get(\"Btxt\");\n msg.fontclr = context.get(\"BFC\");\n }\n return msg;\n}\n","outputs":1,"noerr":0,"initialize":"","finalize":"","x":490,"y":480,"wires":[["1d77a37e.a76b95","217a2e14.0dbb4a"]]},{"id":"aa338cfc.ca1fd","type":"trigger","z":"39ea4ac5.b46c1e","name":"","op1":"","op2":"Z","op1type":"nul","op2type":"str","duration":"3","extend":false,"units":"s","reset":"","bytopic":"all","outputs":1,"x":480,"y":520,"wires":[["e0d476a7.3e5e7"]]},{"id":"1baf0833.3ae918","type":"ui_group","name":"Cmds Received","tab":"41ad2f9a.ed0df","order":1,"disp":true,"width":"6","collapse":false},{"id":"41ad2f9a.ed0df","type":"ui_tab","name":"Logging","icon":"list","order":5,"disabled":false,"hidden":false}]
Note the topic
is set in the button
node.
Seemingly it didn't used to be needed to be set.
This seems to be something happening with the button node.