Subflow - Automatic Naming of Subflow Node

Good Day Wise Oracles!

I am playing with a subflow, mainly to learn, and have come up with something that has me a bit stumped...

The example I am using is a "Math Thingy" ... it is very basic, simply define the 2 topics and how you want to handle them, add, subtract, multiply or divide...

Now here's what has me a bit clueless: If I select "Add" under the setup of the "node", I would like to prefix the name of the node with the text "Add:", automatically... ie if I name the node "Whatever", and I have selected the function Add, I would like the name of the node to reflect "Add: Whatever"

Here is the basic subflow with the test flow to accompany it:

[
    {
        "id": "29ec52db.e36e2e",
        "type": "subflow",
        "name": "Math Thingy",
        "info": "Topic1 and Topic2 -Topics Selectable via node setup.\nFunction - Function to perform",
        "category": "",
        "in": [
            {
                "x": 80,
                "y": 70,
                "wires": [
                    {
                        "id": "5fb41510.349814"
                    }
                ]
            }
        ],
        "out": [
            {
                "x": 310,
                "y": 100,
                "wires": [
                    {
                        "id": "5fb41510.349814",
                        "port": 1
                    }
                ]
            }
        ],
        "env": [
            {
                "name": "Topic1",
                "type": "str",
                "value": "A"
            },
            {
                "name": "Func",
                "type": "str",
                "value": "*",
                "ui": {
                    "label": {
                        "en-US": "Function"
                    },
                    "type": "select",
                    "opts": {
                        "opts": [
                            {
                                "l": {
                                    "en-US": "Multiply by"
                                },
                                "v": "*"
                            },
                            {
                                "l": {
                                    "en-US": "Add to"
                                },
                                "v": "+"
                            },
                            {
                                "l": {
                                    "en-US": "Divide by"
                                },
                                "v": "/"
                            },
                            {
                                "l": {
                                    "en-US": "Minus"
                                },
                                "v": "-"
                            }
                        ]
                    }
                }
            },
            {
                "name": "Topic2",
                "type": "str",
                "value": "B"
            }
        ],
        "meta": {},
        "color": "#DDAA99",
        "status": {
            "x": 310,
            "y": 50,
            "wires": [
                {
                    "id": "5fb41510.349814",
                    "port": 0
                }
            ]
        }
    },
    {
        "id": "5fb41510.349814",
        "type": "function",
        "z": "29ec52db.e36e2e",
        "name": "Math Thingy",
        "func": "T1Val = context.get(\"T1Val\");\nT2Val = context.get(\"T2Val\");\nTopic1 = env.get(\"Topic1\");\nTopic2 = env.get(\"Topic2\");\nFunc = env.get(\"Func\");\n\n/*Put Something in here to modify the name of the node\nTo reflect Add/Subtract/Multiply/Divide to give:\n    Divide: Name of Node\n*/\n\nif (msg.topic == Topic1){T1Val = msg.payload}\nif (msg.topic == Topic2){T2Val = msg.payload}\n\nif(isNaN(T1Val)) {T1Val = 0}\nif(isNaN(T2Val)) {T2Val = 0}\n\ncontext.set(\"T1Val\", T1Val);\ncontext.set(\"T2Val\", T2Val);\nif(Func == \"*\"){\n    Res = T1Val * T2Val;\n    msg.payload = Res;\n    msg.topic = \"Multiply\"}\n    \nif(Func == \"/\"){\n    if(T2Val != 0){\n        Res = T1Val / T2Val;\n        msg.payload = Res;\n        msg.topic = \"Divide\"}\n    else{\n        Res = \"Overflow\"\n        msg.payload = NaN;\n        msg.topic = \"Divide\"\n        }\n    }\n\nif(Func == \"+\"){\n    Res = T1Val + T2Val;\n    msg.payload = Res;\n    msg.topic = \"Add\"}\n\nif(Func == \"-\"){\n    Res = T1Val * T2Val;\n    msg.payload = Res;\n    msg.topic = \"Subtract\"}\n\nlet statusMsg = { text: Res };\nreturn [{payload:statusMsg}, msg ]\n",
        "outputs": 2,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 190,
        "y": 70,
        "wires": [
            [],
            []
        ]
    },
    {
        "id": "21777ffa.e110e",
        "type": "tab",
        "label": "Math Thingy Test",
        "disabled": false,
        "info": ""
    },
    {
        "id": "bd50a195.c58e38",
        "type": "subflow:29ec52db.e36e2e",
        "z": "21777ffa.e110e",
        "name": "Divide: Name of node",
        "env": [
            {
                "name": "Func",
                "value": "/",
                "type": "str"
            }
        ],
        "x": 270,
        "y": 120,
        "wires": [
            [
                "bd77c46f.0fa28"
            ]
        ]
    },
    {
        "id": "cba597af.1e17c",
        "type": "inject",
        "z": "21777ffa.e110e",
        "name": "",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "A",
        "payload": "100",
        "payloadType": "num",
        "x": 90,
        "y": 60,
        "wires": [
            [
                "bd50a195.c58e38"
            ]
        ]
    },
    {
        "id": "63aaf802.7280c8",
        "type": "inject",
        "z": "21777ffa.e110e",
        "name": "",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "B",
        "payload": "2",
        "payloadType": "num",
        "x": 90,
        "y": 140,
        "wires": [
            [
                "bd50a195.c58e38"
            ]
        ]
    },
    {
        "id": "bd77c46f.0fa28",
        "type": "debug",
        "z": "21777ffa.e110e",
        "name": "",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": true,
        "complete": "true",
        "targetType": "full",
        "statusVal": "payload",
        "statusType": "auto",
        "x": 440,
        "y": 120,
        "wires": []
    },
    {
        "id": "bf3fa5d8.ac4b78",
        "type": "inject",
        "z": "21777ffa.e110e",
        "name": "",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "B",
        "payload": "3",
        "payloadType": "num",
        "x": 90,
        "y": 180,
        "wires": [
            [
                "bd50a195.c58e38"
            ]
        ]
    },
    {
        "id": "cf202a26.ed8c9",
        "type": "inject",
        "z": "21777ffa.e110e",
        "name": "",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "B",
        "payload": "0",
        "payloadType": "num",
        "x": 90,
        "y": 100,
        "wires": [
            [
                "bd50a195.c58e38"
            ]
        ]
    }
]

(I hope I got the code insertion right, Apologies if I didn't!!)

Any hints would be appreciated immensely!

Tia
Ed

1 Like

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