# Noobie Function question

**URL:** <https://discourse.nodered.org/t/noobie-function-question/14668>\
**Category:** General\
**Created:** [22 August 2019 21:45 UTC](https://discourse.nodered.org/t/noobie-function-question/14668 "2019-08-22T21:45:13Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Exit2Studios](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/exit2studios/32/10998_2.png) [@Exit2Studios](https://discourse.nodered.org/u/Exit2Studios)\
**Post date:** [22 August 2019 21:45 UTC](https://discourse.nodered.org/t/noobie-function-question/14668/1 "2019-08-22T21:45:13Z")

</div>

I am not a programmer, so struggling a bit trying to figure out how to accomplish the following with a function node. I'm sure it's pretty simple stuff for a pro 🙂

I'm trying to consolidate/simplify a code by turning on/off a relay via serial, but controlled by a switch node (on/off) in Node-red. I can get what I want to happen with the following:

 ![22%20PM](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/9/93e796fb5474c2fa8907bedfee3b172ddf84c80e.png)

But want to be able to control the on/off for the multiple output pins defined all in one function. Any help is much appreciated!

I'm assuming I would need some kind of switch node to pass the appropriate relay number and on/off command but not sure how to implement.

Thank you

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [23 August 2019 21:18 UTC](https://discourse.nodered.org/t/noobie-function-question/14668/2 "2019-08-23T21:18:28Z")

</div>

Hi, it would help if you posted your code as text between triple back-ticks so that it is easily readable.

You may also need to show us more of your flow because it isn't obvious to me why you are using a buffer here. We also need more information about your platform and how you are planning to action the switches - is this a Pi with GPIO? How will you be driving the GPIO pins?

---

<div class="post-metadata">

**Author:** ![Exit2Studios](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/exit2studios/32/10998_2.png) [@Exit2Studios](https://discourse.nodered.org/u/Exit2Studios)\
**Post date:** [24 August 2019 00:38 UTC](https://discourse.nodered.org/t/noobie-function-question/14668/3 "2019-08-24T00:38:17Z")

</div>

I am using an Arduino Nano to control the GPIO from NR running on a Pi. I chose this route because I want the Pi inside and the Nano outside connected by a fairly short USB/Serial run for security and I don't want to rely on wifi or ethernet (hence no ESP devices). I essentially need a "dumb" device that can turn on various external 5v relays and read sensors. The logic will be done on Node-Red.

My flow is kind of a mess right now, because I've taken a flow I borrowed from someone else and trying to get it to cooperate with what I want to do.

I ultimately want to be able to manually control the various devices from Home Assistant, but most of the automatic logic and brains will happen via schedule from NR.

Here is the code from the function node:

```auto
const bPump = 0;
const bLight = 1;
const bBlower = 2;
const bIntake = 3;
const bReturn = 4;
const bCleaner = 5;
const bHeater = 6;

const setBit = (num, position) => {
    let mask = 1 << position;
    return num | mask;
};

const clearBit = (num, position) => {
    let mask = 1 << position;
    return num & ~mask;
};

var buf = Buffer.alloc(1);
buf[0] = 0;

// Pump On
buf[0] = setBit(buf[0], bPump);

msg.payload = buf;

return msg;

```

 ![26%20PM](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/7/7659dd96d69fa7a07bb78debc25fe1f09791e2fb.png)

---

<div class="post-metadata">

**Author:** ![krambriw](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/krambriw/32/5429_2.png) [@krambriw](https://discourse.nodered.org/u/krambriw)\
**Post date:** [24 August 2019 05:33 UTC](https://discourse.nodered.org/t/noobie-function-question/14668/4 "2019-08-24T05:33:00Z")

</div>

If you have not yet invested "heavily" in your relays, maybe looking in a different direction sometimes help

> [@USB HID RELAY node - question/s](https://discourse.nodered.org/t/usb-hid-relay-node-question-s/14624):
>
> I just saw this in the "library": Nice. I can't resolve how to ask the person, so here goes: (From the page) ` Models supported: HW-348 HW-343 HW-341 Models with USB-Relay-1, USB-Relay-2 or USB-Relay-4 printed on the PCB ` Looking on the interweb, I find this (as example) [USB HID relay boards](https://www.banggood.com/search/usb-hid-relay.html?from=nav) But I am not sure if they are compatible. I've opened one of the boards and read the specs, but can't find if they are HW-348 (etc) drivers (what ever). Just asking. Thanks.

Such relay boards mentioned are so cheap and can be controlled directly from NR, no need for Arduino and complex buffer coding

Then interacting with HA would be so simple, just use MQTT between NR and HA

---

<div class="post-metadata">

**Author:** ![Exit2Studios](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/exit2studios/32/10998_2.png) [@Exit2Studios](https://discourse.nodered.org/u/Exit2Studios)\
**Post date:** [28 August 2019 18:00 UTC](https://discourse.nodered.org/t/noobie-function-question/14668/5 "2019-08-28T18:00:09Z")

</div>

I tried replace bPump in the following example (code snippet above) with`{Payload}` and tried passing a "bPumP" string, but it didn't work. Any pointers?

 ![27%20PM](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/4/4a08d4a8af4b2d085b3cf8de90f8820e427ecdf4.png)

I would like to use Topic or Payload, so I can utilize mqtt.

---

<div class="post-metadata">

**Author:** ![ukmoose](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ukmoose/32/13_2.png) [@ukmoose](https://discourse.nodered.org/u/ukmoose)\
**Post date:** [28 August 2019 18:10 UTC](https://discourse.nodered.org/t/noobie-function-question/14668/6 "2019-08-28T18:10:08Z")

</div>

`msg.payload`

PS: it's a lot easier for people reading the forum if you post code as text rather than an image

---

<div class="post-metadata">

**Author:** ![Exit2Studios](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/exit2studios/32/10998_2.png) [@Exit2Studios](https://discourse.nodered.org/u/Exit2Studios)\
**Post date:** [28 August 2019 18:40 UTC](https://discourse.nodered.org/t/noobie-function-question/14668/7 "2019-08-28T18:40:54Z")

</div>

I posted the formatted text previously in this thread. I used the image to indicate where I was trying to pass the variable.

Turns out I solved it by adding `var topic=msg.topic;` and `{topic}` to the function.

Now I just need to figure out the best way to pass a topic from an input boolean in Home Assistant.
