# 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:** 16\
**Page:** 2

<div class="post-metadata">

**Author:** ![afelix](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/afelix/32/9743_2.png) [@afelix](https://discourse.nodered.org/u/afelix)\
**Post date:** [24 February 2020 19:51 UTC](https://discourse.nodered.org/t/use-the-node-red-values-in-python/22086/21 "2020-02-24T19:51:14Z")

</div>

> [@krambriw](#):
>
> The b means buffer

(Slight correction, the b here stands for a bytes string. They’re pretty comparable looking at it from a JavaScript perspective, but in any python documentation you’d see the word bytes used for it. The rest of the information provided here however is spot on!)

---

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

</div>

You can combine the two values in NR before you send the message to the Python script (using the join node) or put them together in the script before you do annotate and start recording and viewing

I assume this is what is missing?

Tomorrow morning I can show you, now is a bit late

---

<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 20:22 UTC](https://discourse.nodered.org/t/use-the-node-red-values-in-python/22086/23 "2020-02-24T20:22:40Z")

</div>

I used the join module to combine the messages in Node Red. Now, as shown in the picture, the X coordinate and then the Y coordinate with a line break arrives in a message.

 ![WhatsApp Image 2020-02-24 at 21.17.29](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/5/5/55abdf01af390855207c7cc64f8c7c8c6f1d080a.jpeg)  
It also works in the video. 🙂  
 ![WhatsApp Image 2020-02-24 at 21.18.17](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/3/9/39d725d662273d81ca38173aa701fe1fe85a1d56.jpeg)  
Now the function is missing that the values are constantly updated. Maybe with a While True loop?

---

<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 20:31 UTC](https://discourse.nodered.org/t/use-the-node-red-values-in-python/22086/24 "2020-02-24T20:31:54Z")

</div>

Thank you for your efforts!  
I keep trying to somehow manage that. We want to test it on the reserved machine tomorrow morning. So I have to keep trying to ensure that the current values are always displayed.  
I am happy about every possible little food for thought how you can achieve this.

---

<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 21:16 UTC](https://discourse.nodered.org/t/use-the-node-red-values-in-python/22086/25 "2020-02-24T21:16:59Z")

</div>

God damn it, I thought I got it with the following script:

```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%S')
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
    camera.start_preview()
    camera.start_recording(videodatei)
    i = 1
    while i < 9999:
        client.on_connect = on_connect
        print(msg.payload.decode("utf-8"))
        i += 1
        camera.annotate_text = str(msg.payload.decode("utf-8"))
        sleep(1)
    sleep(10)
    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()

```

I thought so it works, but since I do not change the position data to control it, I made a small change to node red accordingly to simulate it.  
In the picture you can see that I have changed the payload from 4 to 5 to see whether the program running alongside also notices the change.  
But unfortunately no, the value 4 remains

 ![WhatsApp Image 2020-02-24 at 22.12.23](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/c/8/c829312b8a5be6fb1cfe0a0414b03d99a5557798.jpeg)

If the command "client.on\_connect = on\_connect" in the While Loop is not queried for the current value? Or how is that done?

---

<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 23:15 UTC](https://discourse.nodered.org/t/use-the-node-red-values-in-python/22086/26 "2020-02-24T23:15:45Z")

</div>

Ok, here is my last status for today.

```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%S')
videodatei = f"{dateiname}.h264"
print(videodatei)
camera = PiCamera()

def on_position(msg):
    rt = (msg.payload.decode("utf-8"))
    print(rt)
    return rt
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
    camera.start_preview()
    camera.start_recording(videodatei)
    i = 1
    while i < 9999:
        print(i)
        on_position (rt)
        print(rt)
        i += 1
        camera.annotate_text = rt
        sleep(1)
    sleep(10)
    camera.stop_recording()
    camera.stop_preview()

```

I tried to do an extra function in the While Loop, which polls the new value of the payload every second.  
But it didn't work out. I never managed to get the new value here. At the point where the "on\_position" function was called, the program always stopped and was frozen.  
It didn't even output the value of rt.

---

<div class="post-metadata">

**Author:** ![afelix](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/afelix/32/9743_2.png) [@afelix](https://discourse.nodered.org/u/afelix)\
**Post date:** [24 February 2020 23:55 UTC](https://discourse.nodered.org/t/use-the-node-red-values-in-python/22086/27 "2020-02-24T23:55:07Z")

</div>

> [@gragtoni435](#):
>
> ```auto
> i = 1 
> while i < 9999: 
> print(i) 
> on_position (rt) 
> print(rt)
> i += 1 
> camera.annotate_text = rt 
> sleep(1) 
> sleep(10)
> 
> ```

This part is very likely not doing what you would think it does. Can you explain what you think/hope the program would do here?

---

<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:** [25 February 2020 06:35 UTC](https://discourse.nodered.org/t/use-the-node-red-values-in-python/22086/28 "2020-02-25T06:35:43Z")

</div>

I want the loop to ask for the value from the payload again (with the "on\_position" function) and then to put the new value into the video as text.  
And the whole thing repeats itself every second.  
how often at the end can be set with the value of i. you should then be able to set the duration of the video.

---

<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:** [25 February 2020 06:40 UTC](https://discourse.nodered.org/t/use-the-node-red-values-in-python/22086/29 "2020-02-25T06:40:50Z")

</div>

I have re-worked your script a bit...soon ready

---

<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:** [25 February 2020 06:46 UTC](https://discourse.nodered.org/t/use-the-node-red-values-in-python/22086/30 "2020-02-25T06:46:39Z")

</div>

This is the re-worked script you can try. Hopefully I got things correct 😉

What is important is that we can never allow the script to sleep. So instead I have re-worked it so that it should be awake and always ready for new incoming messages. To achieve this I am using the Timer from threading.

I have also added support for an abort command soo that you can terminate the script correctly, just send the string "abort" from NR and the script will terminate gracefully including cancelling an eventual timer still running

Script is below:

```auto
#!/usr/bin/env python3

from picamera import PiCamera
from time import sleep
import datetime
import paho.mqtt.client as mqtt
from sys import exit
from threading import Timer

def on_connect (client, userdata, flags, rc):
    print("connected with result code" + str(rc)) 
    client.subscribe("positions/#")  

def on_message(client, userdata, msg):
    global th_abort
    global camera
    global recording
    global t
    print(msg.payload.decode("utf-8"))
    if 'abort' in msg.payload.decode("utf-8"):
        if t.is_alive():
            t.cancel()
            stopRecording()
        th_abort = True
    else:
        camera.annotate_text = str(msg.payload.decode("utf-8"))
        if not recording:
            startRecording()

def on_subscribe(client, userdata, mid, granted_qos):
    print ('Subscribed')

def startRecording():
    global camera
    global recording
    global t
    recording = True
    dateiname = datetime.datetime.now().strftime('%Y.%m.%d-%H%M')
    videodatei = f"{dateiname}.h264"
    print(videodatei)
    camera.start_preview()
    camera.start_recording(videodatei)
    t = Timer(10.0, stopRecording)
    t.start()

def stopRecording():
    global camera
    global recording
    camera.stop_recording()
    camera.stop_preview()
    recording = False

# Main starts here -----------------------------------------------------------
t = None
th_abort = False
recording = False
dateipfad = "/home/pi"
camera = PiCamera()

client = mqtt.Client()
client.on_connect = on_connect
client.on_subscribe = on_subscribe
client.on_message = on_message
client.connect("localhost", 1883, 60)

client.loop_start()
while th_abort == False:
    sleep(1)
client.loop_stop()
client.disconnect()
del client
sleep(2)
print ("script terminated")
exit(0)

```

---

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

</div>

Thank you very much! 🙂  
When executing, however, an error message is made as in the picture.

 ![WhatsApp Image 2020-02-25 at 08.09.02](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/3/3/33e10868f19aec0b338902d8aed5f4368e605a36.jpeg)

Edit:  
Ok I wrote this line again and it works.!  
I give later the information about the test

---

<div class="post-metadata">

**Author:** ![afelix](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/afelix/32/9743_2.png) [@afelix](https://discourse.nodered.org/u/afelix)\
**Post date:** [25 February 2020 07:40 UTC](https://discourse.nodered.org/t/use-the-node-red-values-in-python/22086/32 "2020-02-25T07:40:09Z")

</div>

The reason that error appeared is that your variables can’t contain dashes. A dash means minus.

---

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

</div>

In my original script I had an underscore, I think the OP corrected this and then it seems working

Two more suggestions to add:

1. The code to cancel the Timer should be changed to this to avoid eventual error message if there is no timer running:

```auto
    if 'abort' in msg.payload.decode("utf-8"):
        try:
            if t.is_alive():
                t.cancel()
                stopRecording()
        except:
            pass
        th_abort = True

```

1. If you think the script is not updating the text in the video fast enough you could try this

```auto
while th_abort == False:
    sleep(0.1)
client.loop_stop()

```

---

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

</div>

So, the test was successful.  
Only the cloning of the SD card did not work properly. the 2nd raspberry never worked as intended.  
So I set it up again, put everything on and set it up. Then copied the scripts over and then everything worked.  
Thanks for the help here! 😇

---

<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:** [28 February 2020 04:52 UTC](https://discourse.nodered.org/t/use-the-node-red-values-in-python/22086/35 "2020-02-28T04:52:33Z")

</div>

@gragtoni435  
Good to hear! Always nice with feedback when things worked out like this. Did you also use the last small modifications in my previous response?

Anyway, hopefully useful also to other users, at least an excercis in communication NR with Python using MQTT

EDIT: regarding cloning of SD-Cards. In all my pi's I have an extra micro sd card reader with an sd card inserted. I am always using the built in feature "Accessories/SD Card Copier". When I tried it has always been successful in making a perfect clone that worked perfect when I swapped sd cards

---

<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:** [13 March 2020 04:52 UTC](https://discourse.nodered.org/t/use-the-node-red-values-in-python/22086/36 "2020-03-13T04:52:39Z")

</div>

Closed on 13 March 2020 04:52 UTC

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

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