Filter incoming data based on payload

#1

Could someone please help me to switch the payload output to a node based on a key name within the payload. This is a sample of the message I receive from a Xiaomi HT sensor:

08/01/2019, 21:08:16node: 85df857.d44e778
msg.payload : Object
object
cmd: "report"
model: "weather.v1"
sid: "158d000247gb41"
short_id: 42598
data: object
temperature: "2780"

but there are 3 other similar messages for humidity / pressure / voltage that all come at different times. I want to send each type of message to a separate output so that I can assign them to a gauge or graph.

I have been researching through the tutorials, but I can’t get the switch node to filter based on the ‘temperature’ key. If I use the path: msg.payload.data and then set 'temperature' as the switch filter nothing happens. However if I use the path: msg.payload.data.temperature and filter on ‘not null’ then it works fine.
This seems like a very inefficient way of filtering out the data that I need, and I am sure that it is a very simple mistake that I am making somewhere.

I have also tried the Xiaomi nodes from node-red-contrib-mi-senors, but all I get is ‘node not defined’ error messages.

Thank you

0 Likes

#2

I don't understand what you mean by that.
I don't see why switching on msg.payload.data.temperature not null is inefficient.

1 Like

#3

I concur with Colin but anyway let me show you a way to do like you imagined. Perhaps you could simplify this flow as I quickly modified one already available.

[{"id":"558eab7.2aa3c54","type":"tab","label":"Flow control - XIAOMI","disabled":false,"info":"# **Route a message based on one of its properties**\n\n## **Problem**\nYou want to route the message according to the property msg.topic.\n\n## **Solution**\nUse the switch node configured with the property to be checked against the rules.\n\n## **Example**\n![messages](/ckbk/basic-flow-008.png)\n\n## **Discussion**\nYou can configure the switch node to check all rules or to stop after the first match.\nIf there is no match against any rule then the message will not be forwarded. Eventually a message can be forwarded to more than one output as long as the switch node is configured to check all rules and the check is positive for the concerned output."},{"id":"ad002ee.0792dd","type":"comment","z":"558eab7.2aa3c54","name":"Route a message based on one of its properties","info":"","x":260,"y":60,"wires":[]},{"id":"7ed3d509.d143cc","type":"function","z":"558eab7.2aa3c54","name":"Dataset Temp","func":"msg = {\ncmd: \"report\",\nmodel: \"weather.v1\",\nsid: \"158d000247gb41\",\nshort_id: 42598,\ndata: {\ntemperature: \"2780\"\n}\n}\n\nreturn msg;","outputs":1,"noerr":0,"x":240,"y":280,"wires":[["90d5ec2a.8dd76"]]},{"id":"7dd5574b.dc4268","type":"inject","z":"558eab7.2aa3c54","name":"Go","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":90,"y":280,"wires":[["7ed3d509.d143cc"]]},{"id":"43d83f32.09e89","type":"debug","z":"558eab7.2aa3c54","name":"Gauge Temperature","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","x":960,"y":280,"wires":[]},{"id":"387757f9.44e348","type":"split","z":"558eab7.2aa3c54","name":"","splt":"\\n","spltType":"str","arraySplt":1,"arraySpltType":"len","stream":false,"addname":"topic","x":630,"y":340,"wires":[["c823a60a.ffe0d8"]]},{"id":"90d5ec2a.8dd76","type":"change","z":"558eab7.2aa3c54","name":"Data to Payload","rules":[{"t":"move","p":"data","pt":"msg","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":480,"y":340,"wires":[["387757f9.44e348"]]},{"id":"c823a60a.ffe0d8","type":"switch","z":"558eab7.2aa3c54","name":"Route ","property":"topic","propertyType":"msg","rules":[{"t":"eq","v":"temperature","vt":"str"},{"t":"eq","v":"humidity","vt":"str"},{"t":"eq","v":"pressure","vt":"str"},{"t":"eq","v":"voltage","vt":"str"}],"checkall":"true","repair":false,"outputs":4,"x":750,"y":340,"wires":[["43d83f32.09e89"],["158a42e3.3293ed"],["9c561e1.3e392e"],["c1bb023a.d380b"]]},{"id":"158a42e3.3293ed","type":"debug","z":"558eab7.2aa3c54","name":"Gauge Humidity","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","x":940,"y":320,"wires":[]},{"id":"9c561e1.3e392e","type":"debug","z":"558eab7.2aa3c54","name":"Gauge Pressure","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","x":940,"y":360,"wires":[]},{"id":"c1bb023a.d380b","type":"debug","z":"558eab7.2aa3c54","name":"Gauge Voltage","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","x":940,"y":400,"wires":[]},{"id":"fdad8628.fa0828","type":"function","z":"558eab7.2aa3c54","name":"Dataset Humi","func":"msg = {\ncmd: \"report\",\nmodel: \"weather.v1\",\nsid: \"158d000247gb41\",\nshort_id: 42598,\ndata: {\nhumidity: \"50\"\n}\n}\n\nreturn msg;","outputs":1,"noerr":0,"x":240,"y":320,"wires":[["90d5ec2a.8dd76"]]},{"id":"7e9eaf56.d169a","type":"inject","z":"558eab7.2aa3c54","name":"Go","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":90,"y":320,"wires":[["fdad8628.fa0828"]]},{"id":"67afddc9.d1ba84","type":"function","z":"558eab7.2aa3c54","name":"Dataset Pres","func":"msg = {\ncmd: \"report\",\nmodel: \"weather.v1\",\nsid: \"158d000247gb41\",\nshort_id: 42598,\ndata: {\npressure: \"100\"\n}\n}\n\nreturn msg;","outputs":1,"noerr":0,"x":230,"y":360,"wires":[["90d5ec2a.8dd76"]]},{"id":"817c3331.191e2","type":"inject","z":"558eab7.2aa3c54","name":"Go","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":90,"y":360,"wires":[["67afddc9.d1ba84"]]},{"id":"4128fc6.7e12504","type":"function","z":"558eab7.2aa3c54","name":"Dataset Temp","func":"msg = {\ncmd: \"report\",\nmodel: \"weather.v1\",\nsid: \"158d000247gb41\",\nshort_id: 42598,\ndata: {\nvoltage: \"12\"\n}\n}\n\nreturn msg;","outputs":1,"noerr":0,"x":240,"y":400,"wires":[["90d5ec2a.8dd76"]]},{"id":"996b6038.71d19","type":"inject","z":"558eab7.2aa3c54","name":"Go","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":90,"y":400,"wires":[["4128fc6.7e12504"]]},{"id":"23993a4.a0b1dc6","type":"comment","z":"558eab7.2aa3c54","name":"Flow control","info":"","x":110,"y":200,"wires":[]}]
0 Likes

#5

Thanks a lot Andrei. This is exactly what I was trying to do.
I appreciate you taking the time to assist me

1 Like

#6

Hi @MCatGC, you are welcomed. This is a useful use case and it would be great to have a recipe added for it in Node-RED cookbook. I tried to contribute with the missing (basic) recipes but somehow failed to submit the pull requests. You reminded me that I should resume and resend them. Thanks.

2 Likes

#7

As usual, a little JSONata helps:

[{"id":"76707e52.bcf308","type":"change","z":"feaf7cc3.c54078","name":"Set topic & payload","rules":[{"t":"set","p":"topic","pt":"msg","to":"$keys(data)[0]","tot":"jsonata"},{"t":"set","p":"payload","pt":"msg","to":"$lookup(data, topic)","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":490,"y":580,"wires":[["c6d56f09.2979c8"]]},{"id":"c6d56f09.2979c8","type":"switch","z":"feaf7cc3.c54078","name":"Route ","property":"topic","propertyType":"msg","rules":[{"t":"eq","v":"temperature","vt":"str"},{"t":"eq","v":"humidity","vt":"str"},{"t":"eq","v":"pressure","vt":"str"},{"t":"eq","v":"voltage","vt":"str"}],"checkall":"true","repair":false,"outputs":4,"x":650,"y":580,"wires":[["a55174b.8453208"],["48db3557.ad2be4"],["98f62ac4.672938"],["efc55f12.c2e43"]]},{"id":"5f2d39ee.d35cc8","type":"function","z":"feaf7cc3.c54078","name":"Dataset Temp","func":"msg = {\ncmd: \"report\",\nmodel: \"weather.v1\",\nsid: \"158d000247gb41\",\nshort_id: 42598,\ndata: {\ntemperature: \"2780\"\n}\n}\n\nreturn msg;","outputs":1,"noerr":0,"x":260,"y":520,"wires":[["76707e52.bcf308"]]},{"id":"d925c05e.b3fdf8","type":"inject","z":"feaf7cc3.c54078","name":"Go","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":110,"y":520,"wires":[["5f2d39ee.d35cc8"]]},{"id":"ee5d1f26.0b5908","type":"function","z":"feaf7cc3.c54078","name":"Dataset Humi","func":"msg = {\ncmd: \"report\",\nmodel: \"weather.v1\",\nsid: \"158d000247gb41\",\nshort_id: 42598,\ndata: {\nhumidity: \"50\"\n}\n}\n\nreturn msg;","outputs":1,"noerr":0,"x":260,"y":560,"wires":[["76707e52.bcf308"]]},{"id":"42dcc230.19bb1c","type":"inject","z":"feaf7cc3.c54078","name":"Go","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":110,"y":560,"wires":[["ee5d1f26.0b5908"]]},{"id":"cc5cb974.da788","type":"function","z":"feaf7cc3.c54078","name":"Dataset Pres","func":"msg = {\ncmd: \"report\",\nmodel: \"weather.v1\",\nsid: \"158d000247gb41\",\nshort_id: 42598,\ndata: {\npressure: \"100\"\n}\n}\n\nreturn msg;","outputs":1,"noerr":0,"x":250,"y":600,"wires":[["76707e52.bcf308"]]},{"id":"9fb819f1.87a05","type":"inject","z":"feaf7cc3.c54078","name":"Go","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":110,"y":600,"wires":[["cc5cb974.da788"]]},{"id":"283a21a7.7c5a5e","type":"function","z":"feaf7cc3.c54078","name":"Dataset Temp","func":"msg = {\ncmd: \"report\",\nmodel: \"weather.v1\",\nsid: \"158d000247gb41\",\nshort_id: 42598,\ndata: {\nvoltage: \"12\"\n}\n}\n\nreturn msg;","outputs":1,"noerr":0,"x":260,"y":640,"wires":[["76707e52.bcf308"]]},{"id":"81ca3dda.af04c8","type":"inject","z":"feaf7cc3.c54078","name":"Go","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":110,"y":640,"wires":[["283a21a7.7c5a5e"]]},{"id":"a55174b.8453208","type":"debug","z":"feaf7cc3.c54078","name":"Gauge Temperature","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","x":830,"y":520,"wires":[]},{"id":"48db3557.ad2be4","type":"debug","z":"feaf7cc3.c54078","name":"Gauge Humidity","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","x":823.3333382606506,"y":558.8888816833496,"wires":[]},{"id":"98f62ac4.672938","type":"debug","z":"feaf7cc3.c54078","name":"Gauge Pressure","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","x":823.3333382606506,"y":598.8888816833496,"wires":[]},{"id":"efc55f12.c2e43","type":"debug","z":"feaf7cc3.c54078","name":"Gauge Voltage","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","x":823.3333382606506,"y":638.8888816833496,"wires":[]}]
1 Like