# How to write inject in a function

**URL:** <https://discourse.nodered.org/t/how-to-write-inject-in-a-function/82091>\
**Category:** General\
**Created:** [16 October 2023 20:06 UTC](https://discourse.nodered.org/t/how-to-write-inject-in-a-function/82091 "2023-10-16T20:06:25Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![wburst](https://avatars.discourse-cdn.com/v4/letter/w/7feea3/32.png) [@wburst](https://discourse.nodered.org/u/wburst)\
**Post date:** [16 October 2023 20:06 UTC](https://discourse.nodered.org/t/how-to-write-inject-in-a-function/82091/1 "2023-10-16T20:06:25Z")

</div>

can someone tel me ho i can write this inject in a function ?

 ![nodeRed](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/f/c/fc7dab7850c5c7b1539cf418f5f3856fbeb505ba.jpeg)

---

<div class="post-metadata">

**Author:** ![marcus-j-davies](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/marcus-j-davies/32/103435_2.png) [@marcus-j-davies](https://discourse.nodered.org/u/marcus-j-davies)\
**Post date:** [16 October 2023 20:27 UTC](https://discourse.nodered.org/t/how-to-write-inject-in-a-function/82091/2 "2023-10-16T20:27:52Z")

</div>

```auto
msg.payload = {
    mutiple: true,
    data: {
        "20": true,
        "24": "00da03e803e8"
    }
}
return msg

```

---

<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:** [16 October 2023 20:27 UTC](https://discourse.nodered.org/t/how-to-write-inject-in-a-function/82091/3 "2023-10-16T20:27:59Z")

</div>

```auto
msg.payload = {
    multiple: true,
    data: {
        "20": true,
        "24": "00da03e803e8"
    }
}
return msg;

```

---

<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:** [16 October 2023 20:28 UTC](https://discourse.nodered.org/t/how-to-write-inject-in-a-function/82091/4 "2023-10-16T20:28:46Z")

</div>

Wow, we even got the detailed layout identical.

---

<div class="post-metadata">

**Author:** ![marcus-j-davies](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/marcus-j-davies/32/103435_2.png) [@marcus-j-davies](https://discourse.nodered.org/u/marcus-j-davies)\
**Post date:** [16 October 2023 20:29 UTC](https://discourse.nodered.org/t/how-to-write-inject-in-a-function/82091/5 "2023-10-16T20:29:30Z")

</div>

I laughed my head off! literally on the second of each post 😂

But mine has a mistake - can you spot? 🤓

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [16 October 2023 20:32 UTC](https://discourse.nodered.org/t/how-to-write-inject-in-a-function/82091/6 "2023-10-16T20:32:55Z")

</div>

> [@marcus-j-davies](#):
>
> But mine has a mistake - can you spot

Semicolon is not required 😂

But yes your spelling is atrocious 🫣

---

<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:** [16 October 2023 20:35 UTC](https://discourse.nodered.org/t/how-to-write-inject-in-a-function/82091/7 "2023-10-16T20:35:34Z")

</div>

Did you build the inject node and send it to a debug node to check what it would produce?  
I wasn't sure whether the `.20` notation would build an array or an object.

---

<div class="post-metadata">

**Author:** ![wburst](https://avatars.discourse-cdn.com/v4/letter/w/7feea3/32.png) [@wburst](https://discourse.nodered.org/u/wburst)\
**Post date:** [16 October 2023 20:51 UTC](https://discourse.nodered.org/t/how-to-write-inject-in-a-function/82091/8 "2023-10-16T20:51:08Z")

</div>

my problem is when i press button1 it does not the same as button2

 ![nodeRed1](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/c/8/c82e07d0edcfdd90007dfb3592dacf12e90f2969.jpeg)  
 ![nodeRed_button1](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/f/e/fe86672f09c9edcccd88b295ef8321f95cadf78e.jpeg)  
 ![nodeRed_button2](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/d/c/dc68dc4f7d3b1fae6fbaa71528c4f8631b7547e0.jpeg)

---

<div class="post-metadata">

**Author:** ![wburst](https://avatars.discourse-cdn.com/v4/letter/w/7feea3/32.png) [@wburst](https://discourse.nodered.org/u/wburst)\
**Post date:** [16 October 2023 20:51 UTC](https://discourse.nodered.org/t/how-to-write-inject-in-a-function/82091/9 "2023-10-16T20:51:55Z")

</div>

my flow

```auto
[
    {
        "id": "157466c68314f327",
        "type": "tab",
        "label": "Flow 2",
        "disabled": false,
        "info": "",
        "env": []
    },
    {
        "id": "115ada30.1743c6",
        "type": "tuya-local",
        "z": "157466c68314f327",
        "devName": "Test2023",
        "devIp": "192.168.16.214",
        "devId": "bf97ca67f3fbd97b02suxw",
        "devKey": "*TgS)JB5[MQYHi=}",
        "protocolVer": "3.3",
        "renameSchema": "",
        "filterCB": "",
        "x": 760,
        "y": 360,
        "wires": [
            [
                "88349073.f9906"
            ]
        ]
    },
    {
        "id": "88349073.f9906",
        "type": "debug",
        "z": "157466c68314f327",
        "name": "",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "true",
        "targetType": "full",
        "x": 990,
        "y": 360,
        "wires": []
    },
    {
        "id": "797d431a.95651c",
        "type": "inject",
        "z": "157466c68314f327",
        "name": "{\"set\": true, \"dps\" : 20}",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "{\"set\": true, \"dps\" : 20}",
        "payloadType": "json",
        "x": 240,
        "y": 380,
        "wires": [
            [
                "115ada30.1743c6"
            ]
        ]
    },
    {
        "id": "e23704b.8e613f8",
        "type": "inject",
        "z": "157466c68314f327",
        "name": "{\"set\": false, \"dps\" : 20}",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "{\"set\": false, \"dps\" : 20}",
        "payloadType": "json",
        "x": 240,
        "y": 340,
        "wires": [
            [
                "115ada30.1743c6"
            ]
        ]
    },
    {
        "id": "5953c975c9b8758c",
        "type": "inject",
        "z": "157466c68314f327",
        "name": "button2",
        "props": [
            {
                "p": "payload.multiple",
                "v": "true",
                "vt": "bool"
            },
            {
                "p": "payload.data.20",
                "v": "true",
                "vt": "bool"
            },
            {
                "p": "payload.data.24",
                "v": "00da03e803e8",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "x": 250,
        "y": 540,
        "wires": [
            [
                "115ada30.1743c6",
                "0dd461832a677556"
            ]
        ]
    },
    {
        "id": "0dd461832a677556",
        "type": "debug",
        "z": "157466c68314f327",
        "name": "debug 5",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "false",
        "statusVal": "",
        "statusType": "auto",
        "x": 980,
        "y": 540,
        "wires": []
    },
    {
        "id": "2f79a39ce040a1c0",
        "type": "function",
        "z": "157466c68314f327",
        "name": "function 2",
        "func": "msg.payload = {\n mutiple: true,\n data: {\n \"20\": true,\n \"24\": \"00da03e803e8\"\n }\n}\nreturn msg;",
        "outputs": 1,
        "timeout": 0,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 540,
        "y": 120,
        "wires": [
            [
                "115ada30.1743c6",
                "14e94e8765c4a5c6"
            ]
        ]
    },
    {
        "id": "14e94e8765c4a5c6",
        "type": "debug",
        "z": "157466c68314f327",
        "name": "debug 6",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "false",
        "statusVal": "",
        "statusType": "auto",
        "x": 980,
        "y": 120,
        "wires": []
    },
    {
        "id": "97bfeea14724561d",
        "type": "inject",
        "z": "157466c68314f327",
        "name": "button1",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "true",
        "payloadType": "bool",
        "x": 250,
        "y": 120,
        "wires": [
            [
                "2f79a39ce040a1c0"
            ]
        ]
    }
]

```

---

<div class="post-metadata">

**Author:** ![marcus-j-davies](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/marcus-j-davies/32/103435_2.png) [@marcus-j-davies](https://discourse.nodered.org/u/marcus-j-davies)\
**Post date:** [16 October 2023 21:03 UTC](https://discourse.nodered.org/t/how-to-write-inject-in-a-function/82091/10 "2023-10-16T21:03:24Z")

</div>

Mine and @Colin s are identical - only mine has a typo on the spelling of **multiple**

---

<div class="post-metadata">

**Author:** ![wburst](https://avatars.discourse-cdn.com/v4/letter/w/7feea3/32.png) [@wburst](https://discourse.nodered.org/u/wburst)\
**Post date:** [16 October 2023 21:06 UTC](https://discourse.nodered.org/t/how-to-write-inject-in-a-function/82091/11 "2023-10-16T21:06:16Z")

</div>

thats clear, but my problem is, i want press button1 (bolean) to start the funktion (which does not work)  
when is press button2 ist the same as my first picture ist does work.

Means as inject it works, as function it dosent work

---

<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:** [16 October 2023 21:13 UTC](https://discourse.nodered.org/t/how-to-write-inject-in-a-function/82091/12 "2023-10-16T21:13:07Z")

</div>

In order to make code readable and usable it is necessary to surround your code with three backticks (also known as a left quote or backquote `````)

````auto
``` 
   code goes here 
```

````

You can edit and correct your post by clicking the pencil ✏ icon.

See this post for more details - [How to share code or flow json](https://discourse.nodered.org/t/how-to-share-code-or-flow-json/506)

---

<div class="post-metadata">

**Author:** ![wburst](https://avatars.discourse-cdn.com/v4/letter/w/7feea3/32.png) [@wburst](https://discourse.nodered.org/u/wburst)\
**Post date:** [16 October 2023 21:15 UTC](https://discourse.nodered.org/t/how-to-write-inject-in-a-function/82091/13 "2023-10-16T21:15:20Z")

</div>

sorry for my misstake

```auto
[
    {
        "id": "157466c68314f327",
        "type": "tab",
        "label": "Flow 2",
        "disabled": false,
        "info": "",
        "env": []
    },
    {
        "id": "115ada30.1743c6",
        "type": "tuya-local",
        "z": "157466c68314f327",
        "devName": "Test2023",
        "devIp": "192.168.16.214",
        "devId": "bf97ca67f3fbd97b02suxw",
        "devKey": "*TgS)JB5[MQYHi=}",
        "protocolVer": "3.3",
        "renameSchema": "",
        "filterCB": "",
        "x": 760,
        "y": 360,
        "wires": [
            [
                "88349073.f9906"
            ]
        ]
    },
    {
        "id": "88349073.f9906",
        "type": "debug",
        "z": "157466c68314f327",
        "name": "",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "true",
        "targetType": "full",
        "x": 990,
        "y": 360,
        "wires": []
    },
    {
        "id": "797d431a.95651c",
        "type": "inject",
        "z": "157466c68314f327",
        "name": "{\"set\": true, \"dps\" : 20}",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "{\"set\": true, \"dps\" : 20}",
        "payloadType": "json",
        "x": 240,
        "y": 380,
        "wires": [
            [
                "115ada30.1743c6"
            ]
        ]
    },
    {
        "id": "e23704b.8e613f8",
        "type": "inject",
        "z": "157466c68314f327",
        "name": "{\"set\": false, \"dps\" : 20}",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "{\"set\": false, \"dps\" : 20}",
        "payloadType": "json",
        "x": 240,
        "y": 340,
        "wires": [
            [
                "115ada30.1743c6"
            ]
        ]
    },
    {
        "id": "5953c975c9b8758c",
        "type": "inject",
        "z": "157466c68314f327",
        "name": "button2",
        "props": [
            {
                "p": "payload.multiple",
                "v": "true",
                "vt": "bool"
            },
            {
                "p": "payload.data.20",
                "v": "true",
                "vt": "bool"
            },
            {
                "p": "payload.data.24",
                "v": "00da03e803e8",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "x": 250,
        "y": 540,
        "wires": [
            [
                "115ada30.1743c6",
                "0dd461832a677556"
            ]
        ]
    },
    {
        "id": "0dd461832a677556",
        "type": "debug",
        "z": "157466c68314f327",
        "name": "debug 5",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "false",
        "statusVal": "",
        "statusType": "auto",
        "x": 980,
        "y": 540,
        "wires": []
    },
    {
        "id": "2f79a39ce040a1c0",
        "type": "function",
        "z": "157466c68314f327",
        "name": "function 2",
        "func": "msg.payload = {\n mutiple: true,\n data: {\n \"20\": true,\n \"24\": \"00da03e803e8\"\n }\n}\nreturn msg;",
        "outputs": 1,
        "timeout": 0,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 540,
        "y": 120,
        "wires": [
            [
                "115ada30.1743c6",
                "9c6f8f60bd73240b"
            ]
        ]
    },
    {
        "id": "97bfeea14724561d",
        "type": "inject",
        "z": "157466c68314f327",
        "name": "button1",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "",
        "payloadType": "date",
        "x": 250,
        "y": 120,
        "wires": [
            [
                "2f79a39ce040a1c0"
            ]
        ]
    },
    {
        "id": "9c6f8f60bd73240b",
        "type": "debug",
        "z": "157466c68314f327",
        "name": "debug 6",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "false",
        "statusVal": "",
        "statusType": "auto",
        "x": 860,
        "y": 120,
        "wires": []
    }
]

```

---

<div class="post-metadata">

**Author:** ![wburst](https://avatars.discourse-cdn.com/v4/letter/w/7feea3/32.png) [@wburst](https://discourse.nodered.org/u/wburst)\
**Post date:** [16 October 2023 21:20 UTC](https://discourse.nodered.org/t/how-to-write-inject-in-a-function/82091/14 "2023-10-16T21:20:40Z")

</div>

Sorry was my mistake i wrote the word multiple wrong (copied wrong 🙂 )

---

<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:** [16 October 2023 21:20 UTC](https://discourse.nodered.org/t/how-to-write-inject-in-a-function/82091/15 "2023-10-16T21:20:47Z")

</div>

You have copied the typo error to the function.  
`mutiple: true,`

---

<div class="post-metadata">

**Author:** ![marcus-j-davies](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/marcus-j-davies/32/103435_2.png) [@marcus-j-davies](https://discourse.nodered.org/u/marcus-j-davies)\
**Post date:** [16 October 2023 21:22 UTC](https://discourse.nodered.org/t/how-to-write-inject-in-a-function/82091/16 "2023-10-16T21:22:02Z")

</div>

I used to like the word **multiple** - I suddenly don't!

---

<div class="post-metadata">

**Author:** ![wburst](https://avatars.discourse-cdn.com/v4/letter/w/7feea3/32.png) [@wburst](https://discourse.nodered.org/u/wburst)\
**Post date:** [16 October 2023 21:22 UTC](https://discourse.nodered.org/t/how-to-write-inject-in-a-function/82091/17 "2023-10-16T21:22:58Z")

</div>

yes thank you. and now my second question:

how can i create a variable for "00da03e803e8" to input another value

---

<div class="post-metadata">

**Author:** ![marcus-j-davies](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/marcus-j-davies/32/103435_2.png) [@marcus-j-davies](https://discourse.nodered.org/u/marcus-j-davies)\
**Post date:** [16 October 2023 21:30 UTC](https://discourse.nodered.org/t/how-to-write-inject-in-a-function/82091/18 "2023-10-16T21:30:47Z")

</div>

If you mean within the function it's self..

```auto
let someId = "00da03e803e8"

msg.payload = {
    multiple: true,
    data: {
        "20": true,
        "24": someId
    }
}
return msg;

```

Of if you want to set it elsewhere

1. Use a change node, to set a `flow` variable

```auto
[{"id":"51948002f737e19e","type":"change","z":"157466c68314f327","name":"","rules":[{"t":"set","p":"someId","pt":"flow","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":425,"y":640,"wires":[[]]},{"id":"2b45bb0cd5f50fec","type":"inject","z":"157466c68314f327","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"00da03e803e8","payloadType":"str","x":225,"y":640,"wires":[["51948002f737e19e"]]}]

```

Then to grab it.

```auto
msg.payload = {
    multiple: true,
    data: {
        "20": true,
        "24": flow.get("someId")
    }
}
return msg;

```

---

<div class="post-metadata">

**Author:** ![wburst](https://avatars.discourse-cdn.com/v4/letter/w/7feea3/32.png) [@wburst](https://discourse.nodered.org/u/wburst)\
**Post date:** [16 October 2023 21:39 UTC](https://discourse.nodered.org/t/how-to-write-inject-in-a-function/82091/19 "2023-10-16T21:39:48Z")

</div>

You are my Hero this is exactly what i need.

thx a lot

---

<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:** [15 December 2023 21:40 UTC](https://discourse.nodered.org/t/how-to-write-inject-in-a-function/82091/20 "2023-12-15T21:40:38Z")

</div>

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