Node Red Flow - trigger Python function in a Python script on the Raspberry Pi

Hi everyone,
I have a python script running on the raspberry pi and I want certain code parts to only be executed if the value true comes from a Node Red Flow.

Red Node is already running on the raspberry pi

Does anyone have any tips on the easiest way to do this?

On my set-up I just use an Exec node to call a Python script running on the same Pi as Node-RED.


If you need to, you can pass parameters to the script

If the script runs continuously and needs to react to a message from Node-red, the easiest way it is send the message via MQTT.

You need :

  • Mosquitto MQTT broker on your Pi
  • Paho-mqtt code in your Python script
1 Like

If the process is long running but can accept input via stdin then the node-red-node-daemon node is one way.

1 Like

Thanks for the quick tips

yes, the script runs continuously.

and certain code functions should only be executed if the Node Red flow e.g. worth a true story. It all has to happen very quickly.

Unfortunately, stdin doesn't tell me anything, so far I haven't used it in my program. What needs to be installed in Python for this?

STDIN. Eg the running app is sitting there waiting for some keyboard input. When you run it normally from the command line. How do you tell it what to do (while it is running)?

The program monitors the cat flap and requires no user or keyboard actions.

At the moment the program continuously takes pictures that are checked in a Tflite model.

Now I would like to trigger the check via an external motion detector (no Pi Sensor) so that the program does not have to check every meaningless image.

Hence the idea of ​​using Red Node to send movement information to the program

one question before I try this out:
how is the speed? Is in the ms range? Do you have experience here?

It's fast but I'm afraid you will have to count your own milliseconds! :slightly_smiling_face:

1 Like

which Node Red node would be the right one if I used STDIN in the Python code?

@dceejay's suggestion was to use node-red-node-daemon

If you want to try this, you have to install the node via Manage Palette.

Whether you pick the daemon node, MQTT or some other option (for example named pipes), your Python script will most likely need modifying so that it listens for messages and responds when it receives one.
I think the usual approach to do this in Python is by a "callback", but that's ouside the scope of a Node-red forum.

There are several posting around on the forum with examples demonstrating this all in a python script using MQTT. To find it is sometimes like looking for a needle in a haystack but a search may be successful

hi i have Mosquitto MQTT broker on your Pi now
Paho mqtt code in your python script installs both.

What would the flow look like now? I've tried in vain to find help using the search function, but unfortunately I couldn't find anything.

Do I now send from Node Red to the mqtt and this in turn to the Python script?

To publish to mqtt use an MQTT Out node. How to subscribe to that in your python code is a python question, not node red.

Your python script has to "subscribe" to the message topic at the mosquito broker.

Node-red "publishes" to the same topic.

To find an example python program Google for "python mqtt callback"
A callback is the mechanism which allows the script to perform specific processing when a certain event occurs.

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.