# Get output from button

**URL:** <https://discourse.nodered.org/t/get-output-from-button/41771>\
**Category:** Dashboard\
**Created:** [28 February 2021 23:17 UTC](https://discourse.nodered.org/t/get-output-from-button/41771 "2021-02-28T23:17:57Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![mune](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mune/32/37105_2.png) [@mune](https://discourse.nodered.org/u/mune)\
**Post date:** [28 February 2021 23:17 UTC](https://discourse.nodered.org/t/get-output-from-button/41771/1 "2021-02-28T23:17:57Z")

</div>

It seems the easiest thing ever, but I can't do it, even googling and reading for a week.

With a function node I prepare a msg object like:

```auto
msg.payload = "label number 1";
msg.background="cyan";
msg.color="blue";

```

then it does `ret msg`.

The next node is the button one whose properties are `Label -> {{msg.payload}}`, `Color -> {{msg.color}}`, `Background -> {{msg.background}}`.

So far it was pretty easy and everything is ok.

Now I put another function node where I change the button's label to update it once pressed. This arise two problems:

1. I can't get the button output (output=what is the label showed when it is pressed)
2. button-function-button, is a loop(!)

Help for 1 and 2

Thanks

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [28 February 2021 23:22 UTC](https://discourse.nodered.org/t/get-output-from-button/41771/2 "2021-02-28T23:22:06Z")

</div>

> [@mune](#):
>
> button-function-button, is a loop(!)

There is an option on the button to pass through. Switch that off.

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [1 March 2021 07:08 UTC](https://discourse.nodered.org/t/get-output-from-button/41771/3 "2021-03-01T07:08:35Z")

</div>

1. buttons only send what they are configured to send, they can't send the label.

---

<div class="post-metadata">

**Author:** ![mune](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mune/32/37105_2.png) [@mune](https://discourse.nodered.org/u/mune)\
**Post date:** [1 March 2021 11:04 UTC](https://discourse.nodered.org/t/get-output-from-button/41771/4 "2021-03-01T11:04:32Z")

</div>

> [@Steve-Mcl](#):
>
> > [@mune](#):
> >
> > button-function-button, is a loop(!)
> 
> There is an option on the button to pass through. Switch that off.

Yes, in that way I could avoid the loop but the button's label won't be update.

---

<div class="post-metadata">

**Author:** ![mune](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mune/32/37105_2.png) [@mune](https://discourse.nodered.org/u/mune)\
**Post date:** [1 March 2021 11:42 UTC](https://discourse.nodered.org/t/get-output-from-button/41771/5 "2021-03-01T11:42:22Z")

</div>

> [@dceejay](#):
>
> buttons only send what they are configured to send, they can't send the label.

I hoped in a workaround to achieve the same effect.

My idea: a function to set the current and the new label's value (saved in global var) next in the flow is the btn node (to update the btn, with pass through on) then a gate node to the stop the flow loop only if the label isn't equal to the previous.  
I tried this path but you initialize the button with "1". You press it and default value for the gate is open, so it goes to the function and it sets the `current_label="1"` and `new_label="2"`; the btn is update, as it has the pass-through on, it calls the gate `current_label!=new_label` the flow goes over and effect is that the button says "1", you press and the button's label changes in "7". I thought to place a delay node but a) it gets very slow and user keep pressing as anything happen in the ui; b) if a user is very slow the effect 1 to 7 happens again.

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [1 March 2021 17:10 UTC](https://discourse.nodered.org/t/get-output-from-button/41771/6 "2021-03-01T17:10:16Z")

</div>

If you use topic instead that does get passed through...

```auto
[{"id":"c421e3a4.c3d4","type":"ui_button","z":"d3236f0.2a14a9","name":"","group":"82869eba.d348d","order":2,"width":0,"height":0,"passthru":false,"label":"Click {{topic}}","tooltip":"","color":"","bgcolor":"","icon":"","payload":"1","payloadType":"str","topic":"topic","topicType":"msg","x":370,"y":260,"wires":[["2f405f93.2bf4a"]]},{"id":"2f405f93.2bf4a","type":"function","z":"d3236f0.2a14a9","name":"","func":"context.count = (context.count || 0) + 1;\nmsg.topic = context.count;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":500,"y":320,"wires":[["c421e3a4.c3d4"]]},{"id":"82869eba.d348d","type":"ui_group","name":"Old Mayflower","tab":"9979cb8a.2a3238","order":2,"disp":true,"width":"9","collapse":false},{"id":"9979cb8a.2a3238","type":"ui_tab","name":"TEST","icon":"dashboard","order":20,"disabled":false,"hidden":false}]

```

---

<div class="post-metadata">

**Author:** ![mune](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mune/32/37105_2.png) [@mune](https://discourse.nodered.org/u/mune)\
**Post date:** [1 March 2021 21:43 UTC](https://discourse.nodered.org/t/get-output-from-button/41771/7 "2021-03-01T21:43:44Z")

</div>

Congratulations!!  
With just two nodes: amazing.  
I slighly changed it to better understand how it works, I made a "semaphore".

```auto
[{"id":"1612856b.8afac3","type":"tab","label":"semaphore btn","disabled":false,"info":""},{"id":"c421e3a4.c3d4","type":"ui_button","z":"1612856b.8afac3","name":"","group":"82869eba.d348d","order":2,"width":0,"height":0,"passthru":false,"label":"{{topic}}","tooltip":"","color":"{{msg.color}}","bgcolor":"{{msg.background}}","icon":"","payload":"whatever","payloadType":"str","topic":"topic","topicType":"msg","x":380,"y":195,"wires":[["2f405f93.2bf4a"]]},{"id":"2f405f93.2bf4a","type":"function","z":"1612856b.8afac3","name":"","func":"//context.count = (context.count || 0) + 1;\n//msg.topic = context.count;\n\nswitch (context.status) {\n case \"green\" : \n context.status = \"orange\";\n msg.background=\"orange\";\n break;\n case \"orange\" : \n context.status = \"red\";\n msg.background=\"red\";\n break;\n case \"red\" : \n context.status = \"green\";\n msg.background=\"green\";\n break;\n default : \n context.status = \"orange\";\n msg.background=\"orange\";\n break;\n}\nmsg.color = \"black\";\nmsg.topic = context.status;\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":360,"y":345,"wires":[["c421e3a4.c3d4"]]},{"id":"82869eba.d348d","type":"ui_group","name":"Old Mayflower","tab":"9979cb8a.2a3238","order":2,"disp":true,"width":"9","collapse":false},{"id":"9979cb8a.2a3238","type":"ui_tab","name":"TEST","icon":"dashboard","order":20,"disabled":false,"hidden":false}]

```

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [15 March 2021 21:43 UTC](https://discourse.nodered.org/t/get-output-from-button/41771/8 "2021-03-15T21:43:58Z")

</div>

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