# HowTo get name of previous node

**URL:** https://discourse.nodered.org/t/howto-get-name-of-previous-node/77913
**Category:** General
**Created:** [25 April 2023 11:17 UTC](https://discourse.nodered.org/t/howto-get-name-of-previous-node/77913 "2023-04-25T11:17:30Z")
**Posts on this page:** 20
**Page:** 1

<div class="post-metadata">

### Author: ![Moosbueffel](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/moosbueffel/32/36732_2.png) [@Moosbueffel](https://discourse.nodered.org/u/Moosbueffel)
#### Post date: [25 April 2023 11:17 UTC](https://discourse.nodered.org/t/howto-get-name-of-previous-node/77913/1 "2023-04-25T11:17:30Z")

</div>

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.

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

```

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.

---

<div class="post-metadata">

### Author: ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)
#### Post date: [25 April 2023 11:28 UTC](https://discourse.nodered.org/t/howto-get-name-of-previous-node/77913/2 "2023-04-25T11:28:25Z")

</div>

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

---

<div class="post-metadata">

### Author: ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)
#### Post date: [25 April 2023 12:11 UTC](https://discourse.nodered.org/t/howto-get-name-of-previous-node/77913/3 "2023-04-25T12:11:20Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![awneil](https://avatars.discourse-cdn.com/v4/letter/a/49beb7/32.png) [@awneil](https://discourse.nodered.org/u/awneil)
#### Post date: [25 April 2023 13:36 UTC](https://discourse.nodered.org/t/howto-get-name-of-previous-node/77913/4 "2023-04-25T13:36:35Z")

</div>

> [@Moosbueffel](#):
>
> I need the name of the previous node

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

---

<div class="post-metadata">

### Author: ![Moosbueffel](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/moosbueffel/32/36732_2.png) [@Moosbueffel](https://discourse.nodered.org/u/Moosbueffel)
#### Post date: [25 April 2023 14:39 UTC](https://discourse.nodered.org/t/howto-get-name-of-previous-node/77913/5 "2023-04-25T14:39:29Z")

</div>

> [@awneil](#):
>
> Why do you need to do that?

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):

 ![grafik](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/3/f/3f11de7d271f92f2d4094a076f0abe829b314f90.png)

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

```auto
// 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 => c.name === consumerName);
node.status({ fill: "red", shape: "dot", text: "consumer "+consumer.name });
if (consumer) {
    if (payload.toString().toUpperCase() === "ON" || payload === true) {
        consumer.active = true;
        msg.payload = true;
    } else if (payload.toString().toUpperCase() === "OFF" || payload === false) {
        consumer.active = 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" });
}

if(!error){
    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:

```auto
// 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,
    }
];

```

---

<div class="post-metadata">

### Author: ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)
#### Post date: [25 April 2023 15:07 UTC](https://discourse.nodered.org/t/howto-get-name-of-previous-node/77913/6 "2023-04-25T15:07:20Z")

</div>

Did you try my link node suggestion?

---

<div class="post-metadata">

### Author: ![Moosbueffel](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/moosbueffel/32/36732_2.png) [@Moosbueffel](https://discourse.nodered.org/u/Moosbueffel)
#### Post date: [25 April 2023 15:13 UTC](https://discourse.nodered.org/t/howto-get-name-of-previous-node/77913/7 "2023-04-25T15:13:45Z")

</div>

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

```auto
{"payload":false,"topic":"manuSwitch","socketid":"MrTaxUuDWxD4cej1AACS","_msgid":"5a9481c0e659a10e","_event":"node:d892a6d0f6bed195"}

```

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

---

<div class="post-metadata">

### Author: ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)
#### Post date: [25 April 2023 15:15 UTC](https://discourse.nodered.org/t/howto-get-name-of-previous-node/77913/8 "2023-04-25T15:15:43Z")

</div>

![Untitled 4](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/2/3/235319492760eeafa051f3db53662b40de806f27.jpeg)

```auto
[{"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](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/9/6/96c33add645c42cac3ec4f091d44047f48f78643.jpeg)

---

<div class="post-metadata">

### Author: ![Moosbueffel](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/moosbueffel/32/36732_2.png) [@Moosbueffel](https://discourse.nodered.org/u/Moosbueffel)
#### Post date: [25 April 2023 15:23 UTC](https://discourse.nodered.org/t/howto-get-name-of-previous-node/77913/9 "2023-04-25T15:23:06Z")

</div>

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?

---

<div class="post-metadata">

### Author: ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)
#### Post date: [25 April 2023 15:29 UTC](https://discourse.nodered.org/t/howto-get-name-of-previous-node/77913/10 "2023-04-25T15:29:14Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![Moosbueffel](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/moosbueffel/32/36732_2.png) [@Moosbueffel](https://discourse.nodered.org/u/Moosbueffel)
#### Post date: [25 April 2023 15:31 UTC](https://discourse.nodered.org/t/howto-get-name-of-previous-node/77913/11 "2023-04-25T15:31:02Z")

</div>

> [@E1cid](#):
>
> Why not simply define a message property in each wire

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

---

<div class="post-metadata">

### Author: ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)
#### Post date: [25 April 2023 15:32 UTC](https://discourse.nodered.org/t/howto-get-name-of-previous-node/77913/12 "2023-04-25T15:32:28Z")

</div>

Then after each switch output define a property.

---

<div class="post-metadata">

### Author: ![Moosbueffel](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/moosbueffel/32/36732_2.png) [@Moosbueffel](https://discourse.nodered.org/u/Moosbueffel)
#### Post date: [25 April 2023 15:33 UTC](https://discourse.nodered.org/t/howto-get-name-of-previous-node/77913/13 "2023-04-25T15:33:32Z")

</div>

Ok, that would be a possibility I try

---

<div class="post-metadata">

### Author: ![Moosbueffel](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/moosbueffel/32/36732_2.png) [@Moosbueffel](https://discourse.nodered.org/u/Moosbueffel)
#### Post date: [25 April 2023 16:59 UTC](https://discourse.nodered.org/t/howto-get-name-of-previous-node/77913/14 "2023-04-25T16:59:02Z")

</div>

@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:

```auto
{"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?

---

<div class="post-metadata">

### Author: ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)
#### Post date: [25 April 2023 17:33 UTC](https://discourse.nodered.org/t/howto-get-name-of-previous-node/77913/15 "2023-04-25T17:33:01Z")

</div>

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

```auto
[{"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](https://nodered.org/docs/user-guide/editor/workspace/import-export)

---

<div class="post-metadata">

### Author: ![awneil](https://avatars.discourse-cdn.com/v4/letter/a/49beb7/32.png) [@awneil](https://discourse.nodered.org/u/awneil)
#### Post date: [25 April 2023 17:34 UTC](https://discourse.nodered.org/t/howto-get-name-of-previous-node/77913/16 "2023-04-25T17:34:56Z")

</div>

> [@Moosbueffel](#):
>
> **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

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

---

<div class="post-metadata">

### Author: ![Moosbueffel](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/moosbueffel/32/36732_2.png) [@Moosbueffel](https://discourse.nodered.org/u/Moosbueffel)
#### Post date: [25 April 2023 17:53 UTC](https://discourse.nodered.org/t/howto-get-name-of-previous-node/77913/17 "2023-04-25T17:53:30Z")

</div>

That's, the same I got with:

```auto
{"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

---

<div class="post-metadata">

### Author: ![berijan](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/berijan/32/35313_2.png) [@berijan](https://discourse.nodered.org/u/berijan)
#### Post date: [28 April 2023 14:04 UTC](https://discourse.nodered.org/t/howto-get-name-of-previous-node/77913/18 "2023-04-28T14:04:46Z")

</div>

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.

[https://nodered.org/docs/user-guide/editor/workspace/subflows](https://nodered.org/docs/user-guide/editor/workspace/subflows)

---

<div class="post-metadata">

### Author: ![Moosbueffel](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/moosbueffel/32/36732_2.png) [@Moosbueffel](https://discourse.nodered.org/u/Moosbueffel)
#### Post date: [29 April 2023 20:19 UTC](https://discourse.nodered.org/t/howto-get-name-of-previous-node/77913/19 "2023-04-29T20:19:23Z")

</div>

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

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)
#### Post date: [28 June 2023 20:20 UTC](https://discourse.nodered.org/t/howto-get-name-of-previous-node/77913/20 "2023-06-28T20:20:07Z")

</div>

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