# Push button 0/1 GPIO

**URL:** <https://discourse.nodered.org/t/push-button-0-1-gpio/20787>\
**Category:** General\
**Created:** [23 January 2020 22:49 UTC](https://discourse.nodered.org/t/push-button-0-1-gpio/20787 "2020-01-23T22:49:54Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![salguero98](https://avatars.discourse-cdn.com/v4/letter/s/53a042/32.png) [@salguero98](https://discourse.nodered.org/u/salguero98)\
**Post date:** [23 January 2020 22:49 UTC](https://discourse.nodered.org/t/push-button-0-1-gpio/20787/1 "2020-01-23T22:49:54Z")

</div>

Hello, can anyone help me? I need a function that allows me to change the state.

what I want is to press the button and have the output to 1 and press again and output = 0

![Captura de pantalla 2020-01-23 a las 23.45.38](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/3/5/35ce0ee0ff9473314009025e51da97f9fa2a3076.png)

thanks

---

<div class="post-metadata">

**Author:** ![Randy](https://avatars.discourse-cdn.com/v4/letter/r/2acd7d/32.png) [@Randy](https://discourse.nodered.org/u/Randy)\
**Post date:** [24 January 2020 03:10 UTC](https://discourse.nodered.org/t/push-button-0-1-gpio/20787/2 "2020-01-24T03:10:20Z")

</div>

Try this and see if it it what you want.

```auto
[{"id":"7347b78.8b23848","type":"switch","z":"6c4e53bf.97148c","name":"","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"1","vt":"str"},{"t":"eq","v":"0","vt":"str"}],"checkall":"true","repair":false,"outputs":2,"x":390,"y":180,"wires":[["82ff5a1a.efefc8"],["26637948.29d256"]]},{"id":"82ff5a1a.efefc8","type":"change","z":"6c4e53bf.97148c","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"1","tot":"str"},{"t":"set","p":"next","pt":"flow","to":"0","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":600,"y":140,"wires":[[]]},{"id":"26637948.29d256","type":"change","z":"6c4e53bf.97148c","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"0","tot":"str"},{"t":"set","p":"next","pt":"flow","to":"1","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":600,"y":200,"wires":[[]]},{"id":"5047f5ea.66c1ec","type":"function","z":"6c4e53bf.97148c","name":"get next value","func":"msg.payload = flow.get(\"next\")||0;\nreturn msg;","outputs":1,"noerr":0,"x":200,"y":180,"wires":[["7347b78.8b23848"]]}]

```

---

<div class="post-metadata">

**Author:** ![Randy](https://avatars.discourse-cdn.com/v4/letter/r/2acd7d/32.png) [@Randy](https://discourse.nodered.org/u/Randy)\
**Post date:** [24 January 2020 03:39 UTC](https://discourse.nodered.org/t/push-button-0-1-gpio/20787/3 "2020-01-24T03:39:41Z")

</div>

That might not have been the correct flow. I'll try and check back later if it doesn't work for you.

---

<div class="post-metadata">

**Author:** ![salguero98](https://avatars.discourse-cdn.com/v4/letter/s/53a042/32.png) [@salguero98](https://discourse.nodered.org/u/salguero98)\
**Post date:** [24 January 2020 13:37 UTC](https://discourse.nodered.org/t/push-button-0-1-gpio/20787/4 "2020-01-24T13:37:03Z")

</div>

> [@Randy](#):
>
> ```auto
> [{"id":"7347b78.8b23848","type":"switch","z":"6c4e53bf.97148c","name":"","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"1","vt":"str"},{"t":"eq","v":"0","vt":"str"}],"checkall":"true","repair":false,"outputs":2,"x":390,"y":180,"wires":[["82ff5a1a.efefc8"],["26637948.29d256"]]},{"id":"82ff5a1a.efefc8","type":"change","z":"6c4e53bf.97148c","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"1","tot":"str"},{"t":"set","p":"next","pt":"flow","to":"0","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":600,"y":140,"wires":[[]]},{"id":"26637948.29d256","type":"change","z":"6c4e53bf.97148c","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"0","tot":"str"},{"t":"set","p":"next","pt":"flow","to":"1","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":600,"y":200,"wires":[[]]},{"id":"5047f5ea.66c1ec","type":"function","z":"6c4e53bf.97148c","name":"get next value","func":"msg.payload = flow.get(\"next\")||0;\nreturn msg;","outputs":1,"noerr":0,"x":200,"y":180,"wires":[["7347b78.8b23848"]]}]
> 
> ```

sorry but where do I have to put this? in node function?

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [24 January 2020 15:59 UTC](https://discourse.nodered.org/t/push-button-0-1-gpio/20787/5 "2020-01-24T15:59:11Z")

</div>

That is a flow that you will import into your existing flow.

---

<div class="post-metadata">

**Author:** ![cymplecy](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cymplecy/32/2773_2.png) [@cymplecy](https://discourse.nodered.org/u/cymplecy)\
**Post date:** [24 January 2020 17:36 UTC](https://discourse.nodered.org/t/push-button-0-1-gpio/20787/6 "2020-01-24T17:36:07Z")

</div>

> [@salguero98](#):
>
> what I want is to press the button and have the output to 1 and press again and output = 0

If you put a function node between your existing ones and then paste this code into it then it should do what you want

```auto
if (msg.payload == 1) {
    context.set("previousState",!context.get("previousState"))
    msg.payload = 1 * context.get("previousState");
    return msg;
}

```

If you need an explanation - please ask for more information 🙂

---

<div class="post-metadata">

**Author:** ![salguero98](https://avatars.discourse-cdn.com/v4/letter/s/53a042/32.png) [@salguero98](https://discourse.nodered.org/u/salguero98)\
**Post date:** [24 January 2020 21:41 UTC](https://discourse.nodered.org/t/push-button-0-1-gpio/20787/7 "2020-01-24T21:41:09Z")

</div>

thank you very much it is perfect!!!

---

<div class="post-metadata">

**Author:** ![Randy](https://avatars.discourse-cdn.com/v4/letter/r/2acd7d/32.png) [@Randy](https://discourse.nodered.org/u/Randy)\
**Post date:** [25 January 2020 03:35 UTC](https://discourse.nodered.org/t/push-button-0-1-gpio/20787/8 "2020-01-25T03:35:32Z")

</div>

Just got back to this forum. Glad it worked for you.

---

<div class="post-metadata">

**Author:** ![heshampop](https://avatars.discourse-cdn.com/v4/letter/h/ad7895/32.png) [@heshampop](https://discourse.nodered.org/u/heshampop)\
**Post date:** [21 January 2021 14:52 UTC](https://discourse.nodered.org/t/push-button-0-1-gpio/20787/9 "2021-01-21T14:52:52Z")

</div>

Thanks It Worked

---

<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:** [21 January 2021 15:43 UTC](https://discourse.nodered.org/t/push-button-0-1-gpio/20787/10 "2021-01-21T15:43:50Z")

</div>


