Change the Output between 0 and 1 on each message that I receive in the input

Hello Guys. I need your help to finish this part. I want every time I receive msg.payload from the input the output must be between "0" and "1". For example the picture below this timestamp node when I press it for the first time the next function change it to "0" then next time I press the timestamp the function change it to "1".
I will appreciate your cooperation with me. :slight_smile:

Try this in your function node but replace "x" and "y" with your fixed values

if (msg.payload = "x") {
    msg.payload = "0"
    };
if (msg.payload = "y") {
    msg.payload = "1"
};
return msg;

you did not say what you want the third time etc. so I guess you want it to alternate, if so try below code in a function node.

msg.payload = context.get("alternate") || 1;
msg.payload =  (msg.payload === 0) ? 1 : 0;
context.set("alternate", msg.payload);
return msg;

[edit] added return
[edit] @jbudd the|| 1 on the context get seems to evaluate to true while 0 is falsey.

@badr_al_moragab here is a example that may help.

[{"id":"3663e0f5.3dadf8","type":"inject","z":"b779de97.b1b46","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"status/lamp1","payload":"1","payloadType":"num","x":150,"y":80,"wires":[["35909e5b.03fb4a"]]},{"id":"35909e5b.03fb4a","type":"rbe","z":"b779de97.b1b46","name":"","func":"rbe","gap":"","start":"","inout":"out","property":"payload","x":226.99998474121094,"y":223,"wires":[["13f70fb3.084b08"]]},{"id":"73ba21cc.4c945","type":"mqtt in","z":"b779de97.b1b46","name":"","topic":"status/+","qos":"2","datatype":"json","broker":"d675b749.04b9c8","x":76.99998474121094,"y":223,"wires":[["35909e5b.03fb4a"]]},{"id":"8cb0e631.a72308","type":"inject","z":"b779de97.b1b46","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"status/lamp1","payload":"0","payloadType":"num","x":370,"y":80,"wires":[["35909e5b.03fb4a"]]},{"id":"ae3fd493.a9c168","type":"inject","z":"b779de97.b1b46","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"status/lamp2","payload":"1","payloadType":"num","x":150,"y":120,"wires":[["35909e5b.03fb4a"]]},{"id":"311bbb9b.bc6624","type":"inject","z":"b779de97.b1b46","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"status/lamp2","payload":"0","payloadType":"num","x":370,"y":120,"wires":[["35909e5b.03fb4a"]]},{"id":"cf7d050b.7797d","type":"inject","z":"b779de97.b1b46","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"status/lamp3","payload":"1","payloadType":"num","x":150,"y":160,"wires":[["35909e5b.03fb4a"]]},{"id":"6b73a684.0f39e8","type":"inject","z":"b779de97.b1b46","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"status/lamp3","payload":"0","payloadType":"num","x":370,"y":160,"wires":[["35909e5b.03fb4a"]]},{"id":"13f70fb3.084b08","type":"change","z":"b779de97.b1b46","name":"","rules":[{"t":"set","p":"lamp_status","pt":"flow","to":"$merge([$flowContext(\"lamp_status\"),{$split($$.topic,\"/\")[1]:$$.payload}])","tot":"jsonata"},{"t":"set","p":"count","pt":"msg","to":"$count($flowContext(\"lamp_status\").*[$=1])","tot":"jsonata"},{"t":"set","p":"background","pt":"msg","to":"$$.count > 0 ? \"#FFCC00\" : \"#00AAED\"","tot":"jsonata"},{"t":"set","p":"label","pt":"msg","to":"$$.count & ($$.count = 1 ? \" lamp is ON\" : \" lamps are ON\")","tot":"jsonata"},{"t":"set","p":"alternate","pt":"flow","to":"$$.count > 0 ? 0 : 1","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":386.99998474121094,"y":223,"wires":[["94078be9.9dfe08"]]},{"id":"94078be9.9dfe08","type":"ui_button","z":"b779de97.b1b46","name":"","group":"2d4fe667.28f8ba","order":1,"width":0,"height":0,"passthru":false,"label":"{{label}}","tooltip":"","color":"","bgcolor":"{{background}}","icon":"","payload":"alternate","payloadType":"flow","topic":"","topicType":"str","x":576.9999847412109,"y":223,"wires":[["4ecd6814.41238"]]},{"id":"4ecd6814.41238","type":"function","z":"b779de97.b1b46","name":"Function 2","func":"let cmnd =[\"lamp1\", \"lamp2\",\"lamp3\"]\ncmnd.forEach(lamp => node.send({topic: `cmnd/${lamp}`,payload: msg.payload}));\nreturn null;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":736.9999847412109,"y":223,"wires":[["a82b2971.0f332","83056062.a413e"]]},{"id":"a82b2971.0f332","type":"debug","z":"b779de97.b1b46","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":686.9999847412109,"y":283,"wires":[]},{"id":"83056062.a413e","type":"mqtt out","z":"b779de97.b1b46","name":"","topic":"","qos":"","retain":"","broker":"d675b749.04b9c8","x":886.9999847412109,"y":223,"wires":[]},{"id":"a58e839f.17ae7","type":"comment","z":"b779de97.b1b46","name":"simulate mqtt cmnd to status","info":"","x":276.99998474121094,"y":363,"wires":[]},{"id":"5bc932a7.7f706c","type":"mqtt in","z":"b779de97.b1b46","name":"","topic":"cmnd/+","qos":"2","datatype":"json","broker":"d675b749.04b9c8","x":116.99998474121094,"y":403,"wires":[["465c5833.7a463"]]},{"id":"465c5833.7a463","type":"change","z":"b779de97.b1b46","name":"","rules":[{"t":"set","p":"topic","pt":"msg","to":"\"status/\" & $split($$.topic, \"/\")[1]","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":326.99998474121094,"y":403,"wires":[["706f1c5f.04a2bc"]]},{"id":"256c6e12.93ba3a","type":"mqtt out","z":"b779de97.b1b46","name":"","topic":"","qos":"","retain":"","broker":"d675b749.04b9c8","x":550,"y":400,"wires":[]},{"id":"706f1c5f.04a2bc","type":"delay","z":"b779de97.b1b46","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":520,"y":460,"wires":[["256c6e12.93ba3a"]]},{"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":"2d4fe667.28f8ba","type":"ui_group","name":"demo","tab":"1caa8458.b17814","order":1,"disp":true,"width":"6","collapse":false},{"id":"1caa8458.b17814","type":"ui_tab","name":"Demo","icon":"dashboard","disabled":false,"hidden":false}]

I can't see why but that didn't work for me, it just outputs zero.
But this works

msg.payload = context.get("alternate") || 0;
msg.payload = (msg.payload +1)%2;
context.set("alternate", msg.payload);
return msg;
1 Like

Thanks @jbudd for your time to reply. your function works for me fine thanks for that. I still want the function 1 to store the last status ether it is "0" or "1" to change it on function 2 when signal 2 sent to it. As the Example shows bellow if Signal 1 pressed the function 1 will start to send "0", So when I press Signal 2 function 2 will have the change that function 1 sent so function 2 will send "1"

each function will get the last update on the other function. to let it as two way switch.

I can't understand what you are trying to achieve.

If it's just a two way switch, I don't see why you need two function nodes. Both of your injects can be wired into the same function.

If you must have two functions, both modifying the same stored value, use flow.get() and flow.set() rather than context.get() and context.set()

@jbudd The reason to use two function because I need function 1 to only send the status. function 2 to switch the lamp ON/OFF
I'm not that good on functions so please help me on the store set and get for both function. Thanks for your time.

Even though you have introduced a new output to a UI text node, I still see no reason for two function nodes.
You could use a Change node to change "0" to "The lamp has gone out" and "1" to "The lamp is shining" as input to the UI.

That's not what your previous image shows.

I'm not going to guess what new nodes you might introduce.

I need to know how to share to you my export node in side the brackets here on post like this.
@jbudd

To export your flow, select the nodes to be exported then press CTRL+E then click the Copy to Clipboard button, then paste it into a reply surrounded with three backticks
```
like this
```

See this post for more details - How to share code or flow json

@jbudd I need to use the Button to switch all Lump 1, 2 ,3 ON or OFF. but for the ON status of each lump will color the Button by Yellow color witch means that the room light is ON ether lamp 1, 2, 3 is ON or one of them is ON. But for OFF status the Button color will not change by status of one or two lamp OFF. All the lamps must be OFF to change the color of the Button. So If I use the same Function that you gave me for the pressing Button and the OFF status the OFF status will switch all the lamps OFF witch I don't want it

[{"id":"a316e1dc.758d5","type":"function","z":"a5e4c84a.4d4a88","name":"Function 1","func":"msg.payload = context.get(\"alternate\") || 0;\nmsg.payload = (msg.payload +1)%2;\ncontext.set(\"alternate\", msg.payload);\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":1070,"y":4840,"wires":[[]]},{"id":"78f8ce79.6719e","type":"mqtt in","z":"a5e4c84a.4d4a88","name":"","topic":" Lump 3 status","qos":"2","datatype":"auto","broker":"","x":210,"y":4680,"wires":[["927eff97.dc9a6","d77f7be4.787068"]]},{"id":"568629bd.6d3e88","type":"mqtt in","z":"a5e4c84a.4d4a88","name":"","topic":" Lump 2 status","qos":"2","datatype":"auto","broker":"","x":210,"y":4620,"wires":[["927eff97.dc9a6","3372632c.6fa20c"]]},{"id":"39dfd29b.7e241e","type":"mqtt in","z":"a5e4c84a.4d4a88","name":"","topic":" Lump 1 status","qos":"2","datatype":"auto","broker":"","x":210,"y":4560,"wires":[["927eff97.dc9a6","162afd99.d7bfb2"]]},{"id":"71f37857.95ede8","type":"function","z":"a5e4c84a.4d4a88","name":"Switch State","func":"var label;\nvar color;\n\nif(msg.payload === '0'){\n label = 'ON';\n color = \"#FFC815\";\n \n}\nelse{\n label = 'OFF';\n color = \"\";\n lightstate_bureau = \"OFF\";\n}\n\nmsg = {topic:label, background:color};\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":710,"y":4580,"wires":[["ab6b0cd6.fc2e6"]]},{"id":"ab6b0cd6.fc2e6","type":"ui_button","z":"a5e4c84a.4d4a88","name":"switch Lump 1, 2, 3 ON/OFF","group":"4537311c.4f5b5","order":12,"width":0,"height":0,"passthru":false,"label":"The Lamp is {{msg.topic}}","tooltip":"","color":"","bgcolor":"","icon":"","payload":"","payloadType":"str","topic":"topic","topicType":"msg","x":950,"y":4580,"wires":[["c6bfa8c3.aa9318"]]},{"id":"c6bfa8c3.aa9318","type":"function","z":"a5e4c84a.4d4a88","name":"Function 2","func":"msg.payload = context.get(\"alternate\") || 0;\nmsg.payload = (msg.payload +1)%2;\ncontext.set(\"alternate\", msg.payload);\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":870,"y":4720,"wires":[["1c767bf1.bf03e4","dabfd074.19d97","3190dcff.b72474","71f37857.95ede8"]]},{"id":"1c767bf1.bf03e4","type":"mqtt out","z":"a5e4c84a.4d4a88","name":"","topic":"Lump 1","qos":"","retain":"","broker":"","x":1200,"y":4660,"wires":[]},{"id":"3190dcff.b72474","type":"mqtt out","z":"a5e4c84a.4d4a88","name":"","topic":"Lump 2","qos":"","retain":"","broker":"","x":1200,"y":4720,"wires":[]},{"id":"dabfd074.19d97","type":"mqtt out","z":"a5e4c84a.4d4a88","name":"","topic":"Lump 3","qos":"","retain":"","broker":"","x":1200,"y":4780,"wires":[]},{"id":"927eff97.dc9a6","type":"switch","z":"a5e4c84a.4d4a88","name":"0 is the ON status","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"0","vt":"num"},{"t":"eq","v":"1","vt":"num"}],"checkall":"true","repair":false,"outputs":2,"x":430,"y":4580,"wires":[["71f37857.95ede8"],[]]},{"id":"4c29bc2.048ba44","type":"function","z":"a5e4c84a.4d4a88","name":"Lamp 1, 2, 3 OFF status","func":"let lamp1 = msg.payload.Lamp1;\nlet lamp2 = msg.payload.Lamp2;\nlet lamp3 = msg.payload.Lamp3;\n//let away = msg.payload.test4\n\nif (lamp1 === 1 && lamp2 === 1 && lamp3 === 1) {\n    msg.payload = 1\n} else {\n    msg.payload = null;\n}\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":810,"y":4840,"wires":[["71f37857.95ede8","a316e1dc.758d5"]]},{"id":"b08947df.bc3f08","type":"join","z":"a5e4c84a.4d4a88","name":"","mode":"custom","build":"merged","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":false,"timeout":"","count":"3","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":600,"y":4840,"wires":[["4c29bc2.048ba44"]]},{"id":"162afd99.d7bfb2","type":"change","z":"a5e4c84a.4d4a88","name":"Lamp 1","rules":[{"t":"move","p":"payload","pt":"msg","to":"payload.Lamp1","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":450,"y":4700,"wires":[["b08947df.bc3f08"]]},{"id":"3372632c.6fa20c","type":"change","z":"a5e4c84a.4d4a88","name":"Lamp 2","rules":[{"t":"move","p":"payload","pt":"msg","to":"payload.Lump2","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":450,"y":4840,"wires":[["b08947df.bc3f08"]]},{"id":"d77f7be4.787068","type":"change","z":"a5e4c84a.4d4a88","name":"Lamp 3","rules":[{"t":"move","p":"payload","pt":"msg","to":"payload.Lamp3","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":450,"y":4960,"wires":[["b08947df.bc3f08"]]},{"id":"4537311c.4f5b5","type":"ui_group","name":"TV","tab":"7b345faa.30565","order":2,"disp":true,"width":6,"collapse":true},{"id":"7b345faa.30565","type":"ui_tab","name":"SALON","icon":"fa-television","order":1}]

maybe you will not understand me well so please just help me to get the update on function 2 from function 1 and vice versa, store it with set and get.

Thanks I got it :slight_smile: @Steve-Mcl

Please try and help me understand.

Do you have just 1 button or 3?
If it's only 1 button, how do you select which light turns on or off when you press it?
If you have 3 buttons, does the colour of each button show it's own light's status or are ALL buttons yellow if ANY light is on?

It is just one button to switch all the lights ON or OFF. So if I use the wall switch on my room to switch ON one of the lights the button on the dashboard become yellow because one of the light on the room is ON.
To make the explain shorter the button color change to yellow if one of the wall switches switch ON or all switches switch ON. Other wise the button become with out color which means OFF if all the wall switches become OFF not just one wall switch. @jbudd

**IF one or all the 3 wall switches are ON button color become Yellow
**All the 3 wall switches must be OFF to let the button color become with no color (OFF)

Thank you.

Here is a sample flow to take status messages for each lamp and change the colour of a Dashboard button accordingly.
It is not clear what you want to happen when the Dashboard button is clicked so I have just put a debug node to show when that happens.

[{"id":"b779de97.b1b46","type":"tab","label":"Flow 5","disabled":false,"info":""},{"id":"73ba21cc.4c945","type":"mqtt in","z":"b779de97.b1b46","name":"","topic":"status/lamp1","qos":"2","datatype":"auto","broker":"d675b749.04b9c8","nl":false,"rap":true,"rh":0,"x":110,"y":80,"wires":[["13f70fb3.084b08"]]},{"id":"695813d8.1887bc","type":"mqtt in","z":"b779de97.b1b46","name":"","topic":"status/lamp2","qos":"2","datatype":"auto","broker":"d675b749.04b9c8","nl":false,"rap":true,"rh":0,"x":110,"y":180,"wires":[["b326f07a.442b7"]]},{"id":"e392017.2c5a5","type":"mqtt in","z":"b779de97.b1b46","name":"","topic":"status/lamp3","qos":"2","datatype":"auto","broker":"d675b749.04b9c8","nl":false,"rap":true,"rh":0,"x":110,"y":280,"wires":[["654f2845.da9a"]]},{"id":"13f70fb3.084b08","type":"change","z":"b779de97.b1b46","name":"","rules":[{"t":"set","p":"lamp1status","pt":"flow","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":320,"y":80,"wires":[["f15aeb18.402bd"]]},{"id":"b326f07a.442b7","type":"change","z":"b779de97.b1b46","name":"","rules":[{"t":"set","p":"lamp2status","pt":"flow","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":320,"y":180,"wires":[["f15aeb18.402bd"]]},{"id":"654f2845.da9a","type":"change","z":"b779de97.b1b46","name":"","rules":[{"t":"set","p":"lamp3status","pt":"flow","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":320,"y":280,"wires":[["f15aeb18.402bd"]]},{"id":"f15aeb18.402bd","type":"function","z":"b779de97.b1b46","name":"","func":"// This sets the colour and label of a Node-RED Dashboard button.\n\nif ((flow.get(\"lamp1status\") == 0) && (flow.get(\"lamp2status\") == 0) && (flow.get(\"lamp3status\") == 0)) {\n    msg.label = \"OFF\"; \n    msg.background = \"#00AAED\"; \n    node.status(\"All OFF - Blue\");\n}\nelse {\n    msg.label = \"ON\"; \n    msg.background = \"#FFCC00\";  \n    node.status(\"At least one ON - Yellow\");\n}\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":530,"y":180,"wires":[["b5ac0776.eb28e8"]]},{"id":"b5ac0776.eb28e8","type":"ui_button","z":"b779de97.b1b46","name":"","group":"2d4fe667.28f8ba","order":1,"width":0,"height":0,"passthru":false,"label":"The lamp is {{label}}","tooltip":"","color":"","bgcolor":"{{background}}","icon":"","payload":"The Button Was Pressed","payloadType":"str","topic":"topic","topicType":"msg","x":670,"y":180,"wires":[["a82b2971.0f332"]]},{"id":"3663e0f5.3dadf8","type":"inject","z":"b779de97.b1b46","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"1","payloadType":"str","x":130,"y":40,"wires":[["13f70fb3.084b08"]]},{"id":"8cb0e631.a72308","type":"inject","z":"b779de97.b1b46","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"0","payloadType":"str","x":290,"y":40,"wires":[["13f70fb3.084b08"]]},{"id":"f9931c66.6671e","type":"inject","z":"b779de97.b1b46","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"0","payloadType":"str","x":290,"y":140,"wires":[["b326f07a.442b7"]]},{"id":"76cf5cd0.5568e4","type":"inject","z":"b779de97.b1b46","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"1","payloadType":"str","x":130,"y":140,"wires":[["b326f07a.442b7"]]},{"id":"443826df.6a015","type":"inject","z":"b779de97.b1b46","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"0","payloadType":"str","x":290,"y":240,"wires":[["654f2845.da9a"]]},{"id":"686d7b9d.acd534","type":"inject","z":"b779de97.b1b46","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"1","payloadType":"str","x":130,"y":240,"wires":[["654f2845.da9a"]]},{"id":"a82b2971.0f332","type":"debug","z":"b779de97.b1b46","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":830,"y":180,"wires":[]},{"id":"d675b749.04b9c8","type":"mqtt-broker","name":"Localhost","broker":"localhost","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":"2d4fe667.28f8ba","type":"ui_group","name":"demo","tab":"1caa8458.b17814","order":1,"disp":true,"width":"6","collapse":false},{"id":"1caa8458.b17814","type":"ui_tab","name":"Demo","icon":"dashboard","disabled":false,"hidden":false}]

Untitled 3

@jbudd Thank you so much that would be helpful to me :slight_smile: . For the button changing color when pressed I did it on function 4 as it is shown on the picture below and it works fine. but I need to combine function 3 with function 4 to make it on one node.

[{"id":"b779de97.b1b46","type":"tab","label":"Flow 5","disabled":false,"info":""},{"id":"73ba21cc.4c945","type":"mqtt in","z":"b779de97.b1b46","name":"","topic":"status/lamp1","qos":"2","datatype":"auto","broker":"d675b749.04b9c8","x":110,"y":80,"wires":[["13f70fb3.084b08"]]},{"id":"695813d8.1887bc","type":"mqtt in","z":"b779de97.b1b46","name":"","topic":"status/lamp2","qos":"2","datatype":"auto","broker":"d675b749.04b9c8","x":110,"y":180,"wires":[["b326f07a.442b7"]]},{"id":"e392017.2c5a5","type":"mqtt in","z":"b779de97.b1b46","name":"","topic":"status/lamp3","qos":"2","datatype":"auto","broker":"d675b749.04b9c8","x":110,"y":280,"wires":[["654f2845.da9a"]]},{"id":"13f70fb3.084b08","type":"change","z":"b779de97.b1b46","name":"","rules":[{"t":"set","p":"lamp1status","pt":"flow","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":320,"y":80,"wires":[["f15aeb18.402bd"]]},{"id":"b326f07a.442b7","type":"change","z":"b779de97.b1b46","name":"","rules":[{"t":"set","p":"lamp2status","pt":"flow","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":320,"y":180,"wires":[["f15aeb18.402bd"]]},{"id":"654f2845.da9a","type":"change","z":"b779de97.b1b46","name":"","rules":[{"t":"set","p":"lamp3status","pt":"flow","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":320,"y":280,"wires":[["f15aeb18.402bd"]]},{"id":"f15aeb18.402bd","type":"function","z":"b779de97.b1b46","name":"function 3","func":"// This sets the colour and label of a Node-RED Dashboard button.\n\nif ((flow.get(\"lamp1status\") == 0) && (flow.get(\"lamp2status\") == 0) && (flow.get(\"lamp3status\") == 0)) {\n    msg.label = \"OFF\"; \n    msg.background = \"#00AAED\"; \n    node.status(\"All OFF - Blue\");\n    msg.stat = \"statufunction1\";\n}\nelse {\n    msg.label = \"ON\"; \n    msg.background = \"#FFCC00\";  \n    node.status(\"At least one ON - Yellow\");\n}\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":520,"y":180,"wires":[["b5ac0776.eb28e8","bcf6de04.c5906"]]},{"id":"b5ac0776.eb28e8","type":"ui_button","z":"b779de97.b1b46","name":"","group":"2d4fe667.28f8ba","order":1,"width":0,"height":0,"passthru":false,"label":"The lamp is {{label}}","tooltip":"","color":"","bgcolor":"{{background}}","icon":"","payload":"The Button Was Pressed","payloadType":"str","topic":"topic","topicType":"msg","x":670,"y":180,"wires":[["472c702c.5e8d6"]]},{"id":"3663e0f5.3dadf8","type":"inject","z":"b779de97.b1b46","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"1","payloadType":"str","x":130,"y":40,"wires":[["13f70fb3.084b08"]]},{"id":"8cb0e631.a72308","type":"inject","z":"b779de97.b1b46","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"0","payloadType":"str","x":290,"y":40,"wires":[["13f70fb3.084b08"]]},{"id":"f9931c66.6671e","type":"inject","z":"b779de97.b1b46","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"0","payloadType":"str","x":290,"y":140,"wires":[["b326f07a.442b7"]]},{"id":"76cf5cd0.5568e4","type":"inject","z":"b779de97.b1b46","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"1","payloadType":"str","x":130,"y":140,"wires":[["b326f07a.442b7"]]},{"id":"443826df.6a015","type":"inject","z":"b779de97.b1b46","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"0","payloadType":"str","x":290,"y":240,"wires":[["654f2845.da9a"]]},{"id":"686d7b9d.acd534","type":"inject","z":"b779de97.b1b46","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"1","payloadType":"str","x":130,"y":240,"wires":[["654f2845.da9a"]]},{"id":"6a589133.39213","type":"mqtt out","z":"b779de97.b1b46","name":"","topic":"Lump 1","qos":"","retain":"","broker":"d675b749.04b9c8","x":1060,"y":120,"wires":[]},{"id":"6c0067b7.3d99a8","type":"mqtt out","z":"b779de97.b1b46","name":"","topic":"Lump 2","qos":"","retain":"","broker":"d675b749.04b9c8","x":1060,"y":180,"wires":[]},{"id":"3fab2474.a7f85c","type":"mqtt out","z":"b779de97.b1b46","name":"","topic":"Lump 3","qos":"","retain":"","broker":"d675b749.04b9c8","x":1060,"y":240,"wires":[]},{"id":"472c702c.5e8d6","type":"function","z":"b779de97.b1b46","name":"Function 2","func":"msg.payload = flow.get(\"alternate\") || 0;\nmsg.payload = (msg.payload +1)%2;\nflow.set(\"alternate\", msg.payload);\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":830,"y":180,"wires":[["3fab2474.a7f85c","6c0067b7.3d99a8","6a589133.39213","4056e971.0a6588"]]},{"id":"bcf6de04.c5906","type":"switch","z":"b779de97.b1b46","name":"","property":"stat","propertyType":"msg","rules":[{"t":"eq","v":"statufunction1","vt":"str"}],"checkall":"true","repair":false,"outputs":1,"x":650,"y":240,"wires":[["f6461fcd.6c7ef"]]},{"id":"f6461fcd.6c7ef","type":"function","z":"b779de97.b1b46","name":"Function 1","func":"msg.stat = flow.get(\"alternate\") || 0;\nmsg.payload = (msg.stat +1)%2;\nflow.set(\"alternate\", msg.payload);\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":810,"y":240,"wires":[[]]},{"id":"4056e971.0a6588","type":"function","z":"b779de97.b1b46","name":"function 4","func":"//var lightstate_bureau = flow.get(\"lightstate_bureau\") || undefined;\nvar label;\nvar color;\n\nif(msg.payload === 1){\n label = 'ON';\n color = \"#FFC815\";\n}\nelse{\n label = 'OFF';\n color = \"\";\n}\n\n\nmsg = {topic:label, background:color};\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":820,"y":80,"wires":[["b5ac0776.eb28e8"]]},{"id":"d675b749.04b9c8","type":"mqtt-broker","name":"Localhost","broker":"localhost","port":"1883","clientid":"","usetls":false,"compatmode":false,"keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","closeTopic":"","closeQos":"0","closePayload":"","willTopic":"","willQos":"0","willPayload":""},{"id":"2d4fe667.28f8ba","type":"ui_group","name":"demo","tab":"1caa8458.b17814","order":1,"disp":true,"width":"6","collapse":false},{"id":"1caa8458.b17814","type":"ui_tab","name":"Demo","icon":"dashboard","disabled":false,"hidden":false}]

Please explain this part of your flow:

What are the MQTT outputs "Lump 1" etc for?
What receives these messages?
What are the expected topics and payloads?
What happens?

I can only guess that these messages go to your wall switch and therefore turn all lights off or on together, which you have previously said you don't want.

@jbudd exactly, this button is to turn all the lights (((that they have the three previous wall switches))) ON or OFF

The previous switches is just to send the status to the button ((the color)).

wish this below picture easily explain to you my idea.

It is possible that your wall switch sends an MQTT status message to acknowledge your ON or OFF message.
If that is so, it can feed in to the MQTT in nodes at the left of your flow, and so automatically change the button colour.

If not, take a look at this:

[{"id":"193aef1a.873ce1","type":"tab","label":"Flow 5","disabled":false,"info":""},{"id":"d967ad1f.7644f8","type":"change","z":"193aef1a.873ce1","name":"","rules":[{"t":"set","p":"lamp1status","pt":"flow","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":280,"y":80,"wires":[["2ae39f06.3087b8"]]},{"id":"7a2a0ad4.ab61e4","type":"change","z":"193aef1a.873ce1","name":"","rules":[{"t":"set","p":"lamp2status","pt":"flow","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":280,"y":180,"wires":[["2ae39f06.3087b8"]]},{"id":"2f32cc0a.7a6a2c","type":"change","z":"193aef1a.873ce1","name":"","rules":[{"t":"set","p":"lamp3status","pt":"flow","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":280,"y":280,"wires":[["2ae39f06.3087b8"]]},{"id":"2ae39f06.3087b8","type":"function","z":"193aef1a.873ce1","name":"function 3","func":"// This sets the colour and label of a Node-RED Dashboard button.\n\nif ((flow.get(\"lamp1status\") == 0) && (flow.get(\"lamp2status\") == 0) && (flow.get(\"lamp3status\") == 0)) {\n    msg.label = \"OFF\"; \n    msg.background = \"#00AAED\"; \n    node.status(\"All OFF - Blue\");\n    flow.set(\"alternate\", 0);\n}\nelse {\n    msg.label = \"ON\"; \n    msg.background = \"#FFCC00\";  \n    node.status(\"At least one ON - Yellow\");\n    flow.set(\"alternate\", 1);\n}\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":500,"y":180,"wires":[["fee42029.778b5"]]},{"id":"fee42029.778b5","type":"ui_button","z":"193aef1a.873ce1","name":"","group":"2d4fe667.28f8ba","order":1,"width":0,"height":0,"passthru":false,"label":"The lamp is {{label}}","tooltip":"","color":"","bgcolor":"{{background}}","icon":"","payload":"The Button Was Pressed","payloadType":"str","topic":"topic","topicType":"msg","x":690,"y":180,"wires":[["fa09d955.b3fc"]]},{"id":"51510f81.020f5","type":"inject","z":"193aef1a.873ce1","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"1","payloadType":"str","x":90,"y":40,"wires":[["d967ad1f.7644f8"]]},{"id":"77b6c585.c00ebc","type":"inject","z":"193aef1a.873ce1","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"0","payloadType":"str","x":90,"y":80,"wires":[["d967ad1f.7644f8"]]},{"id":"3125c375.64d89c","type":"inject","z":"193aef1a.873ce1","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"0","payloadType":"str","x":90,"y":180,"wires":[["7a2a0ad4.ab61e4"]]},{"id":"cf473aa8.d18f3","type":"inject","z":"193aef1a.873ce1","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"1","payloadType":"str","x":90,"y":140,"wires":[["7a2a0ad4.ab61e4"]]},{"id":"64a2dbd1.b92b5c","type":"inject","z":"193aef1a.873ce1","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"0","payloadType":"str","x":90,"y":280,"wires":[["2f32cc0a.7a6a2c"]]},{"id":"c9efbf60.a679d","type":"inject","z":"193aef1a.873ce1","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"1","payloadType":"str","x":90,"y":240,"wires":[["2f32cc0a.7a6a2c"]]},{"id":"fa09d955.b3fc","type":"function","z":"193aef1a.873ce1","name":"Function 2","func":"msg.payload = flow.get(\"alternate\") || 0;\nmsg.payload = (msg.payload +1)%2;\nflow.set(\"alternate\", msg.payload);\nif(msg.payload === 1){\n msg.label = 'ON';\n msg.background = \"#FFCC00\";\n node.status(\"Lights on, Button yellow\");\n}\nelse{\n msg.label = 'OFF';\n msg.background = \"#000AED\";\n node.status(\"Lights off, Button blue\");\n}\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":690,"y":240,"wires":[["fee42029.778b5","fc087ca.4cdf"]]},{"id":"fc087ca.4cdf","type":"debug","z":"193aef1a.873ce1","name":"Output to MQTT","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":900,"y":240,"wires":[]},{"id":"2d4fe667.28f8ba","type":"ui_group","name":"demo","tab":"1caa8458.b17814","order":1,"disp":true,"width":"6","collapse":false},{"id":"1caa8458.b17814","type":"ui_tab","name":"Demo","icon":"dashboard","disabled":false,"hidden":false}]

Edit - fixed code error

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