Update nodes dynamically based on connection

Hi,
I want to create three kinds of custom nodes A, B and C. Does nodered provide any way to dynamically update fields of C if it is connected by B or C? For example if I connect(wired) A to C, then I want C's fields to be updated with "a" and if A is disconnected from C then I want that field to be cleared dynamically. Similarly this should happen when B is connected or disconnected from C. All these should happen dynamically without deploying. Is there any way to make this happen? Any help on this will be highly appreciated.

Hi @chiru

Does nodered provide any way to dynamically update fields

No, This is why Nodes have configuration editors.

All these should happen dynamically without deploying

Again, for a Nodes configuration to be updated, the user of your Node, will need to deploy to commit changes

1 Like

Oh okay. Thanks for the confirmation.
Further, is there any way to send two different types of output from a particular node. Taking the same example again, lets say I have connections from A to B and A to C. Based on type of node I have connected A to, can I send different data. For example, A has to send "a1" to B and A should send "a2" to C after deploying. Is there any way to achieve this..(may be providing two different kind of output ports for node A etc)?

Of course.

You could either send msg to a different port (their number can be customized to be fairly unlimited)...

image (from the Node-RED docu)

... or - which might be a better way - label msg with a different topic and use e.g. a connected switch node for msg routing, based on topic.

1 Like

Thank you @ralphwetzel . This seems to be what I want. I will try these.

I'm able to add multiple output ports for nodes. Is there any way to color them differently.

No. But you can name them and that name appears as a tooltip

1 Like

okay sure

Similar to support for multiple out-ports, is there support for multiple in-ports..

No. Nodes can only have a single input.

1 Like

Okay. Further, is there any way I can label wires and that data can be passed to nodes somehow?

Comments?
image

Your node can have entirely seperate code based on a message property, eg using msg.topic.

if (msg.topic == "InputA") {
    //do something
    return msg;
}
if (msg.topic == "InputB") {
    //do something else
    return msg;
}

I tried the above design but I'm not able to achieve what I want. Basically, I have two kinds of custome nodes : operand node and subtract node. Let a1, a2, a3 are operand nodes. s1 and s2 be subtract nodes connected as below (image for representation purpose only). a2 is connected to both s1 and s2. I want a2 to act as LHS for s1 and RHS for s2 (it can be connected to more than 2 subtract nodes) based on connection. How can I achieve this as I cant know beforehand whether a particular operand node acts as LHS/RHS for subtract nodes.

Is there any way I can achieve this without making it difficult/awkward from user perspective.

OK, I think you want to calculate two sums : a2 - a1 and a3 - a2.
Your idea of an "operand node" might be misunderstanding how Node-red works.

Just to make sure that we are using the same terminology...

  • Your diagram shows part of a "flow". You have 5 "nodes", 3 functions and two switch nodes.
  • The grey lines are "wires", which define how data, in the form of "messages", flows from node to node.
  • A message holds it's data as a javascript object. This example has 3 "properties": payload, topic and colour, each with it's own value.
    {payload: 42, topic: "eggs", colour: "blue" }
  • Most nodes only know about the data from one message at a time. So your "subtract(s2)" cannot calculate the value a3 - a2 because they are seperate messages. One way to combine data from multiple messages is the join node, using msg.topic to identify which is which.

Here is a little flow that subtracts two values (27 - 21 = 6). You can import it, deploy then click on each inject node at the left to see the result.

[{"id":"f029124f333b6a5c","type":"inject","z":"654f3a63ef6fa743","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"a2","payload":"27","payloadType":"num","x":170,"y":220,"wires":[["52e2f8ecb9232549","014c547035febc52"]]},{"id":"06b3a87bd902d549","type":"inject","z":"654f3a63ef6fa743","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"a3","payload":"21","payloadType":"num","x":170,"y":260,"wires":[["52e2f8ecb9232549","f06843818ac35957"]]},{"id":"52e2f8ecb9232549","type":"join","z":"654f3a63ef6fa743","name":"","mode":"custom","build":"object","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":false,"timeout":"","count":"2","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":310,"y":240,"wires":[["e2a44d2898067001","cffc1e2445d71aaa"]]},{"id":"cffc1e2445d71aaa","type":"debug","z":"654f3a63ef6fa743","name":"Joined message","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":500,"y":180,"wires":[]},{"id":"e2a44d2898067001","type":"function","z":"654f3a63ef6fa743","name":"Subtract","func":"msg.payload = msg.payload.a2 - msg.payload.a3\nreturn msg;","outputs":1,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":480,"y":240,"wires":[["32b4968008015521"]]},{"id":"32b4968008015521","type":"debug","z":"654f3a63ef6fa743","name":"Difference","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":650,"y":240,"wires":[]},{"id":"014c547035febc52","type":"debug","z":"654f3a63ef6fa743","name":"a2","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":310,"y":180,"wires":[]},{"id":"f06843818ac35957","type":"debug","z":"654f3a63ef6fa743","name":"a3","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":310,"y":300,"wires":[]}]

If you are writing flows for non technical users, perhaps the Node-red dashboard would be handy.
A simple dashboard for the subtraction could look like this:

Hello @jbudd ,
Thank you for the response. In my example I'm using custom nodes for operand and subtraction. In the above image I have used function and switch nodes only for representation purpose. In real I'm using custom nodes.
I'm handling the part of doing the subtraction operation only after both messages are received. My only question is, as show in above example, how to design the nodes such that it can act as LHS or RHS dynamically.

Either use the order in which messages arrive at a given node (using a delay node) or else a message property to indicate if this message is left or right handed.

I cannot get my head around the concept of an "operand node" (except maybe for an inject node). A node is a process, not the data.

2 Likes

Been following this for a while...
I might not understand the goal here, but...

For your operand nodes, why not create 2 outputs for them (a1, a2)
and when sending the messages out of them.

tag them.

let A = {
   ...msg,
   _operand:1
}

let B = {
  ...msg,
   _operand:2
}

self.send([A,B])

Then in your subtract node, check msg._operand on input
This will allow the operand nodes to act as either 1, 2 based on what output is connected?

Just my 2c, but according to your example image, this will create the desired effect.

EDIT
The visual

Screenshot 2023-10-30 at 12.05.00

1 Like

@jbudd
Yes, operand node will act similar to inject node. Its only goal is to push the data it holds. I tried approach of adding property to indicate if message is lhs or rhs but that constraints the node to act as only lhs or rhs but not both..

@marcus-j-davies
Thanks for the suggestion. This is one working solution even I was thinking, which allows an operand node to act as both LHS and RHS. However, I was wondering if there's any other approach which I have missed. Thanks for the answer.

Not without over engineering your solution.
My example will abide by the mechanics of Node RED, and allows dynamic use of 1 operand node

1 Like