Only send a message if I press a button

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.

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)

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.

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. 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:

[
    {
        "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.

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

Hello @luis ,

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

Cheers
Ranki

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?

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?

Im using a physical button

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

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

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

Ah! :upside_down_face:

I assumed you are using the 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:

[
    {
        "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

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.

[{"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":[]}]

OK, try this;

  1. save the value to context (as described - Working with context : Node-RED )
  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

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

@Steve-Mcl suggested this solution to you yesterday.

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.

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