Python function and variable

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

[
    {
        "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": []
    }
]

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?

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

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

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 says "The msg is a dictionary"
So shouldn't you have something like this?

if (msg["payload"] == "ON") :
...
1 Like

Why not use the switch node?

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

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:

[{"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"]]}]
1 Like

Python node works with subprocess library

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.

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