How do I set mqtt topic via message properties

2,096 views
Skip to first unread message

dave....@gmail.com

unread,
Mar 11, 2016, 8:23:16 AM3/11/16
to Node-RED
Here's what I try to do 

I have a number of websocket messages coming through a json node into a function node like :

{switch_s1,1} or {switch_s2,1} or {switch_s3,1} or {switch_s4,1}


and I want to make one function that says that sorts these messages into the appropriate mqtt message like :


if ('switch_s1' in msg.payload) {
    
  mqtt topic in60;  
  msg.payload = msg.payload.switch_s1;

}

if ("switch_s2" in msg.payload){

 mqtt topic in65;
 msg.payload = msg.payload.switch_s2;

if ....  etc etc for each input so the mqtt topic changes depending on the msg.payload that comes in 

I just dont get how to set the mqtt topic in the function node help !!



Mark Setrem

unread,
Mar 11, 2016, 9:05:37 AM3/11/16
to Node-RED
When you say "mqtt topic"  I'm guessing that you have a mqtt out node after this function node?

In which case the mqtt topic can be set by using msg.topic 
so msg.topic="WHATEVER YOU WANT";

pass the message to a matt-out node and it should do what you want

dave....@gmail.com

unread,
Mar 11, 2016, 11:31:41 AM3/11/16
to Node-RED
Ok I was almost there when I tried that just missing the "......" so thats working great thanks.... the cogs are turning (admittedly slowly)

so one final question  I guess there's no way to send {msg.topic,msg.payload} in the form  {in60:1} or something instead of {switch_s1,1}   I will always need a json & function node to split the message to the mqtt node right?

Dave C-J

unread,
Mar 11, 2016, 12:14:00 PM3/11/16
to node...@googlegroups.com
Isn't switch_s1 a name you gave it in the ui node ? can you just change that to ln60 ?

dave....@gmail.com

unread,
Mar 11, 2016, 1:22:28 PM3/11/16
to Node-RED


On Friday, 11 March 2016 17:14:00 UTC, Dave C-J wrote:
Isn't switch_s1 a name you gave it in the ui node ? can you just change that to ln60 ?

Yeah sure but I was wondering if it was possible to do away with the function node so like 

wsnode ------ json node ----- mqtt node 

ws node receives {msg.topic,msg.payload} in the form  {in60:1} sends it to mqtt node  but I guess not.

In reality I now have the function node working sweet :  (msg2 passes anything else sent that doesn't get caught by msg1 )

msg1={};
msg2={};
if('switch_s2' in msg.payload) { 
  msg1.topic = "in236";
  msg1.payload = msg.payload.switch_s2;
  node.status({text:"Toggle switch_s2"});  
  msg2=null;
}
else if('switch_s3' in msg.payload) { 
  msg1.topic = "in105";
  msg1.payload = msg.payload.switch_s3;
  node.status({text:"Toggle switch_s3"});  
  msg2=null;
}
else if('switch_s4' in msg.payload) { 
  msg1.topic = "in65";
  msg1.payload = msg.payload.switch_s4;
  node.status({text:"Toggle switch_s4"});  
  msg2=null;
}
else if('switch_s5' in msg.payload) {
  msg1.topic = "in86";
  msg1.payload = msg.payload.switch_s5;
  node.status({text:"Toggle switch_s5"});  
  msg2=null;
}
else if('switch_s6' in msg.payload) {
  msg1.topic = "in199";
  msg1.payload = msg.payload.switch_s6;
  node.status({text:"Toggle switch_s6"});  
  msg2=null;
}
else{
  msg1=null;
  msg2.payload=msg.payload;
  node.status({text:msg2.payload});  //didn't get this working yet because its a object but its just a matter of time 
}
return [msg1,msg2];

Thanks for your help
Reply all
Reply to author
Forward
0 new messages