Noobie Function question

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

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:

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

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?

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:

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;

1 Like

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

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

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?

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


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

1 Like

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.