# Function node for getting mqtt\_in nodename

**URL:** <https://discourse.nodered.org/t/function-node-for-getting-mqtt-in-nodename/92743>\
**Category:** General\
**Created:** [28 October 2024 15:57 UTC](https://discourse.nodered.org/t/function-node-for-getting-mqtt-in-nodename/92743 "2024-10-28T15:57:54Z")\
**Posts on this page:** 4\
**Page:** 2

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [29 October 2024 11:59 UTC](https://discourse.nodered.org/t/function-node-for-getting-mqtt-in-nodename/92743/21 "2024-10-29T11:59:42Z")

</div>

> [@jbudd](#):
>
> I think it's tidier not to have a different mqtt-in node for each sensor. Not sure if it results in less MQTT connection IDs.

I believe any extra overheads are trivial. I much prefer to have multiple mqtt nodes rather than additional logic in the flow routing the messages.

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [29 October 2024 12:03 UTC](https://discourse.nodered.org/t/function-node-for-getting-mqtt-in-nodename/92743/22 "2024-10-29T12:03:38Z")

</div>

> [@refthoom](#):
>
> Before I extracted the msg.topic with a function node but you say that is not the appropriate way, I should keep using msg.topic instead but I don't know which node can provide me with the room name from that to be able to use it as the zone name.

I did not say that you should leave it in the topic, I asked whether you needed to move it.

You can get it from the topic just the same as from a payload member, using, for example, in a function node  
`const room = msg.topic.split("/")[3]`

---

<div class="post-metadata">

**Author:** ![refthoom](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/refthoom/32/95873_2.png) [@refthoom](https://discourse.nodered.org/u/refthoom)\
**Post date:** [30 October 2024 07:52 UTC](https://discourse.nodered.org/t/function-node-for-getting-mqtt-in-nodename/92743/23 "2024-10-30T07:52:46Z")

</div>

> [@jbudd](#):
>
> I think it's tidier not to have a different mqtt-in node for each sensor. Not sure if it results in less MQTT connection IDs

Thanks @jbudd. After some reading and testing I think I understand your approach. With an injectnode that has a timer for example, I could query each Tasmota device to recieve the telemetry data but the automatic `tele` works fine as well for my situation. I've set the `Teleperiod` to 60 in Tasmota giving me data every minute. However that would still involve having to put the room name in every injectnode then. Instead of course I could add some logic and an array of rooms to route messages but at the moment that seems more complex than just using the `tele` and `msg.topic`. So, one connection is indeed tidier but I think I'll go with @Colin's approach here, for the pratical reasons.

> [@Colin](#):
>
> I did not say that you should leave it in the topic, I asked whether you needed to move it.

Apologies, I misread that then.

> [@Colin](#):
>
> You can get it from the topic just the same as from a payload member, using, for example, in a function node  
> `const room = msg.topic.split("/")[3]`

Thank you, that could work very well for me. I was tinkering with a node-contrib option but this is much cleaner 🙂 If I have time today I will try that out and post my results here.

**Update** 30-10-2024 10:55: I have what I wanted now, thank you very much @jbudd and @Colin🙂

 ![Screenshot 2024-10-30 104906](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/8/e/8e72d366096c4d2c8d371c7bce1d3ac6c66248ac.png)  
On the left I've subscribed with a wildcard, extract the room name with `const room = msg.topic.split("/")[3]` and then route the different topics with the switch node. BTW: Obviously this flow is just for testing. The function and switch nodes here will be part of my subflow.

The switch node uses regex matching:

 ![Screenshot 2024-10-30 105357](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/6/0/6076c2423045f2ea2667ca42f02d13915d45b1cb.png)

With this configuration I can have multiple dashboards from one connection and only one subscription for that connection. Yeah! 🙂 (still one connection per room though)

---

<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:** [13 November 2024 07:53 UTC](https://discourse.nodered.org/t/function-node-for-getting-mqtt-in-nodename/92743/24 "2024-11-13T07:53:15Z")

</div>

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

[Previous page](https://discourse.nodered.org/t/function-node-for-getting-mqtt-in-nodename/92743.md?page=1)
