# Only send a message if I press a button

**URL:** https://discourse.nodered.org/t/only-send-a-message-if-i-press-a-button/46355
**Category:** General
**Created:** [27 May 2021 15:57 UTC](https://discourse.nodered.org/t/only-send-a-message-if-i-press-a-button/46355 "2021-05-27T15:57:37Z")
**Posts on this page:** 17
**Page:** 1

<div class="post-metadata">

### Author: ![luis](https://avatars.discourse-cdn.com/v4/letter/l/3e96dc/32.png) [@luis](https://discourse.nodered.org/u/luis)
#### Post date: [27 May 2021 15:57 UTC](https://discourse.nodered.org/t/only-send-a-message-if-i-press-a-button/46355/1 "2021-05-27T15:57:38Z")

</div>

Is there any way I can store a value until I press a button?

The problem Im having is that a button is just an input and I dont know how to make It so the value gets stored until I press the button.

---

<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: [27 May 2021 16:01 UTC](https://discourse.nodered.org/t/only-send-a-message-if-i-press-a-button/46355/2 "2021-05-27T16:01:41Z")

</div>

Use a change node to store the value in a flow or context variable.

Then when the button is pressed, grab the value you stored early.

See the docs (read up on context)

---

<div class="post-metadata">

### Author: ![luis](https://avatars.discourse-cdn.com/v4/letter/l/3e96dc/32.png) [@luis](https://discourse.nodered.org/u/luis)
#### Post date: [27 May 2021 16:05 UTC](https://discourse.nodered.org/t/only-send-a-message-if-i-press-a-button/46355/3 "2021-05-27T16:05:51Z")

</div>

The other problem is that the value can be overwriten by another value and it ranges between 2 and 13, I just want the last one that arrives, and there is no specific time in when the values arrive.

---

<div class="post-metadata">

### Author: ![Ranki](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ranki/32/41282_2.png) [@Ranki](https://discourse.nodered.org/u/Ranki)
#### Post date: [27 May 2021 17:43 UTC](https://discourse.nodered.org/t/only-send-a-message-if-i-press-a-button/46355/4 "2021-05-27T17:43:38Z")

</div>

Hello @luis ,

I think you will be able to handle this with the recommendation of Steve.

I do not understand everything clearly what you want to achive, but I give it a try (maybe it is better, if you provide some more information):

You can use the change node to overwrite the flow variable as Steve mentioned. Normally the current value in the variable is overwritten with the new value. If you now want to implement sth., which should stop this override you can maybe work with the [node-red-contrib-simple-gate](https://flows.nodered.org/node/node-red-contrib-simple-gate). Open it by default and close it, when the button is pressed. This gate comes before the change node.

I implemented this in this example flow:

```auto
[
    {
        "id": "37194080.b3585",
        "type": "random",
        "z": "b4fa726a.49a62",
        "name": "",
        "low": "2",
        "high": "13",
        "inte": "true",
        "property": "payload",
        "x": 300,
        "y": 2560,
        "wires": [
            [
                "3f258200.7bc9be",
                "f81ebbc3.c7d1b8"
            ]
        ]
    },
    {
        "id": "55fb5d52.7e8d94",
        "type": "inject",
        "z": "b4fa726a.49a62",
        "name": "",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "1",
        "crontab": "",
        "once": true,
        "onceDelay": 0.1,
        "topic": "Test",
        "payload": "",
        "payloadType": "date",
        "x": 120,
        "y": 2560,
        "wires": [
            [
                "37194080.b3585"
            ]
        ]
    },
    {
        "id": "3f258200.7bc9be",
        "type": "gate",
        "z": "b4fa726a.49a62",
        "name": "",
        "controlTopic": "testgate",
        "defaultState": "open",
        "openCmd": "open",
        "closeCmd": "close",
        "toggleCmd": "toggle",
        "defaultCmd": "default",
        "statusCmd": "status",
        "persist": true,
        "x": 450,
        "y": 2480,
        "wires": [
            [
                "150a5e05.f42c92",
                "b67c13c.de440f"
            ]
        ]
    },
    {
        "id": "150a5e05.f42c92",
        "type": "change",
        "z": "b4fa726a.49a62",
        "name": "",
        "rules": [
            {
                "t": "set",
                "p": "test",
                "pt": "flow",
                "to": "payload",
                "tot": "msg"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 620,
        "y": 2480,
        "wires": [
            []
        ]
    },
    {
        "id": "71cfd57.7216b2c",
        "type": "ui_button",
        "z": "b4fa726a.49a62",
        "name": "",
        "group": "680bc7a0.869958",
        "order": 0,
        "width": 0,
        "height": 0,
        "passthru": false,
        "label": "Open",
        "tooltip": "",
        "color": "",
        "bgcolor": "",
        "icon": "",
        "payload": "open",
        "payloadType": "str",
        "topic": "testgate",
        "topicType": "str",
        "x": 150,
        "y": 2460,
        "wires": [
            [
                "3f258200.7bc9be"
            ]
        ]
    },
    {
        "id": "56270d03.644f64",
        "type": "ui_button",
        "z": "b4fa726a.49a62",
        "name": "",
        "group": "680bc7a0.869958",
        "order": 0,
        "width": 0,
        "height": 0,
        "passthru": false,
        "label": "Close",
        "tooltip": "",
        "color": "",
        "bgcolor": "",
        "icon": "",
        "payload": "close",
        "payloadType": "str",
        "topic": "testgate",
        "topicType": "str",
        "x": 150,
        "y": 2500,
        "wires": [
            [
                "3f258200.7bc9be"
            ]
        ]
    },
    {
        "id": "b67c13c.de440f",
        "type": "ui_text",
        "z": "b4fa726a.49a62",
        "group": "680bc7a0.869958",
        "order": 2,
        "width": 0,
        "height": 0,
        "name": "",
        "label": "Number (stored)",
        "format": "{{msg.payload}}",
        "layout": "row-spread",
        "x": 620,
        "y": 2520,
        "wires": []
    },
    {
        "id": "f81ebbc3.c7d1b8",
        "type": "ui_text",
        "z": "b4fa726a.49a62",
        "group": "680bc7a0.869958",
        "order": 2,
        "width": 0,
        "height": 0,
        "name": "",
        "label": "Number (random)",
        "format": "{{msg.payload}}",
        "layout": "row-spread",
        "x": 630,
        "y": 2560,
        "wires": []
    },
    {
        "id": "680bc7a0.869958",
        "type": "ui_group",
        "name": "Test",
        "tab": "19c49f89.3bc7f",
        "order": 2,
        "disp": true,
        "width": "6",
        "collapse": false
    },
    {
        "id": "19c49f89.3bc7f",
        "type": "ui_tab",
        "name": "Test",
        "icon": "dashboard",
        "order": 12
    }
]

```

I hope this helps.

Cheers  
Ranki

[EDIT]: Added example flow.

---

<div class="post-metadata">

### Author: ![luis](https://avatars.discourse-cdn.com/v4/letter/l/3e96dc/32.png) [@luis](https://discourse.nodered.org/u/luis)
#### Post date: [28 May 2021 14:50 UTC](https://discourse.nodered.org/t/only-send-a-message-if-i-press-a-button/46355/5 "2021-05-28T14:50:38Z")

</div>

I figured a way to make that gate work but i can't figure out how to keep the value until I want the gate to open and let the value through

---

<div class="post-metadata">

### Author: ![Ranki](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ranki/32/41282_2.png) [@Ranki](https://discourse.nodered.org/u/Ranki)
#### Post date: [28 May 2021 14:55 UTC](https://discourse.nodered.org/t/only-send-a-message-if-i-press-a-button/46355/6 "2021-05-28T14:55:04Z")

</div>

Hello @luis ,

did you try my example flow? I think it cover your need or I didn't get your point ...

Cheers  
Ranki

---

<div class="post-metadata">

### Author: ![luis](https://avatars.discourse-cdn.com/v4/letter/l/3e96dc/32.png) [@luis](https://discourse.nodered.org/u/luis)
#### Post date: [28 May 2021 15:00 UTC](https://discourse.nodered.org/t/only-send-a-message-if-i-press-a-button/46355/7 "2021-05-28T15:00:19Z")

</div>

I dont have intalled some of the nodes of your example flow, is there a way I can know the name of the ones that I need to install?

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/0/b/0b8a9efa48208d437903196be75fa2504dd51d95.png)

---

<div class="post-metadata">

### Author: ![Paul-Reed](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/paul-reed/32/66906_2.png) [@Paul-Reed](https://discourse.nodered.org/u/Paul-Reed)
#### Post date: [28 May 2021 15:06 UTC](https://discourse.nodered.org/t/only-send-a-message-if-i-press-a-button/46355/8 "2021-05-28T15:06:58Z")

</div>

Have you installed the dashboard?  
`node-red-dashboard`  
Those nodes come with the dashboard.

Or, are you using a physical button, and not a dashboard button?

---

<div class="post-metadata">

### Author: ![luis](https://avatars.discourse-cdn.com/v4/letter/l/3e96dc/32.png) [@luis](https://discourse.nodered.org/u/luis)
#### Post date: [28 May 2021 15:09 UTC](https://discourse.nodered.org/t/only-send-a-message-if-i-press-a-button/46355/9 "2021-05-28T15:09:21Z")

</div>

Im using a physical button

---

<div class="post-metadata">

### Author: ![Paul-Reed](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/paul-reed/32/66906_2.png) [@Paul-Reed](https://discourse.nodered.org/u/Paul-Reed)
#### Post date: [28 May 2021 15:09 UTC](https://discourse.nodered.org/t/only-send-a-message-if-i-press-a-button/46355/10 "2021-05-28T15:09:51Z")

</div>

It would have been good to tell us that in your first post!!

---

<div class="post-metadata">

### Author: ![luis](https://avatars.discourse-cdn.com/v4/letter/l/3e96dc/32.png) [@luis](https://discourse.nodered.org/u/luis)
#### Post date: [28 May 2021 15:10 UTC](https://discourse.nodered.org/t/only-send-a-message-if-i-press-a-button/46355/11 "2021-05-28T15:10:49Z")

</div>

You are right, Im pretty new when it comes to node-red or any type of programming

---

<div class="post-metadata">

### Author: ![Paul-Reed](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/paul-reed/32/66906_2.png) [@Paul-Reed](https://discourse.nodered.org/u/Paul-Reed)
#### Post date: [28 May 2021 15:11 UTC](https://discourse.nodered.org/t/only-send-a-message-if-i-press-a-button/46355/12 "2021-05-28T15:11:25Z")

</div>

No worries.  
How are you reading the button state in node-RED?

---

<div class="post-metadata">

### Author: ![Ranki](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ranki/32/41282_2.png) [@Ranki](https://discourse.nodered.org/u/Ranki)
#### Post date: [28 May 2021 15:13 UTC](https://discourse.nodered.org/t/only-send-a-message-if-i-press-a-button/46355/13 "2021-05-28T15:13:23Z")

</div>

Ah! 🙃

I assumed you are using the [node-red-dashboard](https://flows.nodered.org/node/node-red-dashboard) with the corresponding dashboard buttons ...

I adapted the flow to use injection nodes and debug output, so you can see the result w/o the dashboard nodes:

```auto
[
    {
        "id": "babf09ee.f9a118",
        "type": "random",
        "z": "b4fa726a.49a62",
        "name": "",
        "low": "2",
        "high": "13",
        "inte": "true",
        "property": "payload",
        "x": 480,
        "y": 2580,
        "wires": [
            [
                "978c3311.f10d8",
                "a6b91bb.72266e8",
                "a65b9ee2.3996a"
            ]
        ]
    },
    {
        "id": "a5f779b0.2d4a68",
        "type": "inject",
        "z": "b4fa726a.49a62",
        "name": "",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "1",
        "crontab": "",
        "once": true,
        "onceDelay": 0.1,
        "topic": "Test",
        "payload": "",
        "payloadType": "date",
        "x": 300,
        "y": 2580,
        "wires": [
            [
                "babf09ee.f9a118"
            ]
        ]
    },
    {
        "id": "978c3311.f10d8",
        "type": "gate",
        "z": "b4fa726a.49a62",
        "name": "",
        "controlTopic": "testgate",
        "defaultState": "open",
        "openCmd": "open",
        "closeCmd": "close",
        "toggleCmd": "toggle",
        "defaultCmd": "default",
        "statusCmd": "status",
        "persist": true,
        "x": 630,
        "y": 2520,
        "wires": [
            [
                "6169069.d570ff8",
                "9d11374e.fcda58",
                "71effd0.f8b2304"
            ]
        ]
    },
    {
        "id": "6169069.d570ff8",
        "type": "change",
        "z": "b4fa726a.49a62",
        "name": "",
        "rules": [
            {
                "t": "set",
                "p": "test",
                "pt": "flow",
                "to": "payload",
                "tot": "msg"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 800,
        "y": 2480,
        "wires": [
            []
        ]
    },
    {
        "id": "2c2f7177.7f08fe",
        "type": "inject",
        "z": "b4fa726a.49a62",
        "name": "",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "testgate",
        "payload": "open",
        "payloadType": "str",
        "x": 290,
        "y": 2480,
        "wires": [
            [
                "978c3311.f10d8"
            ]
        ]
    },
    {
        "id": "d4cf99f6.bdea28",
        "type": "inject",
        "z": "b4fa726a.49a62",
        "name": "",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "testgate",
        "payload": "close",
        "payloadType": "str",
        "x": 290,
        "y": 2520,
        "wires": [
            [
                "978c3311.f10d8"
            ]
        ]
    },
    {
        "id": "71effd0.f8b2304",
        "type": "debug",
        "z": "b4fa726a.49a62",
        "name": "",
        "active": true,
        "tosidebar": false,
        "console": false,
        "tostatus": true,
        "complete": "payload",
        "targetType": "msg",
        "statusVal": "payload",
        "statusType": "auto",
        "x": 790,
        "y": 2520,
        "wires": []
    },
    {
        "id": "a65b9ee2.3996a",
        "type": "debug",
        "z": "b4fa726a.49a62",
        "name": "",
        "active": true,
        "tosidebar": false,
        "console": false,
        "tostatus": true,
        "complete": "payload",
        "targetType": "msg",
        "statusVal": "payload",
        "statusType": "auto",
        "x": 790,
        "y": 2580,
        "wires": []
    }
]

```

Hope, this helps.

Cheers  
Ranki

---

<div class="post-metadata">

### Author: ![luis](https://avatars.discourse-cdn.com/v4/letter/l/3e96dc/32.png) [@luis](https://discourse.nodered.org/u/luis)
#### Post date: [28 May 2021 15:16 UTC](https://discourse.nodered.org/t/only-send-a-message-if-i-press-a-button/46355/14 "2021-05-28T15:16:58Z")

</div>

So this is what I have ate the moment

As you can see in the flow I want the value to get stored until I click the button that opens the gate, but the issue is that i can't figure out a way to do that.

```auto
[{"id":"553ef23e.b3426c","type":"tab","label":"Flow 2","disabled":false,"info":""},{"id":"6e8e5318.1e493c","type":"comment","z":"553ef23e.b3426c","name":"CLK","info":"","x":270,"y":160,"wires":[]},{"id":"3155ddc3.393852","type":"trigger","z":"553ef23e.b3426c","name":"","op1":"","op2":"","op1type":"pay","op2type":"nul","duration":"250","extend":false,"units":"ms","reset":"","bytopic":"all","topic":"topic","outputs":1,"x":680,"y":200,"wires":[["fdf03b44.5944a8"]]},{"id":"fdf03b44.5944a8","type":"function","z":"553ef23e.b3426c","name":"","func":"if (context.get(\"counter\") === undefined) {\n context.set(\"counter\", 1);\n}\n\nvar counter = context.get('counter');\n if(msg.payload == \"clockwise\")\n counter++;\n \n if(counter > 13){\n counter = 2;\n}\n \ncontext.set(\"counter\", counter);\n\nmsg.payload = counter;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":900,"y":200,"wires":[["be01c952.136548"]]},{"id":"c13f8bed.8d5728","type":"function","z":"553ef23e.b3426c","name":"","func":"var CLK;\n\n\nif ((msg['topic']) == 'CLK') {\n CLK = (msg['payload']);\n msg['payload'] = 'clockwise';\n return msg;\n}","outputs":1,"noerr":0,"initialize":"","finalize":"","x":480,"y":200,"wires":[["3155ddc3.393852"]]},{"id":"33b5ea72.915226","type":"change","z":"553ef23e.b3426c","name":"","rules":[{"t":"set","p":"topic","pt":"msg","to":"CLK","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":280,"y":200,"wires":[["c13f8bed.8d5728"]]},{"id":"3a3a9f92.26d3e","type":"rpi-gpio in","z":"553ef23e.b3426c","name":"","pin":"11","intype":"tri","debounce":"70","read":false,"x":70,"y":200,"wires":[["33b5ea72.915226"]]},{"id":"c055dfae.02ab3","type":"rpi-gpio in","z":"553ef23e.b3426c","name":"","pin":"40","intype":"tri","debounce":"25","read":false,"x":190,"y":420,"wires":[["b7248868.a213c8"]]},{"id":"be01c952.136548","type":"gate","z":"553ef23e.b3426c","name":"gate demo","controlTopic":"control","defaultState":"closed","openCmd":"open","closeCmd":"close","toggleCmd":"toggle","defaultCmd":"default","statusCmd":"status","persist":false,"x":730,"y":300,"wires":[["4bff201a.bbe18"]]},{"id":"273f487a.ebe408","type":"function","z":"553ef23e.b3426c","name":"","func":"var open;\n\n\nif ((msg['payload']) == 'open') {\n gate = (msg['topic']);\n msg['topic'] = 'control';\n return msg;\n}","outputs":1,"noerr":0,"initialize":"","finalize":"","x":480,"y":480,"wires":[["be01c952.136548"]]},{"id":"b7248868.a213c8","type":"change","z":"553ef23e.b3426c","name":"","rules":[{"t":"change","p":"payload","pt":"msg","from":"1","fromt":"num","to":"open","tot":"str"},{"t":"change","p":"payload","pt":"msg","from":"0","fromt":"num","to":"close","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":360,"y":420,"wires":[["273f487a.ebe408","844fd829.245058"]]},{"id":"844fd829.245058","type":"function","z":"553ef23e.b3426c","name":"","func":"var close;\n\n\nif ((msg['payload']) == 'close') {\n close = (msg['topic']);\n msg['topic'] = 'control';\n return msg;\n}","outputs":1,"noerr":0,"initialize":"","finalize":"","x":480,"y":360,"wires":[["be01c952.136548"]]},{"id":"4bff201a.bbe18","type":"debug","z":"553ef23e.b3426c","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":960,"y":300,"wires":[]}]

```

---

<div class="post-metadata">

### Author: ![Paul-Reed](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/paul-reed/32/66906_2.png) [@Paul-Reed](https://discourse.nodered.org/u/Paul-Reed)
#### Post date: [28 May 2021 15:25 UTC](https://discourse.nodered.org/t/only-send-a-message-if-i-press-a-button/46355/15 "2021-05-28T15:25:39Z")

</div>

OK, try this;

1. save the value to context (as described - [Working with context : Node-RED](https://nodered.org/docs/user-guide/context) )
2. connect your gpio node to a change node, that will load the saved context value, and output it as the msg.payload every time you press the button.

![gpio](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/5/b/5bc9745525cae66a46384987cdb9903fdf6b6850.jpeg)

(I don't have the gpio node installed, so I haven't tried your flow)

@Steve-Mcl [suggested this](https://discourse.nodered.org/t/only-send-a-message-if-i-press-a-button/46355/2) solution to you yesterday.

---

<div class="post-metadata">

### Author: ![luis](https://avatars.discourse-cdn.com/v4/letter/l/3e96dc/32.png) [@luis](https://discourse.nodered.org/u/luis)
#### Post date: [28 May 2021 15:58 UTC](https://discourse.nodered.org/t/only-send-a-message-if-i-press-a-button/46355/16 "2021-05-28T15:58:46Z")

</div>

Thanks a lot for helping but thats not quite exaclty what I want, as you can see in the flow I posted In the comments, the value goes from 2 to 13 in order by the times you click on a button, but I just want that value to pass through once I click on another button, so the first button sends an input, and the second one lets it through, the problem Is that i can't find a way to keep that value of the first button until I click the other one, so basicly what's happening is that I need to have the second button pressed for the value to go through and I didn't want that.

---

<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: [27 July 2021 15:58 UTC](https://discourse.nodered.org/t/only-send-a-message-if-i-press-a-button/46355/17 "2021-07-27T15:58:46Z")

</div>

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