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

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

NR_node
NR_node-setup

NR_debug

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?

image

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

You need

{ [topic]: power }

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

Thanks! Yes it's working the way you mentioned! :wink: :+1:

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 :slight_smile:

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

PoC:

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

@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?

It's not that complicated. Most nodes have - as you might have noticed - an integrated documentation of their capabilities.

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... :wink:

Thank you for the dokuemnation link.