# Python function and variable

**URL:** <https://discourse.nodered.org/t/python-function-and-variable/56227>\
**Category:** General\
**Created:** [6 January 2022 22:05 UTC](https://discourse.nodered.org/t/python-function-and-variable/56227 "2022-01-06T22:05:55Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nefedor](https://avatars.discourse-cdn.com/v4/letter/n/e8c25b/32.png) [@Nefedor](https://discourse.nodered.org/u/Nefedor)\
**Post date:** [6 January 2022 22:05 UTC](https://discourse.nodered.org/t/python-function-and-variable/56227/1 "2022-01-06T22:05:55Z")

</div>

I can't get into the condition. I've been all over google for a few days and this forum getting nowhere.  
I ask for help

```auto
[
    {
        "id": "5c0ff699943e1519",
        "type": "inject",
        "z": "d4722e6e2c1da3d1",
        "name": "",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "on",
        "payloadType": "str",
        "x": 170,
        "y": 800,
        "wires": [
            [
                "562d0bd05b1c3589"
            ]
        ]
    },
    {
        "id": "562d0bd05b1c3589",
        "type": "python-function",
        "z": "d4722e6e2c1da3d1",
        "name": "",
        "func": "node.warn (msg);\n\nif msg == \"on\":\n return msg \nelif msg == \"off\":\n return msg \nelse:\n pass",
        "outputs": 1,
        "x": 360,
        "y": 800,
        "wires": [
            [
                "c19a6341f71a49c3"
            ]
        ]
    },
    {
        "id": "c19a6341f71a49c3",
        "type": "debug",
        "z": "d4722e6e2c1da3d1",
        "name": "",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "false",
        "statusVal": "",
        "statusType": "auto",
        "x": 550,
        "y": 800,
        "wires": []
    }
]

```

---

<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:** [6 January 2022 22:36 UTC](https://discourse.nodered.org/t/python-function-and-variable/56227/2 "2022-01-06T22:36:54Z")

</div>

It would be helpful if you would describe what is happening and what you think should be happening. No one here has your python code or knows what you are trying to do or what nodes you are using.

What are you trying to accomplish?

---

<div class="post-metadata">

**Author:** ![Nefedor](https://avatars.discourse-cdn.com/v4/letter/n/e8c25b/32.png) [@Nefedor](https://discourse.nodered.org/u/Nefedor)\
**Post date:** [6 January 2022 23:03 UTC](https://discourse.nodered.org/t/python-function-and-variable/56227/3 "2022-01-06T23:03:01Z")

</div>

The whole example in JSON. Please, try  
How to transfer payload to condition

---

<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:** [6 January 2022 23:55 UTC](https://discourse.nodered.org/t/python-function-and-variable/56227/4 "2022-01-06T23:55:10Z")

</div>

I am only guessing since I do not have that node installed.

```auto
if msg == \"on\":
      return msg
    elif msg == \"off\":
      return msg
   else:
     pass

```

This does not look right to me.  
[https://github.com/arnauorriols/node-red-contrib-python-function](https://github.com/arnauorriols/node-red-contrib-python-function) says "The msg is a dictionary"  
So shouldn't you have something like this?

```auto
if (msg["payload"] == "ON") :
...

```

---

<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:** [7 January 2022 01:44 UTC](https://discourse.nodered.org/t/python-function-and-variable/56227/5 "2022-01-07T01:44:31Z")

</div>

Why not use the `switch` node?

---

<div class="post-metadata">

**Author:** ![Nefedor](https://avatars.discourse-cdn.com/v4/letter/n/e8c25b/32.png) [@Nefedor](https://discourse.nodered.org/u/Nefedor)\
**Post date:** [7 January 2022 09:16 UTC](https://discourse.nodered.org/t/python-function-and-variable/56227/6 "2022-01-07T09:16:40Z")

</div>

If you use a switch node, you need to split the python code into logical blocks. The problem was solved by adding a ballast output `return msg` at the end of the function

---

<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:** [7 January 2022 09:24 UTC](https://discourse.nodered.org/t/python-function-and-variable/56227/7 "2022-01-07T09:24:54Z")

</div>

Unless your python node is doing a lot more, I still don't see the need for it where you could use the switch node. For example:

 ![Screen Shot 2022-01-07 at 4.24.33 AM](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/6/7/67a826dc7b9245ac7584c5adbf44b722d1f7b7d0.png)

```auto
[{"id":"1a108fbbd82635c6","type":"inject","z":"4fcca17531bae1f4","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"on","payloadType":"str","x":190,"y":260,"wires":[["ac3446e7be7c8432"]]},{"id":"a5bc9af12ef0bf21","type":"debug","z":"4fcca17531bae1f4","name":"On","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":630,"y":260,"wires":[]},{"id":"bcb87622e0cdf7d1","type":"inject","z":"4fcca17531bae1f4","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"off","payloadType":"str","x":190,"y":300,"wires":[["ac3446e7be7c8432"]]},{"id":"ac3446e7be7c8432","type":"switch","z":"4fcca17531bae1f4","name":"","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"on","vt":"str"},{"t":"eq","v":"off","vt":"str"},{"t":"else"}],"checkall":"true","repair":false,"outputs":3,"x":410,"y":300,"wires":[["a5bc9af12ef0bf21"],["5b90ed8bc015325f"],["950610d1b2e31e30"]]},{"id":"5b90ed8bc015325f","type":"debug","z":"4fcca17531bae1f4","name":"Off","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":630,"y":300,"wires":[]},{"id":"950610d1b2e31e30","type":"debug","z":"4fcca17531bae1f4","name":"Other","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":630,"y":340,"wires":[]},{"id":"c93e2a6ba8482363","type":"inject","z":"4fcca17531bae1f4","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"fubar","payloadType":"str","x":190,"y":340,"wires":[["ac3446e7be7c8432"]]}]

```

---

<div class="post-metadata">

**Author:** ![Nefedor](https://avatars.discourse-cdn.com/v4/letter/n/e8c25b/32.png) [@Nefedor](https://discourse.nodered.org/u/Nefedor)\
**Post date:** [7 January 2022 09:35 UTC](https://discourse.nodered.org/t/python-function-and-variable/56227/8 "2022-01-07T09:35:00Z")

</div>

Python node works with subprocess library

---

<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:** [7 January 2022 10:45 UTC](https://discourse.nodered.org/t/python-function-and-variable/56227/9 "2022-01-07T10:45:42Z")

</div>

what subprocess library? That is a new piece of information that should have been in your original post.

But if your issue is solved, please mark the post that solved it so the thread will close.

---

<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:** [21 January 2022 10:45 UTC](https://discourse.nodered.org/t/python-function-and-variable/56227/10 "2022-01-21T10:45:55Z")

</div>

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