# How to create function on rpi node red with two gpio - if gpio1 == 1 && gpio2 ==0 then gpio3=1

**URL:** <https://discourse.nodered.org/t/how-to-create-function-on-rpi-node-red-with-two-gpio-if-gpio1-1-gpio2-0-then-gpio3-1/20406>\
**Category:** General\
**Created:** [15 January 2020 20:53 UTC](https://discourse.nodered.org/t/how-to-create-function-on-rpi-node-red-with-two-gpio-if-gpio1-1-gpio2-0-then-gpio3-1/20406 "2020-01-15T20:53:15Z")\
**Posts on this page:** 9\
**Page:** 2

<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:** [30 January 2020 17:30 UTC](https://discourse.nodered.org/t/how-to-create-function-on-rpi-node-red-with-two-gpio-if-gpio1-1-gpio2-0-then-gpio3-1/20406/22 "2020-01-30T17:30:05Z")

</div>

Have you done what I told you to do in the very first response in this thread?

---

<div class="post-metadata">

**Author:** ![devifast](https://avatars.discourse-cdn.com/v4/letter/d/43a26b/32.png) [@devifast](https://discourse.nodered.org/u/devifast)\
**Post date:** [31 January 2020 19:03 UTC](https://discourse.nodered.org/t/how-to-create-function-on-rpi-node-red-with-two-gpio-if-gpio1-1-gpio2-0-then-gpio3-1/20406/24 "2020-01-31T19:03:25Z")

</div>

Thanks to all of you who have responded to this issue. Maybe at the beginning I did not correctly describe the problem for which I apologize. In the end, I achieved the result I expected.  
Special thanks to zenofmud and Colin. Thanks.  
Here is the end result.

```auto
[{"id":"db1e7864.8ec9b8","type":"tab","label":"Flow 1","disabled":false,"info":""},{"id":"2b79041b.1cdbdc","type":"function","z":"db1e7864.8ec9b8","name":"","func":"if (msg.payload.gpio1 == 1 && msg.payload.gpio2 == 1) {\nmsg.payload = 1;\nreturn msg;\n}\nelse{\n msg.payload = 0;\nreturn msg;\n}\n\n ","outputs":1,"noerr":0,"x":490,"y":120,"wires":[["fe139b61.8e5248"]]},{"id":"fe139b61.8e5248","type":"debug","z":"db1e7864.8ec9b8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":670,"y":120,"wires":[]},{"id":"19565d15.04c483","type":"inject","z":"db1e7864.8ec9b8","name":"gpio1 on","topic":"gpio1","payload":"1","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":160,"y":160,"wires":[["2f810a4d.e5da16"]]},{"id":"2f810a4d.e5da16","type":"join","z":"db1e7864.8ec9b8","name":"","mode":"custom","build":"object","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":true,"timeout":"","count":"2","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":370,"y":200,"wires":[["d7fb43c2.8024b","2b79041b.1cdbdc"]]},{"id":"d7fb43c2.8024b","type":"debug","z":"db1e7864.8ec9b8","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":630,"y":200,"wires":[]},{"id":"c7161e7f.1141d","type":"inject","z":"db1e7864.8ec9b8","name":"gpio2 on","topic":"gpio2","payload":"1","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":160,"y":240,"wires":[["2f810a4d.e5da16"]]},{"id":"9d3d4ee9.66c38","type":"inject","z":"db1e7864.8ec9b8","name":"gpio1 off","topic":"gpio1","payload":"0","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":160,"y":200,"wires":[["2f810a4d.e5da16"]]},{"id":"1cc91888.72de17","type":"inject","z":"db1e7864.8ec9b8","name":"gpio2 off","topic":"gpio2","payload":"0","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":160,"y":280,"wires":[["2f810a4d.e5da16"]]}]

```

---

<div class="post-metadata">

**Author:** ![devifast](https://avatars.discourse-cdn.com/v4/letter/d/43a26b/32.png) [@devifast](https://discourse.nodered.org/u/devifast)\
**Post date:** [3 February 2020 07:42 UTC](https://discourse.nodered.org/t/how-to-create-function-on-rpi-node-red-with-two-gpio-if-gpio1-1-gpio2-0-then-gpio3-1/20406/25 "2020-02-03T07:42:28Z")

</div>

The joy was brief. With inject node everything is ok but in practice with hardware pins, things don't work out that way. The attached picture shows the messages on the right. look at functions in a flow. Help.`Preformatted text`

 ![Untitled](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/c/4/c48083b588ca980a56a4be85487c75bf85742d21.png)

```auto
[{"id":"7bbf6c01.b26624","type":"tab","label":"Flow 1","disabled":false,"info":""},{"id":"2db2a33b.dee8dc","type":"function","z":"7bbf6c01.b26624","name":"","func":"if (msg.payload.gpio1 == 1 && msg.payload.gpio2 == 1) {\nmsg.payload = 1;\nreturn msg;\n}\nelse{\n msg.payload = 0;\nreturn msg;\n}\n\n ","outputs":1,"noerr":0,"x":490,"y":120,"wires":[["e7762b1.e6959d8"]]},{"id":"e7762b1.e6959d8","type":"debug","z":"7bbf6c01.b26624","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":690,"y":120,"wires":[]},{"id":"cf592dd9.6aa23","type":"inject","z":"7bbf6c01.b26624","name":"gpio1 on","topic":"gpio1","payload":"1","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":160,"y":160,"wires":[["c1ee0201.be6d9"]]},{"id":"c1ee0201.be6d9","type":"join","z":"7bbf6c01.b26624","name":"","mode":"custom","build":"object","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":true,"timeout":"","count":"1","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":370,"y":200,"wires":[["2257545a.9ea27c","2db2a33b.dee8dc"]]},{"id":"2257545a.9ea27c","type":"debug","z":"7bbf6c01.b26624","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":630,"y":200,"wires":[]},{"id":"7bddbf31.4d53b","type":"inject","z":"7bbf6c01.b26624","name":"gpio2 on","topic":"gpio2","payload":"1","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":160,"y":240,"wires":[["c1ee0201.be6d9"]]},{"id":"b6bb8a6.b792678","type":"inject","z":"7bbf6c01.b26624","name":"gpio1 off","topic":"gpio1","payload":"0","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":160,"y":200,"wires":[["c1ee0201.be6d9"]]},{"id":"50bd9c40.c68974","type":"inject","z":"7bbf6c01.b26624","name":"gpio2 off","topic":"gpio2","payload":"0","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":160,"y":280,"wires":[["c1ee0201.be6d9"]]},{"id":"407a809e.71686","type":"rpi-gpio in","z":"7bbf6c01.b26624","name":"","pin":"37","intype":"up","debounce":"25","read":false,"x":160,"y":340,"wires":[["384d8adf.464636"]]},{"id":"5efa9c17.e96944","type":"rpi-gpio in","z":"7bbf6c01.b26624","name":"","pin":"35","intype":"up","debounce":"25","read":false,"x":160,"y":400,"wires":[["384d8adf.464636"]]},{"id":"db5c91e3.4380c","type":"function","z":"7bbf6c01.b26624","name":"","func":"if (msg.payload.pi/35 == 1 && msg.payload.pi/37 == 1) {\nmsg.payload = 1;\nreturn msg;\n}\nelse{\n msg.payload = 0;\nreturn msg;\n}\n\n ","outputs":1,"noerr":0,"x":490,"y":320,"wires":[["8c2e7942.8a9ef8"]]},{"id":"8c2e7942.8a9ef8","type":"debug","z":"7bbf6c01.b26624","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":650,"y":320,"wires":[]},{"id":"384d8adf.464636","type":"join","z":"7bbf6c01.b26624","name":"","mode":"custom","build":"object","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":false,"timeout":"","count":"1","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":330,"y":380,"wires":[["11365f4b.9ab771","db5c91e3.4380c"]]},{"id":"11365f4b.9ab771","type":"debug","z":"7bbf6c01.b26624","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":610,"y":380,"wires":[]}]

```

---

<div class="post-metadata">

**Author:** ![devifast](https://avatars.discourse-cdn.com/v4/letter/d/43a26b/32.png) [@devifast](https://discourse.nodered.org/u/devifast)\
**Post date:** [3 February 2020 07:49 UTC](https://discourse.nodered.org/t/how-to-create-function-on-rpi-node-red-with-two-gpio-if-gpio1-1-gpio2-0-then-gpio3-1/20406/26 "2020-02-03T07:49:56Z")

</div>

Please review my last post. I made a mistake again. Thanks.

---

<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:** [3 February 2020 08:47 UTC](https://discourse.nodered.org/t/how-to-create-function-on-rpi-node-red-with-two-gpio-if-gpio1-1-gpio2-0-then-gpio3-1/20406/27 "2020-02-03T08:47:10Z")

</div>

Ah the joy of javascript....  
your combined payloads look like this  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/c/7/c7d9bd4997d6e416be0221c3377f21a2dea5f1d7.png)  
all good so far  
Then your function does this  
if (msg.payload.pi/35 == 1 && msg.payload.pi/37 == 1) {

which is a natural thing to do... but javascript doesn't like / in property names so it will be treating it like a divide :-)... so instead you need to do

`if (msg.payload["pi/35"] == 1 && msg.payload["pi/37"] == 1) {`

---

<div class="post-metadata">

**Author:** ![devifast](https://avatars.discourse-cdn.com/v4/letter/d/43a26b/32.png) [@devifast](https://discourse.nodered.org/u/devifast)\
**Post date:** [3 February 2020 10:14 UTC](https://discourse.nodered.org/t/how-to-create-function-on-rpi-node-red-with-two-gpio-if-gpio1-1-gpio2-0-then-gpio3-1/20406/28 "2020-02-03T10:14:54Z")

</div>

Now another problem has arisen. After deploy does not check the status of the pins and even if they are 1 or "high" msg.payload is 0. how to update this information after deploy to read real readings from pins?  
Thanks.

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [3 February 2020 10:26 UTC](https://discourse.nodered.org/t/how-to-create-function-on-rpi-node-red-with-two-gpio-if-gpio1-1-gpio2-0-then-gpio3-1/20406/29 "2020-02-03T10:26:40Z")

</div>

If you do a full deploy then I think it should give you the initial status. If you do a partial deploy not including the i/o nodes then they do not know to send the data.

---

<div class="post-metadata">

**Author:** ![devifast](https://avatars.discourse-cdn.com/v4/letter/d/43a26b/32.png) [@devifast](https://discourse.nodered.org/u/devifast)\
**Post date:** [3 February 2020 12:03 UTC](https://discourse.nodered.org/t/how-to-create-function-on-rpi-node-red-with-two-gpio-if-gpio1-1-gpio2-0-then-gpio3-1/20406/30 "2020-02-03T12:03:41Z")

</div>

Thanks for all the answers, for other questions I will look for a solution and open a new topic

---

<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:** [17 February 2020 12:03 UTC](https://discourse.nodered.org/t/how-to-create-function-on-rpi-node-red-with-two-gpio-if-gpio1-1-gpio2-0-then-gpio3-1/20406/31 "2020-02-17T12:03:54Z")

</div>

Closed on 17 February 2020 12:03 UTC

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

[Previous page](https://discourse.nodered.org/t/how-to-create-function-on-rpi-node-red-with-two-gpio-if-gpio1-1-gpio2-0-then-gpio3-1/20406.md?page=1)
