One button with 2 MQTT topics

Hi All,

Im new with Node RED and i'm trying to create a dashboard to control my home. So far i have been able to do most with the information found on this forum. But i cant figure out how to create one button that sends 2 different mqtt topics, alternating.

Some background information why i need this:
I have a fireplace that works with an infrared remote. It uses different signals for turning ON and OFF. I have successfully connected an IR hub that can send the ON and OFF signal via mqtt. So the button must send the mqtt topic ON and mqtt topic OFF, alternating each other.

can somebody put me in the right direction?

Thanks in advance,
Khoi

Here is an example using function node and context storage, as button value needs to be stored between presses

[{"id":"6421eda3.e174a4","type":"ui_button","z":"57675e72.26a0d8","name":"","group":"b055fa85.83d6e","order":1,"width":0,"height":0,"passthru":false,"label":"button","tooltip":"","color":"","bgcolor":"","icon":"","payload":"button1","payloadType":"flow","topic":"","x":260,"y":1960,"wires":[["b786488b.8bc7a"]]},{"id":"b786488b.8bc7a","type":"function","z":"57675e72.26a0d8","name":"","func":"let button1 = flow.get(\"button1\");\nmsg.payload = button1 === 1 ? 0 : 1;\nflow.set(\"button1\", msg.payload);\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":490,"y":1960,"wires":[["c5d88c3.9b074f"]]},{"id":"c5d88c3.9b074f","type":"debug","z":"57675e72.26a0d8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":610,"y":2120,"wires":[]},{"id":"b055fa85.83d6e","type":"ui_group","z":"","name":"Default","tab":"8f03e639.85956","order":1,"disp":true,"width":"6","collapse":false},{"id":"8f03e639.85956","type":"ui_tab","z":"","name":"Home","icon":"dashboard","disabled":false,"hidden":false}]
1 Like

Have you considered using a ui-switch instead?

1 Like

Hi Colin,
With the ui-switch i can only send 1 topic. eg, homie/homey-topic/fireplaceoff
but i want this switch to send this topic: homie/homey-topic/fireplaceoff AND homie/homey-topic/fireplaceon, alternating

The switch has 2 outputs one for off and one for on. You would then move msg.payload to msg.topic.

[{"id":"c5d88c3.9b074f","type":"debug","z":"57675e72.26a0d8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":610,"y":2120,"wires":[]},{"id":"b786488b.8bc7a","type":"function","z":"57675e72.26a0d8","name":"","func":"let button1 = flow.get(\"button1\");\nmsg.payload = button1 === \"on\" ? \"off\" : \"on\";\nflow.set(\"button1\", msg.payload);\nmsg.topic = \"homie/homey-topic/fireplace\"+msg.payload\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":490,"y":1960,"wires":[["c5d88c3.9b074f"]]},{"id":"6421eda3.e174a4","type":"ui_button","z":"57675e72.26a0d8","name":"","group":"b055fa85.83d6e","order":1,"width":0,"height":0,"passthru":false,"label":"button","tooltip":"","color":"","bgcolor":"","icon":"","payload":"button1","payloadType":"flow","topic":"","x":260,"y":1960,"wires":[["b786488b.8bc7a"]]},{"id":"b623af3a.4d8d88","type":"ui_switch","z":"57675e72.26a0d8","name":"","label":"switch","tooltip":"","group":"b055fa85.83d6e","order":2,"width":0,"height":0,"passthru":true,"decouple":"false","topic":"","style":"","onvalue":"homie/homey-topic/fireplaceon","onvalueType":"str","onicon":"","oncolor":"","offvalue":"homie/homey-topic/fireplaceoff","offvalueType":"str","officon":"","offcolor":"","x":250,"y":2000,"wires":[["c5d88c3.9b074f"]]},{"id":"b055fa85.83d6e","type":"ui_group","z":"","name":"Default","tab":"8f03e639.85956","order":1,"disp":true,"width":"6","collapse":false},{"id":"8f03e639.85956","type":"ui_tab","z":"","name":"Home","icon":"dashboard","disabled":false,"hidden":false}]

i added how to construct topic in function node, take a look.

Hi E1cid,

Im not sure how to use your solution. I can only put one topic eg. homie/homey-topic/fireplaceon in the button node. how can i make it change to a different topic eg. homie/homey-topic/fireplaceoff when pressed

this code for the button node works, but i'm not sure what the purpose is of the switch node.

i have added how to construct topic in function node take a look. You do not have to use the topic input in btton node, you can create msg.topic after the button press.

The switch node was there for example of how to get two different outputs, after switch use a change node to move msg.payload to msg.topic.

Oh i see. The button works perfect!

Is it also possible to use an google mi icon and increase the size of it?
mi-whatshot works but mi-2x mi-whatshot not.

@E1cid a complete different problem you probably also know the answer to:
I currently have the standard lightbulb from font awesome that is yellow when on and black when off, but i would like to use my own custom .png file when a lamp is on and a different .png file when the lamp is off. both files are on the server located.

At the moment i use this switch node

If you have your setup connected to the internet, you can use iconify icons

[{"id":"c5d88c3.9b074f","type":"debug","z":"11af8890.7bdd97","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":670,"y":1180,"wires":[]},{"id":"b786488b.8bc7a","type":"function","z":"11af8890.7bdd97","name":"","func":"let button1 = flow.get(\"button1\");\nmsg.payload = button1 === \"on\" ? \"off\" : \"on\";\nflow.set(\"button1\", msg.payload);\nmsg.topic = \"homie/homey-topic/fireplace\"+msg.payload\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":490,"y":1140,"wires":[["c5d88c3.9b074f"]]},{"id":"6421eda3.e174a4","type":"ui_button","z":"11af8890.7bdd97","name":"","group":"b055fa85.83d6e","order":1,"width":0,"height":0,"passthru":false,"label":"button","tooltip":"","color":"","bgcolor":"","icon":"","payload":"button1","payloadType":"flow","topic":"","x":260,"y":1140,"wires":[["b786488b.8bc7a"]]},{"id":"b623af3a.4d8d88","type":"ui_switch","z":"11af8890.7bdd97","name":"","label":"switch","tooltip":"","group":"b055fa85.83d6e","order":2,"width":0,"height":0,"passthru":true,"decouple":"false","topic":"","style":"","onvalue":"homie/homey-topic/fireplaceon","onvalueType":"str","onicon":"iconify-ic:baseline-local-fire-department","oncolor":"yellow","offvalue":"homie/homey-topic/fireplaceoff","offvalueType":"str","officon":"iconify-ic:baseline-local-fire-department","offcolor":"orange","x":250,"y":1180,"wires":[["c5d88c3.9b074f"]]},{"id":"5269ca05.80f3d4","type":"ui_template","z":"11af8890.7bdd97","group":"d8f1e676.bd2948","name":"allow iconify","order":3,"width":0,"height":0,"format":"<script src=\"https://code.iconify.design/1/1.0.7/iconify.min.js\"></script>","storeOutMessages":true,"fwdInMessages":true,"resendOnRefresh":true,"templateScope":"global","x":270,"y":1080,"wires":[[]]},{"id":"b055fa85.83d6e","type":"ui_group","name":"Default","tab":"8f03e639.85956","order":1,"disp":true,"width":"6","collapse":false},{"id":"d8f1e676.bd2948","type":"ui_group","name":"Default","tab":"84350e6b.7f5f3","order":1,"disp":true,"width":"6","collapse":false},{"id":"8f03e639.85956","type":"ui_tab","name":"Home","icon":"dashboard","disabled":false,"hidden":false},{"id":"84350e6b.7f5f3","type":"ui_tab","name":"Away","icon":"dashboard","disabled":false,"hidden":false}]

Great this is working!
Can i also increase the size of the icon?

Hi E1cid,

I''m trying to make it work with the switch node. I added the change node behind it but it doesnt seems to work. Can you have a look at my code and see what i did wrong?

[{"id":"8ac9cc28.ef381","type":"ui_button","z":"48518d4f.6375e4","name":"","group":"ddc011ab.654ef","order":1,"width":0,"height":0,"passthru":false,"label":"button","tooltip":"","color":"","bgcolor":"","icon":"","payload":"button1","payloadType":"flow","topic":"","x":770,"y":360,"wires":[["924dfabf.0070a8"]]},{"id":"924dfabf.0070a8","type":"function","z":"48518d4f.6375e4","name":"","func":"let button1 = flow.get(\"button1\");\nmsg.payload = button1 === \"on\" ? \"off\" : \"on\";\nflow.set(\"button1\", msg.payload);\nmsg.topic = \"homie/homey-topic/fireplace\"+msg.payload\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":1010,"y":360,"wires":[["cb97940.21f4c7","6d199caf.958224"]]},{"id":"cb97940.21f4c7","type":"debug","z":"48518d4f.6375e4","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1210,"y":400,"wires":[]},{"id":"6d199caf.958224","type":"mqtt out","z":"48518d4f.6375e4","name":"MQTT Transmitter","topic":"","qos":"","retain":"","broker":"d094d3b7.a558a8","x":1210,"y":460,"wires":[]},{"id":"d83370d1.cdf99","type":"change","z":"48518d4f.6375e4","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"topic","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":960,"y":420,"wires":[["6d199caf.958224","cb97940.21f4c7"]]},{"id":"b8232141.96144","type":"ui_switch","z":"48518d4f.6375e4","name":"","label":"","tooltip":"","group":"ddc011ab.654ef","order":2,"width":0,"height":0,"passthru":true,"decouple":"false","topic":"","style":"","onvalue":"homie/homey-topic/fireplaceon","onvalueType":"str","onicon":"iconify-ic:baseline-local-fire-department","oncolor":"yellow","offvalue":"homie/homey-topic/fireplaceoff","offvalueType":"str","officon":"iconify-ic:baseline-local-fire-department","offcolor":"orange","x":770,"y":400,"wires":[["d83370d1.cdf99"]]},{"id":"ddc011ab.654ef","type":"ui_group","name":"Group 1","tab":"bcf02f19.c8136","order":1,"disp":false,"width":15,"collapse":false},{"id":"d094d3b7.a558a8","type":"mqtt-broker","name":"Homey MQTT","broker":"192.168.178.83","port":"1883","clientid":"","usetls":false,"compatmode":true,"keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","closeTopic":"","closeQos":"0","closePayload":"","willTopic":"","willQos":"0","willPayload":""},{"id":"bcf02f19.c8136","type":"ui_tab","name":"Music","icon":"my_library_music","order":5,"disabled":false,"hidden":false}]

not able to get iconify to work on my end, but here is it with MD icon

[{"id":"ec217c2a.a3711","type":"ui_switch","z":"57675e72.26a0d8","name":"","label":"switch","tooltip":"","group":"2e58add5.e83e22","order":2,"width":0,"height":0,"passthru":false,"decouple":"false","topic":"","style":"","onvalue":"homie/homey-topic/fireplaceon","onvalueType":"str","onicon":"mi-local_fire_department","oncolor":"red","offvalue":"homie/homey-topic/fireplaceoff","offvalueType":"str","officon":"mi-local_fire_department","offcolor":"black","x":80,"y":2380,"wires":[["67d59fe5.1bedb"]]},{"id":"67d59fe5.1bedb","type":"change","z":"57675e72.26a0d8","name":"","rules":[{"t":"set","p":"topic","pt":"msg","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":280,"y":2380,"wires":[["1cf192fa.dc719d","ec217c2a.a3711"]]},{"id":"1cf192fa.dc719d","type":"debug","z":"57675e72.26a0d8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":540,"y":2360,"wires":[]},{"id":"e7e84458.43c2f","type":"function","z":"57675e72.26a0d8","name":"","func":"let button1 = flow.get(\"button1\");\nmsg.payload = button1 === \"on\" ? \"off\" : \"on\";\nflow.set(\"button1\", msg.payload);\nmsg.buttoncolor = msg.payload ===\"on\" ? \"red\" : \"blacK\";\nmsg.topic = \"homie/homey-topic/fireplace\"+msg.payload\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":310,"y":2320,"wires":[["1cf192fa.dc719d","979d1588.20ee6"]]},{"id":"979d1588.20ee6","type":"ui_button","z":"57675e72.26a0d8","name":"","group":"2e58add5.e83e22","order":1,"width":0,"height":0,"passthru":false,"label":"button","tooltip":"","color":"{{buttoncolor}}","bgcolor":"","icon":"mi-local_fire_department","payload":"button1","payloadType":"flow","topic":"","x":100,"y":2320,"wires":[["e7e84458.43c2f"]]},{"id":"2e58add5.e83e22","type":"ui_group","name":"Group 1","tab":"a3108c9b.a3cac8","order":1,"disp":false,"width":15,"collapse":false},{"id":"a3108c9b.a3cac8","type":"ui_tab","name":"Music","icon":"my_library_music","order":5,"disabled":false,"hidden":false}]

you had payload and topic reversed

it worked!

There is many ways to do it and to choose the correct way really depends on too many things :slight_smile:

[{"id":"ec217c2a.a3711","type":"ui_switch","z":"e8af3254.77f7e","name":"","label":"switch","tooltip":"","group":"2e58add5.e83e22","order":2,"width":0,"height":0,"passthru":false,"decouple":"false","topic":"","style":"","onvalue":"homie/homey-topic/fireplaceon","onvalueType":"str","onicon":"mi-local_fire_department","oncolor":"red","offvalue":"homie/homey-topic/fireplaceoff","offvalueType":"str","officon":"mi-local_fire_department","offcolor":"black","x":270,"y":340,"wires":[["67d59fe5.1bedb"]]},{"id":"67d59fe5.1bedb","type":"change","z":"e8af3254.77f7e","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"topic","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":480,"y":340,"wires":[["1cf192fa.dc719d","ec217c2a.a3711"]]},{"id":"1cf192fa.dc719d","type":"debug","z":"e8af3254.77f7e","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":730,"y":320,"wires":[]},{"id":"e7e84458.43c2f","type":"function","z":"e8af3254.77f7e","name":"","func":"let button1 = flow.get(\"button1\");\nmsg.payload = button1 === \"on\" ? \"off\" : \"on\";\nflow.set(\"button1\", msg.payload);\nmsg.buttoncolor = msg.payload ===\"on\" ? \"red\" : \"blacK\";\nmsg.topic = \"homie/homey-topic/fireplace\"+msg.payload\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":500,"y":280,"wires":[["1cf192fa.dc719d","979d1588.20ee6"]]},{"id":"979d1588.20ee6","type":"ui_button","z":"e8af3254.77f7e","name":"","group":"2e58add5.e83e22","order":1,"width":0,"height":0,"passthru":false,"label":"button","tooltip":"","color":"{{buttoncolor}}","bgcolor":"","icon":"mi-local_fire_department","payload":"button1","payloadType":"flow","topic":"","x":290,"y":280,"wires":[["e7e84458.43c2f"]]},{"id":"8ca3e4b4.aa6228","type":"ui_template","z":"e8af3254.77f7e","group":"ea1c04a1.f51148","name":"dashboard CSS","order":3,"width":0,"height":0,"format":"<style id=\"dashboard-style-override\">\n    /*icon size for switches*/\n    .nr-dashboard-switch ui-icon > * i{\n        font-size:36px;\n    }\n    /*icon size for buttons*/\n    .nr-dashboard-button i {\n        font-size: 36px;\n    }\n</style>","storeOutMessages":true,"fwdInMessages":true,"resendOnRefresh":true,"templateScope":"global","x":360,"y":440,"wires":[[]]},{"id":"2e58add5.e83e22","type":"ui_group","name":"Group 1","tab":"a3108c9b.a3cac8","order":1,"disp":false,"width":6,"collapse":false},{"id":"ea1c04a1.f51148","type":"ui_group","name":"ConexĂŁo","tab":"b1ab5f32.33e4d8","order":1,"disp":true,"width":"6","collapse":false},{"id":"a3108c9b.a3cac8","type":"ui_tab","name":"Music","icon":"my_library_music","order":5,"disabled":false,"hidden":false},{"id":"b1ab5f32.33e4d8","type":"ui_tab","name":"triumph","icon":"dashboard","disabled":false,"hidden":false}]

Yes this worked. Thank you

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