[solved] Concate msg.payload to array value in template

Hi there,
I'm using a template for preparing a msg to send it with simple sendmail.

How can I append the value with the msg.payload? (text line for email message)?
"text": "Device: "+ msg.payload is syntactically incorrect.

{
    "transport": {
        "host": "localhost",
        "port": 587,
        "auth": {
            "user": "foo", 
            "pass": "000"
        }
    },
    "options": {
        "from": "test@dom.com",
        "to": "user@test",
        "subject": "WARNING",
        "text": "Device: "+ msg.payload
    }
}

trying "text": "Device: {{msg.payload}}" brings no payload value into the string.

Thanks for helping.
Frank

Try

{
    "transport": {
        "host": "localhost",
        "port": 587,
        "auth": {
            "user": "foo", 
            "pass": "000"
        }
    },
    "options": {
        "from": "test@dom.com",
        "to": "user@test",
        "subject": "WARNING",
        "text": "Device: {{payload}}"
    }
}

Thanks, but get the error: "Unexpected token in JSON at position 366"

Please post your flow as you said template, i assumed template node. I see from your reply this may not be the case.

If this is a HA node try JSONata J:

{
    "transport": {
        "host": "localhost",
        "port": 587,
        "auth": {
            "user": "foo", 
            "pass": "000"
        }
    },
    "options": {
        "from": "test@dom.com",
        "to": "user@test",
        "subject": "WARNING",
        "text": "Device: " & $$.payload
    }
}

After error and try: GOT IT. Your first answer was the right direction but not:

{{payload}}

The are to add white spaces before and after "payload":

{{ payload }}

now its worling well. Thanks.

You are mistaken, the white space makes no difference
eg

[{"id":"1c0bb849507f28ad","type":"inject","z":"452103ea51141731","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"test","payloadType":"str","x":170,"y":3260,"wires":[["7ea7f6fecad53836","273f04f42cd37a26"]]},{"id":"7ea7f6fecad53836","type":"template","z":"452103ea51141731","name":"","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"{\n    \"transport\": {\n        \"host\": \"localhost\",\n        \"port\": 587,\n        \"auth\": {\n            \"user\": \"foo\", \n            \"pass\": \"000\"\n        }\n    },\n    \"options\": {\n        \"from\": \"test@dom.com\",\n        \"to\": \"user@test\",\n        \"subject\": \"WARNING\",\n        \"text\": \"Device: {{payload}}\"\n    }\n}","output":"str","x":320,"y":3260,"wires":[["543d77c6f929e577"]]},{"id":"273f04f42cd37a26","type":"template","z":"452103ea51141731","name":"","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"{\n    \"transport\": {\n        \"host\": \"localhost\",\n        \"port\": 587,\n        \"auth\": {\n            \"user\": \"foo\", \n            \"pass\": \"000\"\n        }\n    },\n    \"options\": {\n        \"from\": \"test@dom.com\",\n        \"to\": \"user@test\",\n        \"subject\": \"WARNING\",\n        \"text\": \"Device: {{ payload }}\"\n    }\n}","output":"str","x":320,"y":3300,"wires":[["1cd8e2bd56d0b608"]]},{"id":"543d77c6f929e577","type":"debug","z":"452103ea51141731","name":"debug 106","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":510,"y":3240,"wires":[]},{"id":"1cd8e2bd56d0b608","type":"debug","z":"452103ea51141731","name":"debug 107","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":510,"y":3300,"wires":[]}]

You can see both formats work in image below


Also if you are using aJSON node, it is not required as template node can output an object.
e.g.

[{"id":"1c0bb849507f28ad","type":"inject","z":"452103ea51141731","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"test","payloadType":"str","x":170,"y":3260,"wires":[["7ea7f6fecad53836"]]},{"id":"7ea7f6fecad53836","type":"template","z":"452103ea51141731","name":"","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"{\n    \"transport\": {\n        \"host\": \"localhost\",\n        \"port\": 587,\n        \"auth\": {\n            \"user\": \"foo\", \n            \"pass\": \"000\"\n        }\n    },\n    \"options\": {\n        \"from\": \"test@dom.com\",\n        \"to\": \"user@test\",\n        \"subject\": \"WARNING\",\n        \"text\": \"Device: {{payload}}\"\n    }\n}","output":"json","x":320,"y":3260,"wires":[["543d77c6f929e577"]]},{"id":"543d77c6f929e577","type":"debug","z":"452103ea51141731","name":"debug 106","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":510,"y":3240,"wires":[]}]

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