# Function to a command

**URL:** <https://discourse.nodered.org/t/function-to-a-command/86784>\
**Category:** General\
**Created:** [26 March 2024 19:23 UTC](https://discourse.nodered.org/t/function-to-a-command/86784 "2024-03-26T19:23:23Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![madmax](https://avatars.discourse-cdn.com/v4/letter/m/8baadc/32.png) [@madmax](https://discourse.nodered.org/u/madmax)\
**Post date:** [26 March 2024 19:23 UTC](https://discourse.nodered.org/t/function-to-a-command/86784/1 "2024-03-26T19:23:23Z")

</div>

Hello!

Is it possible in Node Red to pass a function to a command that is then executed?

For example: msg.payload = msg.payload.1 + msg.payload.2

I hope someone can help me.

Thanks in advance.

---

<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:** [26 March 2024 19:28 UTC](https://discourse.nodered.org/t/function-to-a-command/86784/2 "2024-03-26T19:28:43Z")

</div>

Well yes, of course it is possible, provided that a single msg.payload has properties 1 and 2.

But if you are talking about two discrete messages then you will need to look at a Join node to combine them into one.

Unless you show us an example of your message.payload it is impossible to give definite advice.

---

<div class="post-metadata">

**Author:** ![madmax](https://avatars.discourse-cdn.com/v4/letter/m/8baadc/32.png) [@madmax](https://discourse.nodered.org/u/madmax)\
**Post date:** [26 March 2024 19:32 UTC](https://discourse.nodered.org/t/function-to-a-command/86784/3 "2024-03-26T19:32:36Z")

</div>

Hello!

Here please.

```auto
[
    {
        "id": "517ed4eac9dc7e2e",
        "type": "inject",
        "z": "e503d20f125dba49",
        "name": "",
        "props": [
            {
                "p": "topic",
                "vt": "str"
            },
            {
                "p": "payload.start",
                "v": "msg.payload = msg.payload.1 + msg.payload.2",
                "vt": "str"
            },
            {
                "p": "payload.1",
                "v": "10",
                "vt": "num"
            },
            {
                "p": "payload.2",
                "v": "5",
                "vt": "num"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "x": 210,
        "y": 280,
        "wires": [
            [
                "0e4ccf8e5d36cde0"
            ]
        ]
    },
    {
        "id": "6c2d9be5cd2ab4c7",
        "type": "debug",
        "z": "e503d20f125dba49",
        "name": "debug 5",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "false",
        "statusVal": "",
        "statusType": "auto",
        "x": 660,
        "y": 280,
        "wires": []
    },
    {
        "id": "0e4ccf8e5d36cde0",
        "type": "function",
        "z": "e503d20f125dba49",
        "name": "function 1",
        "func": "msg.payload = msg.payload.start;\nreturn msg;",
        "outputs": 1,
        "timeout": 0,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 380,
        "y": 280,
        "wires": [
            [
                "6c2d9be5cd2ab4c7"
            ]
        ]
    }
]

```

---

<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:** [26 March 2024 19:34 UTC](https://discourse.nodered.org/t/function-to-a-command/86784/4 "2024-03-26T19:34:02Z")

</div>

Yes, I see it now.

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

msg.payload.start is a string. It contains "msg.payload = msg.payload.1 + msg.payload.2"

msg.payload.1 and msg.payload.2 are numbers. If your function node was

```auto
msg.payload= msg.payload[1] + msg.payload[2]
return msg

```

then the output would be 15.  
You need to use square bracket notation because 1 and 2 are not alphabetical.  
If you had used msg.payload.a and msg.payload.b then msg.payload.a + msg.payload.b would work too.

---

<div class="post-metadata">

**Author:** ![madmax](https://avatars.discourse-cdn.com/v4/letter/m/8baadc/32.png) [@madmax](https://discourse.nodered.org/u/madmax)\
**Post date:** [26 March 2024 19:36 UTC](https://discourse.nodered.org/t/function-to-a-command/86784/5 "2024-03-26T19:36:18Z")

</div>

Hello!

I have uploaded the example.

---

<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:** [26 March 2024 19:49 UTC](https://discourse.nodered.org/t/function-to-a-command/86784/6 "2024-03-26T19:49:01Z")

</div>

I edited my post above to reply.

You might find the Template node slightly neater for setting msg properties than the inject node, like this

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

```auto
[{"id":"bd36e7c30c416c6d","type":"template","z":"11165ecbf36dadc8","name":"","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"{\n \"start\": \"This will give msg.payload three properties\",\n \"a\": 10,\n \"b\": 5\n}","output":"json","x":280,"y":420,"wires":[["01d173805bdf03f3"]]},{"id":"8c5398417bdd4b55","type":"inject","z":"11165ecbf36dadc8","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":120,"y":420,"wires":[["bd36e7c30c416c6d"]]},{"id":"48ed13bfdf237b56","type":"debug","z":"11165ecbf36dadc8","name":"debug 476","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":630,"y":420,"wires":[]},{"id":"01d173805bdf03f3","type":"function","z":"11165ecbf36dadc8","name":"function 58","func":"msg.payload = msg.payload.a + msg.payload.b\nreturn msg;","outputs":1,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":450,"y":420,"wires":[["48ed13bfdf237b56"]]}]

```

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [26 March 2024 20:13 UTC](https://discourse.nodered.org/t/function-to-a-command/86784/7 "2024-03-26T20:13:25Z")

</div>

You can use [eval()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/eval) to do this, BUT please be careful and make sure the input into eval() is sanitised as eval() can be used to inject code that you may not want run on your system  
example

```auto
[{"id":"517ed4eac9dc7e2e","type":"inject","z":"d1395164b4eec73e","name":"","props":[{"p":"topic","vt":"str"},{"p":"payload.start","v":"msg.payload = msg.payload[\"1\"] + msg.payload[\"2\"]","vt":"str"},{"p":"payload[\"1\"]","v":"10","vt":"num"},{"p":"payload[\"2\"]","v":"5","vt":"num"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":170,"y":7680,"wires":[["0e4ccf8e5d36cde0"]]},{"id":"0e4ccf8e5d36cde0","type":"function","z":"d1395164b4eec73e","name":"function 1","func":"msg.payload = eval(msg.payload.start);\nreturn msg;","outputs":1,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":340,"y":7680,"wires":[["6c2d9be5cd2ab4c7"]]},{"id":"6c2d9be5cd2ab4c7","type":"debug","z":"d1395164b4eec73e","name":"debug 5","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":620,"y":7680,"wires":[]}]

```

> **[eval() isn't evil, just misunderstood - Human Who Codes](https://humanwhocodes.com/blog/2013/06/25/eval-isnt-evil-just-misunderstood/)**
>
> The Official Web Site of Nicholas C. Zakas

> **[Reasons Why You Should Never Use eval() in JavaScript | DigitalOcean](https://www.digitalocean.com/community/tutorials/js-eval)**
>
> You may have never heard of the eval function in JavaScript, and that’s probably for a good reason! It should almost never be used and here we explain why.

p.s. When using numbers as property names it is better to use square bracket notation, as JS number are not really correct syntax

---

<div class="post-metadata">

**Author:** ![madmax](https://avatars.discourse-cdn.com/v4/letter/m/8baadc/32.png) [@madmax](https://discourse.nodered.org/u/madmax)\
**Post date:** [27 March 2024 14:13 UTC](https://discourse.nodered.org/t/function-to-a-command/86784/9 "2024-03-27T14:13:40Z")

</div>

Hello!

Many thanks for the answer.

That's exactly what I was looking for.

---

<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:** [25 June 2024 14:14 UTC](https://discourse.nodered.org/t/function-to-a-command/86784/10 "2024-06-25T14:14:28Z")

</div>

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