Array query
Array query
This subflow allows you to query an array for a specific property in this array.
This subflow was created by Smart-Tech as part of the ST-One project.
Input
msg.payload
(required) value of the property to be queriedmsg.data
(required) the array to be queriedmsg.property
(optional) the name of the property. You can also configure it from the subflow enviroment variable
Output
msg.payload
the last complete object found in the array where the previous payload content matches with the configured propertymsg.results
amount of results found
[{"id":"1efed261.c9b80e","type":"tab","label":"Example","disabled":false,"info":""},{"id":"9d1092a9.9b30d","type":"subflow","name":"query array","info":"# Array query\nThis subflow allows you to query an array for a specific property in this array. \n\n## Input\n``msg.payload`` (required) value of the property to be queried \n``msg.data`` (required) the array to be queried \n``msg.property`` (optional) the name of the property. You can also configure it from the subflow enviroment variable \n\n## Output\n``msg.payload`` the last complete object found in the array where the previous payload content matches with the configured property \n``msg.results`` amount of results found \n","category":"","in":[{"x":160,"y":200,"wires":[{"id":"ed0f2ae5.deb468"}]}],"out":[{"x":840,"y":140,"wires":[{"id":"b855422d.90e2c","port":0}]},{"x":840,"y":220,"wires":[{"id":"5c5cc5dc.0d50bc","port":0},{"id":"b855422d.90e2c","port":1}]}],"env":[{"name":"property","type":"str","value":""}]},{"id":"c299cb06.12d6d8","type":"ui_base","theme":{"name":"theme-custom","lightTheme":{"default":"#0094CE","baseColor":"#0094CE","baseFont":"-apple-system,BlinkMacSystemFont,Segoe UI,Roboto,Oxygen-Sans,Ubuntu,Cantarell,Helvetica Neue,sans-serif","edited":false,"reset":false},"darkTheme":{"default":"#097479","baseColor":"#097479","baseFont":"-apple-system,BlinkMacSystemFont,Segoe UI,Roboto,Oxygen-Sans,Ubuntu,Cantarell,Helvetica Neue,sans-serif","edited":true,"reset":false},"customTheme":{"name":"Untitled Theme 1","default":"#4B7930","baseColor":"#0094ce","baseFont":"-apple-system,BlinkMacSystemFont,Segoe UI,Roboto,Oxygen-Sans,Ubuntu,Cantarell,Helvetica Neue,sans-serif","reset":false},"themeState":{"base-color":{"default":"#4B7930","value":"#0094ce","edited":true},"page-titlebar-backgroundColor":{"value":"#0094ce","edited":false},"page-backgroundColor":{"value":"#fafafa","edited":false},"page-sidebar-backgroundColor":{"value":"#ffffff","edited":false},"group-textColor":{"value":"#1bbfff","edited":false},"group-borderColor":{"value":"#dbdbdb","edited":true},"group-backgroundColor":{"value":"#ffffff","edited":false},"widget-textColor":{"value":"#111111","edited":false},"widget-backgroundColor":{"value":"#0094ce","edited":false},"widget-borderColor":{"value":"#ffffff","edited":false},"base-font":{"value":"-apple-system,BlinkMacSystemFont,Segoe UI,Roboto,Oxygen-Sans,Ubuntu,Cantarell,Helvetica Neue,sans-serif"}},"angularTheme":{"primary":"indigo","accents":"blue","warn":"red","background":"grey"}},"site":{"name":"Node-RED Dashboard","hideToolbar":"false","allowSwipe":"false","lockMenu":"false","allowTempTheme":"true","dateFormat":"DD/MM/YYYY","sizes":{"sx":48,"sy":48,"gx":6,"gy":6,"cx":6,"cy":6,"px":0,"py":0}}},{"id":"91885429.d46108","type":"ui_tab","z":"","name":"Monitoramento On-Line","icon":"dashboard","order":1,"disabled":false,"hidden":false},{"id":"e0c7a5dc.4490d8","type":"ui_tab","name":"Tab 2","icon":"dashboard","order":2},{"id":"ed0f2ae5.deb468","type":"switch","z":"9d1092a9.9b30d","name":"Validate .data","property":"data","propertyType":"msg","rules":[{"t":"istype","v":"array","vt":"array"},{"t":"else"}],"checkall":"true","repair":false,"outputs":2,"x":300,"y":200,"wires":[["3a6d283a.a70638"],["5c5cc5dc.0d50bc"]]},{"id":"a03c934d.64a95","type":"debug","z":"9d1092a9.9b30d","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"error","targetType":"msg","x":880,"y":260,"wires":[]},{"id":"5c5cc5dc.0d50bc","type":"change","z":"9d1092a9.9b30d","name":"","rules":[{"t":"set","p":"error","pt":"msg","to":"Error - msg.data is not an array","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":530,"y":260,"wires":[["a03c934d.64a95"]]},{"id":"3a6d283a.a70638","type":"function","z":"9d1092a9.9b30d","name":"Query array","func":"var array = msg.data;\nvar property = msg.property ? msg.property : env.get(\"property\")\nvar condition = msg.payload\n\nvar newPayload\nvar results = 0\narray.forEach(element => {\n if(element[property] == condition){\n newPayload = element\n results++\n }\n});\n\nmsg.results = results \n\nif (results > 0 ){\n msg.payload = newPayload\n} else{\n msg.error = \"Error - no results found\"\n}\n\nreturn msg;","outputs":1,"noerr":0,"x":530,"y":160,"wires":[["b855422d.90e2c"]]},{"id":"6b30a4df.6ca57c","type":"inject","z":"1efed261.c9b80e","name":"Data","topic":"","payload":"[{\"id\":1,\"name\":\"foo\",\"value\":111},{\"id\":2,\"name\":\"bar\",\"value\":222},{\"id\":3,\"name\":\"test\",\"value\":333},{\"id\":4,\"name\":\"test2\",\"value\":333}]","payloadType":"json","repeat":"","crontab":"","once":true,"onceDelay":0.1,"x":270,"y":140,"wires":[["9d2bf974.d6e578"]]},{"id":"9d2bf974.d6e578","type":"change","z":"1efed261.c9b80e","name":"Save data","rules":[{"t":"set","p":"data","pt":"flow","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":480,"y":140,"wires":[[]]},{"id":"f221b04d.d4fec","type":"comment","z":"1efed261.c9b80e","name":"Save payload to flow.data","info":"","x":210,"y":80,"wires":[]},{"id":"a059139b.84039","type":"comment","z":"1efed261.c9b80e","name":"Query data with fixed property","info":"","x":220,"y":240,"wires":[]},{"id":"847a4ce9.84435","type":"inject","z":"1efed261.c9b80e","name":"id 1","topic":"","payload":"1","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":270,"y":300,"wires":[["44c24107.13723"]]},{"id":"44c24107.13723","type":"change","z":"1efed261.c9b80e","name":"getData","rules":[{"t":"set","p":"data","pt":"msg","to":"data","tot":"flow"}],"action":"","property":"","from":"","to":"","reg":false,"x":480,"y":300,"wires":[["e1e01176.ee02a"]]},{"id":"7b2659fa.c8f9b8","type":"inject","z":"1efed261.c9b80e","name":"name \"bar\"","topic":"","payload":"bar","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":280,"y":380,"wires":[["73da147f.31b9ec"]]},{"id":"73da147f.31b9ec","type":"change","z":"1efed261.c9b80e","name":"getData","rules":[{"t":"set","p":"data","pt":"msg","to":"data","tot":"flow"}],"action":"","property":"","from":"","to":"","reg":false,"x":480,"y":380,"wires":[["46d6db95.ae0344"]]},{"id":"6323caf8.4b17c4","type":"inject","z":"1efed261.c9b80e","name":"value 333","topic":"","payload":"333","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":280,"y":460,"wires":[["e527b384.9acbc"]]},{"id":"e527b384.9acbc","type":"change","z":"1efed261.c9b80e","name":"getData","rules":[{"t":"set","p":"data","pt":"msg","to":"data","tot":"flow"}],"action":"","property":"","from":"","to":"","reg":false,"x":480,"y":460,"wires":[["94440467.50f608"]]},{"id":"e1e01176.ee02a","type":"subflow:9d1092a9.9b30d","z":"1efed261.c9b80e","name":"","env":[{"name":"property","type":"str","value":"id"}],"x":670,"y":300,"wires":[["26a89fa1.97c81"],[]]},{"id":"46d6db95.ae0344","type":"subflow:9d1092a9.9b30d","z":"1efed261.c9b80e","name":"","env":[{"name":"property","type":"str","value":"name"}],"x":670,"y":380,"wires":[["c699a3f6.f15fd"],[]]},{"id":"94440467.50f608","type":"subflow:9d1092a9.9b30d","z":"1efed261.c9b80e","name":"","env":[{"name":"property","type":"str","value":"value"}],"x":670,"y":460,"wires":[["722c6324.c8324c"],[]]},{"id":"26a89fa1.97c81","type":"debug","z":"1efed261.c9b80e","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":870,"y":280,"wires":[]},{"id":"c699a3f6.f15fd","type":"debug","z":"1efed261.c9b80e","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":870,"y":360,"wires":[]},{"id":"722c6324.c8324c","type":"debug","z":"1efed261.c9b80e","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":870,"y":440,"wires":[]},{"id":"5fa92146.7707d","type":"inject","z":"1efed261.c9b80e","name":"value 333","topic":"","payload":"333","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":280,"y":760,"wires":[["370883b2.f63d3c"]]},{"id":"370883b2.f63d3c","type":"subflow:9d1092a9.9b30d","z":"1efed261.c9b80e","name":"","env":[{"name":"property","type":"str","value":"value"}],"x":670,"y":760,"wires":[[],["26461b5f.b4be54"]]},{"id":"26461b5f.b4be54","type":"debug","z":"1efed261.c9b80e","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":870,"y":780,"wires":[]},{"id":"ea9183d5.8f9a4","type":"comment","z":"1efed261.c9b80e","name":"Invalid queries","info":"","x":170,"y":680,"wires":[]},{"id":"b855422d.90e2c","type":"switch","z":"9d1092a9.9b30d","name":"Error?","property":"results","propertyType":"msg","rules":[{"t":"gt","v":"0","vt":"num"},{"t":"else"}],"checkall":"true","repair":false,"outputs":2,"x":690,"y":160,"wires":[[],["4edba79f.de26a8"]]},{"id":"4edba79f.de26a8","type":"debug","z":"9d1092a9.9b30d","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"error","targetType":"msg","x":880,"y":180,"wires":[]},{"id":"6e8d410f.00213","type":"inject","z":"1efed261.c9b80e","name":"value 555","topic":"","payload":"555","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":280,"y":840,"wires":[["8567461b.c5ba28"]]},{"id":"8567461b.c5ba28","type":"change","z":"1efed261.c9b80e","name":"getData","rules":[{"t":"set","p":"data","pt":"msg","to":"data","tot":"flow"}],"action":"","property":"","from":"","to":"","reg":false,"x":480,"y":840,"wires":[["282dd8.8495f228"]]},{"id":"282dd8.8495f228","type":"subflow:9d1092a9.9b30d","z":"1efed261.c9b80e","name":"","env":[{"name":"property","type":"str","value":"value"}],"x":670,"y":840,"wires":[[],["516e79a8.f13308"]]},{"id":"516e79a8.f13308","type":"debug","z":"1efed261.c9b80e","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":870,"y":860,"wires":[]},{"id":"9d9b9149.cc39f","type":"inject","z":"1efed261.c9b80e","name":"value 333","topic":"","payload":"333","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":280,"y":600,"wires":[["3a399dff.6bac72"]]},{"id":"c085ad94.6d68a","type":"change","z":"1efed261.c9b80e","name":"getData","rules":[{"t":"set","p":"data","pt":"msg","to":"data","tot":"flow"}],"action":"","property":"","from":"","to":"","reg":false,"x":620,"y":600,"wires":[["d22a2a1.6c3edd8"]]},{"id":"d22a2a1.6c3edd8","type":"subflow:9d1092a9.9b30d","z":"1efed261.c9b80e","name":"","env":[],"x":810,"y":600,"wires":[["74ab0372.a42a1c"],[]]},{"id":"74ab0372.a42a1c","type":"debug","z":"1efed261.c9b80e","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":1010,"y":580,"wires":[]},{"id":"8df96a69.4206b8","type":"comment","z":"1efed261.c9b80e","name":"Query data with property on message","info":"","x":250,"y":540,"wires":[]},{"id":"3a399dff.6bac72","type":"change","z":"1efed261.c9b80e","name":"set property","rules":[{"t":"set","p":"property","pt":"msg","to":"value","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":450,"y":600,"wires":[["c085ad94.6d68a"]]}]