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 ?
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.
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.
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.
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.
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