Replicate Flow - Find/Replace

Hey All,

I've done several great things with Node-RED, but now i need to replicate (100 times) a flow that i have developed.

The flow which was developed, creates a Group BOX with several information and displays. I tried exporting node and used find/replace tool and reimported to generate a new flow. Well, it've worked substituting the texts (on function blocks), but the group name of displays wasn't updated.

Is there a fast way to replicate flows?

Best regards.

Luis

How? To different instances or to the same instance of Node-RED?

Assuming a single instance. Have you tried to use a sub-flow instead of duplicated flows? Or have you tried to refactor your flow so that you only need to duplicate a couple of nodes rather than the whole thing? If you can refactor, you can probably either use subflows or link nodes.

Thanks for your reply, TotallyInformation. I don't know if i got what you mean with "Refactor" my flow.

But there are different instances of MQTT and SQL queries. For example: Flow01 is referred to device01, flow02 to device 02, until flow n to device N. I would like to replicate automatically.

Hope i was clearer now. i sent 2 pictures to illustrate what i want to do.

Which options i have?

Thanks!

First of all, subflows cannot be used to "replicate" ui nodes...

Secondly, you cannot view 100 sets of graphs at once -- so what is the maximum number of devices you would want to see in the dashboard? You only need to define that many sets of ui elements, and feed in the data for any of the devices when it's time to look at them.

The trick here is to give the user some way of selecting 1 device out of a possible 100... so regular html pulldowns are not good for that. Instead, you will probably need some kind of search box that helps locate the device as you type.

Each time a device has been selected to be shown, that kicks off a flow that retrieves all the information about that device (name, label, id, + data) and updates whichever group that will be used to show it. In this case, the group names will not be shown (since they never change, just the data shown inside each group). You can put ui_text boxes inside each group to show the device info, then pass the new set of data to the chart, which will be redrawn.

So, conceptually, you have one flow that uses mqtt wildcards to listen for data from all 100 devices, extracts the device number from the topic it received, and pushes each record to the database. Then the UI has its own flows to draw the dashboard and switch between which devices are shown. All of which is not easy to set up, but certainly much more maintainable than copy/paste of 100 nearly identical flows!

Code refactoring is the process of restructuring existing computer code—changing the factoring—without changing its external behavior. Refactoring is intended to improve nonfunctional attributes of the software.

Depending on the way you format your MQTT topics and a little code and using a template, you might be able to have a single node for the SQL. I just helped someone change a flow that had 7 MQTT in nodes to one and seven SQL inserts to one.

From your image, it looks like you are doing the same thing for each device. It should be pretty easy.

For detailed guidance, you're going to have to supply your flow for someont to look at. And keep it to the code for two devices if they are all doing the same thing

3 Likes

Thanks for your reply shrickus. Certainly i won't set 100 graphs at once, at the same dashboard.

I got what you mean when you say i need to have a selection to define a device. I've already done it in another project, with historical chart. But i don't realize how can i setup/change the datasource of a MQTT using this kind of tool, for example.

By the way, i need 10 devices at once. So, in this case i need to develop a flow with 10 "boxes" and change each target from what i've selected. Right?

Thanks for your support mr zenofmud. Can you indicate me which was this discussion? Here's the sample code, i need to change MQTT sources and SQL queries inside my functions.

[{"id":"5bdca107.08072","type":"tab","label":"BOX002","disabled":false,"info":""},{"id":"89f73184.004d3","type":"subflow","name":"Subflow 1","info":"","in":[],"out":[]},{"id":"af24e30e.3d73a","type":"ui_base","theme":{"name":"theme-dark","lightTheme":{"default":"#0094CE","baseColor":"#0094CE","baseFont":"-apple-system,BlinkMacSystemFont,Segoe UI,Roboto,Oxygen-Sans,Ubuntu,Cantarell,Helvetica Neue,sans-serif","edited":true,"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":"#4B7930","baseFont":"-apple-system,BlinkMacSystemFont,Segoe UI,Roboto,Oxygen-Sans,Ubuntu,Cantarell,Helvetica Neue,sans-serif","reset":false},"themeState":{"base-color":{"default":"#097479","value":"#097479","edited":true},"page-titlebar-backgroundColor":{"value":"#097479","edited":false},"page-backgroundColor":{"value":"#111111","edited":false},"page-sidebar-backgroundColor":{"value":"#000000","edited":false},"group-textColor":{"value":"#0eb8c0","edited":false},"group-borderColor":{"value":"#555555","edited":false},"group-backgroundColor":{"value":"#333333","edited":false},"widget-textColor":{"value":"#eeeeee","edited":false},"widget-backgroundColor":{"value":"#097479","edited":false},"widget-borderColor":{"value":"#333333","edited":false},"base-font":{"value":"-apple-system,BlinkMacSystemFont,Segoe UI,Roboto,Oxygen-Sans,Ubuntu,Cantarell,Helvetica Neue,sans-serif"}}},"site":{"name":"Sistema de Monitoramento - Luis Alves ","hideToolbar":"false","allowSwipe":"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":"898ab8dd.aabe48","type":"MySQLdatabase","z":"","host":"127.0.0.1","port":"3306","db":"raspberry","tz":""},{"id":"1fa7c754.76d4a9","type":"ui_group","z":"","name":"CPU","tab":"","order":5,"disp":false,"width":"6","collapse":false},{"id":"16388d31.a84573","type":"ui_group","z":"","name":"graficoDiag","tab":"","disp":false,"width":"12","collapse":false},{"id":"1a6b8339.4af3ad","type":"serial-port","z":"","serialport":"/dev/ttyS0","serialbaud":"9600","databits":"8","parity":"none","stopbits":"1","newline":"","bin":"false","out":"char","addchar":false},{"id":"98ff9f05.9027a","type":"mqtt-broker","z":"","name":"mosquittoLuisAlves","broker":"192.168.0.180","port":"1883","clientid":"","usetls":false,"compatmode":true,"keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","willTopic":"","willQos":"0","willPayload":""},{"id":"4c09b751.16c1e8","type":"ui_group","z":"","name":"energia_graficos","tab":"","disp":false,"width":"18","collapse":false},{"id":"7f4eeabf.a34354","type":"ui_group","z":"","name":"energia_gauges","tab":"","disp":false,"width":"7","collapse":false},{"id":"8feb8bd5.be70e8","type":"ui_tab","z":"","name":"Via Alecrim","icon":"dashboard","order":1},{"id":"2c420a6c.c1f001","type":"ui_group","z":"","name":"BOX001","tab":"8feb8bd5.be70e8","order":1,"disp":false,"width":"6","collapse":false},{"id":"ec2dac8c.62002","type":"ui_group","z":"","name":"BOX002","tab":"8feb8bd5.be70e8","order":2,"disp":false,"width":"6","collapse":false},{"id":"ec2dac8c.62003","type":"ui_group","z":"","name":"BOX003","tab":"8feb8bd5.be70e8","order":3,"disp":false,"width":"6","collapse":false},{"id":"ec2dac8c.62004","type":"ui_group","z":"","name":"BOX004","tab":"8feb8bd5.be70e8","order":4,"disp":false,"width":"6","collapse":false},{"id":"ec2dac8c.62005","type":"ui_group","z":"","name":"BOX005","tab":"8feb8bd5.be70e8","order":5,"disp":false,"width":"6","collapse":false},{"id":"ec2dac8c.62006","type":"ui_group","z":"","name":"BOX006","tab":"8feb8bd5.be70e8","order":6,"disp":false,"width":"6","collapse":false},{"id":"ec2dac8c.62007","type":"ui_group","z":"","name":"BOX007","tab":"8feb8bd5.be70e8","order":7,"disp":false,"width":"6","collapse":false},{"id":"ec2dac8c.62008","type":"ui_group","z":"","name":"BOX008","tab":"8feb8bd5.be70e8","order":8,"disp":false,"width":"6","collapse":false},{"id":"ec2dac8c.62009","type":"ui_group","z":"","name":"BOX009","tab":"8feb8bd5.be70e8","order":9,"disp":false,"width":"6","collapse":false},{"id":"ec2dac8c.62010","type":"ui_group","z":"","name":"BOX010","tab":"8feb8bd5.be70e8","order":10,"disp":false,"width":"6","collapse":false},{"id":"887067af.2442f8","type":"MySQLdatabase","z":"","host":"192.168.3.10","port":"3306","db":"ram_test","tz":""},{"id":"a5c906c5.0dc908","type":"ui_group","z":"","name":"PROJECTOR SCREEN INFO","tab":"","order":4,"disp":true,"width":"6"},{"id":"87449808.3d3d78","type":"mqtt in","z":"45b04052.1d25e","name":"","topic":"/box001/energia/correnteEntrada","qos":"2","broker":"98ff9f05.9027a","x":290,"y":120,"wires":[["e20588e0.7167c8","3887521d.8fa6ce"]]},{"id":"e20588e0.7167c8","type":"debug","z":"45b04052.1d25e","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":650,"y":160,"wires":[]},{"id":"3f785491.4b6e8c","type":"function","z":"45b04052.1d25e","name":"calculaPotencia","func":"msg.payload = msg.payload * 219;\nreturn msg;","outputs":1,"noerr":0,"x":840,"y":140,"wires":[["b598284c.622808","9166a1da.4966"]]},{"id":"1bf5fcab.7bfb63","type":"comment","z":"45b04052.1d25e","name":"BOX001","info":"","x":200,"y":60,"wires":[]},{"id":"462596d7.8e5758","type":"function","z":"45b04052.1d25e","name":"writeMySQL","func":"msg.topic = \"INSERT INTO001-box001-energia(value,sensor) VALUES(\" + msg.payload + \",'corrente')\";\nreturn msg;","outputs":1,"noerr":0,"x":830,"y":100,"wires":[["5c6c829f.ac73dc"]]},{"id":"5c6c829f.ac73dc","type":"mysql","z":"45b04052.1d25e","mydb":"898ab8dd.aabe48","name":"DBraspberry","x":990,"y":100,"wires":[[]]},{"id":"74bdb555.8b87bc","type":"mqtt in","z":"45b04052.1d25e","name":"","topic":"/box001/energia/energiaConsumida","qos":"2","broker":"98ff9f05.9027a","x":280,"y":240,"wires":[["bc694190.584b3","1fe51ef9.64c801"]]},{"id":"bc694190.584b3","type":"debug","z":"45b04052.1d25e","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":690,"y":240,"wires":[]},{"id":"76878998.4ab7a8","type":"mysql","z":"45b04052.1d25e","mydb":"898ab8dd.aabe48","name":"DBraspberry","x":970,"y":280,"wires":[[]]},{"id":"15d1ab3b.4907f5","type":"function","z":"45b04052.1d25e","name":"writeMySQL","func":"var oldvalue;\n\nif(msg.payload != oldvalue)\n{\nmsg.topic = \"INSERT INTO001-box001-energia(value,sensor) VALUES(\" + msg.payload + \",'energia')\";\n}\noldvalue = msg.payload;\n\nreturn msg;","outputs":1,"noerr":0,"x":810,"y":280,"wires":[["76878998.4ab7a8"]]},{"id":"1fe51ef9.64c801","type":"delta","z":"45b04052.1d25e","name":"","minTime":"","maxTime":"","minDelta":"0.01","x":670,"y":280,"wires":[["15d1ab3b.4907f5"],[]]},{"id":"3887521d.8fa6ce","type":"delta","z":"45b04052.1d25e","name":"","minTime":"","maxTime":"","minDelta":"0.03","x":630,"y":120,"wires":[["3f785491.4b6e8c","462596d7.8e5758"],[]]},{"id":"dfb93a70.311218","type":"function","z":"45b04052.1d25e","name":"consumoHoje","func":"var sql = [];\nvar d = new Date();\nvar current = d.getTime();\nvar today0h = d.setHours(0,0,0,0);\n\nfromdate = today0h;\n\nsql.push({ topic: \"SELECT MAX(value)-MIN(Value) AS value FROM001-box001-energiaWHERE sensor='energia' AND (UNIX_TIMESTAMP(datahora)*1000) >=\"+fromdate});\nsql[sql.length-1].complete=true;\n\nreturn [ sql ];","outputs":1,"noerr":0,"x":420,"y":360,"wires":[["459647cc.cb9ae8"]]},{"id":"fd84fb4a.ad4e98","type":"inject","z":"45b04052.1d25e","name":"","topic":"","payload":"","payloadType":"date","repeat":"120","crontab":"","once":false,"onceDelay":0.1,"x":220,"y":360,"wires":[["dfb93a70.311218","e83674ed.9358e8","dbc2b71c.fe5e58"]]},{"id":"459647cc.cb9ae8","type":"mysql","z":"45b04052.1d25e","mydb":"898ab8dd.aabe48","name":"raspberry","x":640,"y":360,"wires":[["ed7ae497.7ddb18"]]},{"id":"ea4186c7.5e29c8","type":"ui_text","z":"45b04052.1d25e","group":"2c420a6c.c1f001","order":0,"width":0,"height":0,"name":"","label":"Consumo Hoje","format":"{{msg.payload | number: 2}} kWh","layout":"row-spread","x":960,"y":360,"wires":[]},{"id":"ed7ae497.7ddb18","type":"function","z":"45b04052.1d25e","name":"formataDado","func":"var str = msg.payload;\nstr = str[0]['value'];\nmsg.payload = str;\n\nreturn msg;","outputs":1,"noerr":0,"x":790,"y":360,"wires":[["ea4186c7.5e29c8"]]},{"id":"b598284c.622808","type":"ui_text","z":"45b04052.1d25e","group":"2c420a6c.c1f001","order":2,"width":0,"height":0,"name":"","label":"PotĂȘncia","format":"{{msg.payload | number: 0}} W","layout":"row-spread","x":1040,"y":140,"wires":[]},{"id":"9166a1da.4966","type":"ui_chart","z":"45b04052.1d25e","name":"","group":"2c420a6c.c1f001","order":1,"width":"0","height":"0","label":"BOX001","chartType":"line","legend":"false","xformat":"HH:mm:ss","interpolate":"linear","nodata":"","dot":false,"ymin":"","ymax":"","removeOlder":"1","removeOlderPoints":"","removeOlderUnit":"3600","cutout":0,"useOneColor":false,"colors":["#1f77b4","#aec7e8","#ff7f0e","#2ca02c","#98df8a","#d62728","#ff9896","#9467bd","#c5b0d5"],"useOldStyle":false,"x":1040,"y":180,"wires":[[],[]]},{"id":"e83674ed.9358e8","type":"function","z":"45b04052.1d25e","name":"consumoMes","func":"var sql = [];\nvar d = new Date();\nvar current = d.getTime();\nvar p_1d = 1000*60*60*24 ; // 1 Day\nvar p_7d = 1000*60*60*24*7 ; //7 Days\n\nsql.push({ topic: \"SELECT MAX(value)-MIN(Value) AS value FROM001-box001-energiaWHERE sensor='energia' AND month(datahora)=month(CURRENT_DATE) AND year(datahora)=year(CURRENT_DATE)\"});\nsql[sql.length-1].complete=true;\n\nreturn [ sql ];","outputs":1,"noerr":0,"x":410,"y":400,"wires":[["502a3bf8.d60174"]]},{"id":"502a3bf8.d60174","type":"mysql","z":"45b04052.1d25e","mydb":"898ab8dd.aabe48","name":"raspberry","x":640,"y":400,"wires":[["77d4947e.becb3c"]]},{"id":"1f3e2c3.08636d4","type":"ui_text","z":"45b04052.1d25e","group":"2c420a6c.c1f001","order":0,"width":0,"height":0,"name":"","label":"Consumo Este MĂȘs","format":"{{msg.payload | number: 2}} kWh","layout":"row-spread","x":970,"y":400,"wires":[]},{"id":"77d4947e.becb3c","type":"function","z":"45b04052.1d25e","name":"formataDado","func":"var str = msg.payload;\nstr = str[0]['value'];\nmsg.payload = str;\n\nreturn msg;","outputs":1,"noerr":0,"x":790,"y":400,"wires":[["1f3e2c3.08636d4"]]},{"id":"c854f15c.f02a1","type":"start-up-trigger","z":"45b04052.1d25e","x":220,"y":400,"wires":[["dfb93a70.311218","e83674ed.9358e8","dbc2b71c.fe5e58"]]},{"id":"dbc2b71c.fe5e58","type":"function","z":"45b04052.1d25e","name":"consumoMesPassado","func":"var sql = [];\nvar d = new Date();\nvar current = d.getTime();\nvar p_1d = 1000*60*60*24 ; // 1 Day\nvar p_7d = 1000*60*60*24*7 ; //7 Days\n\nsql.push({ topic: \"SELECT MAX(value)-MIN(Value) AS value FROM001-box001-energiaWHERE sensor='energia' AND month(datahora)=(month(CURRENT_DATE)-1) AND year(datahora)=year(CURRENT_DATE)\"});\nsql[sql.length-1].complete=true;\n\nreturn [ sql ];","outputs":1,"noerr":0,"x":440,"y":440,"wires":[["1b00d81c.3ff368"]]},{"id":"1b00d81c.3ff368","type":"mysql","z":"45b04052.1d25e","mydb":"898ab8dd.aabe48","name":"raspberry","x":640,"y":440,"wires":[["28ee9986.937596"]]},{"id":"ecd2ff9f.c5205","type":"ui_text","z":"45b04052.1d25e","group":"2c420a6c.c1f001","order":0,"width":0,"height":0,"name":"","label":"Consumo MĂȘs Passado","format":"{{msg.payload | number: 2}} kWh","layout":"row-spread","x":990,"y":440,"wires":[]},{"id":"28ee9986.937596","type":"function","z":"45b04052.1d25e","name":"formataDado","func":"var str = msg.payload;\nstr = str[0]['value'];\nmsg.payload = str;\n\nreturn msg;","outputs":1,"noerr":0,"x":790,"y":440,"wires":[["ecd2ff9f.c5205"]]},{"id":"f51b2ce3.4b45c","type":"mqtt in","z":"5bdca107.08072","name":"","topic":"/BOX002/energia/correnteEntrada","qos":"2","broker":"98ff9f05.9027a","x":300,"y":120,"wires":[["d6d0670d.21de68","4ea48a2e.1f9b24"]]},{"id":"d6d0670d.21de68","type":"debug","z":"5bdca107.08072","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":650,"y":160,"wires":[]},{"id":"936fd166.c12a5","type":"function","z":"5bdca107.08072","name":"calculaPotencia","func":"msg.payload = msg.payload * 219;\nreturn msg;","outputs":1,"noerr":0,"x":840,"y":140,"wires":[["cb238d6c.648e1","61674596.b370ac"]]},{"id":"52131b68.dd0b94","type":"comment","z":"5bdca107.08072","name":"BOX002","info":"","x":200,"y":60,"wires":[]},{"id":"83804cba.cefee","type":"function","z":"5bdca107.08072","name":"writeMySQL","func":"msg.topic = \"INSERT INTO001-BOX002-energia(value,sensor) VALUES(\" + msg.payload + \",'corrente')\";\nreturn msg;","outputs":1,"noerr":0,"x":830,"y":100,"wires":[["68989932.9e24a8"]]},{"id":"68989932.9e24a8","type":"mysql","z":"5bdca107.08072","mydb":"898ab8dd.aabe48","name":"DBraspberry","x":990,"y":100,"wires":[[]]},{"id":"817de9f5.da6848","type":"mqtt in","z":"5bdca107.08072","name":"","topic":"/BOX002/energia/energiaConsumida","qos":"2","broker":"98ff9f05.9027a","x":280,"y":240,"wires":[["aec877f0.9a05c8","df1ee5e6.abe218"]]},{"id":"aec877f0.9a05c8","type":"debug","z":"5bdca107.08072","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":690,"y":240,"wires":[]},{"id":"136cdd4a.821563","type":"mysql","z":"5bdca107.08072","mydb":"898ab8dd.aabe48","name":"DBraspberry","x":970,"y":280,"wires":[[]]},{"id":"4c7ec786.e558a8","type":"function","z":"5bdca107.08072","name":"writeMySQL","func":"var oldvalue;\n\nif(msg.payload != oldvalue)\n{\nmsg.topic = \"INSERT INTO001-BOX002-energia(value,sensor) VALUES(\" + msg.payload + \",'energia')\";\n}\noldvalue = msg.payload;\n\nreturn msg;","outputs":1,"noerr":0,"x":810,"y":280,"wires":[["136cdd4a.821563"]]},{"id":"df1ee5e6.abe218","type":"delta","z":"5bdca107.08072","name":"","minTime":"","maxTime":"","minDelta":"0.01","x":670,"y":280,"wires":[["4c7ec786.e558a8"],[]]},{"id":"4ea48a2e.1f9b24","type":"delta","z":"5bdca107.08072","name":"","minTime":"","maxTime":"","minDelta":"0.03","x":630,"y":120,"wires":[["936fd166.c12a5","83804cba.cefee"],[]]},{"id":"566e0794.b36bf8","type":"function","z":"5bdca107.08072","name":"consumoHoje","func":"var sql = [];\nvar d = new Date();\nvar current = d.getTime();\nvar today0h = d.setHours(0,0,0,0);\n\nfromdate = today0h;\n\nsql.push({ topic: \"SELECT MAX(value)-MIN(Value) AS value FROM001-BOX002-energiaWHERE sensor='energia' AND (UNIX_TIMESTAMP(datahora)*1000) >=\"+fromdate});\nsql[sql.length-1].complete=true;\n\nreturn [ sql ];","outputs":1,"noerr":0,"x":420,"y":360,"wires":[["9d42f57d.b2db48"]]},{"id":"3be1631e.698d1c","type":"inject","z":"5bdca107.08072","name":"","topic":"","payload":"","payloadType":"date","repeat":"120","crontab":"","once":false,"onceDelay":0.1,"x":220,"y":360,"wires":[["566e0794.b36bf8","a8709b56.e3bd58","285e8e30.64b4a2"]]},{"id":"9d42f57d.b2db48","type":"mysql","z":"5bdca107.08072","mydb":"898ab8dd.aabe48","name":"raspberry","x":640,"y":360,"wires":[["f06d4b12.bb9df8"]]},{"id":"cbcd106e.c1042","type":"ui_text","z":"5bdca107.08072","group":"ec2dac8c.62002","order":0,"width":0,"height":0,"name":"","label":"Consumo Hoje","format":"{{msg.payload | number: 2}} kWh","layout":"row-spread","x":960,"y":360,"wires":[]},{"id":"f06d4b12.bb9df8","type":"function","z":"5bdca107.08072","name":"formataDado","func":"var str = msg.payload;\nstr = str[0]['value'];\nmsg.payload = str;\n\nreturn msg;","outputs":1,"noerr":0,"x":790,"y":360,"wires":[["cbcd106e.c1042"]]},{"id":"cb238d6c.648e1","type":"ui_text","z":"5bdca107.08072","group":"ec2dac8c.62002","order":2,"width":0,"height":0,"name":"","label":"PotĂȘncia","format":"{{msg.payload | number: 0}} W","layout":"row-spread","x":1040,"y":140,"wires":[]},{"id":"61674596.b370ac","type":"ui_chart","z":"5bdca107.08072","name":"","group":"ec2dac8c.62002","order":1,"width":"0","height":"0","label":"BOX002","chartType":"line","legend":"false","xformat":"HH:mm:ss","interpolate":"linear","nodata":"","dot":false,"ymin":"","ymax":"","removeOlder":"1","removeOlderPoints":"","removeOlderUnit":"3600","cutout":0,"useOneColor":false,"colors":["#1f77b4","#aec7e8","#ff7f0e","#2ca02c","#98df8a","#d62728","#ff9896","#9467bd","#c5b0d5"],"useOldStyle":false,"x":1040,"y":180,"wires":[[],[]]},{"id":"a8709b56.e3bd58","type":"function","z":"5bdca107.08072","name":"consumoMes","func":"var sql = [];\nvar d = new Date();\nvar current = d.getTime();\nvar p_1d = 1000*60*60*24 ; // 1 Day\nvar p_7d = 1000*60*60*24*7 ; //7 Days\n\nsql.push({ topic: \"SELECT MAX(value)-MIN(Value) AS value FROM001-BOX002-energiaWHERE sensor='energia' AND month(datahora)=month(CURRENT_DATE) AND year(datahora)=year(CURRENT_DATE)\"});\nsql[sql.length-1].complete=true;\n\nreturn [ sql ];","outputs":1,"noerr":0,"x":410,"y":400,"wires":[["eead6897.528bf8"]]},{"id":"eead6897.528bf8","type":"mysql","z":"5bdca107.08072","mydb":"898ab8dd.aabe48","name":"raspberry","x":640,"y":400,"wires":[["5764664.f43e298"]]},{"id":"bd8984b3.8cf198","type":"ui_text","z":"5bdca107.08072","group":"ec2dac8c.62002","order":0,"width":0,"height":0,"name":"","label":"Consumo Este MĂȘs","format":"{{msg.payload | number: 2}} kWh","layout":"row-spread","x":970,"y":400,"wires":[]},{"id":"5764664.f43e298","type":"function","z":"5bdca107.08072","name":"formataDado","func":"var str = msg.payload;\nstr = str[0]['value'];\nmsg.payload = str;\n\nreturn msg;","outputs":1,"noerr":0,"x":790,"y":400,"wires":[["bd8984b3.8cf198"]]},{"id":"e2ae5db2.902bd","type":"start-up-trigger","z":"5bdca107.08072","x":220,"y":400,"wires":[["566e0794.b36bf8","a8709b56.e3bd58","285e8e30.64b4a2"]]},{"id":"285e8e30.64b4a2","type":"function","z":"5bdca107.08072","name":"consumoMesPassado","func":"var sql = [];\nvar d = new Date();\nvar current = d.getTime();\nvar p_1d = 1000*60*60*24 ; // 1 Day\nvar p_7d = 1000*60*60*24*7 ; //7 Days\n\nsql.push({ topic: \"SELECT MAX(value)-MIN(Value) AS value FROM001-BOX002-energiaWHERE sensor='energia' AND month(datahora)=(month(CURRENT_DATE)-1) AND year(datahora)=year(CURRENT_DATE)\"});\nsql[sql.length-1].complete=true;\n\nreturn [ sql ];","outputs":1,"noerr":0,"x":440,"y":440,"wires":[["9c2aa525.7cbf68"]]},{"id":"9c2aa525.7cbf68","type":"mysql","z":"5bdca107.08072","mydb":"898ab8dd.aabe48","name":"raspberry","x":640,"y":440,"wires":[["b5614d93.1a181"]]},{"id":"a27ba158.92086","type":"ui_text","z":"5bdca107.08072","group":"ec2dac8c.62002","order":0,"width":0,"height":0,"name":"","label":"Consumo MĂȘs Passado","format":"{{msg.payload | number: 2}} kWh","layout":"row-spread","x":990,"y":440,"wires":[]},{"id":"b5614d93.1a181","type":"function","z":"5bdca107.08072","name":"formataDado","func":"var str = msg.payload;\nstr = str[0]['value'];\nmsg.payload = str;\n\nreturn msg;","outputs":1,"noerr":0,"x":790,"y":440,"wires":[["a27ba158.92086"]]}]

That seems like a sensible approach -- it's still a lot of nodes to manage, but not terrible.

Don't think of it as changing MQTT datasources -- use a single wildcarded topic hierarchy, so you only need one mqtt in node listening to all your devices. Then use the msg.topic string to route the realtime messages through a switch node with 10 outputs, connected to each of the 10 charts.

So let's say you have a flow array of 10 slots, each with a device # in it. I would pass every mqtt msg into a JSONata expression (or a function node) that did a lookup against the slots array. Any that were not found would just be ignored. As an example, I've used a lookup-based "router" like this:

image
note: only the first 3 of the 10 outputs are defined in this picture

The trick is to use a JSONata expression to lookup the device id and return its index (or -1 if not found):

(
    $index := function($v, $i) {
        $v = topic ? $i
    };
    $slot := $map($flowContext("slots_array"), $index);
    $slot ? $slot : -1
)

Any msg.topic that is not found in the slots_array is silently dropped, while the other 10 active device topics are passed to one of the numbered outputs.

1 Like

Thanks again for your explanations! I've created a selection box, and i'm already indexing my queries based on dropdown entry.

So, from what i understand all of my devices should publish at the same mqtt topic. Then i should pass the index of my device, through topic and create a routine to verify at which index it was published. Right?

I don't think that is what was meant. I think that it was meant that they should all be in a single hierarchy so you can use a wildcard subscription to get them all from the same MQTT In node. Then you can filter out all except the topic(s) you need for the current selection.

When I immport your flow, there are two nodes you have installed that are not standard - delta and start-up-trigger. What are the actual node names (i.e. node-red-contrib-xxxxx)?