# How to recieve room name from mqtt topic?

**URL:** <https://discourse.nodered.org/t/how-to-recieve-room-name-from-mqtt-topic/38263>\
**Category:** General\
**Created:** [30 December 2020 08:19 UTC](https://discourse.nodered.org/t/how-to-recieve-room-name-from-mqtt-topic/38263 "2020-12-30T08:19:19Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![bodagrave](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bodagrave/32/36775_2.png) [@bodagrave](https://discourse.nodered.org/u/bodagrave)\
**Post date:** [30 December 2020 08:19 UTC](https://discourse.nodered.org/t/how-to-recieve-room-name-from-mqtt-topic/38263/1 "2020-12-30T08:19:19Z")

</div>

Hi all!  
I'm pretty new to node red, so pardon me if this question was answered before. I didn't find a solution though.

I want to get a message in telegram, when in a room the temperature drops or rises beyond a specified range. It should look like this:  
"The temperature in %ROOM% dropped (16°C)"

I'm managing all my hardware through fhem and want to use mqtt as bridge to get the information in node red (some troubles here, too, but they are not linked to the question above). Telegram is set up as well, Tests with injections do work like a charm.

Through mqtt-topic I know which room the temperature belongs to. My topic looks like:

"$SYS/fhem/room-name/temp/set"  
So between second and third / I always have the room name.

But: how do I get it out of there? Any help would really be appreciated!

Thanks in advance!

BG

---

<div class="post-metadata">

**Author:** ![duaw](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/duaw/32/20970_2.png) [@duaw](https://discourse.nodered.org/u/duaw)\
**Post date:** [30 December 2020 08:30 UTC](https://discourse.nodered.org/t/how-to-recieve-room-name-from-mqtt-topic/38263/2 "2020-12-30T08:30:52Z")

</div>

Hi,  
you may want to use `split` e.g. in a function, to get the room.  
Head over to [https://www.w3schools.com/jsref/tryit.asp?filename=tryjsref\_split](https://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_split)  
and try it by yourself with

```auto
function myFunction() {
  var str = "$SYS/fhem/room-name/temp/set";
  var res = str.split("/");
  document.getElementById("demo").innerHTML = res[2];
}

```

Regards, Uwe

---

<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:** [30 December 2020 08:33 UTC](https://discourse.nodered.org/t/how-to-recieve-room-name-from-mqtt-topic/38263/3 "2020-12-30T08:33:31Z")

</div>

split it with a function node or change node

here is change node example.

```auto
[{"id":"337cc23.e8022be","type":"inject","z":"8d22ae29.7df6d","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"test/anothe/room/in/this/topic","payload":"{}","payloadType":"json","x":200,"y":2340,"wires":[["efb018ff.1c1b7"]]},{"id":"efb018ff.1c1b7","type":"change","z":"8d22ae29.7df6d","name":"","rules":[{"t":"set","p":"payload.room","pt":"msg","to":"$split(topic, \"/\")[2]","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":440,"y":2340,"wires":[["ffd491fb.4123b8"]]},{"id":"ffd491fb.4123b8","type":"debug","z":"8d22ae29.7df6d","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":550,"y":2220,"wires":[]}]

```

---

<div class="post-metadata">

**Author:** ![bodagrave](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bodagrave/32/36775_2.png) [@bodagrave](https://discourse.nodered.org/u/bodagrave)\
**Post date:** [30 December 2020 09:24 UTC](https://discourse.nodered.org/t/how-to-recieve-room-name-from-mqtt-topic/38263/4 "2020-12-30T09:24:51Z")

</div>

Wow! Thanks for these solutions! E1cid: I'll use this one - and you forced me to learn how to import a node, thanks for both!

Uwe: Thank you, to, I'll have look into your link and function, that definitively will help better understandig the code! What I do not understand at the moment: will the defined function just be available in the specific node/flow or is it available globally?

---

<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:** [30 December 2020 10:00 UTC](https://discourse.nodered.org/t/how-to-recieve-room-name-from-mqtt-topic/38263/5 "2020-12-30T10:00:07Z")

</div>

> [@bodagrave](#):
>
> "$SYS/fhem/room-name/temp/set"

It is not recommended to use $SYS topics in this way. See [SYS-Topics: Why you shouldn’t use SYS-Topics for Monitoring](https://www.hivemq.com/blog/why-you-shouldnt-use-sys-topics-for-monitoring/)

---

<div class="post-metadata">

**Author:** ![bodagrave](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bodagrave/32/36775_2.png) [@bodagrave](https://discourse.nodered.org/u/bodagrave)\
**Post date:** [30 December 2020 11:35 UTC](https://discourse.nodered.org/t/how-to-recieve-room-name-from-mqtt-topic/38263/6 "2020-12-30T11:35:37Z")

</div>

Good point, thank you. I'll gone change that better now than later, when all is more complex.

---

<div class="post-metadata">

**Author:** ![duaw](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/duaw/32/20970_2.png) [@duaw](https://discourse.nodered.org/u/duaw)\
**Post date:** [30 December 2020 12:26 UTC](https://discourse.nodered.org/t/how-to-recieve-room-name-from-mqtt-topic/38263/7 "2020-12-30T12:26:12Z")

</div>

With "function" i meant "function node" in your flow.

I found the w3schools incredibly useful to learn about Javascript , objects, etc. -- therefore the link!

---

<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 January 2021 12:26 UTC](https://discourse.nodered.org/t/how-to-recieve-room-name-from-mqtt-topic/38263/8 "2021-01-13T12:26:15Z")

</div>

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