Is it possible to create a function node that combines two payloads while still having the functionallity?
Welcome to the forum @ivexy24
Can you explain in more detail what you mean and why you don't want to use the Join node?
Im working on some project and i would like to make a custom function that combines join node and another function in 1 function
Why do you want to do that? It is certainly possible using the node context but is surprisingly tricky so I would not recommend that as a starter for a beginner. But if you really want to do it then have a look at the Storing Data section in the node red docs page Writing Functions, which presumably you have already read if you are contemplating such a task.
Really though, stand on the shoulders of those who have come before and use the Join node.
I get that. My boss gave me an assingment to try and implement that. Im quite new to node red so i dont know how should i do it.I'll check the storing data again and hopefully i manage to complete the task.
Thanks for the answer.
The first thing to do is to define exactly what you want the function to do. The Join node has many options and the more of those you need to support the more complex it becomes.
Let me try to be as clear as possible. I want to have 1 custom node that has functionallity of a join node and simple function node. So that when i join them i can access the joined payloads in the custom node and after it write the function.If u understand what i mean great but i think u wont.Im coding for 5 months so i dont know how to properly explain what i want to do:D
use msg.topic
as a property key name and context
to store the incoming payload values.
var key = msg.topic;
if(!key) key = "__nokey__";
var joined = context.get("joined") || {}; //get the memory object || or create new empty object {}
joined[key] = msg.payload; //store the payload in the joined object under the key
context.set("joined", joined); //store the joined object for later calls to the function
//
// now you can use joined.myKeyName or joined["some-key:with--odd--chars"]
//
msg.payload = joined; //pass the joined object to next node
return msg;
^ that is untested but enough to get you going.
Read the node-red docs - especially around writing functions and context.
This is very helpfull.I have just 1 question.In my project i have 3 Datapoints which is an object containing some data.When I use ur method i get 3 msgs in debugger.How do i make it to return only 1 msg which is an object containing my joined nodes.
use Object.keys(joined).length
if it == 3 then return msg
otherwise return null
;
And how could i put count like its in join node?
Several ways.
One such way...
const REQUIRED_COUNT = 3;
var key = msg.topic;
if(!key) key = "__nokey__";
var joined = context.get("joined") || {}; //get the memory object || or create new empty object {}
joined[key] = msg.payload; //store the payload in the joined object under the key
context.set("joined", joined); //store the joined object for later calls to the function
//
// now you can use joined.myKeyName or joined["some-key:with--odd--chars"]
//
if (Object.keys(joined).length === REQUIRED_COUNT) {
msg.payload = joined; //pass the joined object to next node
return msg;
}
return null; //
NOTE: be mindful of using ===
- if you send a 4th variable, it will stop working. You might elect to use >=
instead. Up to you.
I have 1 more question.I made this custom function node that works as 2 combined.But now my boss wants me to make a custom node from scratch that will work like for example join node. U click what u want to do with it and it does it without writing any code.Hope u understand what i mean.
Is that even possible for someone whose been coding frontend dev for 5 months.
Why not. gotta start somewhere. The current join node src code is available in github and there is documentation on developing nodes in the node-red docs.
However, that said, I find the task a bit odd. I understand the task of making you replicate the functionality of a join node in a function node (as it forces you to learn certain aspects of node-red) but creating a new node - not sure what value that has unless you are going to be creating nodes in your job?
I am working on some scheme that will monitor the work of some pumps in some station.On scheme i have lots of nodes but there is 1 "node" that is combination of 3 nodes and whose output depends on given inputs.(IF statements).My boss want me to make a node that will do exactly that. So i must combine Join node and Function node in 1 node for it to work.Its so unclear to me idk
There is a feature called "Subflow" where you can pack your nodes into a sub flow & re-use it.
To all intents and purposes, to the end user, it looks like 1 node when you use an instance of a subflow.
Have you asked him/her why he doesn't want you to use the Join node? The only reason that I can think of is if this is a training exercise. If it is for production then use the Join node. Does your boss understand the implications of what he is asking or is he also new to node-red?
If you do need to design a new node then start with Creating Nodes : Node-RED
Did you tell your boss you haven't actually written any code yet but have just used code provided here? We are happy to help you to learn how to do it, but we should not be writing the code for you unless you are going to donate your pay to the node-red project.
Its becouse he wants to have a custom nodes that we make in it and not the ones provided form node-red. I dont understand why not simply use the nodes that are already on, its 1000x simpler.I understand so i wont even bother u anymore. Just wanted to know how to start or something like that.