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

**URL:** <https://discourse.nodered.org/t/node-red-flow-trigger-python-function-in-a-python-script-on-the-raspberry-pi/72617>\
**Category:** General\
**Created:** [23 December 2022 09:42 UTC](https://discourse.nodered.org/t/node-red-flow-trigger-python-function-in-a-python-script-on-the-raspberry-pi/72617 "2022-12-23T09:42:18Z")\
**Posts on this page:** 17\
**Page:** 1

<div class="post-metadata">

**Author:** ![staebchen0](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/staebchen0/32/72990_2.png) [@staebchen0](https://discourse.nodered.org/u/staebchen0)\
**Post date:** [23 December 2022 09:42 UTC](https://discourse.nodered.org/t/node-red-flow-trigger-python-function-in-a-python-script-on-the-raspberry-pi/72617/1 "2022-12-23T09:42:18Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![dynamicdave](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dynamicdave/32/96_2.png) [@dynamicdave](https://discourse.nodered.org/u/dynamicdave)\
**Post date:** [23 December 2022 09:57 UTC](https://discourse.nodered.org/t/node-red-flow-trigger-python-function-in-a-python-script-on-the-raspberry-pi/72617/2 "2022-12-23T09:57:42Z")

</div>

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

 ![exec_to_python](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/f/5/f587d17fb99ff9f9ef0e230bb4ad2ef69f7ad754.png)  
If you need to, you can pass parameters to the script

---

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [23 December 2022 10:23 UTC](https://discourse.nodered.org/t/node-red-flow-trigger-python-function-in-a-python-script-on-the-raspberry-pi/72617/3 "2022-12-23T10:23:14Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [23 December 2022 10:32 UTC](https://discourse.nodered.org/t/node-red-flow-trigger-python-function-in-a-python-script-on-the-raspberry-pi/72617/4 "2022-12-23T10:32:47Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![staebchen0](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/staebchen0/32/72990_2.png) [@staebchen0](https://discourse.nodered.org/u/staebchen0)\
**Post date:** [23 December 2022 10:54 UTC](https://discourse.nodered.org/t/node-red-flow-trigger-python-function-in-a-python-script-on-the-raspberry-pi/72617/5 "2022-12-23T10:54:18Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![staebchen0](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/staebchen0/32/72990_2.png) [@staebchen0](https://discourse.nodered.org/u/staebchen0)\
**Post date:** [23 December 2022 10:56 UTC](https://discourse.nodered.org/t/node-red-flow-trigger-python-function-in-a-python-script-on-the-raspberry-pi/72617/6 "2022-12-23T10:56:20Z")

</div>

> [@dceejay](#):
>
> Eingaben über stdin

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?

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [23 December 2022 11:12 UTC](https://discourse.nodered.org/t/node-red-flow-trigger-python-function-in-a-python-script-on-the-raspberry-pi/72617/7 "2022-12-23T11:12:01Z")

</div>

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)?

---

<div class="post-metadata">

**Author:** ![staebchen0](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/staebchen0/32/72990_2.png) [@staebchen0](https://discourse.nodered.org/u/staebchen0)\
**Post date:** [23 December 2022 11:54 UTC](https://discourse.nodered.org/t/node-red-flow-trigger-python-function-in-a-python-script-on-the-raspberry-pi/72617/8 "2022-12-23T11:54:41Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![staebchen0](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/staebchen0/32/72990_2.png) [@staebchen0](https://discourse.nodered.org/u/staebchen0)\
**Post date:** [23 December 2022 12:42 UTC](https://discourse.nodered.org/t/node-red-flow-trigger-python-function-in-a-python-script-on-the-raspberry-pi/72617/9 "2022-12-23T12:42:58Z")

</div>

> [@jbudd](#):
>
> - Mosquitto MQTT-Broker auf Ihrem Pi
> - Paho-mqtt-Code in Ihrem Python-Skript

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

---

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [23 December 2022 13:32 UTC](https://discourse.nodered.org/t/node-red-flow-trigger-python-function-in-a-python-script-on-the-raspberry-pi/72617/10 "2022-12-23T13:32:27Z")

</div>

> [@staebchen0](#):
>
> one question before I try this out:  
> how is the speed? Is in the ms range?

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

---

<div class="post-metadata">

**Author:** ![staebchen0](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/staebchen0/32/72990_2.png) [@staebchen0](https://discourse.nodered.org/u/staebchen0)\
**Post date:** [23 December 2022 16:27 UTC](https://discourse.nodered.org/t/node-red-flow-trigger-python-function-in-a-python-script-on-the-raspberry-pi/72617/11 "2022-12-23T16:27:52Z")

</div>

> [@dceejay](#):
>
> STDIN. ZB sitzt die laufende App da und wartet auf eine Tastatureingabe. Wenn Sie es normal von der Befehlszeile aus ausführen. Wie sagen Sie ihm, was er tun soll (während er läuft)?

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

---

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [23 December 2022 17:07 UTC](https://discourse.nodered.org/t/node-red-flow-trigger-python-function-in-a-python-script-on-the-raspberry-pi/72617/12 "2022-12-23T17:07:55Z")

</div>

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

> [@dceejay](#):
>
> If the process is long running but can accept input via stdin then the node-red-node-daemon node is one way.

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.

---

<div class="post-metadata">

**Author:** ![krambriw](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/krambriw/32/5429_2.png) [@krambriw](https://discourse.nodered.org/u/krambriw)\
**Post date:** [23 December 2022 17:55 UTC](https://discourse.nodered.org/t/node-red-flow-trigger-python-function-in-a-python-script-on-the-raspberry-pi/72617/13 "2022-12-23T17:55:06Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![staebchen0](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/staebchen0/32/72990_2.png) [@staebchen0](https://discourse.nodered.org/u/staebchen0)\
**Post date:** [27 December 2022 16:34 UTC](https://discourse.nodered.org/t/node-red-flow-trigger-python-function-in-a-python-script-on-the-raspberry-pi/72617/14 "2022-12-27T16:34:44Z")

</div>

> [@jbudd](#):
>
> - Mosquitto MQTT-Broker auf Ihrem Pi
> - Paho-mqtt-Code in Ihrem Python-Skript

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?

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [27 December 2022 16:48 UTC](https://discourse.nodered.org/t/node-red-flow-trigger-python-function-in-a-python-script-on-the-raspberry-pi/72617/15 "2022-12-27T16:48:22Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [27 December 2022 17:07 UTC](https://discourse.nodered.org/t/node-red-flow-trigger-python-function-in-a-python-script-on-the-raspberry-pi/72617/16 "2022-12-27T17:07:58Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [25 February 2023 17:08 UTC](https://discourse.nodered.org/t/node-red-flow-trigger-python-function-in-a-python-script-on-the-raspberry-pi/72617/17 "2023-02-25T17:08:04Z")

</div>

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