# Use the node red values in Python

**URL:** <https://discourse.nodered.org/t/use-the-node-red-values-in-python/22086>\
**Category:** General\
**Created:** [22 February 2020 10:16 UTC](https://discourse.nodered.org/t/use-the-node-red-values-in-python/22086 "2020-02-22T10:16:56Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![gragtoni435](https://avatars.discourse-cdn.com/v4/letter/g/bcef8e/32.png) [@gragtoni435](https://discourse.nodered.org/u/gragtoni435)\
**Post date:** [22 February 2020 10:16 UTC](https://discourse.nodered.org/t/use-the-node-red-values-in-python/22086/1 "2020-02-22T10:16:56Z")

</div>

Hello,

I can not find a solution how I can process the values from Node-Red in a Python script.  
Here, I get the X & Y coordinate from a Siemens CPU (317F) over a OPC UA Server to my Raspberry

 ![WhatsApp Image 2020-02-22 at 11.01.46](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/b/9/b9ad9807e05572843c099fc65f1f8cf167aa69be.jpeg)

The coordinates are in the payload. At the moment I wirte the coordinates in two different text files. The values are overwritten every 2 seconds.  
Now I'm trying to integrate the values into a Python script.  
Is it necessary at all that I write the values for it in text files or is there any other option like a block that provides the values?  
With the script I want to start a video recording automatically after starting the Pi and the coordinates should be integrated as text / subtitles in the video.

Here is my actual python-script:

1. from picamera import PiCamera
2. from time import sleep
3. import datetime
4. dateipfad = "/home/pi"
5. dateiname = datetime.datetime.now().strftime('%Y.%m.%d-%H%M%S')
6. videodatei = f"{dateiname}.h264"
7. print(videodatei)
8. camera = PiCamera()
9. camera.start\_preview()
10. camera.start\_recording(videodatei)
11. sleep(10)
12. camera.stop\_recording()
13. camera.stop\_preview()

Thanks for any help!

Best Regards

---

<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:** [22 February 2020 10:22 UTC](https://discourse.nodered.org/t/use-the-node-red-values-in-python/22086/2 "2020-02-22T10:22:11Z")

</div>

No, no, no, use MQTT instead to integrate NR with Python. Works excellent and fast  
Need help? Google or ask questions here. Share the code of your python script (or part of it) and I or other users will help. Also share the exact output from the OPC UA Client nodes (the messages holding the coordinates to be used)

---

<div class="post-metadata">

**Author:** ![gragtoni435](https://avatars.discourse-cdn.com/v4/letter/g/bcef8e/32.png) [@gragtoni435](https://discourse.nodered.org/u/gragtoni435)\
**Post date:** [22 February 2020 15:59 UTC](https://discourse.nodered.org/t/use-the-node-red-values-in-python/22086/3 "2020-02-22T15:59:16Z")

</div>

We are currently unable to switch to MQTT. We bought an OPC-UA server especially for this. Is there no way to implement it with OPC-UA? I hope that it can't be that difficult to read or use the value from a text file with Python 😑

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [22 February 2020 16:15 UTC](https://discourse.nodered.org/t/use-the-node-red-values-in-python/22086/4 "2020-02-22T16:15:04Z")

</div>

You can call a python script several ways from NR - using the `exec` node, node-red-node-daemon or one of the 'python contrib nodes which I'm sure you have searched and evaluated.

So in regards to your your question is "How do I pass paramerters to a python program from Node-RED" It depends on how you are doing it.

After reading the readme's for each method you should be able to try passing something to a simple python program to get familiar with what it takes. If you get stuck, paste a copy of an export of your little test flow and indicate where you are having an issue.

---

<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:** [22 February 2020 19:52 UTC](https://discourse.nodered.org/t/use-the-node-red-values-in-python/22086/5 "2020-02-22T19:52:23Z")

</div>

@gragtoni435  
You write "I can not find a solution how I can process the values from Node-Red in a Python script."

From this and the information given in the picture etc, I assume your system structure would be like this: OPC UA Server \<----\> NR(OPC UA Client -\> File) -----\> Python (Reading file, processing)

What I suggested is a structure like below where you use MQTT, you are not switching to, you keep the OPC server and OPC client connections in NR as is, you just send the data to the python script via MQTT, instead of writing to a file  
OC UA Server \<----\> NR(OPC UA Client -\> MQTT out) -----\> MQTT Broker -----\> Python (MQTT Client, processing)

You can run the broker in the same pi as NR. Besides, writing to a file as an interface is very inefficient and will also wear out your sd card at some time if you write frequently to it

---

<div class="post-metadata">

**Author:** ![gragtoni435](https://avatars.discourse-cdn.com/v4/letter/g/bcef8e/32.png) [@gragtoni435](https://discourse.nodered.org/u/gragtoni435)\
**Post date:** [22 February 2020 22:31 UTC](https://discourse.nodered.org/t/use-the-node-red-values-in-python/22086/6 "2020-02-22T22:31:37Z")

</div>

aaaaaah okay.  
Now I think I understand the idea.  
I have installed MQTT on my Pi.  
If I understand it right, I have to take a "mqtt in" and a "mqtt out" between the OPC UA Clients and the msg, haven't I?  
Like on this picture

 ![WhatsApp Image 2020-02-22 at 23.23.40](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/1/0/10d029a1aded77c7c34dcbff792f85ce9ddd5235.jpeg)

But how can I then query the values in my Python script?  
I think this is the easiest command to get the text in the video.  
camera.annotate\_text = "text"

But how get I the informations from the MQTT in this line?

---

<div class="post-metadata">

**Author:** ![gragtoni435](https://avatars.discourse-cdn.com/v4/letter/g/bcef8e/32.png) [@gragtoni435](https://discourse.nodered.org/u/gragtoni435)\
**Post date:** [23 February 2020 00:07 UTC](https://discourse.nodered.org/t/use-the-node-red-values-in-python/22086/7 "2020-02-23T00:07:40Z")

</div>

Hello again,

After many more searches, I continued a little.  
Hier der aktuelle Status:

 ![WhatsApp Image 2020-02-23 at 00.59.44](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/9/e/9e37a17c898f8e762a299fd101713ae4b7c6fe1d.jpeg)  
Now I have the MQTT connection in node red. I tested it in a small flow, it worked perfectly there. But in this flow the lower msg.payload receives no data.  
Only the upper one receives the position data from the OPC-UA client continuously.  
The debug window for the lower payload is always empty. Why?  
The upper one receives the news updated every second as normal.

Zum weiteren habe ich die Abfrage mal mit diesem Programm hier getestet:

 ![WhatsApp Image 2020-02-23 at 00.59.45](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/4/c/4c3a5c551148c7434689cc4cd73d6d3d09777319.jpeg)  
Es wird auch kein Fehler im Script angezeigt aber wie man sehen kann, ohne Werte kommt eben auch kein Wert hier raus.

Kann mir bei den Problem jemand helfen? Fehlt mir ein spezieller Baustein der von OPC-UA auf MQTT umwandelt ?

Thanks for any help! 😇

---

<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 February 2020 08:23 UTC](https://discourse.nodered.org/t/use-the-node-red-values-in-python/22086/8 "2020-02-23T08:23:17Z")

</div>

Some basics first

In MQTT you work with topics, it is similar to "Postfach"

When you send data via MQTT you publish it to a specific topic. If you are interested to receive this data you subscribe to the same topic

In the flow where you are checking this, it is therefore very important that you publish to the same topic as you subscribe to

In the test python script you show, are you subscribing to the same topic that you publish to?

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [23 February 2020 11:13 UTC](https://discourse.nodered.org/t/use-the-node-red-values-in-python/22086/9 "2020-02-23T11:13:10Z")

</div>

@krambriw Walter, just to clarify, if I'm not mistaken, your suggestionn will require that the python script be running constantly because it will be waiting for a MQTT msg to arrive. In addition the python code needs to subscribe to the same topic that the NR `MQTT-out` is using.

If the NR flow needs acknowledgment that the python code got the msg, then the python code will have to publish a msg using a mqtt topic and the NR flow will have to subscribe to that topic to receive the acknowledgement.

---

<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 February 2020 11:18 UTC](https://discourse.nodered.org/t/use-the-node-red-values-in-python/22086/10 "2020-02-23T11:18:31Z")

</div>

Yes, you are absolutely correct in all you say. I just thought best to take it step-wise since the user seems new to MQTT. Currently the subscription seems to be the (first) blocking point

First thing to solve is to check that the subscription to the same topic works in NR as the user already is trying but failing

The final python script will for sure have all those parts included but it is not a big concern, done it maybe 100 times 😉

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [23 February 2020 11:20 UTC](https://discourse.nodered.org/t/use-the-node-red-values-in-python/22086/11 "2020-02-23T11:20:13Z")

</div>

> [@krambriw](#):
>
> The final python script will for sure have all those parts included but it is not a big concern, done it maybe 100 times 😉

Ahhh, but has the OP? Everything is easy once you have done it a 100 times 😂

---

<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 February 2020 11:21 UTC](https://discourse.nodered.org/t/use-the-node-red-values-in-python/22086/12 "2020-02-23T11:21:42Z")

</div>

I am willing to write that script for the OP, I have already many good "templates" in my own system running since years

---

<div class="post-metadata">

**Author:** ![gragtoni435](https://avatars.discourse-cdn.com/v4/letter/g/bcef8e/32.png) [@gragtoni435](https://discourse.nodered.org/u/gragtoni435)\
**Post date:** [24 February 2020 12:09 UTC](https://discourse.nodered.org/t/use-the-node-red-values-in-python/22086/13 "2020-02-24T12:09:55Z")

</div>

It works! 🥳  
I adjusted the topics everywhere and now I have the X and Y values in the Python script.  
Now the last problem is, that I need the values as text in the video.

 ![WhatsApp Image 2020-02-24 at 13.02.14](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/6/5/650191e2fa84628a1651183cfe89d16e2a08e68c.jpeg)

Do i need every of this commands? Or can I delete some of this, which are not required?

I want the msg.payload in the line "camera.annotate\_text" so that the current values can always be seen in the video. How can I do that?  
Is the easiest way to use a for-loop, that updated every e.g. 2 seconds the values?  
The video will be very long, 2-3 h for example.

---

<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:** [24 February 2020 14:18 UTC](https://discourse.nodered.org/t/use-the-node-red-values-in-python/22086/14 "2020-02-24T14:18:22Z")

</div>

> [@gragtoni435](#):
>
> Do i need every of this commands? Or can I delete some of this, which are not required?  
> _Well, what I can see from the print screen I think you need them but better copy & paste the complete script here in your answer. You may eventually skip some print statements if you do not need them anymore but you need at least something in a function. At least you must add a line with "pass"_
> 
> I want the msg.payload in the line "camera.annotate\_text" so that the current values can always be seen in the video. How can I do that?  
> _Here you need to change the script. I don't think anything will be executed after the line "client.loop\_forever()" as long as the the loop is running and it is until you kill the script. You should put all those lines in the function "def\_on\_message" where you will receive the msg.payload, I would try it like this. Try and see if it works_

```auto
def on_message(client, userdata, msg):
    global camera
    global videodatei
    print(msg.payload)
# camera.annotate_text = "whatever"
    camera.annotate_text = str(msg.payload)
    camera.start_preview()    
    camera.start_recording(videodatei)
    time.sleep(15) #Don't forget to add the line "import time" in the beginning of the script!!!
    camera.stop_recording()
    camera.stop_preview()    

```

---

<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:** [24 February 2020 14:57 UTC](https://discourse.nodered.org/t/use-the-node-red-values-in-python/22086/15 "2020-02-24T14:57:49Z")

</div>

As the above is an example just to test if it is working, I think there might be some more logic needed. Suppose the recording is already ongoing and you receive a new message (with updated text). What should happen? Maybe the text gets updated? Should the recording stop and then restart?

To be able to check if the recording is already ongoing some more stuff needs to be added. But try the above example first so we can verify if that part works or not

---

<div class="post-metadata">

**Author:** ![gragtoni435](https://avatars.discourse-cdn.com/v4/letter/g/bcef8e/32.png) [@gragtoni435](https://discourse.nodered.org/u/gragtoni435)\
**Post date:** [24 February 2020 16:02 UTC](https://discourse.nodered.org/t/use-the-node-red-values-in-python/22086/16 "2020-02-24T16:02:05Z")

</div>

Okay, here are my scripts:

1. The script is for automatic video recording when the Pi starts. The name file is associated with the time so that the files do not overwrite. The time is only 10s for testing. At the end it is something like 3h, but this doesn`t matter at the moment.

```auto
from picamera import PiCamera
from time import sleep
import datetime

dateipfad = "/home/pi"
dateiname = datetime.datetime.now().strftime('%Y.%m.%d-%H%M')
videodatei = f"{dateiname}.h264"
print(videodatei)
camera = PiCamera()
camera.start_preview()
camera.start_recording(videodatei)
sleep(10)
camera.stop_recording()
camera.stop_preview()

```

1. Then after getting the connection in Node-Red, I took this script for testing. Here you can always see that the two values (in this case X coordinate: 3 and Y: 10) are constantly transmitted every second in the payload.

```auto
import paho.mqtt.client as mqtt

def on_connect (client, userdata, flags, rc):
    print("connected with result code" + str(rc))
    
    client.subscribe("positions/#")
    
def on_message(client, userdata, msg):
    print(msg.payload)
          
client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message

client.connect("localhost", 1883, 10)
client.loop_forever()

```

Is it better to load the coordinates in different payloads so that you can distinguish them?  
Does the payload actually overwrite itself, meaning that only the later value is still there? or can both values be in the payload?  
At the moment node-red is like that:

 ![WhatsApp Image 2020-02-24 at 17.00.00](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/d/3/d39094abf8cc02e89e4668d32c88ba6492ce5ff9.jpeg)

So now I've tried the script above. Not even a video recording is started here ☹  
Here is the first try.

```auto
from picamera import PiCamera
from time import sleep
import datetime
import paho.mqtt.client as mqtt

dateipfad = "/home/pi"
dateiname = datetime.datetime.now().strftime('%Y.%m.%d-%H%M')
videodatei = f"{dateiname}.h264"
print(videodatei)
camera = PiCamera()

def on_connect (client, userdata, flags, rc):
    print("connected with result code" + str(rc)) 
    client.subscribe("positions/#")  
def on_message(client, userdata, msg):
    global camera
    global videodatei
    print(msg.payload)
    camera.annotate_text = str(msg.payload)
    camera.start_preview()
    camera.start_recording(videodatei)
    sleep(15)
    camera.stop_recording()
    camera.stop_preview()
    
client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message
client.connect("localhost", 1883, 10)

```

> As the above is an example just to test if it is working, I think there might be some more logic needed. Suppose the recording is already ongoing and you receive a new message (with updated text). What should happen? Maybe the text gets updated? Should the recording stop and then restart?To be able to check if the recording is already ongoing some more stuff needs to be added. But try the above example first so we can verify if that part works or not

The updated text (X/Y-coordinates) should be constantly updated in the video

I am happy for any help! 😇  
We want to test it tomorrow. I think today will be a long night. 🤔

---

<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:** [24 February 2020 17:27 UTC](https://discourse.nodered.org/t/use-the-node-red-values-in-python/22086/17 "2020-02-24T17:27:10Z")

</div>

In the last example I miss the line "client.loop\_forever()". Without it, you will not receive any messages

Does the recording start with your script 1) ??

---

<div class="post-metadata">

**Author:** ![gragtoni435](https://avatars.discourse-cdn.com/v4/letter/g/bcef8e/32.png) [@gragtoni435](https://discourse.nodered.org/u/gragtoni435)\
**Post date:** [24 February 2020 17:44 UTC](https://discourse.nodered.org/t/use-the-node-red-values-in-python/22086/18 "2020-02-24T17:44:46Z")

</div>

Script 1 is just my booth before I connect Node-Red. Now I'm trying to combine Script 1 with 2 so that it works as intended.  
Now with the line it works.  
But now a video is recorded for 15 seconds with the `b10` as text in the video. After the 15 seconds the next recording starts with `b3` as text  
But both values should be displayed ..

```auto
from picamera import PiCamera
from time import sleep
import datetime
import paho.mqtt.client as mqtt

dateipfad = "/home/pi"
dateiname = datetime.datetime.now().strftime('%Y.%m.%d-%H%M')
videodatei = f"{dateiname}.h264"
print(videodatei)
camera = PiCamera()

def on_connect (client, userdata, flags, rc):
    print("connected with result code" + str(rc)) 
    client.subscribe("positions/#")  
def on_message(client, userdata, msg):
    global camera
    global videodatei
    print(msg.payload)
    camera.annotate_text = str(msg.payload)
    camera.start_preview()
    camera.start_recording(videodatei)
    sleep(15)
    camera.stop_recording()
    camera.stop_preview()
    
client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message

client.connect("localhost", 1883, 10)
client.loop_forever()

```

What does this b mean? In Node-Red I see only the value and not the b

---

<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:** [24 February 2020 19:23 UTC](https://discourse.nodered.org/t/use-the-node-red-values-in-python/22086/19 "2020-02-24T19:23:07Z")

</div>

The b means buffer so you need to decode it to get the actual text. Typically you would do

```auto
    print(msg.payload.decode("utf-8"))
    camera.annotate_text = str(msg.payload.decode("utf-8"))

```

This is what happens:  
When you send a message to the script it takes the text in the msg.payload and puts it in the video view. Next the recording and viewing starts and the script goes into sleep. During the sleep the script will NOT receive any additional messages, they will be kept in the MQTT broker. When the sleep is over, recording and viewing is stopped and the message kept in the MQTT broker is received. The whole thing is starting over now with the new text received.

So your wish to present both values cannot be fulfilled in this way. You have to do it differently. You have to send both values in the same message, not as two separate messages.

Are you fully sure you feel enough comfortable working in Python? Or is this getting too complex?

---

<div class="post-metadata">

**Author:** ![gragtoni435](https://avatars.discourse-cdn.com/v4/letter/g/bcef8e/32.png) [@gragtoni435](https://discourse.nodered.org/u/gragtoni435)\
**Post date:** [24 February 2020 19:46 UTC](https://discourse.nodered.org/t/use-the-node-red-values-in-python/22086/20 "2020-02-24T19:46:35Z")

</div>

OK. Thank you.  
Yes, I already understand how the current script works and that it just doesn't work the way I want it to.  
But unfortunately I myself have no idea how I have to realize that it just works as intended.  
If you could help me here, I would be very grateful.  
At the moment I find everything in python understandable & logical.

[Next page](https://discourse.nodered.org/t/use-the-node-red-values-in-python/22086.md?page=2)
