# Python function return variable

**URL:** <https://discourse.nodered.org/t/python-function-return-variable/73928>\
**Category:** General\
**Tags:** python\
**Created:** [19 January 2023 17:29 UTC](https://discourse.nodered.org/t/python-function-return-variable/73928 "2023-01-19T17:29:06Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![superlogan72](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/superlogan72/32/74680_2.png) [@superlogan72](https://discourse.nodered.org/u/superlogan72)\
**Post date:** [19 January 2023 17:29 UTC](https://discourse.nodered.org/t/python-function-return-variable/73928/1 "2023-01-19T17:29:06Z")

</div>

Hello  
using python-function i'm not able to get get the variable value

```auto
import sys
sys.path.append("/home/pi/.local/lib/python3.9/site-packages")
from pigpio_encoder.rotary import Rotary
import threading
import time
global my_counter
my_counter = 0

def sw_short():
    pass

def up_callback(counter):
    global my_counter
    if counter%40==0:
        my_counter+=1
        msg['payload'] = {'pos_x':my_counter}
        print(msg)
        #print(my_counter)

def down_callback(counter):
    global my_counter
    if counter%40==0:
        my_counter-=1
        msg['payload'] = {'pos_x':my_counter}
        return msg
        #return my_counter
        #print(my_counter)
    
def pippo():
    global my_counter
    print(my_counter)
    time.sleep(0.3)
    pippo()

my_rotary = Rotary(clk_gpio=13, dt_gpio=12,sw_gpio = 17)
my_rotary.setup_rotary( min=-1000000,
                        max=1000000,
                        debounce = 1,
                        up_callback=up_callback,
                        down_callback=down_callback,)
                    #rotary_callback=rotary_callback,
my_rotary.setup_switch(sw_short_callback=sw_short)
return msg

```

This is the code, i'm not able to desplay the value of my\_counter

```auto
[
    {
        "id": "f23f10101329d95f",
        "type": "python-function",
        "z": "a2713db36e62f1e7",
        "name": "Encoder",
        "func": "import sys\nsys.path.append(\"/home/pi/.local/lib/python3.9/site-packages\")\nfrom pigpio_encoder.rotary import Rotary\nimport threading\nimport time\nglobal my_counter\nmy_counter = 0\n\ndef sw_short():\n pass\n\ndef up_callback(counter):\n global my_counter\n if counter%40==0:\n my_counter+=1\n msg['payload'] = {'pos_x':my_counter}\n print(msg)\n #print(my_counter)\n\ndef down_callback(counter):\n global my_counter\n if counter%40==0:\n my_counter-=1\n msg['payload'] = {'pos_x':my_counter}\n return msg\n #return my_counter\n #print(my_counter)\n \ndef pippo():\n global my_counter\n print(my_counter)\n time.sleep(0.3)\n pippo()\n\n\nmy_rotary = Rotary(clk_gpio=13, dt_gpio=12,sw_gpio = 17)\nmy_rotary.setup_rotary( min=-1000000,\n max=1000000,\n debounce = 1,\n up_callback=up_callback,\n down_callback=down_callback,)\n #rotary_callback=rotary_callback,\nmy_rotary.setup_switch(sw_short_callback=sw_short)\nreturn msg\n# x = threading.Thread(target=pippo)\n# x.start()\n\n",
        "outputs": 1,
        "x": 220,
        "y": 100,
        "wires": [
            [
                "393990a61c77173e"
            ]
        ]
    }
]

```

---

<div class="post-metadata">

**Author:** ![edje11](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/edje11/32/572_2.png) [@edje11](https://discourse.nodered.org/u/edje11)\
**Post date:** [19 January 2023 19:29 UTC](https://discourse.nodered.org/t/python-function-return-variable/73928/2 "2023-01-19T19:29:32Z")

</div>

Hard to test without the required hardware.  
Function throws at least some error's so there's is happening something 😉  
Strip your code and see where it's going wrong.

---

<div class="post-metadata">

**Author:** ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)\
**Post date:** [19 January 2023 20:22 UTC](https://discourse.nodered.org/t/python-function-return-variable/73928/3 "2023-01-19T20:22:04Z")

</div>

If you installed node-red via the script, it gave you an option to install Pi specific nodes, one of which are the GPIO nodes if I am not mistaken, you can install them [separately](https://flows.nodered.org/node/node-red-node-pi-gpio) too. No need for external python scripts if you can do it all inside node-red.

---

<div class="post-metadata">

**Author:** ![superlogan72](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/superlogan72/32/74680_2.png) [@superlogan72](https://discourse.nodered.org/u/superlogan72)\
**Post date:** [20 January 2023 11:50 UTC](https://discourse.nodered.org/t/python-function-return-variable/73928/4 "2023-01-20T11:50:41Z")

</div>

i get no error, simply i never get the msg

---

<div class="post-metadata">

**Author:** ![superlogan72](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/superlogan72/32/74680_2.png) [@superlogan72](https://discourse.nodered.org/u/superlogan72)\
**Post date:** [20 January 2023 11:53 UTC](https://discourse.nodered.org/t/python-function-return-variable/73928/5 "2023-01-20T11:53:13Z")

</div>

i will try but i don't understand why if i run the script outside node-red i get the needed value, inside node-red i'm not able to get the needed value in the msg objects

---

<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:** [20 January 2023 14:15 UTC](https://discourse.nodered.org/t/python-function-return-variable/73928/6 "2023-01-20T14:15:17Z")

</div>

when you run python are you using the `-u` parameter to ensure it flushes any results to the output ?

---

<div class="post-metadata">

**Author:** ![superlogan72](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/superlogan72/32/74680_2.png) [@superlogan72](https://discourse.nodered.org/u/superlogan72)\
**Post date:** [20 January 2023 17:48 UTC](https://discourse.nodered.org/t/python-function-return-variable/73928/7 "2023-01-20T17:48:59Z")

</div>

no i never put the -u, where i have to put it ?

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/3/a/3a7d28a05756baeca884f14d2fa1ebfae7b21438.png)

this is what i see when i run the flow but i have no output

---

<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:** [20 January 2023 19:06 UTC](https://discourse.nodered.org/t/python-function-return-variable/73928/8 "2023-01-20T19:06:24Z")

</div>

Ah sorry didn’t spot you were using a custom node. I usually just call python -u from an exec node

---

<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:** [20 January 2023 22:15 UTC](https://discourse.nodered.org/t/python-function-return-variable/73928/9 "2023-01-20T22:15:16Z")

</div>

I remember that others have had problems using that node, as Dave suggests, you might be better using an exec node to call your script. If you do that make sure you put the full path to the script in the exec node.

---

<div class="post-metadata">

**Author:** ![superlogan72](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/superlogan72/32/74680_2.png) [@superlogan72](https://discourse.nodered.org/u/superlogan72)\
**Post date:** [23 January 2023 17:25 UTC](https://discourse.nodered.org/t/python-function-return-variable/73928/10 "2023-01-23T17:25:21Z")

</div>

Hello  
Now i'm in this situation but no output:  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/3/8/3862f755a8c55e95b414234b07cfed97eaaa3a8c.png)

inject

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/0/7/0730cf4e2695b26d7b6051e5ddbf11a17a9bed75.png)

exec

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/3/1/315d11ea062bf2928ca48df3b71d992e7b4d29c1.png)

Debug

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/4/9/4928147bd922c4e653814c496b6a96aeda7cc3e1.png)

Python code

```auto
import RPi.GPIO as GPIO
from time import sleep
 
counter = 10
 
Enc_A = 13  
Enc_B = 12  
 
 
def init():
    print("Rotary Encoder Test Program")
    GPIO.setwarnings(True)
    GPIO.setmode(GPIO.BCM)
    GPIO.setup(Enc_A, GPIO.IN)
    GPIO.setup(Enc_B, GPIO.IN)
    GPIO.add_event_detect(Enc_A, GPIO.RISING, callback=rotation_decode, bouncetime=10)
    return
 
 
def rotation_decode(Enc_A):
    global counter
    sleep(0.002)
    Switch_A = GPIO.input(Enc_A)
    Switch_B = GPIO.input(Enc_B)
 
    if (Switch_A == 1) and (Switch_B == 0):
        counter += 1
        print("direction -> ", counter)
        while Switch_B == 0:
            Switch_B = GPIO.input(Enc_B)
        while Switch_B == 1:
            Switch_B = GPIO.input(Enc_B)
        return
 
    elif (Switch_A == 1) and (Switch_B == 1):
        counter -= 1
        print("direction <- ", counter)
        while Switch_A == 1:
            Switch_A = GPIO.input(Enc_A)
        return
    else:
        return
def main():
    try:
        init()
        while(True) :
            sleep(1)
    except KeyboardInterrupt:
        GPIO.cleanup()
 
if __name__ == ' __main__':
    main()

```

I really don't know why i never see any value in the debug.

---

<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:** [23 January 2023 21:14 UTC](https://discourse.nodered.org/t/python-function-return-variable/73928/11 "2023-01-23T21:14:28Z")

</div>

Check all outputs of the exec node, set the debug nodes to show the full message.

Add some debug output to your script to see what is happening and what is not happening.

---

<div class="post-metadata">

**Author:** ![superlogan72](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/superlogan72/32/74680_2.png) [@superlogan72](https://discourse.nodered.org/u/superlogan72)\
**Post date:** [25 January 2023 11:31 UTC](https://discourse.nodered.org/t/python-function-return-variable/73928/12 "2023-01-25T11:31:17Z")

</div>

hello i start from easy test just for understand the problem  
if i do so:

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/a/7/a74bf305c477ec753258636696a7a19c6484c97a.png)  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/d/7/d761a8e8794729cd8b69a90391076681fed72cc3.png)

as you can see in the debug i have the return value  
but how can do if the return value is inside a def  
for example:

```auto
def test()
    msg = 'test'
   return test
while True:
    test()

```

it run but i don't have any return value

---

<div class="post-metadata">

**Author:** ![superlogan72](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/superlogan72/32/74680_2.png) [@superlogan72](https://discourse.nodered.org/u/superlogan72)\
**Post date:** [25 January 2023 16:26 UTC](https://discourse.nodered.org/t/python-function-return-variable/73928/13 "2023-01-25T16:26:55Z")

</div>

i have solved this issue doing this:

```auto
global pippo
def test():
    global pippo
    pippo = 'test'
    return pippo
while True:
    test()
    msg['payload'] = {'pippo':pippo}
    return msg

```

but i don't understand why in node-red i have the msg one time and not repeated.  
But my big problem is this:

```auto
from pigpio_encoder.rotary import Rotary
global my_counter
my_counter = 0

def sw_short():
    pass

def up_callback(counter):
    global my_counter
    if counter%40==0:
        my_counter+=1
        print(my_counter)
        return my_counter

def down_callback(counter):
    global my_counter
    if counter%40==0:
        my_counter-=1
        print(my_counter)
        return my_counter
    

my_rotary = Rotary(clk_gpio=13, dt_gpio=12,sw_gpio = 17)
my_rotary.setup_rotary( min=-10000,
                        max=10000,
                        debounce = 1,
                        up_callback=up_callback,
                        down_callback=down_callback,)
                    #rotary_callback=rotary_callback,
my_rotary.setup_switch(sw_short_callback=sw_short)
msg['payload'] = {'my_counter':my_counter}
return msg

```

Always return  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/9/e/9e85d042b7e28f91a71dddc40fa218fdcf93aab6.png)  
it return my\_counter = 0 because he get the value from the beginning but not when the value change  
can you tell me where is my mistake  
Thanks

---

<div class="post-metadata">

**Author:** ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)\
**Post date:** [25 January 2023 16:48 UTC](https://discourse.nodered.org/t/python-function-return-variable/73928/14 "2023-01-25T16:48:19Z")

</div>

I am not at home in python, but aren't variables global by default if they are defined outside a function ? ie. can you remove all the "global" definitions ?

---

<div class="post-metadata">

**Author:** ![superlogan72](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/superlogan72/32/74680_2.png) [@superlogan72](https://discourse.nodered.org/u/superlogan72)\
**Post date:** [26 January 2023 15:16 UTC](https://discourse.nodered.org/t/python-function-return-variable/73928/15 "2023-01-26T15:16:18Z")

</div>

sorry guys but i really don't understand:

```auto
from pigpio_encoder.rotary import Rotary
global my_counter
my_counter = 0

def sw_short():
    pass

def up_callback(counter):
    global my_counter
    if counter%40==0:
        my_counter+=1
        print(my_counter)
        msg['payload'] = {'my_counter':my_counter}
        return msg

def down_callback(counter):
    global my_counter
    if counter%40==0:
        my_counter-=1
        print(my_counter)
        msg['payload'] = {'my_counter':my_counter}
        return msg
    

my_rotary = Rotary(clk_gpio=13, dt_gpio=12,sw_gpio = 17)
my_rotary.setup_rotary( min=-10000,
                        max=10000,
                        debounce = 1,
                        up_callback=up_callback,
                        down_callback=down_callback,)
                    #rotary_callback=rotary_callback,
my_rotary.setup_switch(sw_short_callback=sw_short)
msg['payload'] = {'my_counter':my_counter}
return msg

```

i get only the first msg object with my\_counter = 0  
then when i move the encoder in the node-red shell i get all the print message :  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/5/1/5149dfca144c6ad3638cfa3cc9e4921a622d8325.png)  
but i get no return object.

dear bakman2 in node-red if i never set as global the my\_counter variable i get:  
NameError: name 'my\_counter' is not defined

Any help will be appreciated

---

<div class="post-metadata">

**Author:** ![OliverWieland](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/oliverwieland/32/74937_2.png) [@OliverWieland](https://discourse.nodered.org/u/OliverWieland)\
**Post date:** [26 January 2023 15:54 UTC](https://discourse.nodered.org/t/python-function-return-variable/73928/16 "2023-01-26T15:54:44Z")

</div>

Your script does the following:

- set your counter variable to the value of 0
- create an instance of Rotary and set a few properties of it
- set your msg['payload'] to a value
- return this msg

After the return command the script is terminated because there are no more commands. Therefore you get once the value of msg.

The two function (the callback functions for the Roraty object) will never have been called, because the script is finished before the Rotary object can fire any event.

---

<div class="post-metadata">

**Author:** ![superlogan72](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/superlogan72/32/74680_2.png) [@superlogan72](https://discourse.nodered.org/u/superlogan72)\
**Post date:** [26 January 2023 15:54 UTC](https://discourse.nodered.org/t/python-function-return-variable/73928/17 "2023-01-26T15:54:58Z")

</div>

I have solved in this way :

```auto
from pigpio_encoder.rotary import Rotary
global my_counter
my_counter = 0

def sw_short():
    pass

def up_callback(counter):
    global my_counter
    if counter%40==0:
        my_counter+=1
        #print(my_counter)
        msg['payload'] = {'my_counter':my_counter}
        #return msg
        node.send(msg)

def down_callback(counter):
    global my_counter
    if counter%40==0:
        my_counter-=1
        #print(my_counter)
        msg['payload'] = {'my_counter':my_counter}
        node.send(msg)
    

my_rotary = Rotary(clk_gpio=13, dt_gpio=12,sw_gpio = 17)
my_rotary.setup_rotary( min=-10000,
                        max=10000,
                        debounce = 1,
                        up_callback=up_callback,
                        down_callback=down_callback,)
                    #rotary_callback=rotary_callback,
my_rotary.setup_switch(sw_short_callback=sw_short)
#msg['payload'] = {'my_counter':my_counter}
#return msg

```

Regards

---

<div class="post-metadata">

**Author:** ![superlogan72](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/superlogan72/32/74680_2.png) [@superlogan72](https://discourse.nodered.org/u/superlogan72)\
**Post date:** [26 January 2023 15:57 UTC](https://discourse.nodered.org/t/python-function-return-variable/73928/18 "2023-01-26T15:57:41Z")

</div>

Hello Oliver  
the lib used pigpio do in backgroud the work (treading etc...) but i have solved usin the nod.sed(msg)  
regards

---

<div class="post-metadata">

**Author:** ![OliverWieland](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/oliverwieland/32/74937_2.png) [@OliverWieland](https://discourse.nodered.org/u/OliverWieland)\
**Post date:** [26 January 2023 16:02 UTC](https://discourse.nodered.org/t/python-function-return-variable/73928/19 "2023-01-26T16:02:48Z")

</div>

Okay, I see.  
BTW: your problem was the fact, that callback functions cannot return anything by definition. So your solution is the right way.

---

<div class="post-metadata">

**Author:** ![cymplecy](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cymplecy/32/2773_2.png) [@cymplecy](https://discourse.nodered.org/u/cymplecy)\
**Post date:** [26 January 2023 16:06 UTC](https://discourse.nodered.org/t/python-function-return-variable/73928/20 "2023-01-26T16:06:31Z")

</div>

Just linking in case you find this easier

> [@Using rotary encoder](https://discourse.nodered.org/t/using-rotary-encoder/29775/13):
>
> More compact version using a function node [{"id":"2c40d42d.176cbc","type":"debug","z":"dfaf687e.30fb88","name":"STATUS\_FUNCTION","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","statusVal":"payload","statusType":"auto","x":680,"y":540,"wires":[]},{"id":"e2775f3d.af2bd","type":"change","z":"dfaf687e.30fb88","name":"","rules":[{"t":"set","p":"topic","pt":"msg","to":"CLK","tot":"str"}],"action":"","property":"","from":"","to":"","re…

[Next page](https://discourse.nodered.org/t/python-function-return-variable/73928.md?page=2)
