Changing the topic

I have an incoming MQTT message with msg.topic of "HA/Presence" and a msg.payload that varies depending on where it originated, so let's say it's "test".

I need to.create a msg.topic for the acknowledgement MQTT node of "HA/Presence_response" plus the incoming msg.payload so, in this case, the outgoing msg.topic would be "HA/Presence_response/test".

I've spent 9 hours Googling and playing with function and change nodes but I can't get it to work as required.

Any ideas???

How to concatenate strings in javascript. http://2ality.com/2011/10/string-concatenation.html

msg.topic=msg.topic+”whatever/“+msg.payload

Just jumping in and borrowing the thread :wink:
This is a very typical code segment I also use a lot. Until now, I tend to use a function node for such purposes. Typically, you wan't to add something to an incoming payload like

msg.payload = "whatever"+msg.payload;

I tried to figure out how to do it with a "standard" node like the Change node instead of using a lot of function nodes. I think you can use a JSONata expression in a Change node rule like

'whatever' & payload

So as an example for a solution to the topic authors question when using a Change node:

image

Another possible alternative is to use a standard Template node:

2 Likes