Question about Function Node and Topic syntax

This is my first post on this forum and I am very new to Node Red. It seems very exciting and I have already developed a very nice home automation application which has encouraged me to explore and develop more complex applications. My environment is Node Red (V1.2.3) running on a RasPi3.

I have a question which seems very basic but I am unable to find documentation on it anywhere.

I am injecting two numbers into a function node using two input ports and one output port. The first input is Topic: distance and the second input is Topic: speed. Within the function node I would like to send the product (lets say) of distance*speed to the output. What is the syntax to identify both variables and perform a mathematical function on the two numbers within the function node ?

Any help would be appreciated.

Hi and welcome to the forum and to Node-RED.

Remember that Node-RED is flow based. Your two injections are completely independent so by default, the function node is triggered twice with different inputs.

To combine 2 inputs you need a temporary variable. Better to combine the inputs into 1 though. But it depends what really needs to generate those inputs which you haven't described.

Either inject all the data at once or add a change node to add the speed data to the distance data.

If for some reason you cannot combine the data, you will need to save the data to a context variable and retrieve the appropriate speed or distance when the opposing distance or speed data is input.

Hello @Trinican,

as @TotallyInformation suggested you can inject the data at once that will solve the problem.

In case you want to receive input from two inject nodes(sources) then you can use a join node to merge the two message objects (one that has distance and another one with speed) and then pass it to the function node(which will have both distance and speed). This should solve your problem.

Best,
-Ravi

See this article in the cookbook for an example of how to join messages into one object.

I am now a happy camper !!! Thanks to everyone who responded, it works. I used the join node and created an array with the two injected variables.

I am sure I will have further questions as I go along this Node Red journey and I am very thankful for this assistance and encouragement.

1 Like

I think it more likely that you should be using the Key/Value setting in the join node (as in the example linked) otherwise you will no know which element of the array is which.

thanks Colin, I have used the key/Value setting and I have created an array. It works fine. My next question is what is the NodeRed/JS syntax for extracting the individual elements of the array in the function node which follows the join node ? In this case the topics are Speed and Distance.

It shouldn't be an array in that case, feed it into a debug and show us what it looks like. You can paste a screenshot here.

2/3/2021, 11:56:57 AMnode: 9b54197.de4cfe8Duration : msg.payload : Object

{ Speed: 4.8, Duration: 50.5 }

This is the debug output. The variables are in numbers format.

Good, that isn't an array it is a javascript object.
Since you don't know how to reference the data you want then I guess you are a beginner so I recommend watching this playlist: Node-RED Essentials. The videos are done by the developers of node-red. They're nice & short and to the point. You will understand a whole lot more in about 1 hour. A small investment for a lot of gain.

Then have a read of the docs page Working with Messages which shows you how to use the little buttons in the debug pane to get the path to a property for use in a function or change node.

1 Like

Thanks Colin. I will review the tutorial. Yes I am very new to Node Red.

1 Like

It's all good now . Thanks again to everyone who responded.

This is the simple JS in the function node.

x = msg.payload.Speed ;
y = msg.payload.Duration ;
z = (y*x/60);
// z.toFixed(2);
msg = {payload:z, topic:"Distance"} ;
return msg;

A couple of minor points.
You have not declared x, y and z. It is safer to declare them. It doesn't matter here but it is a good habit to get into.
You don't need the parentheses round the calculation, and you don't need the semi colons.
Also it is generally considered good practice to re-use the existing msg object rather than creating a new one, so you might have

const x = msg.payload.Speed 
const y = msg.payload.Duration 
const z = y*x/60
msg.payload = z
msg.topic = "Distance"
return msg

Personally I would probably just use

msg.payload = msg.payload.Duration * msg.payload.Speed  / 60
msg.topic = "Distance"
return msg

but that is a matter of personal preference.