Store data in flow variable to be used in another node (in Python3 function)


Hello everyone.

I am trying to save the name of a file into a variable so that I can call it in another function node later. I have read that there is a flow variable that is available in the flow scope, which is what I need. I have been trying to test the examples in the Node-RED Documentation but I can't get them to work ( I am not a programmer, so I just test until it works). The other problem is that I have been using Python3 Function nodes so I would like to continue this way for the sake of simplicity.

Can anyone tell me how to just save the name of my file "10:23.txt" into a variable and how I can call it in a Python3 function later?
The point is that I am creating a file called "test.txt". The payload will be saved in this file and name of this file is gonna be replaced with a time stamp every 5 seconds. Then the file with the time stamp name will be uploaded to the cloud. The goal is to keep deleting the files as soon as they get uploaded to the cloud. That's why I need to pass their name, so that after it gets uploaded I can delete for example the "10:23.txt"



I would recommend you look into the use of a database instead of writing/deleting files.



I am uploading it to the cloud database so that is not the point. I just want to know how to save "10:23.txt" into a flow context variable. Thanks.



Using a : in a filename is bad practice.
To store flow context:

To get:
filename = flow.get("filename");

Not sure how a python node works, but I can imagine that you can use the context vars from node-red.



Thank you in advance. I tried this and I get this error in the function node where I tried to save the name of the file:

File "<string>", line 69, in python_function
NameError: global name 'flow' is not defined


You will have to put that in a "normal" function node return {"payload":filename} and connect it to the python node and read the payload there, probably something like msg['payload']. As I don't work with python I have no idea how it works.



the flow and global context are only available within Node-RED - when you call out to python it has no concept of them. You can pass in the variable as part of the command line parameters (as if you typed on the command line) - or indeed (very ugly but you could) create a known file name and write to that - then have the python read that file and get the variable then proceed... You're better off with the former.



I suspect once you get to grips with node-red you will see the irony in that statement.

Unless you are doing something in python that cant be done in NODE JS / JS, then you are making things overly complicated

Surely it would be easier to do this all within node-red - and probably - without writing a single file



@xoani based on your other posts .... you can leave your python script writing to standard-out then rely on Node-Red to write the payload to a file for you.

Here's an example of what you might want to do:

  • Set msg.filename (using change node)
  • Run the command (using exec node)
  • Save the out put (using file node)
  • Set msg.payload to the filename (using change node) -- as required by Azure blob
  • Call Azure blob storage node

(screen shot of rough proto-type ... no function code or context variables required)