Accessing node name e.g. in a function

To get a better glance, I use multiple function nodes for lets say JSON-attributes and go on with them.
In my specific use case, I pick some special attributes and send them to distinct MQTT-topics.
So far, so good, all my nodes look somehing like this:

// JSON parsen
parsed = JSON.parse(msg.payload);
msg.payload = parsed.vehicleStatus.evStatus.batteryStatus;
msg.topic = "batterySoC";
return msg;

what would make life a bit easier would be, if the mgs.topic could get its value from the name of the Node, so something like

msg.topic = NODE_NAME;

Because then I'd only have to name the nodes and the MQTT-topic and -message would be in msg as it should be?

see:

Node-Export
[
    {
        "id": "260f4b90e1219457",
        "type": "function",
        "z": "a351eaa.aed7b98",
        "name": "batteryStatus",
        "func": "// JSON parsen\nparsed = JSON.parse(msg.payload);\nmsg.payload = parsed.vehicleStatus.evStatus.batteryStatus;\nmsg.topic = \"batterySoC\"\nreturn msg;\n",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 840,
        "y": 320,
        "wires": [
            []
        ]
    }
]

try...

msg.topic = env.get("NR_NODE_NAME");
1 Like

or

msg.topic = node.name;

... that is little easier to remember :wink:

1 Like

works now. Thanks!

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