Adding payload into JSON object?

hi all... im trying to put a number being output from a smooth node into a JSON object.. here is the JSON object

{
"inputName": "Tempo",
"inputSettings": {
"text": "hello test"
}
}

the number being output from the smooth node is a tempo calculation as msg.payload.. i want to put that tempo value in the text field instead of the words "hello test".. i have tried various things such as

"text": "{{msg.payload}}"

but when i do that it doesnt store the payload value just the words {{msg.payload}}.. everything i try seems to just store the text not the payload value... anyone know where im going wrong ?

Does it work if you get rid of the quotes and mustache braces?

Or in a function node jsonObject.inputSettings.text = msg.payload

nothing seems to work when changing mustache or quotes around.. it remains formatted as text and not the payload value..

I tried the function node also and didnt seem to work ?

my flow runs out of the smooth node into a change node to set flow.tempo with the JSON.. the aim is to store the tempo data in flow.tempo.. not sure if the change node might be affecting it ? i wouldn't have thought so...

It's impossible to advise without seeing your flow.

Please also use a debug node set to display the whole msg object to share the data as it enters your flow.

Export your code using the hamburger menu and paste it here using the </>.
Copy the value that your debug node shows, not a screen capture. Again use the </> button to post it here.

There’s a great page in the docs (Working with messages : Node-RED) that will explain how to use the debug panel to find the right path to any data item.

Pay particular attention to the part about the buttons that appear under your mouse pointer when you over hover a debug message property in the sidebar.

BX00Cy7yHi

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

``` 
   code goes here 
```

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

See this post for more details - How to share code or flow json

i'll paste the flow below... you'll see the function node i made which converts milliseconds to tempo value is pretty poor.. im not a coder just somebody who scrapes through..

im looking at the context data tab and refreshing it to see what got stored in tempo.flow, im not using a debug node for that.. hopefully this pastes ok..

[    
{
        "id": "e4753500b15548c2",
        "type": "tab",
        "label": "Flow 1",
        "disabled": false,
        "info": "",
        "env": []
    },
    {
        "id": "05b9acc0dd978661",
        "type": "inject",
        "z": "e4753500b15548c2",
        "name": "Tap Tempo",
        "props": [
            {
                "p": "payload"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "",
        "payloadType": "date",
        "x": 260,
        "y": 180,
        "wires": [
            [
                "36450e7311faa1ce"
            ]
        ]
    },
    {
        "id": "36450e7311faa1ce",
        "type": "interval-length",
        "z": "e4753500b15548c2",
        "format": "mills",
        "bytopic": false,
        "minimum": "",
        "maximum": "",
        "window": "",
        "timeout": false,
        "msgTimeout": "2",
        "minimumunit": "msecs",
        "maximumunit": "msecs",
        "windowunit": "msecs",
        "msgTimeoutUnit": "secs",
        "reset": true,
        "startup": false,
        "msgField": "interval",
        "timestampField": "timestamp",
        "repeatTimeout": false,
        "name": "",
        "x": 520,
        "y": 160,
        "wires": [
            [
                "faf41facc0237798"
            ],
            [
                "1369bdaf58d57c3d"
            ]
        ]
    },
    {
        "id": "1369bdaf58d57c3d",
        "type": "change",
        "z": "e4753500b15548c2",
        "name": "",
        "rules": [
            {
                "t": "set",
                "p": "reset",
                "pt": "msg",
                "to": "true",
                "tot": "bool"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 520,
        "y": 240,
        "wires": [
            [
                "36450e7311faa1ce"
            ]
        ]
    },
    {
        "id": "faf41facc0237798",
        "type": "function",
        "z": "e4753500b15548c2",
        "name": "Convert to Tempo",
        "func": "\nlet interval = msg.interval\nlet count = 0\nlet tempo = 0\n\n\ncount = 1-(interval / 1000);\ntempo = count*4*60;\n\n\nmsg.payload = tempo;\nreturn msg;",
        "outputs": 1,
        "timeout": 0,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 770,
        "y": 160,
        "wires": [
            [
                "b90ea8aa7e498f13"
            ]
        ]
    },
    {
        "id": "b90ea8aa7e498f13",
        "type": "smooth",
        "z": "e4753500b15548c2",
        "name": "",
        "property": "payload",
        "action": "mean",
        "count": "3",
        "round": "0",
        "mult": "single",
        "reduce": false,
        "x": 960,
        "y": 160,
        "wires": [
            [
                "825f905dfd692977",
                "14c74b69c4a5f12f"
            ]
        ]
    },
    {
        "id": "825f905dfd692977",
        "type": "change",
        "z": "e4753500b15548c2",
        "name": "",
        "rules": [
            {
                "t": "set",
                "p": "tempo",
                "pt": "flow",
                "to": "{\"inputName\":\"Text\",\"inputSettings\":{\"text\":\"{{msg.payload}}\"}}",
                "tot": "json"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 1160,
        "y": 160,
        "wires": [
            []
        ]
    },
    {
        "id": "14c74b69c4a5f12f",
        "type": "debug",
        "z": "e4753500b15548c2",
        "name": "debug 48",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "false",
        "statusVal": "",
        "statusType": "auto",
        "x": 1180,
        "y": 220,
        "wires": []
    }
]

Without any test data I can only guess.

In your change node "Set flow.tempo" you are trying to use mustache syntax in a json field.
I believe this is illegal though you enclosed {{msg.payload}} in double quotes which converts it to a legal but unwanted string literal, as you already discovered.

Probably the easiest approach is to use a function node
image

flow.set("flow.tempo", 
{
    "inputName":"Tempo",
    "inputSettings":{"text":msg.payload}}
)

awesome that worked.. only thing is its naming the context as "flow" is there anyway to call it tempo ? based on what you wrote it seems like it would call it tempo but its not ?

also is it possible to make it so inputSettings is the only "object" and the inputName is not embedded as another object also ? basically formatted the same as the change node does ? hope that makes sense..

appreciated for the help..

EDIT: no worries i think i worked it out.. i just changed it to

flow.set("tempo")

that seemed to fix both things i asked about :slight_smile: thanks again for the help..

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