Function node for specific message payload objects

I have node that produces a msg.payload object with two contents:

activity
status

I need to continue the flow only when 'activity' = 48481328 and 'status' = 2.

I'm new to node red and struggling to find an example of how to achieve this. I assume the Function node will do it, I just need some help to write it.

TIA

You can do it with a function node, 2 switch nodes in series and probably with a change node using JSONata. :grinning: I may have missed some options too.

Easiest with a function node though probably.

const activity = msg.payload.activity
const status = msg.payload.status

if ( activity === '48481328' && status === 2 ) return msg

// You must make sure you remove the default `return msg`
//return msg

Note two things:

  1. I've assumed that the status is numeric, but activity is a numeric string. Make sure you get this right.

  2. I've used a positive test. This is the recommended best practice because positive tests are generally easier to comprehend. However, they can sometimes result in more long-winded code. In this case, the impact is that you MUST remove the default return msg at the end of the function code.

With a negative test, the code would look like:

const activity = msg.payload.activity
const status = msg.payload.status

// use an OR and return nothing
if ( activity !== '48481328' || status !== 2 ) return

return msg
1 Like

It is easier to use a switch node for filtering purposes:

image

You can add false as output as well.

I guess it is an output of the harmony node with a specific activity, :wink:

If the numbers are strings - then just embed the numbers in quotation marks.

so instead of

payload =  {"activity":48481328, "status":2 }

use

payload =  {"activity":"48481328", "status":"2" }

or a combination of string/number

payload =  {"activity":"48481328", "status":2 }
1 Like

Or just use two switch nodes in sequence - 1st one passes if msg.payload.activity == 48481328, 2nd one if msg.payload.status == 2

2 Likes

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