Only one input on function nodes?

Hi,

i would like to react on temperature values from a sensor by comparing them to threshold values from a dashboard like this:

temperature = msg.payload;
if (temperature > threshold){
return {payload: "over threshold"
}

I expected to be able to do that with a function node, but for that i would need a second input for the dynamic threshold value from my dashboard.
And i guess, as node-red is flow driven, this is not possible by definition.

But how can i implement this?

Thanks

Hi @pawihd

one approach would be to store the threshold value in context.

So you would have one flow where the dashboard input is connected to Change node configured to set flow.threshold to the provided value.

In your function node you can add:

let threshold = flow.get("threshold");

to get its current value.

You'd probably also need to add a line to check if threshold has been set to anything yet and if not, set a default...

let threshold = flow.get("threshold");
if (threshold === undefined) {
   threshold = 1234;
}
1 Like

Thanks for the prompt answer!
I tried it out, but i obviously missed (or misunderstood) something!?

I have one function node which receives the threshold value from the dashboard:

flow.threshold = msg.payload

then another function node which receives the values and should do the comparision:

let threshold = flow.get("threshold")
if (msg.payload > threshold) {return {payload:"over threshold"}}

What is wrong?

Either use a change node to set the flow.threshold as suggested. Or in the function you need flow.set(“threshold “) = msg.payload;

Thats it, i understand and it works!
Thanks again!

1 Like

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