Function notification sending the whole payload instead of the message by email

Hello!

I want to send a notification by email but I'm receiving the whole payload by email instead of the message I wrote in the function. If the alarm = 1 then send the email notification. The else part should do nothing! Thank you!

Function Notification

var temp=msg.payload
msg.to="c9230b70491a79+1@inbox.mailtrap.io";
msg.from="c9230b70491a79+1@inbox.mailtrap.io";

var d = new Date();
var message="";
if(msg.alarm)
{
    msg.alarm=1;
    message= " High temperature Alarm check device";
}
else
{
    message= " NONE  ";
}


return msg;

NODES

[
    {
        "id": "181547422299f50a",
        "type": "inject",
        "z": "0685c3c587aa4084",
        "name": "",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "start",
        "payload": "1",
        "payloadType": "num",
        "x": 130,
        "y": 320,
        "wires": [
            [
                "40b40302d111638e",
                "c32fdfcd6980d88b"
            ]
        ]
    },
    {
        "id": "f42e3850419a1abf",
        "type": "csv",
        "z": "0685c3c587aa4084",
        "name": "",
        "sep": ",",
        "hdrin": true,
        "hdrout": "",
        "multi": "mult",
        "ret": "\\n",
        "temp": "",
        "skip": "0",
        "strings": true,
        "include_empty_strings": false,
        "include_null_values": false,
        "x": 550,
        "y": 280,
        "wires": [
            [
                "845aa6dfe380c903"
            ]
        ]
    },
    {
        "id": "845aa6dfe380c903",
        "type": "function",
        "z": "0685c3c587aa4084",
        "name": "graph generate",
        "func": "let payload=msg.payload;\n\nlet data = [[],[]];\nfor(let i=0;i<payload.length;i++){\n    let time = new Date(\"1970-01-01T\"+payload[i].TIME.replace(\".\",\":\").padEnd(8,\"0\"));\n    data[0].push({\"x\": time, \"y\": payload[i].IA});\n    data[1].push({\"x\": time, \"y\": payload[i].IB});\n}\nlet labels = msg.columns.split(\",\").slice(1);\n\nmsg.payload=[{\n\"series\": labels,\n\"data\":data,\n\"labels\": labels\n}];\n\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 760,
        "y": 320,
        "wires": [
            [
                "980b469730d08203",
                "66c6aff13893e03c",
                "bee42052adfe9674"
            ]
        ]
    },
    {
        "id": "980b469730d08203",
        "type": "ui_chart",
        "z": "0685c3c587aa4084",
        "name": "",
        "group": "2ac8e3fa.8b8584",
        "order": 5,
        "width": 0,
        "height": 0,
        "label": "Time Graph Test",
        "chartType": "line",
        "legend": "true",
        "xformat": "HH:mm:ss",
        "interpolate": "linear",
        "nodata": "",
        "dot": false,
        "ymin": "",
        "ymax": "",
        "removeOlder": "10",
        "removeOlderPoints": "",
        "removeOlderUnit": "3600",
        "cutout": 0,
        "useOneColor": false,
        "useUTC": false,
        "colors": [
            "#400000",
            "#ff8000",
            "#ff7f0e",
            "#2ca02c",
            "#98df8a",
            "#d62728",
            "#ff9896",
            "#9467bd",
            "#c5b0d5"
        ],
        "outputs": 1,
        "useDifferentColor": false,
        "className": "",
        "x": 880,
        "y": 140,
        "wires": [
            []
        ]
    },
    {
        "id": "66c6aff13893e03c",
        "type": "debug",
        "z": "0685c3c587aa4084",
        "name": "",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "true",
        "targetType": "full",
        "statusVal": "",
        "statusType": "auto",
        "x": 1110,
        "y": 160,
        "wires": []
    },
    {
        "id": "c32fdfcd6980d88b",
        "type": "template",
        "z": "0685c3c587aa4084",
        "name": "",
        "field": "payload",
        "fieldType": "msg",
        "format": "handlebars",
        "syntax": "mustache",
        "template": "TIME,IA,IB\n09:21.3,-3.984,2.188\n09:22.3,-2.984,1.406\n09:23.3,-1.75,0.703",
        "output": "str",
        "x": 360,
        "y": 280,
        "wires": [
            [
                "f42e3850419a1abf"
            ]
        ]
    },
    {
        "id": "bee42052adfe9674",
        "type": "function",
        "z": "0685c3c587aa4084",
        "name": "triggerAlarm",
        "func": "var payload=msg.payload[0].data[0][0].y;\nvar alarm_flag=context.get(\"alarm_flag\");\nif(typeof alarm_flag==\"undefined\")\nalarm_flag=false;\n\nif (payload<-3 && !alarm_flag)\n{\n    alarm_flag=true;\n    msg.alarm=1;\n    context.set(\"alarm_flag\",alarm_flag);\n}\nelse if (payload<=-3 && alarm_flag)\n{\n    alarm_flag=false;\n    msg.alarm=0;\n    context.set(\"alarm_flag\",alarm_flag);\n}\nreturn msg;\n",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 870,
        "y": 440,
        "wires": [
            [
                "e2077f8173696fa2",
                "5dd6b786f427ce25"
            ]
        ]
    },
    {
        "id": "5dd6b786f427ce25",
        "type": "debug",
        "z": "0685c3c587aa4084",
        "name": "",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "true",
        "targetType": "full",
        "statusVal": "",
        "statusType": "auto",
        "x": 1110,
        "y": 340,
        "wires": []
    },
    {
        "id": "58782dda1425ada3",
        "type": "e-mail",
        "z": "0685c3c587aa4084",
        "server": "smtp.mailtrap.io",
        "port": "2525",
        "secure": false,
        "tls": false,
        "name": "c9230b70491a79+1@inbox.mailtrap.io",
        "dname": "Email Alert",
        "x": 850,
        "y": 720,
        "wires": []
    },
    {
        "id": "e2077f8173696fa2",
        "type": "function",
        "z": "0685c3c587aa4084",
        "name": "functionNotification",
        "func": "var temp=msg.payload\nmsg.to=\"c9230b70491a79+1@inbox.mailtrap.io\";\nmsg.from=\"c9230b70491a79+1@inbox.mailtrap.io\";\n\nvar d = new Date();\nvar message=\"\";\nif(msg.alarm)\n{\n    msg.alarm=1;\n    message= \" High temperature Alarm \";\n}\nelse\n{\n    message= \" All good \";\n}\n\n\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 890,
        "y": 580,
        "wires": [
            [
                "58782dda1425ada3"
            ]
        ]
    },
    {
        "id": "2ac8e3fa.8b8584",
        "type": "ui_group",
        "name": "Default",
        "tab": "ebb1ed1c.5ebe2",
        "order": 1,
        "disp": true,
        "width": "12",
        "collapse": false,
        "className": ""
    },
    {
        "id": "ebb1ed1c.5ebe2",
        "type": "ui_tab",
        "name": "Home",
        "icon": "dashboard",
        "disabled": false,
        "hidden": false
    }
]

If you are receiving msg.payload as the email body, try using msg.payload as the message body.

    msg.payload = " High temperature Alarm ";

Thank you! It worked. I'm not receiving the whole payload, just the message "high temperature alarm". However, it triggers the email no matter if the alarm is 1 or 0, it's sending the message every time I send a payload.

I think I solved. However, I'm not sure if it's a good practice. Actually, I do not need the NONE message. How can I just get the alarms when is 1? Please check this out:
Thanks!

var temp=msg.payload
msg.to="c9230b70491a79+1@inbox.mailtrap.io";
msg.from="c9230b70491a79+1@inbox.mailtrap.io";

var d = new Date();
var message="";
if(msg.alarm)
{
    msg.alarm=1;
    message= " High temperature Alarm check device";
}
else
{
    message= " NONE  ";
}

msg.payload="time: "+d+message+msg.payload;


return msg;

You can filter only change of a property
e.g.

[{"id":"549bdc49.c2248c","type":"inject","z":"bf9e1e33.030598","name":"msg.alarm 1","props":[{"p":"alarm","v":"1","vt":"num"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":460,"y":3080,"wires":[["c908f372.a7c888"]]},{"id":"c908f372.a7c888","type":"rbe","z":"bf9e1e33.030598","name":"","func":"rbe","gap":"","start":"","inout":"out","septopics":true,"property":"alarm","x":710,"y":3100,"wires":[["11880bfb.2640c4"]]},{"id":"212b7780.c2fc68","type":"inject","z":"bf9e1e33.030598","name":"msg.alarm 0","props":[{"p":"alarm","v":"0","vt":"num"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":460,"y":3140,"wires":[["c908f372.a7c888"]]},{"id":"11880bfb.2640c4","type":"function","z":"bf9e1e33.030598","name":"functionNotification","func":"var temp=msg.payload\nmsg.to=\"c9230b70491a79+1@inbox.mailtrap.io\";\nmsg.from=\"c9230b70491a79+1@inbox.mailtrap.io\";\n\nvar d = new Date();\nvar message=\"\";\nif(msg.alarm)\n{\n    msg.alarm=1;\n    msg.payload = \" High temperature Alarm \";\n}\nelse\n{\n    msg.payload = \" All good \";\n}\n\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":930,"y":3100,"wires":[["199c038e.3040a4"]]},{"id":"199c038e.3040a4","type":"debug","z":"bf9e1e33.030598","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":1180,"y":3040,"wires":[]}]

The email would only be sent, if msg.alarm changes from 1 to 0 or vice versa

You can use a switch node to only execute the function if msg.alarm equals 1.

The switch works perfect! Thanks a lot.

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