Saving button background color while disabled

Thanks to both of you for your replies!

I eventually found this post:

That got me experimenting along new lines and I came up with this solution, which basically follows what gerry said. I am still new to this whole writing code thing, which is why I love Node-Red because I can use nodes for the bulk of the logic and learn some code in a function node on a very specific task.

It took a bit to figure out how to use node.send with multiple outputs, and then I clearly don't understand exactly what the timeout function is doing because it seems to be backwards compared to how I would think it would go but this code is working for me now.

Thanks for the tips on writing better code!

I still feel like an isolated enable command shouldn't reset the button state, it should just enable it. That feels like a bug to me but I'm not complaining. I'm grateful to have access to Node-Red at all!

[{"id":"dab9bb38.6245e8","type":"ui_button","z":"f1d06a71.51424","name":"State 2","group":"dd64112c.15ad78","order":22,"width":0,"height":0,"passthru":false,"label":"State 2","tooltip":"","color":"{{msg.color}}","bgcolor":"{{msg.background}}","icon":"","payload":"2","payloadType":"str","topic":"","x":300,"y":180,"wires":[["c8ca7726.51e9"]]},{"id":"6ff77c47.0a405c","type":"ui_button","z":"f1d06a71.51424","name":"State 1","group":"dd64112c.15ad78","order":21,"width":0,"height":0,"passthru":false,"label":"State 1","tooltip":"","color":"{{msg.color}}","bgcolor":"{{msg.background}}","icon":"","payload":"1","payloadType":"str","topic":"","x":300,"y":140,"wires":[["c8ca7726.51e9"]]},{"id":"c8ca7726.51e9","type":"function","z":"f1d06a71.51424","name":"Button State","func":"if (msg.payload == \"1\"){\n    setTimeout(function(){\n    msg1 = {enabled:true, background:\"#0094CE\", color:\"white\"};\n    msg2 = {enabled:true, background:\"#036288\", color:\"grey\"};\n    node.send([msg1, msg2]);\n    }, 5000);\n    msg1 = {enabled:false, background:\"#0094CE\", color:\"white\"};\n    msg2 = {enabled:false, background:\"#036288\", color:\"grey\"};\n    node.send([msg1, msg2]);\n}\nelse if (msg.payload == \"2\"){\n    setTimeout(function(){\n    msg1 = {enabled:true, background:\"#036288\", color:\"grey\"};\n    msg2 = {enabled:true, background:\"#0094CE\", color:\"white\"};\n    node.send([msg1, msg2]);\n    }, 5000);\n    msg1 = {enabled:false, background:\"#036288\", color:\"grey\"};\n    msg2 = {enabled:false, background:\"#0094CE\", color:\"white\"};\n    node.send([msg1, msg2]);\n}","outputs":2,"noerr":0,"x":290,"y":240,"wires":[["6ff77c47.0a405c"],["dab9bb38.6245e8"]]},{"id":"dd64112c.15ad78","type":"ui_group","z":"","name":"Test","tab":"6d2ffe37.0d7c8","disp":true,"width":"6","collapse":false},{"id":"6d2ffe37.0d7c8","type":"ui_tab","z":"","name":"Test","icon":"dashboard","disabled":false,"hidden":false}]
1 Like