NodeRED and python

How can we get the output from a python file which is run not using exec node from the editor into NodeRED ?

there are MQTT libraries for python, so MQTT is one option

@ukmoose anything else using, stdout ?

output to tcp/udp port and listen for the port in NR
or write to a file and read the file in NR ?

Have you figured out yet why it doesn’t run?

@afelix Hey see the code can be executed from the NodeRED editor easily. The requirement is to run when it is required. The whole system is a combination of python script's output and processing it in NodeRED.

@bakman2 I will try it out.Thanks.

Sounds like you can indeed call it from an exec node and process it, but have to design your flow in a way that the scripts get called at the moment(s) you want to... which there’s plenty of nodes available to take care of that depending on your needs

@afelix Like which node ? can you suggest me ?

That fully depends on the conditions on when to execute those. Which would be helpful if you explained those. You can use a switch node to branch a flow depending on conditions, inject/trigger/delay nodes for time based execution/repeated action. For repeated execution following a time schedule you can look at the node-red-contrib-cron-plus node. For repeatedly executing following script output you use the output of the exec node followed by processing it and more conditional logic. Possibilities are near endless.

@afelix I think you didn't understand my concern fully. The python program runs whenever a user calls it. So then only the should be captured by any nodes. The "stdout" would work when it is run using editor but not from the script which is running through any python IDLE .

Just to clarify, the python script is run completely independently of Node-RED? It is not Node-RED that will be starting the script.

But when the script does run, you want its output to get to Node-RED in some form.

In which case you need to use some form of interprocess communication. A few options have already been mentioned; the python script could publish messages to a local MQTT broker (eg mosquitto) and have Node-RED subscribe to those messages. That would fully decouple the running of the script from Node-RED receiving its output. You could configure a Node-RED flow to listen on a TCP port for incoming connections - your python script could connect to that port and write its data.

There are lots of options - all depends which approach you feel comfortable with.

@knolleary Thanks. i started working on MQTT publish using paho MQTT and i got the expected results so that it kept both the things independent.

1 Like