Help, processing multiply payload

301 views
Skip to first unread message

kovaccs

unread,
Mar 2, 2018, 1:38:28 PM3/2/18
to Node-RED
Does anybody help me how to process multiply payload...

Environment: there is a Sonoff 433RF bride controlled by mqtt messages.
The bridge can not learn  Data parts of remote codes that's why I have to send multiply commands through Backlog topic.
The inserted picture shows what Node-RED answered this mqtt action.

My question is: how do I process that payload which one containing "{"RfCode":"#154515"}". I have to make a decision on this result.
This is the 5th payload among the mosquitto's answer on topic: "stat/sonoff433br_2/RESULT"
(every message come from this topic).


Thanks for the comments...
Kovaccsaba


multiply-payload.PNG

steve rickus

unread,
Mar 2, 2018, 2:11:23 PM3/2/18
to Node-RED
So you have 5 msgs coming into your flow on the same topic at the same moment, it appears? To further complicate matters, each msg.payload is a JSON string, representing an object with a single field:value -- like this in your example:

{ "RfCode": "#154515" }

I would first convert each of these payloads into a javascript object, using a simple function like this:

msg.payload = JSON.parse(msg.payload);
return msg;

Then I would use a "join" node to merge all 5 msgs into one object. At that point, each merged object should have a msg.payload.RfCode field with the value you need. You can then use a "switch" node to branch based on its value (or if it's missing), or you can use a "change" node to "Move" msg.payload.RfCode "to" msg.payload, if all you need is the code value itself...

But if you really don't need the other msg values at all, and just want to strip out the RfCode string, add a test to that function, and only return the code value:

var obj = JSON.parse(msg.payload);
if (obj && obj.RfCode) {
    msg
.payload = obj.RfCode;
   
return msg; // return the code
}
return null; // ignore the rest

Finally, one other method would be to just treat each payload as a string, and use a regex or the node-red-contrib-string node to search for "RfCode" and extract the value following the ":" inside the quotes...
--
Steve

Garry Hayne

unread,
Mar 2, 2018, 3:02:12 PM3/2/18
to Node-RED
I have seen that Steve has offered a solution but I think you may be able to query the Sonoff directly to get the value you need, can you post your flow please.

Garry

kovaccs

unread,
Mar 2, 2018, 3:02:35 PM3/2/18
to Node-RED
Thank you Steve your rapid answer.
It's perfectly meets my needs. The key is the join node that did not come to my mind. The JSON.parse() is common for me.

Kovaccsaba

kovaccs

unread,
Mar 2, 2018, 4:07:02 PM3/2/18
to Node-RED
This was the soluton:

[{"id":"80e0d14f.d16fa8","type":"mqtt in","z":"38f093eb.fa7fac","name":"","topic":"stat/sonoff433br_2/RESULT","qos":"0","broker":"b6516bca.c4fbb8","x":1304.28564453125,"y":1227.142822265625,"wires":[["1cff156a.8cfc93"]]},{"id":"1cff156a.8cfc93","type":"join","z":"38f093eb.fa7fac","name":"join 5 msg.payload","mode":"custom","build":"array","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":false,"timeout":"","count":"5","x":1580.5242919921875,"y":1226.4285888671875,"wires":[["bacdce2.5cd8e3"]]},{"id":"88a688f.a116df8","type":"debug","z":"38f093eb.fa7fac","name":"","active":true,"console":"false","complete":"payload","x":1975.35693359375,"y":1228.214111328125,"wires":[]},{"id":"bacdce2.5cd8e3","type":"function","z":"38f093eb.fa7fac","name":"","func":"var obj = JSON.parse(msg.payload[4]);\nmsg.payload = obj.RfCode;\nreturn msg;","outputs":1,"noerr":0,"x":1793.3807373046875,"y":1227.142822265625,"wires":[["88a688f.a116df8"]]},{"id":"b6516bca.c4fbb8","type":"mqtt-broker","z":"","broker":"172.30.1.242","port":"1883","clientid":"","usetls":false,"compatmode":true,"keepalive":"60","cleansession":true,"willTopic":"","willQos":"0","willPayload":"","birthTopic":"","birthQos":"0","birthPayload":""}]

Kovaccsaba

2018. március 2., péntek 20:11:23 UTC+1 időpontban steve rickus a következőt írta:
multiply-payload2.PNG

kovaccs

unread,
Mar 2, 2018, 4:21:25 PM3/2/18
to Node-RED
Sorry Garry, but as I mentioned before, the bridge couldn't able to learn codes of this remote.
I must send directly Sync, Low, High and Data datas to mqtt Backlog topic. 
That's why my stat/sonoff433br_2/RfKey<x> mqtt topic can not show any RfCodes.  :-(

Kovaccsaba
Reply all
Reply to author
Forward
0 new messages