How to pass payload to JSON

I think this might be something easy. I have a payload coming in as a number and using a change node to change it to JSON. Been playing around with quotes and curly brackets, but it's just pulling the text in on my debug. Any ideas what i'm doing wrong?

{
    "method": "setPilot",
    "params": {
        "dimming": "{{payload}}"
    }
}

That would work in a template node (though you'd probably not want the quotes if you want it to be a number)

If you want to do it in a change node, then use JSONata OR copy the payload to a temporary msg property in the first row, set the payload to your format in the 2nd row then finally, in the 3rd row, set payload.params.dimming to the value of the _temp value from row 1.

@Steve-Mcl appreciate it, I was able to use the template node and use the JSON node to convert it. I'm not too familiar with how to do the second option, but hope to get there eventually.

No need for JSON node the template node can output a JSON string, or if you select output parsed JSON a Javascript object.

p.s share your solution so others can see the solutuion, export you working example flow and paste it, then your solution can help others.

1 Like

@E1cid I see that option and that's great! 1 less node not to have to use.

Here is my flow. It's still work in progress, but have a virtual light/bulb (Wiz) that I'm passing commands to dim/color/on and off.

[
    {
        "id": "77ccb529164df155",
        "type": "tab",
        "label": "Flow 1",
        "disabled": false,
        "info": "",
        "env": []
    },
    {
        "id": "766a83b7d19b8b74",
        "type": "alexa-smart-home-v3",
        "z": "77ccb529164df155",
        "conf": "c90ca88c.3bab68",
        "device": "60075",
        "acknowledge": true,
        "name": "Light Test",
        "topic": "",
        "x": 320,
        "y": 380,
        "wires": [
            [
                "7f36d38888653298",
                "628d0399d643e52f",
                "c31cacb32c23489e"
            ]
        ]
    },
    {
        "id": "252442223f8aab14",
        "type": "udp out",
        "z": "77ccb529164df155",
        "name": "Send",
        "addr": "",
        "iface": "",
        "port": "38899",
        "ipv": "udp4",
        "outport": "",
        "base64": false,
        "multicast": "false",
        "x": 1530,
        "y": 360,
        "wires": []
    },
    {
        "id": "3628125aa8a9575e",
        "type": "json",
        "z": "77ccb529164df155",
        "name": "",
        "property": "payload",
        "action": "",
        "pretty": false,
        "x": 1350,
        "y": 360,
        "wires": [
            [
                "252442223f8aab14"
            ]
        ]
    },
    {
        "id": "f92fc7811dde1c42",
        "type": "change",
        "z": "77ccb529164df155",
        "name": "",
        "rules": [
            {
                "t": "set",
                "p": "ip",
                "pt": "msg",
                "to": "192.168.107.74",
                "tot": "str"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 1150,
        "y": 360,
        "wires": [
            [
                "3628125aa8a9575e"
            ]
        ]
    },
    {
        "id": "c6d669a573d7e897",
        "type": "change",
        "z": "77ccb529164df155",
        "name": "on",
        "rules": [
            {
                "t": "set",
                "p": "payload",
                "pt": "msg",
                "to": "{     \"method\": \"setPilot\",     \"params\": {         \"state\": true     } }",
                "tot": "json"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 770,
        "y": 300,
        "wires": [
            [
                "f92fc7811dde1c42"
            ]
        ]
    },
    {
        "id": "7f36d38888653298",
        "type": "switch",
        "z": "77ccb529164df155",
        "name": "",
        "property": "payload",
        "propertyType": "msg",
        "rules": [
            {
                "t": "eq",
                "v": "ON",
                "vt": "str"
            },
            {
                "t": "eq",
                "v": "OFF",
                "vt": "str"
            },
            {
                "t": "cont",
                "v": "",
                "vt": "num"
            }
        ],
        "checkall": "true",
        "repair": false,
        "outputs": 3,
        "x": 550,
        "y": 380,
        "wires": [
            [
                "c6d669a573d7e897"
            ],
            [
                "c1651469f5119aeb"
            ],
            [
                "12dc22329943da81"
            ]
        ]
    },
    {
        "id": "c1651469f5119aeb",
        "type": "change",
        "z": "77ccb529164df155",
        "name": "off",
        "rules": [
            {
                "t": "set",
                "p": "payload",
                "pt": "msg",
                "to": "{\"method\":\"setPilot\",\"params\":{\"state\":false}}",
                "tot": "json"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 770,
        "y": 380,
        "wires": [
            [
                "f92fc7811dde1c42"
            ]
        ]
    },
    {
        "id": "12dc22329943da81",
        "type": "debug",
        "z": "77ccb529164df155",
        "name": "debug 64",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "false",
        "statusVal": "",
        "statusType": "auto",
        "x": 750,
        "y": 440,
        "wires": []
    },
    {
        "id": "628d0399d643e52f",
        "type": "debug",
        "z": "77ccb529164df155",
        "name": "debug 65",
        "active": false,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "true",
        "targetType": "full",
        "statusVal": "",
        "statusType": "auto",
        "x": 460,
        "y": 280,
        "wires": []
    },
    {
        "id": "c31cacb32c23489e",
        "type": "switch",
        "z": "77ccb529164df155",
        "name": "",
        "property": "command",
        "propertyType": "msg",
        "rules": [
            {
                "t": "eq",
                "v": "SetBrightness",
                "vt": "str"
            }
        ],
        "checkall": "true",
        "repair": false,
        "outputs": 1,
        "x": 530,
        "y": 500,
        "wires": [
            [
                "9f4ebb254bbe4f28"
            ]
        ]
    },
    {
        "id": "9f4ebb254bbe4f28",
        "type": "template",
        "z": "77ccb529164df155",
        "name": "",
        "field": "payload",
        "fieldType": "msg",
        "format": "handlebars",
        "syntax": "mustache",
        "template": "{\"method\":\"setPilot\",\"params\":{\"dimming\":{{payload}}}}\n",
        "output": "json",
        "x": 760,
        "y": 540,
        "wires": [
            [
                "f92fc7811dde1c42"
            ]
        ]
    },
    {
        "id": "c90ca88c.3bab68",
        "type": "alexa-smart-home-v3-conf",
        "username": "allenle37",
        "mqttserver": "mq-red.cb-net.co.uk",
        "webapiurl": "red.cb-net.co.uk",
        "contextName": "memoryOnly"
    }
]
1 Like

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