How to recieve room name from mqtt topic?

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

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
and try it by yourself with

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

Regards, Uwe

split it with a function node or change node

here is change node example.

[{"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":[]}]

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?

It is not recommended to use $SYS topics in this way. See https://www.hivemq.com/blog/why-you-shouldnt-use-sys-topics-for-monitoring/

1 Like

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

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

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

1 Like

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