In node-red how to store message for using in flows

#1

In node red suppose my flow is like

[1st API call]--------->[2nd API call]--->merge both and display data on dashboard

the first node will call API and each time the result will be different and after second call I have to merge both response, so how I can store the first API call response for using in next step.

0 Likes

#2

See the join node

0 Likes

#3

In general for simple cases like this you may be better doing them serially.. First api call, then process or move the result to another msg property then pass to second api call. The resulting msg should still have the added property from the first, so you can now do whatever you need to both.

0 Likes

#4

if i use flow variable or global variable to store and pass messages between flows, does each trigger will have different values stored in variable as values will change for each api call or same variable will be updated.

I mean if two users are accessing same flow using dashboard and does the variable differentiates while storing based on user (based on message context) or it will treat them as one and it will keep on updating values irrespective of users and will show same for all in dashboard.

0 Likes

#5

It is a shared space, so a second call will overwrite the first

0 Likes

#6

So, as I asked in other thread the question about navigating and passing message to another tab with click(How to add slider on map sensor once on click it should do some action), So if I use variables then if two users are simultaneously accessing dashboard, then as i am storing messages in variables the action done by one user in dashboard, will affect another user's dashboard.

0 Likes

#7

There is only one flow running, and one set of flow variables... so in general yes unless you are careful events from one browser pointed at the same flow will affect the flow and thus the other dashboard. However there are some extra variables passed back from events that you can use to help separate them. If you put a debug node set to show complete message onto the worldmap in you will see all the properties set including an _sessionid that you can use to spot which browser connection the event came from.
(And also similar for dashboard ui_control node)

0 Likes

#8

So if you are storing, lets say, a simple value - lets call it "x". Change your variable to be an object and save/read the value as "x[_sessionid]" instead of just "x".

As always, you need to give some thought as to the size of all of the things you are keeping in memory. Especially if you are expecting a LOT of simultaneous users.

0 Likes

#9

Be careful of that though - as the sessionid is NOT a userid - and changes EVERY time the browser refreshes... so you may over time build up a load of crud in memory.

0 Likes

#10

Thanks for the reminder Dave. You are, as always right and setting a session cookie would be a lot more reliable.

0 Likes

#11

I have the below flow first i am calling one API and after selection from dropdown i am saving that as msg.controllerid(selection made: S001). After the first selection I am calling another API and fetching the result and feed those data to dropdown for selection, when the second selction made from the second drop down I am storing as sensorid in change node.

But the selection from first dropdown msg.controllerid is visible in first debug node. But in second debug node it is coming as undefined. msg.sensorid is correctly coming L001.
Why in second debug node undefined is coming.

0 Likes

#12

From your explanation and photo, it is hard to tell which debug goes where in the flow. You could name your debug nodes and those names will show in the debug log. In addition

  1. suppling the flow - see How to share code or flow json
  2. what platform - i.e.os and release
  3. what version of NR and node.js
  4. what 'contrib' nodes are you using? i.e. node-red-contrib-xxxxxxxx
0 Likes

#13

Platform: Windows 10
nodejs: v8.12.0
nodered version: v0.19.5
Flow:
[{"id":"b028e4a.a8b4f18","type":"http request","z":"35f40a33.0c9b46","name":"","method":"GET","ret":"obj","url":"","tls":"","x":370,"y":80,"wires":[["5dd9dc8c.1e9ed4"]]}]

In the above flow I have removed the API call url as I cant able to share.

[quote="zenofmud, post:12, topic:8766, full:true"]
From your explanation and photo, it is hard to tell which debug goes where in the flow. You could name your debug nodes and those names will show in the debug log. In addition

what 'contrib' nodes are you using? i.e. node-red-contrib-xxxxxxxx
from where I can able to see.

0 Likes

#14

What you shared is only the HTTP Request node.

What I should have said it
4. if using any 'contrib' nodes (i.e. node-red-contrib-xxxxxxxx) what are they?
in your case you aren't so it wouldn't matter.

0 Likes

#15

Sorry by mistake I have selected only HTTP node , please find the flow below.

[{"id":"35f40a33.0c9b46","type":"tab","label":"Flow 1","disabled":false,"info":""},{"id":"136562b0.9ca1cd","type":"inject","z":"35f40a33.0c9b46","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":true,"onceDelay":0.1,"x":210,"y":80,"wires":[["b028e4a.a8b4f18"]]},{"id":"b028e4a.a8b4f18","type":"http request","z":"35f40a33.0c9b46","name":"","method":"GET","ret":"obj","url":"","tls":"","x":370,"y":80,"wires":[["5dd9dc8c.1e9ed4"]]},{"id":"6eec1c38.fadb34","type":"change","z":"35f40a33.0c9b46","name":"","rules":[{"t":"set","p":"sensorid","pt":"msg","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":630,"y":200,"wires":[["f0aff9e4.f3e848","4ffd0c1d.3c3364"]]},{"id":"f0aff9e4.f3e848","type":"debug","z":"35f40a33.0c9b46","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"sensorid","x":1010,"y":160,"wires":[]},{"id":"5e82b87e.0974d8","type":"ui_dropdown","z":"35f40a33.0c9b46","name":"Data source","label":"Sensor ID","tooltip":"","place":"Select Controller ID","group":"d09cf9ee.58e9d8","order":3,"width":"7","height":"1","passthru":false,"options":[{"label":"","value":"","type":"str"}],"payload":"","topic":"datasource","x":430,"y":200,"wires":[["6eec1c38.fadb34"]]},{"id":"98e089da.76a558","type":"function","z":"35f40a33.0c9b46","name":"","func":"var arr=[];\nvar a=flow.get(\"res\");\nconsole.log('index---'+[a.findIndex(p => p.macId == msg.payload)]);\nfor(var i=0;i<a[a.findIndex(p => p.macId == msg.payload)].senConf.length;i++)\n{\n arr.push(a[a.findIndex(p => p.macId == msg.payload)].senConf[i].sensorMacId)\n}\nmsg.options=arr;\nreturn msg;\n\n\n","outputs":1,"noerr":0,"x":210,"y":200,"wires":[["5e82b87e.0974d8","ada70a4d.582e88"]]},{"id":"5dd9dc8c.1e9ed4","type":"function","z":"35f40a33.0c9b46","name":"","func":"// create random position\nvar res=msg.payload;\nflow.set(\"res\",res)\nvar a=[];\nfor(var i=0;i<res.length;i++)\n{\n \n \n if(res[i].deviceType==18)\n {\n a.push(res[i].macId)\n }\n \n \n}\n\nmsg.payload=a;\nconsole.log(msg.payload);\nreturn msg;","outputs":1,"noerr":0,"x":530,"y":80,"wires":[["8d368bb8.4ead88"]]},{"id":"8d368bb8.4ead88","type":"change","z":"35f40a33.0c9b46","name":"","rules":[{"t":"set","p":"options","pt":"msg","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":700,"y":80,"wires":[["f8d1fa50.84b878"]]},{"id":"f8d1fa50.84b878","type":"ui_dropdown","z":"35f40a33.0c9b46","name":"Data source","label":"Controller ID","tooltip":"","place":"Select Controller ID","group":"d09cf9ee.58e9d8","order":1,"width":"7","height":"1","passthru":false,"options":[{"label":"","value":"","type":"str"}],"payload":"","topic":"msg.topic","x":890,"y":80,"wires":[["35c12b07.a0d444"]]},{"id":"209ba86a.c1eb68","type":"debug","z":"35f40a33.0c9b46","name":"controllerid-1","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"controllerid","x":1270,"y":80,"wires":[]},{"id":"35c12b07.a0d444","type":"change","z":"35f40a33.0c9b46","name":"","rules":[{"t":"set","p":"controllerid","pt":"msg","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":1090,"y":80,"wires":[["209ba86a.c1eb68","98e089da.76a558"]]},{"id":"ada70a4d.582e88","type":"debug","z":"35f40a33.0c9b46","name":"controllerid-2","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","x":560,"y":300,"wires":[]},{"id":"4ffd0c1d.3c3364","type":"debug","z":"35f40a33.0c9b46","name":"controllerid-3","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"controllerid","x":970,"y":260,"wires":[]},{"id":"d09cf9ee.58e9d8","type":"ui_group","z":"","name":"light rule","tab":"3022bccd.b5f134","order":2,"disp":true,"width":"20","collapse":false},{"id":"3022bccd.b5f134","type":"ui_tab","z":"","name":"testinput","icon":"dashboard","order":13,"disabled":false,"hidden":false}]

0 Likes

#16

Can you put a debug (display Complete msg object) on the output of the HTTP Request node and copy/past the results so I don't have to spend a bunch of time trying to recreate the data being passed on.

0 Likes