Change node propery from directly from "function"

Hello all,

Rather than using the node "Change" I would like to type directly the change command (set msg.payload) in my already existing javascript function node. Is it possible?

Otherwise, it means that after my function, I will have to add one node for "switch" + one node for "change"... that will make my work ugly as I have to do this in many places.

Sure, you can alter the msg in a function node all you want.
msg.payload = ‘some string’;
or
msg.payload = 45;
Or
var n = 45;
msg.payload = n + 37;

Thank you zenofmud, my bad I was not too clear:

I mean I want to modify the payload value in the child node (my child node is a button, and the payload was set to "string" = "1", I wanted to change this setting dynamically.

Well actually I thought I found a way using "flow.set('myvar' , '1')" and configuring the child node to send the flow.myvar. instead of direct value.
But that will not work if I copy the same nodes for another component.. I think because the flow value is comon for the whole flow but I need a value that will affect only the child nodes.. well I still need to find a solution then...

I’m unclear on the terms you are using. What do you mean by ‘child’ node? Can you put together a small demo flow of what you are doing and your issue?

A ‘flow’ variable exists for all the nodes on that flow tab so a flow variable on tab1 is not useable on tab2. However if you have two flows on a single flow tab, they can share the same flow variable.

The uniqueness is the magic you are looking for. But that uniqueness needs also be known. Thus there is no way other but to create it for every flow you want to be easy to copy and use for similar target.

I created a sample sketch with comments so I think it will be more clear then.
The example is not complete (My button is currently only able to turn the LED ON for 2 hours). It should be very simple I think inside a js function (if led_state==1 then button-node.payload = 0 else button-node.payload = 1), but I can't find the solution.

I need to copy past my functions on the same flow tab.

[{"id":"7e114365.1742cc","type":"tab","label":"Flow 2","disabled":false,"info":""},{"id":"213f6259.fe852e","type":"trigger","z":"7e114365.1742cc","name":"","op1":"1","op2":"0","op1type":"str","op2type":"str","duration":"2","extend":true,"overrideDelay":false,"units":"hr","reset":"0","bytopic":"all","topic":"topic","outputs":1,"x":890,"y":160,"wires":[["408a5f7f.5fb5b"]]},{"id":"bb305fd3.a2729","type":"ui_button","z":"7e114365.1742cc","name":"","group":"50a4a77e.1d8888","order":5,"width":0,"height":0,"passthru":false,"label":"START/STOP LED","tooltip":"","color":"","bgcolor":"{{background}}","icon":"fa-lightbulb-o fa-2x","payload":"1","payloadType":"str","topic":"","topicType":"str","x":630,"y":160,"wires":[["213f6259.fe852e"]]},{"id":"fd04efe.5341c1","type":"function","z":"7e114365.1742cc","name":"ChangeColor","func":"var m = msg.payload[0];\nif (m === \"0\") {\n    msg.background = \"\"\n} else if (m === \"1\") {\n    msg.background = \"limegreen\"\n} else {\n    msg.background = \"grey\"\n}\n\nreturn msg","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":410,"y":160,"wires":[["bb305fd3.a2729"]]},{"id":"6eb35e51.f1372","type":"inject","z":"7e114365.1742cc","name":"LED STATE = 0","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"0","payloadType":"str","x":200,"y":140,"wires":[["fd04efe.5341c1"]]},{"id":"e0de3112.3e26a","type":"inject","z":"7e114365.1742cc","name":"LED STATE = 1","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"1","payloadType":"str","x":200,"y":180,"wires":[["fd04efe.5341c1"]]},{"id":"408a5f7f.5fb5b","type":"debug","z":"7e114365.1742cc","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1130,"y":160,"wires":[]},{"id":"561b524a.3ba6dc","type":"comment","z":"7e114365.1742cc","name":"LED STATE (MQTT INPUT)","info":"","x":160,"y":220,"wires":[]},{"id":"7926228.51b5ddc","type":"comment","z":"7e114365.1742cc","name":"Button to start the LED for 2 hours if LED state is = 0","info":"","x":690,"y":200,"wires":[]},{"id":"fc1ed48e.74f4f8","type":"comment","z":"7e114365.1742cc","name":"OR force reset trigger (and led) if led state = 1","info":"","x":670,"y":240,"wires":[]},{"id":"150f22aa.70d6ed","type":"comment","z":"7e114365.1742cc","name":"OBJECTIVE:","info":"I would like to add a function between the web UI and the Trigger node that will evaluate led state to define if we shoudl turn it ON or OFF.\n\nThen I must copy/past this complete sequence about 10 time to handle 10 different equipements. I want to change only the mqtt variables.","x":530,"y":300,"wires":[]},{"id":"bfbaaae8.091968","type":"comment","z":"7e114365.1742cc","name":"LED COMMAND (MQTT OUTPUT)","info":"","x":1140,"y":200,"wires":[]},{"id":"50a4a77e.1d8888","type":"ui_group","name":"Default","tab":"9dfab700.316b68","order":3,"disp":true,"width":"6","collapse":false},{"id":"9dfab700.316b68","type":"ui_tab","name":"1-Home","icon":"dashboard","order":1,"disabled":false,"hidden":false}]

So let me see if I understand what you want to do. You want to be able to push the dashboard button and if the LED is OFF, send a msg to turn the LED ON and after 2 hours automatically turn it OFF.
If it is ON you want to turn it OFF.

There will be several LED's that need this functionality.

Of thei is correce I have a voupe questions:
What does the mqtt-in data look like?
For each of the devices, does the topic have a unique indicator?
What version of Node-red are you using?

Yes the MQTT topics are all unique.
example :
mqtt-in -> "ha/arduino1/out1/state". value can be 1 or 0.
mqtt-out -> "ha/arduino1/out1/command". value can be 1 or 0.

my node-red version is 9.2.1

Be aware I am a beginner in node red so I assume the solution can be very simple in fact...
What I expect is to be able to modify dynamically the parameter "payload" of the ui node (see first copy screen below), the same way I did for the background color of this same ui node. Or find an alternative if that is not possible. but I want to avoid working with global or flow variables which will need to be modified when I copy/past the blocks.

image

image
image

What will the topic from/to another LED look like?

I'm trying to see if the mqtt msgs to each LED can be identified seperately.

umm, what?? the latest version of Node-RED is v2.0.5

Inside node red I see revision number : 1.3.5
Mhh. My node red is installed from "Home Assistant", so I assume I have the revision from HA addon. Anyway I use the latest addon.
image

image

Here is the complete example with 4 different outputs managed. Only change I need: the ui button must be able to reset the output if it is already ON.

[{"id":"c521a575.8f8748","type":"mqtt out","z":"561ce2e1.cea10c","name":"do3/cmd","topic":"HA/dev/NanoPrisesBureau/do5/cmd","qos":"","retain":"","respTopic":"","contentType":"","userProps":"","correl":"","expiry":"","broker":"3afcd276.6f631e","x":860,"y":100,"wires":[]},{"id":"661c8df3.c14074","type":"trigger","z":"561ce2e1.cea10c","name":"","op1":"1","op2":"0","op1type":"str","op2type":"str","duration":"1.75","extend":true,"overrideDelay":false,"units":"hr","reset":"0","bytopic":"all","topic":"topic","outputs":1,"x":690,"y":140,"wires":[["c521a575.8f8748"]]},{"id":"a8a79217.8d2e9","type":"ui_button","z":"561ce2e1.cea10c","name":"","group":"55aa4dc6.c7ee74","order":5,"width":0,"height":0,"passthru":false,"label":"Marche lampe bureau","tooltip":"","color":"","bgcolor":"{{background}}","icon":"fa-lightbulb-o fa-2x","payload":"1","payloadType":"str","topic":"","topicType":"str","x":440,"y":180,"wires":[["661c8df3.c14074","c521a575.8f8748"]]},{"id":"1fd66ba3.ba4ff4","type":"mqtt in","z":"561ce2e1.cea10c","name":"do5/state","topic":"HA/dev/NanoPrisesBureau/do5/state","qos":"2","datatype":"auto","broker":"3afcd276.6f631e","nl":false,"rap":true,"rh":0,"x":80,"y":100,"wires":[["6cac2b8.aa59fd4","432a9e0f.05ac","42c415ec.28abac"]]},{"id":"42c415ec.28abac","type":"function","z":"561ce2e1.cea10c","name":"ChangeColor","func":"var m = msg.payload[0];\nif (m === \"0\") {\n    msg.background = \"\"\n} else if (m === \"1\") {\n    msg.background = \"limegreen\"\n} else {\n    msg.background = \"grey\"\n}\n\nreturn msg","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":250,"y":180,"wires":[["a8a79217.8d2e9"]]},{"id":"6cac2b8.aa59fd4","type":"function","z":"561ce2e1.cea10c","name":"Reset","func":"var m = msg.payload[0];\nif (m === \"0\") {\n    msg.reset = true;\n    return msg;\n}\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":250,"y":140,"wires":[["661c8df3.c14074"]]},{"id":"432a9e0f.05ac","type":"ui_switch","z":"561ce2e1.cea10c","name":"Lampe du bureau","label":"","tooltip":"","group":"55aa4dc6.c7ee74","order":0,"width":0,"height":0,"passthru":false,"decouple":"true","topic":"","topicType":"str","style":"","onvalue":"1","onvalueType":"str","onicon":"","oncolor":"","offvalue":"0","offvalueType":"str","officon":"","offcolor":"","animate":false,"x":430,"y":100,"wires":[["c521a575.8f8748"]]},{"id":"305e9595.a0ceba","type":"mqtt in","z":"561ce2e1.cea10c","name":"Click BP 1 ecran PC","topic":"HA/dev/NanoPrisesBureau/di4/state","qos":"2","datatype":"auto","broker":"3afcd276.6f631e","nl":false,"rap":true,"rh":0,"x":110,"y":220,"wires":[["58541927.5c3068"]]},{"id":"c7a4190a.cd7d48","type":"comment","z":"561ce2e1.cea10c","name":"MARCHE TEMPORISEE LAMPE BUREAU","info":"","x":160,"y":60,"wires":[]},{"id":"f668016a.2f45","type":"mqtt in","z":"561ce2e1.cea10c","name":"LongPress BP 1 ecran PC","topic":"HA/dev/NanoPrisesBureau/di4/longpress","qos":"2","datatype":"auto","broker":"3afcd276.6f631e","nl":false,"rap":true,"rh":0,"x":130,"y":260,"wires":[["496b40c.3c3d1c"]]},{"id":"496b40c.3c3d1c","type":"function","z":"561ce2e1.cea10c","name":"Reset","func":"var m = msg.payload[0];\nif (m === \"1\") {\n    msg.payload = 0;\n    msg.reset = true;\n    return msg;\n}\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":470,"y":260,"wires":[["c521a575.8f8748"]]},{"id":"58541927.5c3068","type":"function","z":"561ce2e1.cea10c","name":"Set","func":"var m = msg.payload[0];\nif (m === \"1\") {\n    msg.set = true;\n    return msg;\n}\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":470,"y":220,"wires":[["661c8df3.c14074"]]},{"id":"2fd6ce9e.170b42","type":"mqtt out","z":"561ce2e1.cea10c","name":"do2/cmd","topic":"HA/dev/NanoPrisesBureau/do2/cmd","qos":"","retain":"","respTopic":"","contentType":"","userProps":"","correl":"","expiry":"","broker":"3afcd276.6f631e","x":860,"y":400,"wires":[]},{"id":"1a35addf.4c8b92","type":"trigger","z":"561ce2e1.cea10c","name":"","op1":"1","op2":"0","op1type":"str","op2type":"str","duration":"2","extend":true,"overrideDelay":false,"units":"hr","reset":"","bytopic":"all","topic":"topic","outputs":1,"x":650,"y":440,"wires":[["2fd6ce9e.170b42"]]},{"id":"649230a3.7dfff","type":"ui_button","z":"561ce2e1.cea10c","name":"","group":"55aa4dc6.c7ee74","order":5,"width":0,"height":0,"passthru":false,"label":"Marche Ampli","tooltip":"","color":"","bgcolor":"{{background}}","icon":"fa-volume-up fa-2x","payload":"1","payloadType":"str","topic":"","topicType":"str","x":420,"y":480,"wires":[["1a35addf.4c8b92","2fd6ce9e.170b42"]]},{"id":"7bb3800a.ac90f","type":"mqtt in","z":"561ce2e1.cea10c","name":"do2/state","topic":"HA/dev/NanoPrisesBureau/do2/state","qos":"2","datatype":"auto","broker":"3afcd276.6f631e","nl":false,"rap":true,"rh":0,"x":80,"y":400,"wires":[["7cdd8184.6407b","eb66a2a2.5f40b","c69e2db3.b886f"]]},{"id":"c69e2db3.b886f","type":"function","z":"561ce2e1.cea10c","name":"ChangeColor","func":"var m = msg.payload[0];\nif (m === \"0\") {\n    msg.background = \"\"\n} else if (m === \"1\") {\n    msg.background = \"limegreen\"\n} else {\n    msg.background = \"grey\"\n}\n\nreturn msg","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":250,"y":480,"wires":[["649230a3.7dfff"]]},{"id":"7cdd8184.6407b","type":"function","z":"561ce2e1.cea10c","name":"Reset","func":"var m = msg.payload[0];\nif (m === \"0\") {\n    msg.reset = true;\n    return msg;\n}\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":250,"y":440,"wires":[["1a35addf.4c8b92"]]},{"id":"eb66a2a2.5f40b","type":"ui_switch","z":"561ce2e1.cea10c","name":"Ampli","label":"","tooltip":"","group":"55aa4dc6.c7ee74","order":0,"width":0,"height":0,"passthru":false,"decouple":"true","topic":"","topicType":"str","style":"","onvalue":"1","onvalueType":"str","onicon":"","oncolor":"","offvalue":"0","offvalueType":"str","officon":"","offcolor":"","animate":false,"x":390,"y":400,"wires":[["2fd6ce9e.170b42"]]},{"id":"9ef4ac13.0094f","type":"mqtt in","z":"561ce2e1.cea10c","name":"Click BP 2 ecran PC","topic":"HA/dev/NanoPrisesBureau/di3/state","qos":"2","datatype":"auto","broker":"3afcd276.6f631e","nl":false,"rap":true,"rh":0,"x":110,"y":520,"wires":[["f1256038.a9c63"]]},{"id":"e9e6e138.ffc3e","type":"comment","z":"561ce2e1.cea10c","name":"MARCHE TEMPORISEE AMPLI","info":"","x":130,"y":360,"wires":[]},{"id":"d7fd9d42.07da1","type":"mqtt in","z":"561ce2e1.cea10c","name":"LongPress BP 2 ecran PC","topic":"HA/dev/NanoPrisesBureau/di3/longpress","qos":"2","datatype":"auto","broker":"3afcd276.6f631e","nl":false,"rap":true,"rh":0,"x":130,"y":560,"wires":[["3a95fe09.e2a492"]]},{"id":"3a95fe09.e2a492","type":"function","z":"561ce2e1.cea10c","name":"Reset","func":"var m = msg.payload[0];\nif (m === \"1\") {\n    msg.payload = 0;\n    msg.reset = true;\n    return msg;\n}\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":470,"y":560,"wires":[["2fd6ce9e.170b42"]]},{"id":"f1256038.a9c63","type":"function","z":"561ce2e1.cea10c","name":"Set","func":"var m = msg.payload[0];\nif (m === \"1\") {\n    msg.set = true;\n    return msg;\n}\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":470,"y":520,"wires":[["1a35addf.4c8b92"]]},{"id":"72aaa51a.3e080c","type":"mqtt in","z":"561ce2e1.cea10c","name":"Click BP 5 ecran PC","topic":"HA/dev/NanoPrisesBureau/di0/state","qos":"2","datatype":"auto","broker":"3afcd276.6f631e","nl":false,"rap":true,"rh":0,"x":110,"y":300,"wires":[["496b40c.3c3d1c"]]},{"id":"bde94fec.51e99","type":"mqtt in","z":"561ce2e1.cea10c","name":"Click BP 5 ecran PC","topic":"HA/dev/NanoPrisesBureau/di0/state","qos":"2","datatype":"auto","broker":"3afcd276.6f631e","nl":false,"rap":true,"rh":0,"x":110,"y":600,"wires":[["3a95fe09.e2a492"]]},{"id":"1e2bfeff.70dd01","type":"mqtt out","z":"561ce2e1.cea10c","name":"do3/cmd","topic":"HA/dev/NanoPrisesBureau/do3/cmd","qos":"","retain":"","respTopic":"","contentType":"","userProps":"","correl":"","expiry":"","broker":"3afcd276.6f631e","x":860,"y":700,"wires":[]},{"id":"415d4fc5.fe011","type":"trigger","z":"561ce2e1.cea10c","name":"","op1":"1","op2":"0","op1type":"str","op2type":"str","duration":"2","extend":true,"overrideDelay":false,"units":"hr","reset":"","bytopic":"all","topic":"topic","outputs":1,"x":650,"y":740,"wires":[["1e2bfeff.70dd01"]]},{"id":"26ed38f4.105f88","type":"ui_button","z":"561ce2e1.cea10c","name":"","group":"55aa4dc6.c7ee74","order":5,"width":0,"height":0,"passthru":false,"label":"Marche multiprise lit","tooltip":"","color":"","bgcolor":"{{background}}","icon":"","payload":"1","payloadType":"str","topic":"","topicType":"str","x":440,"y":780,"wires":[["415d4fc5.fe011","1e2bfeff.70dd01"]]},{"id":"c003534f.a62c","type":"mqtt in","z":"561ce2e1.cea10c","name":"do3/state","topic":"HA/dev/NanoPrisesBureau/do3/state","qos":"2","datatype":"auto","broker":"3afcd276.6f631e","nl":false,"rap":true,"rh":0,"x":80,"y":700,"wires":[["e4b6d60a.375958","eafeb05f.fcf41","21bbfd8f.8a0ee2"]]},{"id":"21bbfd8f.8a0ee2","type":"function","z":"561ce2e1.cea10c","name":"ChangeColor","func":"var m = msg.payload[0];\nif (m === \"0\") {\n    msg.background = \"\"\n} else if (m === \"1\") {\n    msg.background = \"limegreen\"\n} else {\n    msg.background = \"grey\"\n}\n\nreturn msg","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":250,"y":780,"wires":[["26ed38f4.105f88"]]},{"id":"e4b6d60a.375958","type":"function","z":"561ce2e1.cea10c","name":"Reset","func":"var m = msg.payload[0];\nif (m === \"0\") {\n    msg.reset = true;\n    return msg;\n}\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":250,"y":740,"wires":[["415d4fc5.fe011"]]},{"id":"eafeb05f.fcf41","type":"ui_switch","z":"561ce2e1.cea10c","name":"Prise 1 : Lit","label":"","tooltip":"","group":"55aa4dc6.c7ee74","order":0,"width":0,"height":0,"passthru":false,"decouple":"true","topic":"","topicType":"str","style":"","onvalue":"1","onvalueType":"str","onicon":"","oncolor":"","offvalue":"0","offvalueType":"str","officon":"","offcolor":"","animate":false,"x":410,"y":700,"wires":[["1e2bfeff.70dd01"]]},{"id":"7d1594e7.3ed5bc","type":"mqtt in","z":"561ce2e1.cea10c","name":"Click BP 3 ecran PC","topic":"HA/dev/NanoPrisesBureau/di2/state","qos":"2","datatype":"auto","broker":"3afcd276.6f631e","nl":false,"rap":true,"rh":0,"x":110,"y":820,"wires":[["ccfe171e.563b48"]]},{"id":"610605ee.79cf6c","type":"comment","z":"561ce2e1.cea10c","name":"MARCHE MULTIPRISE LIT","info":"","x":120,"y":660,"wires":[]},{"id":"1b824610.082fda","type":"mqtt in","z":"561ce2e1.cea10c","name":"LongPress BP 3 ecran PC","topic":"HA/dev/NanoPrisesBureau/di2/longpress","qos":"2","datatype":"auto","broker":"3afcd276.6f631e","nl":false,"rap":true,"rh":0,"x":130,"y":860,"wires":[["586dec46.7ab7d4"]]},{"id":"586dec46.7ab7d4","type":"function","z":"561ce2e1.cea10c","name":"Reset","func":"var m = msg.payload[0];\nif (m === \"1\") {\n    msg.payload = 0;\n    msg.reset = true;\n    return msg;\n}\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":470,"y":860,"wires":[["1e2bfeff.70dd01"]]},{"id":"ccfe171e.563b48","type":"function","z":"561ce2e1.cea10c","name":"Set","func":"var m = msg.payload[0];\nif (m === \"1\") {\n    msg.set = true;\n    return msg;\n}\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":470,"y":820,"wires":[["415d4fc5.fe011"]]},{"id":"7c6775d2.6d226c","type":"mqtt in","z":"561ce2e1.cea10c","name":"Click BP 5 ecran PC","topic":"HA/dev/NanoPrisesBureau/di0/state","qos":"2","datatype":"auto","broker":"3afcd276.6f631e","nl":false,"rap":true,"rh":0,"x":110,"y":900,"wires":[["586dec46.7ab7d4"]]},{"id":"c0b310b9.94ff1","type":"mqtt out","z":"561ce2e1.cea10c","name":"do4/cmd","topic":"HA/dev/NanoPrisesBureau/do4/cmd","qos":"","retain":"","respTopic":"","contentType":"","userProps":"","correl":"","expiry":"","broker":"3afcd276.6f631e","x":860,"y":1000,"wires":[]},{"id":"e0cdcb.2bc07238","type":"trigger","z":"561ce2e1.cea10c","name":"","op1":"1","op2":"0","op1type":"str","op2type":"str","duration":"2","extend":true,"overrideDelay":false,"units":"hr","reset":"","bytopic":"all","topic":"topic","outputs":1,"x":650,"y":1040,"wires":[["c0b310b9.94ff1"]]},{"id":"38bbfbf2.97d154","type":"ui_button","z":"561ce2e1.cea10c","name":"","group":"55aa4dc6.c7ee74","order":5,"width":0,"height":0,"passthru":false,"label":"Marche prise 4","tooltip":"","color":"","bgcolor":"{{background}}","icon":"","payload":"1","payloadType":"str","topic":"","topicType":"str","x":420,"y":1080,"wires":[["e0cdcb.2bc07238","c0b310b9.94ff1"]]},{"id":"867a457c.bc6478","type":"mqtt in","z":"561ce2e1.cea10c","name":"do2/state","topic":"HA/dev/NanoPrisesBureau/do4/state","qos":"2","datatype":"auto","broker":"3afcd276.6f631e","nl":false,"rap":true,"rh":0,"x":80,"y":1000,"wires":[["343ce58b.051aea","f8807b77.d51928","775a7393.bfca3c"]]},{"id":"775a7393.bfca3c","type":"function","z":"561ce2e1.cea10c","name":"ChangeColor","func":"var m = msg.payload[0];\nif (m === \"0\") {\n    msg.background = \"\"\n} else if (m === \"1\") {\n    msg.background = \"limegreen\"\n} else {\n    msg.background = \"grey\"\n}\n\nreturn msg","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":250,"y":1080,"wires":[["38bbfbf2.97d154"]]},{"id":"343ce58b.051aea","type":"function","z":"561ce2e1.cea10c","name":"Reset","func":"var m = msg.payload[0];\nif (m === \"0\") {\n    msg.reset = true;\n    return msg;\n}\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":250,"y":1040,"wires":[["e0cdcb.2bc07238"]]},{"id":"f8807b77.d51928","type":"ui_switch","z":"561ce2e1.cea10c","name":"Prise 4","label":"","tooltip":"","group":"55aa4dc6.c7ee74","order":0,"width":0,"height":0,"passthru":false,"decouple":"true","topic":"","topicType":"str","style":"","onvalue":"1","onvalueType":"str","onicon":"","oncolor":"","offvalue":"0","offvalueType":"str","officon":"","offcolor":"","animate":false,"x":400,"y":1000,"wires":[["c0b310b9.94ff1"]]},{"id":"1c12b7a2.408bb8","type":"mqtt in","z":"561ce2e1.cea10c","name":"Click BP 4 ecran PC","topic":"HA/dev/NanoPrisesBureau/di1/state","qos":"2","datatype":"auto","broker":"3afcd276.6f631e","nl":false,"rap":true,"rh":0,"x":110,"y":1120,"wires":[["a42b1ab6.963bb8"]]},{"id":"3d8f4db2.fe5702","type":"comment","z":"561ce2e1.cea10c","name":"MARCHE PRISE 4","info":"","x":90,"y":960,"wires":[]},{"id":"c6fc4bce.fc45e8","type":"mqtt in","z":"561ce2e1.cea10c","name":"LongPress BP 4 ecran PC","topic":"HA/dev/NanoPrisesBureau/di1/longpress","qos":"2","datatype":"auto","broker":"3afcd276.6f631e","nl":false,"rap":true,"rh":0,"x":130,"y":1160,"wires":[["39ca1d86.3902c2"]]},{"id":"39ca1d86.3902c2","type":"function","z":"561ce2e1.cea10c","name":"Reset","func":"var m = msg.payload[0];\nif (m === \"1\") {\n    msg.payload = 0;\n    msg.reset = true;\n    return msg;\n}\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":470,"y":1160,"wires":[["c0b310b9.94ff1"]]},{"id":"a42b1ab6.963bb8","type":"function","z":"561ce2e1.cea10c","name":"Set","func":"var m = msg.payload[0];\nif (m === \"1\") {\n    msg.set = true;\n    return msg;\n}\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":470,"y":1120,"wires":[["e0cdcb.2bc07238"]]},{"id":"1390051d.cf09db","type":"mqtt in","z":"561ce2e1.cea10c","name":"Click BP 5 ecran PC","topic":"HA/dev/NanoPrisesBureau/di0/state","qos":"2","datatype":"auto","broker":"3afcd276.6f631e","nl":false,"rap":true,"rh":0,"x":110,"y":1200,"wires":[["39ca1d86.3902c2"]]},{"id":"3afcd276.6f631e","type":"mqtt-broker","name":"beelink","broker":"192.168.1.39","port":"1883","clientid":"","usetls":false,"protocolVersion":"4","keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","birthMsg":{},"closeTopic":"","closeQos":"0","closePayload":"","closeMsg":{},"willTopic":"","willQos":"0","willPayload":"","willMsg":{},"sessionExpiry":""},{"id":"55aa4dc6.c7ee74","type":"ui_group","name":"Multiprise Bureau","tab":"9dfab700.316b68","order":3,"disp":true,"width":"6","collapse":false},{"id":"9dfab700.316b68","type":"ui_tab","name":"1-Home","icon":"dashboard","order":1,"disabled":false,"hidden":false}]

Ok I think I found something that will help me a lot! What I need is to work with "context variables"?
I will give it a try right now (well.. it will still be a complex process for a simple task but...)

  • The context object -stores data for a node
  • The Flow object – stores data for a flow
  • The global object -stores data for the canvas

Storing Data in Node-Red Variables (stevesnoderedguide.com)

You should try to unclutter you flow. You have four mqtt-in nodes 'Click BP 5 ecran PC' getting the same message. Why not have one and use a link-out with four link-in nodes?

you could have one mqtt-in node with this topic 'HA/dev/NanoPrisesBureau/*/state' and a switch to check if the topic contained 'di1' or 'di2' etc, and send the msg on to the next part of each flow leg.

in the long run It should make it easier to maintain.

I did not know/thought about it, but I agree it will be easier to maintain, I will look forward to apply this solution now.
(yet that will not solve my original issue :frowning: ).

Maybe this is relevant, another way to implement colour changing buttons which turn off after a delay.
The payload input from MQTT should be "ON" or "OFF" ("TOGGLE" works too) - no doubt you can easily change that to 0 or 1
MQTT topic in this demo is "Button1" or "Button2" and it is preserved through to the output.

[{"id":"4289f334.5d859c","type":"tab","label":"Buttons","disabled":false,"info":""},{"id":"45c50ce0.fb4214","type":"ui_button","z":"4289f334.5d859c","name":"Button 1","group":"a6e358b.672ffa8","order":1,"width":"3","height":"1","passthru":false,"label":"Button 1","tooltip":"","color":"{{foreground}}","bgcolor":"{{background}}","icon":"","payload":"toggle","payloadType":"str","topic":"","topicType":"str","x":420,"y":140,"wires":[["6b8aab19.4127d4"]]},{"id":"6b8aab19.4127d4","type":"function","z":"4289f334.5d859c","name":"","func":"const thisbutton = \"Button1\";\nstate = flow.get (thisbutton);\nif (msg.payload != state)\n{\n    if (state === \"ON\")\n    {\n        msg.payload = \"OFF\";\n        msg.foreground = \"#33ee00\";\n        msg.background = \"grey\";\n    }\n    else\n    {\n        msg.payload = \"ON\";\n        msg.foreground = \"#ff3300\";\n        msg.background =\"limegreen\";\n    }\n    flow.set(thisbutton, msg.payload);\n    return msg;\n}","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":420,"y":180,"wires":[["45c50ce0.fb4214","b0b3e878.c8b67","d9b56f1f.001f8"]]},{"id":"d9b56f1f.001f8","type":"trigger","z":"4289f334.5d859c","name":"Countdown OFF","op1":"","op2":"OFF","op1type":"nul","op2type":"str","duration":"10","extend":true,"overrideDelay":false,"units":"s","reset":"OFF","bytopic":"all","topic":"topic","outputs":1,"x":440,"y":220,"wires":[["6b8aab19.4127d4"]]},{"id":"b0b3e878.c8b67","type":"debug","z":"4289f334.5d859c","name":"To MQTT Out","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":640,"y":180,"wires":[]},{"id":"99cbcfa.f0ba3b","type":"comment","z":"4289f334.5d859c","name":"These buttons turn off after 10 sec","info":"","x":500,"y":100,"wires":[]},{"id":"8de619e8.f0fbb8","type":"inject","z":"4289f334.5d859c","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"Button1","payload":"ON","payloadType":"str","x":110,"y":180,"wires":[["6b8aab19.4127d4"]]},{"id":"3714b719.eaebf8","type":"inject","z":"4289f334.5d859c","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":true,"onceDelay":"0.2","topic":"Button1","payload":"OFF","payloadType":"str","x":110,"y":140,"wires":[["6b8aab19.4127d4"]]},{"id":"f5d1bb54.6606a","type":"inject","z":"4289f334.5d859c","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"Button1","payload":"TOGGLE","payloadType":"str","x":120,"y":220,"wires":[["6b8aab19.4127d4"]]},{"id":"4f64882f3a8c32cc","type":"ui_button","z":"4289f334.5d859c","name":"Button 2","group":"a6e358b.672ffa8","order":2,"width":"3","height":"1","passthru":false,"label":"Button2","tooltip":"","color":"{{foreground}}","bgcolor":"{{background}}","icon":"","payload":"toggle","payloadType":"str","topic":"","topicType":"str","x":420,"y":300,"wires":[["467435513c4c743d"]]},{"id":"467435513c4c743d","type":"function","z":"4289f334.5d859c","name":"","func":"const thisbutton = \"Button2\";\nstate = flow.get (thisbutton);\nif (msg.payload != state)\n{\n    if (state === \"ON\")\n    {\n        msg.payload = \"OFF\";\n        msg.foreground = \"#33ee00\";\n        msg.background = \"grey\";\n    }\n    else\n    {\n        msg.payload = \"ON\";\n        msg.foreground = \"#ff3300\";\n        msg.background =\"limegreen\";\n    }\n    flow.set(thisbutton, msg.payload);\n    return msg;\n}","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":420,"y":340,"wires":[["4f64882f3a8c32cc","2bf62b2d1bdafe19","47b6fa590f682aab"]]},{"id":"47b6fa590f682aab","type":"trigger","z":"4289f334.5d859c","name":"Countdown OFF","op1":"","op2":"OFF","op1type":"nul","op2type":"str","duration":"10","extend":true,"overrideDelay":false,"units":"s","reset":"OFF","bytopic":"all","topic":"topic","outputs":1,"x":440,"y":380,"wires":[["467435513c4c743d"]]},{"id":"2bf62b2d1bdafe19","type":"debug","z":"4289f334.5d859c","name":"To MQTT Out","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":640,"y":340,"wires":[]},{"id":"3b6ad707f6571744","type":"inject","z":"4289f334.5d859c","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"Button2","payload":"ON","payloadType":"str","x":110,"y":340,"wires":[["467435513c4c743d"]]},{"id":"90ed8f5dcbffd3a6","type":"inject","z":"4289f334.5d859c","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":true,"onceDelay":"0.2","topic":"Button2","payload":"OFF","payloadType":"str","x":110,"y":300,"wires":[["467435513c4c743d"]]},{"id":"07500c8021cb62c4","type":"inject","z":"4289f334.5d859c","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"Button2","payload":"TOGGLE","payloadType":"str","x":120,"y":380,"wires":[["467435513c4c743d"]]},{"id":"a6e358b.672ffa8","type":"ui_group","name":"Auto Off","tab":"4e528085.a1bfa","order":1,"disp":true,"width":"3","collapse":false},{"id":"4e528085.a1bfa","type":"ui_tab","name":"Tab","icon":"dashboard","order":1}]
1 Like

Thanks jbudd that is almost what I am currently writing, (but this one is in a cleaner way...!)
I think the only problem of this sketch is that it is still using "flow" to store the state so if you copy/past to multiple output, it should no longer work. But I will take this and copy into context instead of flow. I will keep you informed.

(sorry I mean it will work only if you change the const in the function, best would be if I can avoid to modify the function after copy/past)

I think the only problem of this sketch is that it is still using "flow" to store the state

You have to use flow.buttonname not context.buttonname because it is set in the function and used in the button.

Edit: Hmm, maybe that's not true and it will work with context.buttonname?

(sorry I mean it will work only if you change the const in the function, best would be if I can avoid to modify the function after copy/past)

Yes if you copy and paste the nodes you have to edit the button name & label and the single variable in the function.
I guess you could use something derived from msg.topic to define the flow.variable name, thus removing any need to edit the function.

I do have a test flow where you can copy and paste all but the inputs and it works without any changes in either function or button. It isn't really practical for real life use though.

I don't think you need to use context storage. The difficulty is you can not set a msg in the button payload. As a workaround I have passed the values in the topic then separated after the button press

If I understand your requirements this example may help

[{"id":"beb873aa.750148","type":"inject","z":"c74669a0.6a34f8","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"ha/arduino1/out1/command","payload":"1","payloadType":"num","x":270,"y":2520,"wires":[["b6eae46a.bc8c38"]]},{"id":"b6eae46a.bc8c38","type":"rbe","z":"c74669a0.6a34f8","name":"rbe","func":"rbe","gap":"","start":"","inout":"out","property":"payload","x":470,"y":2660,"wires":[["f1dd6c86.a81528","946933e0.1e6ba"]]},{"id":"f3b6080f.ed7948","type":"mqtt in","z":"c74669a0.6a34f8","name":"","topic":"ha/arduino1/+/command","qos":"2","datatype":"json","broker":"d675b749.04b9c8","x":250,"y":2660,"wires":[["b6eae46a.bc8c38"]]},{"id":"875036e1.840888","type":"inject","z":"c74669a0.6a34f8","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"ha/arduino1/out3/command","payload":"1","payloadType":"num","x":270,"y":2600,"wires":[["b6eae46a.bc8c38"]]},{"id":"214ea4d6.699464","type":"inject","z":"c74669a0.6a34f8","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"ha/arduino1/out2/command","payload":"1","payloadType":"num","x":270,"y":2560,"wires":[["b6eae46a.bc8c38"]]},{"id":"5b801489.bab7dc","type":"inject","z":"c74669a0.6a34f8","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"ha/arduino1/out1/command","payload":"0","payloadType":"num","x":650,"y":2520,"wires":[["b6eae46a.bc8c38"]]},{"id":"25c6eef0.31c052","type":"inject","z":"c74669a0.6a34f8","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"ha/arduino1/out2/command","payload":"0","payloadType":"num","x":650,"y":2560,"wires":[["b6eae46a.bc8c38"]]},{"id":"8d43cc9a.cd8608","type":"inject","z":"c74669a0.6a34f8","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"ha/arduino1/out3/command","payload":"0","payloadType":"num","x":650,"y":2600,"wires":[["b6eae46a.bc8c38"]]},{"id":"f1dd6c86.a81528","type":"change","z":"c74669a0.6a34f8","name":"","rules":[{"t":"set","p":"topic","pt":"msg","to":"\"ha/arduino1/\" & $split($$.topic, \"/\")[2] & \"/state\"\t","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":630,"y":2660,"wires":[["f370ccdd.93c6c8"]]},{"id":"946933e0.1e6ba","type":"switch","z":"c74669a0.6a34f8","name":"","property":"topic","propertyType":"msg","rules":[{"t":"eq","v":"ha/arduino1/out1/command","vt":"str"},{"t":"eq","v":"ha/arduino1/out2/command","vt":"str"},{"t":"eq","v":"ha/arduino1/out3/command","vt":"str"}],"checkall":"true","repair":false,"outputs":3,"x":560,"y":2780,"wires":[["936fced1.2216f"],["728e9020.c1d25"],["3dfc4d5e.3c6792"]]},{"id":"f370ccdd.93c6c8","type":"delay","z":"c74669a0.6a34f8","name":"delay for testing","pauseType":"rate","timeout":"5","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":740,"y":2700,"wires":[["c60e8a8f.60df9"]]},{"id":"936fced1.2216f","type":"ui_text","z":"c74669a0.6a34f8","group":"8b5cde76.edd58","order":6,"width":0,"height":0,"name":"","label":"state {{msg.topic}}","format":"{{msg.payload}}","layout":"col-center","x":710,"y":2740,"wires":[]},{"id":"728e9020.c1d25","type":"ui_text","z":"c74669a0.6a34f8","group":"8b5cde76.edd58","order":7,"width":0,"height":0,"name":"","label":"state {{msg.topic}}","format":"{{msg.payload}}","layout":"col-center","x":708.3333129882812,"y":2776.333251953125,"wires":[]},{"id":"3dfc4d5e.3c6792","type":"ui_text","z":"c74669a0.6a34f8","group":"8b5cde76.edd58","order":8,"width":0,"height":0,"name":"","label":"state {{msg.topic}}","format":"{{msg.payload}}","layout":"col-center","x":710,"y":2820,"wires":[]},{"id":"c60e8a8f.60df9","type":"mqtt out","z":"c74669a0.6a34f8","name":"","topic":"","qos":"","retain":"","broker":"d675b749.04b9c8","x":810,"y":2660,"wires":[]},{"id":"db2857a.fb7fea8","type":"comment","z":"c74669a0.6a34f8","name":"simulate arduino mqtt command to state","info":"","x":620,"y":2460,"wires":[]},{"id":"321d8418.3e7784","type":"mqtt in","z":"c74669a0.6a34f8","name":"","topic":"ha/arduino1/+/state","qos":"2","datatype":"json","broker":"d675b749.04b9c8","x":290,"y":2360,"wires":[["b9060d.004b99f"]]},{"id":"b9060d.004b99f","type":"rbe","z":"c74669a0.6a34f8","name":"rbe","func":"rbe","gap":"","start":"","inout":"out","property":"payload","x":470,"y":2360,"wires":[["8bb8c6f5.9cfa58"]]},{"id":"8bb8c6f5.9cfa58","type":"change","z":"c74669a0.6a34f8","name":"","rules":[{"t":"set","p":"topic","pt":"msg","to":"[$split($$.topic,\"/\")[2], $$.payload = 1 ? 0 : 1]","tot":"jsonata"},{"t":"set","p":"background","pt":"msg","to":"$$.payload = 0 ? \"blue\" : \"green\"","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":650,"y":2360,"wires":[["dde3683a.844b18"]]},{"id":"dde3683a.844b18","type":"switch","z":"c74669a0.6a34f8","name":"","property":"topic[0]","propertyType":"msg","rules":[{"t":"eq","v":"out1","vt":"str"},{"t":"eq","v":"out2","vt":"str"},{"t":"eq","v":"out3","vt":"str"}],"checkall":"false","repair":false,"outputs":3,"x":840,"y":2360,"wires":[["be585612.27d7c"],["28ad5833.ef40d"],["bafe16dc.7990f8"]]},{"id":"be585612.27d7c","type":"ui_button","z":"c74669a0.6a34f8","name":"","group":"8b5cde76.edd58","order":2,"width":0,"height":0,"passthru":false,"label":"{{msg.topic[0]}}","tooltip":"","color":"","bgcolor":"{{background}}","icon":"","payload":"1","payloadType":"num","topic":"topic","topicType":"msg","x":1000,"y":2320,"wires":[["7adac835.d3737"]]},{"id":"28ad5833.ef40d","type":"ui_button","z":"c74669a0.6a34f8","name":"","group":"8b5cde76.edd58","order":3,"width":0,"height":0,"passthru":false,"label":"{{msg.topic[0]}}","tooltip":"","color":"","bgcolor":"{{background}}","icon":"","payload":"1","payloadType":"num","topic":"topic","topicType":"msg","x":1000,"y":2360,"wires":[["7adac835.d3737"]]},{"id":"bafe16dc.7990f8","type":"ui_button","z":"c74669a0.6a34f8","name":"","group":"8b5cde76.edd58","order":4,"width":0,"height":0,"passthru":false,"label":"{{msg.topic[0]}}","tooltip":"","color":"","bgcolor":"{{background}}","icon":"","payload":"1","payloadType":"num","topic":"topic","topicType":"msg","x":1000,"y":2400,"wires":[["7adac835.d3737"]]},{"id":"7adac835.d3737","type":"change","z":"c74669a0.6a34f8","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"topic[1]","tot":"msg"},{"t":"set","p":"topic","pt":"msg","to":"\"ha/arduino1/\" & $$.topic[0] & \"/command\"","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":1190,"y":2360,"wires":[["1862d1d1.7e640e","74aba336.c5a114","1214d7a0.cacc58"]]},{"id":"1862d1d1.7e640e","type":"debug","z":"c74669a0.6a34f8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":1270,"y":2420,"wires":[]},{"id":"74aba336.c5a114","type":"trigger","z":"c74669a0.6a34f8","name":"","op1":"","op2":"0","op1type":"nul","op2type":"str","duration":"10","extend":true,"overrideDelay":false,"units":"s","reset":"0","bytopic":"topic","topic":"topic","outputs":1,"x":1400,"y":2340,"wires":[["1214d7a0.cacc58"]]},{"id":"1214d7a0.cacc58","type":"mqtt out","z":"c74669a0.6a34f8","name":"","topic":"","qos":"","retain":"","broker":"d675b749.04b9c8","x":1570,"y":2360,"wires":[]},{"id":"d675b749.04b9c8","type":"mqtt-broker","name":"Localhost","broker":"localhost","port":"1883","clientid":"","usetls":false,"keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","closeTopic":"","closeQos":"0","closePayload":"","willTopic":"","willQos":"0","willPayload":""},{"id":"8b5cde76.edd58","type":"ui_group","name":"","tab":"8f03e639.85956","order":1,"disp":true,"width":"12","collapse":false},{"id":"8f03e639.85956","type":"ui_tab","name":"Home","icon":"dashboard","disabled":false,"hidden":false}]
1 Like

@jbudd, Ok, I came up with a solution based on your sketch. I tested every cases and that works exactly as I want so far (using context storage).

The code inside the function is maybe a bit less clean than what you had originally but that is the best I could do so far. + I feel like I have the flexibility to add new options if necessary.

Thank you for your help

[{"id":"3409f98d.d71b16","type":"mqtt in","z":"8cda1e3a.d7de","name":"","topic":"HA/msg/test","qos":"2","datatype":"auto","broker":"3afcd276.6f631e","nl":false,"rap":true,"rh":0,"x":70,"y":380,"wires":[["88bf3edf.8aab9"]]},{"id":"f82256a2.8b4b38","type":"ui_button","z":"8cda1e3a.d7de","name":"Button 1","group":"50a4a77e.1d8888","order":1,"width":"3","height":"1","passthru":false,"label":"Button 1","tooltip":"","color":"{{foreground}}","bgcolor":"{{background}}","icon":"","payload":"toggle","payloadType":"str","topic":"","topicType":"str","x":440,"y":380,"wires":[["88bf3edf.8aab9"]]},{"id":"88bf3edf.8aab9","type":"function","z":"8cda1e3a.d7de","name":"","func":"const memState = context.get (\"state\");\nconst newState = msg.payload;\nvar msgButton = msg ;\nvar msgMQTT   = msg ;\nvar msgTimer  = msg ;\n\n// toggle OFF\nif (newState === \"toggle\" & memState==\"1\"){\n    msgButton = null ;        \n    msgMQTT.payload = \"0\" ;\n    msgTimer.reset = true ;\n    }\n// toggle ON with timer OFF\nelse if (newState === \"toggle\"){\n    msgButton = null ;      \n    msgMQTT.payload = \"1\" ;\n    msgTimer.payload = \"1\";\n}   \n// toggle ON & timer OFF restart\nelse if (newState === \"set\"){\n    msgButton = null ;      \n    msgMQTT.payload = \"1\" ;\n    msgTimer.payload = \"1\";\n} \n// toggle OFF\nelse if (newState === \"reset\"){\n    msgButton = null ;      \n    msgMQTT.payload = \"0\" ;\n    msgTimer.payload = \"0\" ;\n    msgTimer.reset = true ;\n}   \n// Update button state\nelse if (newState != memState & newState === \"1\")\n{\n    msgTimer.payload = \"1\";\n    msgButton.background = \"limegreen\";\n    msgMQTT = null;\n    context.set(\"state\", newState);        \n}\nelse if (newState != memState & newState === \"0\")\n{\n    msgButton.background =\"\";\n    context.set(\"state\", newState);    \n    msgTimer.reset = true ;\n    msgMQTT = null;\n}  \nelse{\n    msgButton = null ;\n    msgMQTT   = null ;\n    msgTimer  = null ;\n}\n\nreturn [msgButton,msgMQTT,msgTimer]","outputs":3,"noerr":0,"initialize":"","finalize":"","libs":[],"x":440,"y":420,"wires":[["f82256a2.8b4b38"],["998bcc44.202f3"],["a9ab8eca.92189"]]},{"id":"a9ab8eca.92189","type":"trigger","z":"8cda1e3a.d7de","name":"Countdown OFF","op1":"","op2":"reset","op1type":"nul","op2type":"str","duration":"10","extend":true,"overrideDelay":false,"units":"s","reset":"\"0\"","bytopic":"all","topic":"topic","outputs":1,"x":460,"y":460,"wires":[["88bf3edf.8aab9"]]},{"id":"998bcc44.202f3","type":"mqtt out","z":"8cda1e3a.d7de","name":"","topic":"HA/msg/test","qos":"","retain":"","respTopic":"","contentType":"","userProps":"","correl":"","expiry":"","broker":"3afcd276.6f631e","x":690,"y":420,"wires":[]},{"id":"43137b75.00a4c4","type":"mqtt in","z":"8cda1e3a.d7de","name":"Click BP 5 ecran PC","topic":"HA/dev/NanoPrisesBureau/di0/state","qos":"2","datatype":"auto","broker":"3afcd276.6f631e","nl":false,"rap":true,"rh":0,"x":90,"y":440,"wires":[["86883a07.827ff8"]]},{"id":"86883a07.827ff8","type":"function","z":"8cda1e3a.d7de","name":"Reset","func":"var m = msg.payload[0];\nif (m === \"1\") {\n    msg.payload = \"reset\";\n    return msg;\n}\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":250,"y":440,"wires":[["88bf3edf.8aab9"]]},{"id":"9f95e761.743a08","type":"mqtt in","z":"8cda1e3a.d7de","name":"","topic":"HA/msg/test2","qos":"2","datatype":"auto","broker":"3afcd276.6f631e","nl":false,"rap":true,"rh":0,"x":70,"y":520,"wires":[["9fd737b7.ad5b88"]]},{"id":"dbe46bbe.172a48","type":"ui_button","z":"8cda1e3a.d7de","name":"Button 2","group":"50a4a77e.1d8888","order":1,"width":"3","height":"1","passthru":false,"label":"Button 2","tooltip":"","color":"{{foreground}}","bgcolor":"{{background}}","icon":"","payload":"toggle","payloadType":"str","topic":"","topicType":"str","x":440,"y":520,"wires":[["9fd737b7.ad5b88"]]},{"id":"9fd737b7.ad5b88","type":"function","z":"8cda1e3a.d7de","name":"","func":"const memState = context.get (\"state\");\nconst newState = msg.payload;\nvar msgButton = msg ;\nvar msgMQTT   = msg ;\nvar msgTimer  = msg ;\n\n// toggle OFF\nif (newState === \"toggle\" & memState==\"1\"){\n    msgButton = null ;        \n    msgMQTT.payload = \"0\" ;\n    msgTimer.reset = true ;\n    }\n// toggle ON with timer OFF\nelse if (newState === \"toggle\"){\n    msgButton = null ;      \n    msgMQTT.payload = \"1\" ;\n    msgTimer.payload = \"1\";\n}   \n// toggle ON & timer OFF restart\nelse if (newState === \"set\"){\n    msgButton = null ;      \n    msgMQTT.payload = \"1\" ;\n    msgTimer.payload = \"1\";\n} \n// toggle OFF\nelse if (newState === \"reset\"){\n    msgButton = null ;      \n    msgMQTT.payload = \"0\" ;\n    msgTimer.payload = \"0\" ;\n    msgTimer.reset = true ;\n}   \n// Update button state\nelse if (newState != memState & newState === \"1\")\n{\n    msgTimer.payload = \"1\";\n    msgButton.background = \"limegreen\";\n    msgMQTT = null;\n    context.set(\"state\", newState);        \n}\nelse if (newState != memState & newState === \"0\")\n{\n    msgButton.background =\"\";\n    context.set(\"state\", newState);    \n    msgTimer.reset = true ;\n    msgMQTT = null;\n}  \nelse{\n    msgButton = null ;\n    msgMQTT   = null ;\n    msgTimer  = null ;\n}\n\nreturn [msgButton,msgMQTT,msgTimer]","outputs":3,"noerr":0,"initialize":"","finalize":"","libs":[],"x":440,"y":560,"wires":[["dbe46bbe.172a48"],["e397b9b6.269b88"],["a7458bc7.46a658"]]},{"id":"a7458bc7.46a658","type":"trigger","z":"8cda1e3a.d7de","name":"Countdown OFF","op1":"","op2":"reset","op1type":"nul","op2type":"str","duration":"10","extend":true,"overrideDelay":false,"units":"s","reset":"\"0\"","bytopic":"all","topic":"topic","outputs":1,"x":460,"y":600,"wires":[["9fd737b7.ad5b88"]]},{"id":"e397b9b6.269b88","type":"mqtt out","z":"8cda1e3a.d7de","name":"","topic":"HA/msg/test2","qos":"","retain":"","respTopic":"","contentType":"","userProps":"","correl":"","expiry":"","broker":"3afcd276.6f631e","x":700,"y":560,"wires":[]},{"id":"c4ea0300.ab3c4","type":"mqtt in","z":"8cda1e3a.d7de","name":"Click BP 5 ecran PC","topic":"HA/dev/NanoPrisesBureau/di0/state","qos":"2","datatype":"auto","broker":"3afcd276.6f631e","nl":false,"rap":true,"rh":0,"x":90,"y":580,"wires":[["fa196dba.cf6f9"]]},{"id":"fa196dba.cf6f9","type":"function","z":"8cda1e3a.d7de","name":"Reset","func":"var m = msg.payload[0];\nif (m === \"1\") {\n    msg.payload = \"reset\";\n    return msg;\n}\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":250,"y":580,"wires":[["9fd737b7.ad5b88"]]},{"id":"3afcd276.6f631e","type":"mqtt-broker","name":"beelink","broker":"192.168.1.39","port":"1883","clientid":"","usetls":false,"protocolVersion":"4","keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","birthMsg":{},"closeTopic":"","closeQos":"0","closePayload":"","closeMsg":{},"willTopic":"","willQos":"0","willPayload":"","willMsg":{},"sessionExpiry":""},{"id":"50a4a77e.1d8888","type":"ui_group","name":"Default","tab":"9dfab700.316b68","order":3,"disp":true,"width":"6","collapse":false},{"id":"9dfab700.316b68","type":"ui_tab","name":"1-Home","icon":"dashboard","order":1,"disabled":false,"hidden":false}]

@E1cid,

Thanks for your example, it is a bit more than I expect but I will learn from it, especially the management of mqtt messages will be helpful. For now I have a satisfying method with a javascript function.