Button question about "remembering" state

This is a tricky one which has just come to light for me.

See flow.

The idea is that the "LED" can be turned on/off but pressing the on or off button.

Fair enough.

Here it is simple: all on one machine and on one screen.

The problem I have is not as easy.

I am wanting a way so I can have a button to press that will remember what was last pressed and re-send that.

So, say the machine on which the LED is rebooted/reset/what ever and it no longer knows the state of affairs for that LED, it can request the last known state to be re-sent to it.

For now I have a button for the sake of keeping it simple.

Anyone got any ideas on how to do this?

The flow has a comment node and function node "extra" which is leaning towards how I would think it could be done, but I am stuck with how to save that information and re-send it when the button is pressed.

[{"id":"482cb7b8.9089e8","type":"function","z":"ce6685c3.4b0cd","name":"Test","func":"msg.color = (msg.payload === \"ON\")?\"lime\":\"red\";\nreturn msg;","outputs":1,"noerr":0,"x":620,"y":640,"wires":[["8f0ac71f.d1914"]]},{"id":"b9c17396.622c","type":"inject","z":"ce6685c3.4b0cd","name":"","topic":"","payload":"ON","payloadType":"str","repeat":"","crontab":"","once":false,"x":320,"y":680,"wires":[["fa97900.48783f"]]},{"id":"8f0ac71f.d1914","type":"ui_text","z":"ce6685c3.4b0cd","group":"54ced452.f54c4c","order":1,"width":"3","height":"1","name":"","label":"LED","format":"<font color={{msg.color}} ><i class=\"fa fa-circle\" style=\"font-size:24px;\"></i></font>","layout":"row-left","x":910,"y":640,"wires":[]},{"id":"286b299e.170566","type":"inject","z":"ce6685c3.4b0cd","name":"","topic":"","payload":"OFF","payloadType":"str","repeat":"","crontab":"","once":false,"x":320,"y":600,"wires":[["6759ba0.1f3aa48"]]},{"id":"6759ba0.1f3aa48","type":"ui_button","z":"ce6685c3.4b0cd","name":"","group":"54ced452.f54c4c","order":2,"width":"3","height":"1","passthru":true,"label":"OFF","color":"","bgcolor":"","icon":"","payload":"OFF","payloadType":"str","topic":"","x":460,"y":600,"wires":[["482cb7b8.9089e8","481a6060.c53f48"]]},{"id":"fa97900.48783f","type":"ui_button","z":"ce6685c3.4b0cd","name":"","group":"54ced452.f54c4c","order":3,"width":"3","height":"1","passthru":true,"label":"ON","color":"","bgcolor":"","icon":"","payload":"ON","payloadType":"str","topic":"","x":460,"y":680,"wires":[["482cb7b8.9089e8","481a6060.c53f48"]]},{"id":"daae9940.ab86c","type":"comment","z":"ce6685c3.4b0cd","name":"Stuck here","info":"Below is sort of what is wanted.","x":650,"y":510,"wires":[]},{"id":"481a6060.c53f48","type":"function","z":"ce6685c3.4b0cd","name":"","func":"\nreturn msg;","outputs":1,"noerr":0,"x":590,"y":550,"wires":[["ce36cb30.c24de"]]},{"id":"ce36cb30.c24de","type":"ui_button","z":"ce6685c3.4b0cd","name":"","group":"54ced452.f54c4c","order":2,"width":"3","height":"1","passthru":true,"label":"Resend","color":"","bgcolor":"","icon":"","payload":"???","payloadType":"str","topic":"","x":730,"y":550,"wires":[["8f0ac71f.d1914"]]},{"id":"54ced452.f54c4c","type":"ui_group","z":"","name":"LED Testing","tab":"bf921cba.037998","disp":true,"width":"3","collapse":false},{"id":"bf921cba.037998","type":"ui_tab","z":"","name":"Tab","icon":"home","order":1}]

If your are stuck on saving information then it might help to revise this part of Node-RED documentation.

https://nodered.org/docs/writing-functions#storing-data

Probably the flow below will work as you want but I am not sure it will work as you need. The point is that the data will be saved to a global variable but this information will be lost in case of restart / reset of the computer running the flow. If saving information this way is an issue then you will have to pursue data persistence. There are plenty of posts in the forum that points on alternatives.

[{"id":"e3f6f534.128038","type":"tab","label":"Flow 8","disabled":false,"info":""},{"id":"ae12c198.5a1f5","type":"function","z":"e3f6f534.128038","name":"Set msg.color","func":"msg.color = (msg.payload === \"ON\")?\"lime\":\"red\";\n\nreturn msg;","outputs":1,"noerr":0,"x":600,"y":200,"wires":[["8ac839ce.a3d048","760930d4.701f7"]]},{"id":"5fd45c47.9a8644","type":"inject","z":"e3f6f534.128038","name":"","topic":"","payload":"ON","payloadType":"str","repeat":"","crontab":"","once":false,"x":110,"y":300,"wires":[["373f49e7.dfad06"]]},{"id":"8ac839ce.a3d048","type":"ui_text","z":"e3f6f534.128038","group":"9c56bc4b.bc79a","order":1,"width":"3","height":"1","name":"Display","label":"LED","format":"<font color={{msg.color}} ><i class=\"fa fa-circle\" style=\"font-size:24px;\"></i></font>","layout":"row-left","x":840,"y":200,"wires":[]},{"id":"23a86c12.f61d14","type":"inject","z":"e3f6f534.128038","name":"","topic":"","payload":"OFF","payloadType":"str","repeat":"","crontab":"","once":true,"onceDelay":"","x":110,"y":220,"wires":[["652ff918.77a3f8"]]},{"id":"652ff918.77a3f8","type":"ui_button","z":"e3f6f534.128038","name":"","group":"9c56bc4b.bc79a","order":2,"width":"3","height":"1","passthru":true,"label":"OFF","color":"","bgcolor":"","icon":"","payload":"OFF","payloadType":"str","topic":"","x":230,"y":220,"wires":[["c48e88f7.0b8af8"]]},{"id":"373f49e7.dfad06","type":"ui_button","z":"e3f6f534.128038","name":"","group":"9c56bc4b.bc79a","order":3,"width":"3","height":"1","passthru":true,"label":"ON","color":"","bgcolor":"","icon":"","payload":"ON","payloadType":"str","topic":"","x":230,"y":300,"wires":[["c48e88f7.0b8af8"]]},{"id":"bf59dbb2.527bf8","type":"ui_button","z":"e3f6f534.128038","name":"","group":"9c56bc4b.bc79a","order":2,"width":"3","height":"1","passthru":true,"label":"Resend","color":"","bgcolor":"","icon":"","payload":"resend","payloadType":"global","topic":"","x":380,"y":160,"wires":[["ae12c198.5a1f5"]]},{"id":"760930d4.701f7","type":"debug","z":"e3f6f534.128038","name":"Check Display","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","x":820,"y":300,"wires":[]},{"id":"c48e88f7.0b8af8","type":"change","z":"e3f6f534.128038","name":"Set global","rules":[{"t":"set","p":"resend","pt":"global","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":380,"y":260,"wires":[["ae12c198.5a1f5"]]},{"id":"9c56bc4b.bc79a","type":"ui_group","z":"","name":"LED Testing","tab":"749f2269.5291ac","disp":true,"width":"3","collapse":false},{"id":"749f2269.5291ac","type":"ui_tab","z":"","name":"Tab","icon":"home","order":1}]

Thank you very much Andrei.

I had started to mess around with context.set and context.get after posting.

I shall look at your version and see how it fits in to the scheme of things.

Write the state to a retained topic in MQTT and drive the led from that topic. Then when you reboot, the led will be automatically restored

Take note of @Andrei's comment that using the context will not save and restore values over a node-red restart.