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.
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:
-
I've assumed that the status is numeric, but activity is a numeric string. Make sure you get this right.
-
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](https://global.discourse-cdn.com/business6/uploads/nodered/original/3X/5/5/5593b592ba9a7e1df6ddfff83db32c0195183de0.png)
You can add false as output as well.
I guess it is an output of the harmony node with a specific activity, ![:wink: :wink:](https://emoji.discourse-cdn.com/twitter/wink.png?v=12)
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.