HowTo get name of previous node

I need the name of the previous node. I can't find a hint on internet. Tried some things without success. ChatGPT suggest the following, which also doesnt work.

let currentNode = RED.nodes.getNode(msg._msgid.split('.')[0]);
let nodeName =;

I imported node-red in Setup, and tried also:
msg.result = nodeRed.getNode(msg._msgid.split('.')[0]);
But didn't also work

So how can I get the name of the previous node. It is a switch and the only way, I see to get an identifier for my function node, so I can copy and reuse this node.

1 Like

Access the function node via a link-in, which sets msg._linkSource ?

It is not possible to get the name of the previous node from within a Function node - it just doesn't have access to any of the necessary APIs.

What ChatGPT has suggested is completely wrong - it is too eager to provide something that 'looks right' without really caring if its accurate or not. The _msgid property has nothing to do with the nodes in the flow.

Why do you need to do that?

If you explain what you're actually trying to achieve, people may be able to suggest a way (or ways) to get there...

1 Like

Thanks, let's try! I'm not sure, how much you need to know. But in some words:

I try to control my consumers (heater, boiler,..) automatic depending on photovoltaic overload. But also want to control them manual. For manual control I have an switch to on/off automatic. The switch for Manual on/off in below example is named Hzg Bad_A.

The function node after this switch has to set the MQTT-topic and the active flag in the regarding consumer. This part of flow I want to reuse for every consumer. So it would be fine to get the name of the consumer from the switch. In this case I can copy the whole code for every consumer and only have to change the switch node.

(Perhaps I find a way to integrate this in the subflow automatic, which would be quite better for reuse and than it is mandatory)

So this ist currently the flow to test the functionality(I made a screenshot with debug-valuesfor better understanding):

Source of "set topic &". There I need to calculate the appropriate consumer name in line 8.

// search for the consumer by name and 
//  - set its "active" property to the payload
//  - set msg.topic to MQTT-topic of consumer

let payload = msg.payload;
let error = false;

let consumerName = "Hzg Bad_A";

let consumer = flow.get("consumers").find(c => === consumerName);
node.status({ fill: "red", shape: "dot", text: "consumer " });
if (consumer) {
    if (payload.toString().toUpperCase() === "ON" || payload === true) { = true;
        msg.payload = true;
    } else if (payload.toString().toUpperCase() === "OFF" || payload === false) { = false;
        msg.payload = false;
    } else {
        error = true;
        node.status({fill:"red",shape:"dot",text:"ERROR: wrong payload. Expected: true||false"});

    // update the topic in the message
    msg.topic = consumer.topic;
} else {
    error = true;
    node.status({ fill: "red", shape: "dot", text: "ERROR: wrong name of switchNode. Expected: flow.consumers[].name" });

    return msg;

Here I need to calculate the line 8 to the correct consumerName to get the values out of the flow variable..

The consumers:

// Define consumers
const consumers = [
        "name": "Hzg Bad_A",            // Consumer name
        "topic": "eg/bad/th01/power",   // MQTT topic; zum steuern "cmnd/" voransetzen
        "power": 300,                   // Power requirement in W
        "active": false,                // true == consumer active/switched on; false == consumer switched off
        "working": true ,               // true==consumes power; false==does not consume power
        "restartTimer" : 0,             // Delay time for restart, if working==false
        "autoControl": true,            // true==mode automatic; false==mode manual
        "timer" : 0,                    // Switch-off delay, if autoControl==false
        "name": "Boiler Bad_A",
        "topic": "eg/bad/boiler/power",
        "power": 400,
        "active": false,            
        "working": true ,           
        "restartTimer" : 0,         
        "autoControl": true,        
        "timer" : 0,
        "name": "Boiler Bad_OG",
        "topic": "shellies/shellyEM3-01/relay/0/command",
        "power": 1000,
        "active": false,            
        "working": true ,           
        "restartTimer" : 0,         
        "autoControl": true,        
        "timer" : 0,

Did you try my link node suggestion?

I don't get a useful information. Th output of link node:


No msg._linkSource.Or do you mean link call. I don'T understand this node

[{"id":"7ae702bf9aecb386","type":"inject","z":"79efd96ddb7e506d","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":280,"y":460,"wires":[["3e49404a2a39a4c6"]]},{"id":"edbaf708b1d607fb","type":"inject","z":"79efd96ddb7e506d","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":280,"y":500,"wires":[["ab4c6a1084d87fc1"]]},{"id":"3e49404a2a39a4c6","type":"link call","z":"79efd96ddb7e506d","name":"","links":["f1234e769fa090d0"],"linkType":"static","timeout":"30","x":420,"y":460,"wires":[[]]},{"id":"ab4c6a1084d87fc1","type":"link call","z":"79efd96ddb7e506d","name":"","links":["f1234e769fa090d0"],"linkType":"static","timeout":"30","x":420,"y":500,"wires":[[]]},{"id":"f1234e769fa090d0","type":"link in","z":"79efd96ddb7e506d","name":"link in 1","links":[],"x":555,"y":480,"wires":[["274d3b4d2af58a22"]]},{"id":"274d3b4d2af58a22","type":"function","z":"79efd96ddb7e506d","name":"function 35","func":"node.warn( \"This function was called from node \" + msg._linkSource[0].node)\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":670,"y":480,"wires":[[]]}]

I get this
Untitled 1

I get this, but what to do with this?

I get the node numer. And how do I get the name I definded in the switch node?

Why not simply define a message property in each wire, e.g. msg.wire_route. Then you will know which route the message has taken.


wire? Perhaps my first idea, but the switch node has no possibility to influence the msg-object - or yet? It's true, or false

Then after each switch output define a property.

Ok, that would be a possibility I try

@E1cid this was the crucial hint :). But it would be more better, if it is possible to integrate it into the switch. I have seen that with type JSON it is nearly possible.

I don't get the right syntax for:
msg.payload = value
msg.consumerName = name
(or msg.payload.consumerName = name)

value = true or false
name = e.g. "Hzg Bad_A"

Is this possibel?

With the JSON this is always an object on payload. What I try:

{"value":true,"consumerName":"Hzg Bad_A"}

into the field: whe clicked, send: OnPaylaod. Than {}.

Is there a way to enter the "value" direct into msg.payload and an additiona property consumerName?

Not on payload, but you can use msg.topic.

[{"id":"9d8eab84600d016e","type":"inject","z":"b9860b4b9de8c8da","name":"","props":[{"p":"payload"},{"p":"topic","v":"[\"buton_topic\",\"Hzg Bad_A\"]","vt":"json"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"true","payloadType":"bool","x":110,"y":660,"wires":[["9c3b5e9d3f174cda"]]},{"id":"9c3b5e9d3f174cda","type":"ui_switch","z":"b9860b4b9de8c8da","name":"","label":"switch","tooltip":"","group":"2d4fe667.28f8ba","order":23,"width":0,"height":0,"passthru":true,"decouple":"false","topic":"topic","topicType":"msg","style":"","onvalue":"true","onvalueType":"bool","onicon":"","oncolor":"","offvalue":"false","offvalueType":"bool","officon":"","offcolor":"","animate":false,"className":"","x":270,"y":660,"wires":[["b35a4b8788cb4942"]]},{"id":"23f2645ad8a2b319","type":"inject","z":"b9860b4b9de8c8da","name":"","props":[{"p":"payload"},{"p":"topic","v":"[\"buton_topic\",\"Hzg Bad_A\"]","vt":"json"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"false","payloadType":"bool","x":110,"y":720,"wires":[["9c3b5e9d3f174cda"]]},{"id":"b35a4b8788cb4942","type":"change","z":"b9860b4b9de8c8da","name":"","rules":[{"t":"move","p":"payload","pt":"msg","to":"payload.value","tot":"msg"},{"t":"set","p":"payload.consumerName","pt":"msg","to":"topic[1]","tot":"msg"},{"t":"set","p":"topic","pt":"msg","to":"topic[0]","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":440,"y":660,"wires":[["049e5fd209f9f708"]]},{"id":"049e5fd209f9f708","type":"debug","z":"b9860b4b9de8c8da","name":"debug 264","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":670,"y":680,"wires":[]},{"id":"2d4fe667.28f8ba","type":"ui_group","name":"demo","tab":"1caa8458.b17814","order":2,"disp":true,"width":"12","collapse":false,"className":""},{"id":"1caa8458.b17814","type":"ui_tab","name":"Demo","icon":"dashboard","order":1,"disabled":false,"hidden":false}]

How to import a flow

Best is probably to include the name in what the switch sends - maybe as a message property, or use the Topic ...

That's, the same I got with:

{"value":true,"consumerName":"Hzg Bad_A"}

in the switch node, as described before. Not choosing value boolean, but choosing {} (JSON).

The advantage will be, that I can put the name in the topc and split this topic afterwards. So it is one possible solution.

Ok, I think it is not possible to get a JSON, in this case, where I can set payload direct and also an additional property. The problem ist, that the JSON I write is then the input for payload.

I'll take the solution with the change node after the switch and set there the consumerName.

Thanks all

What do You think about creating a subflow containing needed nodes?
In the instaqnce of a sublow You can for example declare the consumer name and use it in Your flow.

In a subflow I can only declare one consumer name. To use it I need to provide the different consumer name from outside.

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