# NR/MQTT and node 'function' to build modified payload

**URL:** <https://discourse.nodered.org/t/nr-mqtt-and-node-function-to-build-modified-payload/73728>\
**Category:** General\
**Tags:** function-node, mqtt, javascript\
**Created:** [15 January 2023 20:48 UTC](https://discourse.nodered.org/t/nr-mqtt-and-node-function-to-build-modified-payload/73728 "2023-01-15T20:48:38Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![gNeandr](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gneandr/32/73947_2.png) [@gNeandr](https://discourse.nodered.org/u/gNeandr)\
**Post date:** [15 January 2023 20:48 UTC](https://discourse.nodered.org/t/nr-mqtt-and-node-function-to-build-modified-payload/73728/1 "2023-01-15T20:48:38Z")

</div>

Starting with NR is probably not as easy as I thought.  
While I have a (good) javascript background, I am at a loss here:  
**Task:**  
From a MQTT status query I get an object. From certain elements a new payload should be formed.  
The current approach is the following:

NR\_flow  
 ![NR_flow](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/d/f/df4de1ca09decbbea23b1562536c24c8de8becc5.png)

NR\_node  
 ![NR_node-setup](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/1/9/19dd088f214d960d4e284a43cd93a67f947dc2f7.png)

NR\_debug

 ![NR_debug](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/e/8/e8862d7b3dae4ede8ab3669f204dd90eb4438973.png)

**Result:**  
The debug shows as result an object: `{"topic":3}`.  
With the node JS code I would expect:` {"wz_ez" : 3 }`  
_Where is my error?_

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [15 January 2023 21:16 UTC](https://discourse.nodered.org/t/nr-mqtt-and-node-function-to-build-modified-payload/73728/2 "2023-01-15T21:16:02Z")

</div>

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/2/f/2fd792a1e26e04f35effaa5eb00bdb7448050f56.png)

That is ambiguous and probably doesn't quite do what you think. Well, actually, you've already seen that.

You need

```auto
{ [topic]: power }

```

Because JavaScript is rightly assuming that the property name is just that, a name, not a variable.

---

<div class="post-metadata">

**Author:** ![gNeandr](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gneandr/32/73947_2.png) [@gNeandr](https://discourse.nodered.org/u/gNeandr)\
**Post date:** [15 January 2023 21:23 UTC](https://discourse.nodered.org/t/nr-mqtt-and-node-function-to-build-modified-payload/73728/3 "2023-01-15T21:23:15Z")

</div>

Thanks! Yes it's working the way you mentioned! 😉 👍

---

<div class="post-metadata">

**Author:** ![cymplecy](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cymplecy/32/2773_2.png) [@cymplecy](https://discourse.nodered.org/u/cymplecy)\
**Post date:** [15 January 2023 22:15 UTC](https://discourse.nodered.org/t/nr-mqtt-and-node-function-to-build-modified-payload/73728/4 "2023-01-15T22:15:43Z")

</div>

One thing to learn about NodeRED is that is no need to write JavaScript to pick out an object value  
object form a payload

Two sequential change nodes can do the job for you without having to write any code

One to change the topic - one to change the payload

Have a play with the core nodes 🙂

---

<div class="post-metadata">

**Author:** ![ralphwetzel](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ralphwetzel/32/53713_2.png) [@ralphwetzel](https://discourse.nodered.org/u/ralphwetzel)\
**Post date:** [15 January 2023 23:30 UTC](https://discourse.nodered.org/t/nr-mqtt-and-node-function-to-build-modified-payload/73728/5 "2023-01-15T23:30:00Z")

</div>

> [@cymplecy](#):
>
> Two sequential change nodes can do the job

You don't even need two. One is enough:

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/5/3/53264b87c899eb48a3e1b8b62ed428b16dee5747.png)

PoC:

```json
[
    {
        "id": "c6758a93fb147227",
        "type": "change",
        "z": "22efc83c4ca67aba",
        "name": "",
        "rules": [
            {
                "t": "set",
                "p": "buffer",
                "pt": "msg",
                "to": "{}",
                "tot": "json"
            },
            {
                "t": "set",
                "p": "buffer[msg.payload.Status.Topic]",
                "pt": "msg",
                "to": "payload.Status.Power",
                "tot": "msg"
            },
            {
                "t": "set",
                "p": "payload",
                "pt": "msg",
                "to": "buffer",
                "tot": "msg"
            },
            {
                "t": "delete",
                "p": "buffer",
                "pt": "msg"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "_mcu": {
            "mcu": false
        },
        "x": 480,
        "y": 420,
        "wires": [
            [
                "1c1c5904834d9071"
            ]
        ]
    },
    {
        "id": "de0f99cdf1aeb480",
        "type": "inject",
        "z": "22efc83c4ca67aba",
        "name": "",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "payload.Status",
                "v": "{}",
                "vt": "json"
            },
            {
                "p": "payload.Status.Topic",
                "v": "wz_ez",
                "vt": "str"
            },
            {
                "p": "payload.Status.Power",
                "v": "3",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "{}",
        "payloadType": "json",
        "_mcu": {
            "mcu": false
        },
        "x": 330,
        "y": 420,
        "wires": [
            [
                "c6758a93fb147227"
            ]
        ]
    },
    {
        "id": "1c1c5904834d9071",
        "type": "debug",
        "z": "22efc83c4ca67aba",
        "name": "debug 81",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "false",
        "statusVal": "",
        "statusType": "auto",
        "_mcu": {
            "mcu": false
        },
        "x": 640,
        "y": 420,
        "wires": []
    }
]

```

---

<div class="post-metadata">

**Author:** ![gNeandr](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gneandr/32/73947_2.png) [@gNeandr](https://discourse.nodered.org/u/gNeandr)\
**Post date:** [15 January 2023 23:50 UTC](https://discourse.nodered.org/t/nr-mqtt-and-node-function-to-build-modified-payload/73728/6 "2023-01-15T23:50:00Z")

</div>

@cymplecy  
I finally ended up with 'function' and JS code because I didn't understood the behaviour of the 'change'.  
Maybe only because I can't find a good explanation. So thanks to ...  
... @ralphwetzel for his solution.  
Where can I find such tricks like using 'buffer' you used?

---

<div class="post-metadata">

**Author:** ![ralphwetzel](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ralphwetzel/32/53713_2.png) [@ralphwetzel](https://discourse.nodered.org/u/ralphwetzel)\
**Post date:** [16 January 2023 06:40 UTC](https://discourse.nodered.org/t/nr-mqtt-and-node-function-to-build-modified-payload/73728/7 "2023-01-16T06:40:37Z")

</div>

> [@gNeandr](#):
>
> Where can I find such tricks like using 'buffer' you used?

It's not that complicated. Most nodes have - as you might have noticed - an [integrated documentation](https://nodered.org/docs/user-guide/editor/sidebar/help) of their capabilities.

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

As you can see, the `Change` node provides means to change the properties of three objects: msg, global & flow. The rest is standard Javascript - with the constraint that you can't build another (main) object.

And then, there's the community here in the forum to support by patiently answering all upcoming questions... 😉

---

<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:** [17 March 2023 10:06 UTC](https://discourse.nodered.org/t/nr-mqtt-and-node-function-to-build-modified-payload/73728/9 "2023-03-17T10:06:54Z")

</div>

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