Dahua Palette Topic extraction

Hello Everybody,

I am testing the Dahua Palette for node-red.

I receive the following debug message:

12/9/2019, 18:46:42node: 27808558.5ae2ea
VideoMotion/1/Stop : msg.payload : string[4]
"Stop"
12/9/2019, 18:46:51node: 27808558.5ae2ea
VideoMotion/1/Start : msg.payload : string[5]
"Start"

The data should be:
TOPIC VideoMotion/1/Stop or VideoMotion/1/Start
PAYLOAD STOP or START

If I have n.4 video channels I will have Videomotion/0...3

My question is: what is the right way to obtain n.8 outputs? I tried to edit function palette but I am not able to undertsand how to extract the topic and then the payload....

Thanks in advance.

Set the debug node to “complete msg object”

1 Like

12/9/2019, 19:43:15node: 27808558.5ae2eaVideoMotion/1/Stop : msg : Object

{ topic: "VideoMotion/1/Stop", payload: "Stop", index: "1", code: "VideoMotion", _msgid: "2b871991.556fb6" }

I'd like to extract only the topic.

If topic is VideoMotion/1/Stop send msg.payload cam2 motion stop

If topic is VideoMotion/1/Start send msg.payload cam2 motion start

If topic is VideoMotion/0/Stop send msg.payload cam1 motion stop

If topic is VideoMotion/0/Stop send msg.payload cam1 motion stop

I don't know how to write the code to read the topic

msg.topic

You cand do the initial if bit with a switch node (not the dashboard one)

Probably also reading the page in the docs about "working with messages"

Ok, I red the page "working with messages" and I compile the following code:


var newMsg = { topic: msg.topic, payload: msg.payload, index: msg.index, code: msg.code };
var topic=msg.topic;
var msg1 = {payload:"0"};
var msg2 = {payload:"1"};
if (msg.topic=="Videomotion/1/Stop"){
return msg1; 
}
if (msg.topic=="Videomotion/1/Start"){
    return msg2;
}

It seems works... What do you think?

What is the var newMsg for ?

Looks overcomplicated to me, why not using a switch node instead ?

[{"id":"a8102671.cb1ee","type":"inject","z":"7307521d.db778c","name":"Videomotion/1/Stop","topic":"Videomotion/1/Stop","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":314,"y":224,"wires":[["7ca00b6a.aa8754"]]},{"id":"7ca00b6a.aa8754","type":"switch","z":"7307521d.db778c","name":"","property":"topic","propertyType":"msg","rules":[{"t":"eq","v":"Videomotion/1/Stop","vt":"str"},{"t":"eq","v":"Videomotion/1/Start","vt":"str"}],"checkall":"true","repair":false,"outputs":2,"x":498,"y":252,"wires":[["39726e74.e0f3d2"],["b596218.1e882e"]]},{"id":"39726e74.e0f3d2","type":"change","z":"7307521d.db778c","name":"0","rules":[{"t":"set","p":"payload","pt":"msg","to":"0","tot":"num"}],"action":"","property":"","from":"","to":"","reg":false,"x":638,"y":224,"wires":[["fa706fc.e7c379"]]},{"id":"a31cbdc6.a144","type":"inject","z":"7307521d.db778c","name":"Videomotion/1/Start","topic":"Videomotion/1/Start","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":314,"y":280,"wires":[["7ca00b6a.aa8754"]]},{"id":"b596218.1e882e","type":"change","z":"7307521d.db778c","name":"1","rules":[{"t":"set","p":"payload","pt":"msg","to":"1","tot":"num"}],"action":"","property":"","from":"","to":"","reg":false,"x":638,"y":280,"wires":[["e28bd41d.ab15c"]]},{"id":"fa706fc.e7c379","type":"debug","z":"7307521d.db778c","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":798,"y":224,"wires":[]},{"id":"e28bd41d.ab15c","type":"debug","z":"7307521d.db778c","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":798,"y":280,"wires":[]}]

It is a note, I evaluated to use payload, index and code to develop some function.

Thanks for the suggestion.