Notification from node red

Hello everyone. Before i tell you my problem i will explain you what i have done so far. So, i have 2 Arduino which communicate via LoRa (because i have added 2 Dragino shields on Arduino). Arduino send the following data to node red (serial) : temperature, humidity and water level. I want to receive notification "danger" from node red when the water level is over 3 mm (millimeter) and notification "normal" when the water level is under 3 mm. The problem is that when i put my water sensor in a glass of water (over 3 mm) the notification is "normal". I will send you pictures of my function and results.

TRIGGER ALARM FUNCTION:
trigger_alarm1
NOTIFICATION FUNCTION:
function_notification

FUNCTION WHICH RECEIVE DATA:
function_LORA

RESULTS:

The first thing I would try is in the Notification function change:

if (msg.alarm == 1) 
{

Good idea but unfortunately it doesn't works. Here are results:

and the function with change:
neo function notification

  1. Since you have two function nodes called 'function' it is hard to determing which code goes to which function node.
  2. I would add a debug node (set to display the "Complete msg object") after the COMS node and after each function node so you can see the data coming out of them.
  3. Add node.warn() into the function nodes to display what the data is and/or where you are in the code. For example after line 7 of the TRIGGER ALARM FUNCTION add
    node.warn("debug1 - mmwaterValue=" + mmwaterValue)

Before i run this, i will show you what changes i have done, to tell me if is correct.

TRIGGER ALARM:

TRIGGER ALARM3

NODES:

So, should i add the mandate "node.warn()" in another function also or in trigger alarm only?

You can use the node.warn() anywhere you want to see what is happening. If you read the documentation (Writing Functions : Node-RED) you will find this:

Use node.warn() to show warnings in the sidebar to help you debug. For example:

node.warn("my var xyz = " + xyz);

See logging section below for more details.

So, i did that and the results are these:

I don't undersand this result. By the way i added "node.warn() only in trigger alarm function.

1 - you did not name the debug nodes so I have no idea where each debug msg came from.
2 - the idea of node.warn() is to add it in the function nodes in multiple sport so you can see which path thru the code the data is taking you.

What does the data coming out of the coms node look like?

You're right, i send you again the nodes with names.

Also node COM5 it's like this:

It's greate you named the debug nodes, but you don't show the debug log after the change.

Also please export your flow and attach it to a reply.

if I understood correctly do you mean that?

So sometimes you have a msg going thru where msg.payload is an empty string. How do you handle that? I can’t tell since you haven’t provided an export of the flow

Unfortunately i don't know why it is sometimes an empty string.

So, the export is this:

[{"id":"48eae479.82f56c","type":"serial in","z":"41cc7a8d.3919e4","name":"","serial":"a09a02e6.22d38","x":70,"y":180,"wires":[["80060cef.bb6e2","53c46c2.11c3294"]]},{"id":"a09a02e6.22d38","type":"serial-port","serialport":"COM2","serialbaud":"115200","databits":"8","parity":"none","stopbits":"1","waitfor":"","dtr":"none","rts":"none","cts":"none","dsr":"none","newline":"100","bin":"false","out":"time","addchar":"false","responsetimeout":""}]

That’s not your entire flow, it’s just the coms node

Sorry, I thought that you wanted the nodes.So, which export on the flow you want to see?

The entire flow you are dealing with I.e. the one in the image you have posted

i hope you mean that:

[{"id":"41cc7a8d.3919e4","type":"tab","label":"Flow 3","disabled":false,"info":""},{"id":"48eae479.82f56c","type":"serial in","z":"41cc7a8d.3919e4","name":"","serial":"a09a02e6.22d38","x":70,"y":180,"wires":[["80060cef.bb6e2","53c46c2.11c3294"]]},{"id":"8b221541.41f188","type":"function","z":"41cc7a8d.3919e4","name":"temp","func":"var temp1= msg.payload[6]-48;\nvar temp2= msg.payload[7]-48;\nvar temp3= msg.payload[9]-48;\nvar temp4= msg.payload[10]-48;\nvar result = (temp1*1000)+ (temp2*100) + (temp3*10) + temp4; \nresult = result/100;\nvar result1 = {payload:result};\nreturn[result1];","outputs":1,"noerr":0,"initialize":"","finalize":"","x":170,"y":700,"wires":[[]]},{"id":"274f5616.2fd32a","type":"function","z":"41cc7a8d.3919e4","name":"humidity","func":"var hum1= msg.payload[0]-48;\nvar hum2= msg.payload[1]-48;\nvar hum3= msg.payload[3]-48;\nvar hum4= msg.payload[4]-48;\nvar result = (hum1*1000)+ (hum2*100) + (hum3*10) + hum4;\nresult = result/100;\nvar result1 = {payload:result};\nreturn[result1];","outputs":1,"noerr":0,"initialize":"","finalize":"","x":160,"y":860,"wires":[[]]},{"id":"6951fd8b.107fc4","type":"function","z":"41cc7a8d.3919e4","name":"water level value","func":"var waterlevelValue1= msg.payload[0]-48;\nvar waterlevelValue2= msg.payload[1]-48;\nvar waterlevelValue3= msg.payload[3]-48;\nvar waterlevelValue4= msg.payload[4]-48;\nvar result = (waterlevelValue1*1000)+ (waterlevelValue2*100) + (waterlevelValue3*10) + waterlevelValue4;\nresult = result/100;\nvar result1 = {payload:result};\nreturn[result1];","outputs":1,"noerr":0,"initialize":"","finalize":"","x":190,"y":780,"wires":[[]]},{"id":"4555a817.50f388","type":"function","z":"41cc7a8d.3919e4","name":"","func":"msg.payload = {\"t\":msg.payload};\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":80,"y":580,"wires":[[]]},{"id":"b94885ff.74eee8","type":"function","z":"41cc7a8d.3919e4","name":"","func":"msg.payload = {\"mmwaterValue\":msg3};\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":720,"y":460,"wires":[[]]},{"id":"15436bcc.f153d4","type":"mobius azureiothub","z":"41cc7a8d.3919e4","hubid":"","scopeid":"","deviceid":"","primarykey":"","connectTo":"iotHub","connectionString":"HostName=EarlyWarning.azure-devices.net;DeviceId=Arduino;SharedAccessKey=RYeksPH8lCkEjXeiggmZKr/Vginbu4zhl/iQUiiXuHY=","name":"","x":560,"y":660,"wires":[[]]},{"id":"235150c.c42b2b","type":"ui_gauge","z":"41cc7a8d.3919e4","name":"","group":"d5da3e9b.46abf","order":0,"width":0,"height":0,"gtype":"gage","title":"temp","label":"units","format":"{{value}}","min":0,"max":"100","colors":["#00b500","#e6e600","#ca3838"],"seg1":"","seg2":"","x":510,"y":620,"wires":[]},{"id":"f9704501.d13ea8","type":"ui_gauge","z":"41cc7a8d.3919e4","name":"","group":"d5da3e9b.46abf","order":2,"width":0,"height":0,"gtype":"gage","title":"water level","label":"units","format":"{{value}}","min":0,"max":"100","colors":["#00b500","#e6e600","#ca3838"],"seg1":"","seg2":"","x":350,"y":660,"wires":[]},{"id":"1755e7d9.0a3ea8","type":"ui_gauge","z":"41cc7a8d.3919e4","name":"","group":"d5da3e9b.46abf","order":1,"width":0,"height":0,"gtype":"gage","title":"humidity","label":"units","format":"{{value}}","min":0,"max":"100","colors":["#00b500","#e6e600","#ca3838"],"seg1":"","seg2":"","x":520,"y":580,"wires":[]},{"id":"e111a407.33b718","type":"function","z":"41cc7a8d.3919e4","name":"","func":"msg.payload = {\"h\":msg.payload};\nreturn msg;","outputs":1,"noerr":0,"initialize":"\n","finalize":"","x":80,"y":660,"wires":[[]]},{"id":"c9b9f8e1.ce31c8","type":"buffer-maker","z":"41cc7a8d.3919e4","name":"","specification":"spec","specificationType":"ui","items":[{"name":"humidity","type":"uint8","length":1,"dataType":"jsonata","data":"payload"}],"swap1":"","swap2":"","swap3":"","swap1Type":"swap","swap2Type":"swap","swap3Type":"swap","msgProperty":"payload","msgPropertyType":"str","x":410,"y":720,"wires":[[]],"info":"msg.payload = {\"humidity\":msg.payload};\nreturn msg;"},{"id":"a28547b4.ebb428","type":"buffer-parser","z":"41cc7a8d.3919e4","name":"","data":"payload","dataType":"msg","specification":"spec","specificationType":"ui","items":[{"type":"uint8","name":"item1","offset":0,"length":1,"offsetbit":0,"scale":"1","mask":""}],"swap1":"","swap2":"","swap3":"","swap1Type":"swap","swap2Type":"swap","swap3Type":"swap","msgProperty":"payload","msgPropertyType":"str","resultType":"keyvalue","resultTypeType":"output","multipleResult":false,"fanOutMultipleResult":false,"setTopic":true,"outputs":1,"x":430,"y":800,"wires":[[]]},{"id":"27fb195d.0a6db6","type":"json","z":"41cc7a8d.3919e4","name":"","property":"payload.temp","action":"str","pretty":true,"x":210,"y":620,"wires":[[]],"info":"msg.payload = {\"temp\":msg.payload};\nreturn msg;"},{"id":"4a87d288.fb65cc","type":"json","z":"41cc7a8d.3919e4","name":"","property":"payload.humidity","action":"str","pretty":true,"x":210,"y":540,"wires":[[]],"info":"msg.payload = {\"humidity\":msg.payload};\nreturn msg;"},{"id":"5c32be48.09539","type":"json","z":"41cc7a8d.3919e4","name":"","property":"payload.mmwaterValue","action":"str","pretty":true,"x":210,"y":580,"wires":[[]],"info":"msg.payload = {\"mmwaterValue\":msg.payload};\nreturn msg;"},{"id":"13e4b411.b658bc","type":"delay","z":"41cc7a8d.3919e4","name":"","pauseType":"delay","timeout":"5","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":340,"y":620,"wires":[[]]},{"id":"e0de248e.141398","type":"change","z":"41cc7a8d.3919e4","name":"","rules":[{"t":"change","p":"payload","pt":"msg","from":"msg.payload.temp","fromt":"str","to":"msg.payload","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":760,"y":540,"wires":[[]]},{"id":"9343e80e.6933b8","type":"split","z":"41cc7a8d.3919e4","name":"","splt":"1\\n","spltType":"len","arraySplt":"1","arraySpltType":"len","stream":true,"addname":"","x":330,"y":500,"wires":[[]]},{"id":"95b58812.c31348","type":"function","z":"41cc7a8d.3919e4","name":"","func":"msg.payload = {\"data\":msg.payload};\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":380,"y":860,"wires":[[]]},{"id":"40baec5.060d914","type":"function","z":"41cc7a8d.3919e4","name":"","func":"msg = JSON.parse(msg.payload);\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":80,"y":540,"wires":[[]]},{"id":"4539c79a.67e308","type":"switch","z":"41cc7a8d.3919e4","name":"","property":"payload","propertyType":"msg","rules":[{"t":"gte","v":"0","vt":"num"}],"checkall":"true","repair":false,"outputs":1,"x":210,"y":500,"wires":[[]],"info":"switch (msg.payload)\n{\n    case \"1\":\n    if (msg.payload = {\"temp\":msg.paload})\n    return msg;\n    break;\n    case \"2\":\n    if (msg.payload = {\"humidity\":msg.paload})\n    return msg;\n    break;\n    case \"3\":\n    if (msg.payload = {\"mmwaterValue\":msg.paload})\n    return msg;\n}"},{"id":"617348fa.76af98","type":"function","z":"41cc7a8d.3919e4","name":"","func":"var msg1 = { payload: {\n        device: msg.payload.dev_id,\n        temp: msg.payload.payload_fields.temp\n        \n    }\n}\nvar msg2= { payload : {\n        device: msg.payload.dev_id,\n        humidity: msg.payload.payload_fields.humidity\n    }\n}\nvar msg3 = { payload : {\n        device: msg.payload.dev_id,\n        mmwaterValue: msg.payload.payload_fields.mmwaterValue\n    }\n}\n\nreturn [msg1, msg2, msg3];\n","outputs":1,"noerr":0,"initialize":"","finalize":"","x":340,"y":580,"wires":[[]]},{"id":"a75aa48d.b3cd18","type":"function","z":"41cc7a8d.3919e4","name":"","func":"Var = Number(msg.payload)\nif (Var>20 && Var<45)\nmsg.payload = {\"temp\": Var};\nif (Var>=45)\nmsg.payload = {\"humidity\": Var};\nif (Var>=0 && Var<20)\nmsg.payload = {\"mmwaterValue\": Var};\nreturn Var;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":340,"y":540,"wires":[[]]},{"id":"99a2990a.6017c8","type":"change","z":"41cc7a8d.3919e4","name":"","rules":[{"t":"change","p":"payload","pt":"msg","from":"payload.temp","fromt":"msg","to":"payload.temp","tot":"msg"},{"t":"change","p":"payload","pt":"msg","from":"payload.humidity","fromt":"msg","to":"payload.humidity","tot":"msg"},{"t":"change","p":"payload","pt":"msg","from":"payload.mmwaterValue","fromt":"msg","to":"payload.mmwaterValue","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":360,"y":460,"wires":[[]]},{"id":"afe7da6b.2fd628","type":"Split Threshold","z":"41cc7a8d.3919e4","name":"","property":"payload.temp","startCompare":">=","startValue":20,"stopCompare":"<","stopValue":45,"duration":60,"durationUnit":"seconds","payload":"msg.payload.temp","payloadType":"json","handling":"all","each":"topic","persistMode":true,"aggressiveMode":false,"x":540,"y":540,"wires":[[]]},{"id":"5ee89332.69924c","type":"Split Threshold","z":"41cc7a8d.3919e4","name":"","property":"payload.humidity","startCompare":">=","startValue":23,"stopCompare":"<","stopValue":100,"duration":60,"durationUnit":"seconds","payload":"msg.payload.humidity","payloadType":"json","handling":"all","each":"topic","persistMode":true,"aggressiveMode":false,"x":540,"y":500,"wires":[[]]},{"id":"5617b6b.1f95f48","type":"Split Threshold","z":"41cc7a8d.3919e4","name":"","property":"payload.mmwaterValue","startCompare":">=","startValue":0,"stopCompare":"<","stopValue":5,"duration":60,"durationUnit":"seconds","payload":"msg.payload.mmwaterValue","payloadType":"json","handling":"all","each":"topic","persistMode":true,"aggressiveMode":false,"x":540,"y":460,"wires":[[]]},{"id":"242cbfa6.fe0f8","type":"function","z":"41cc7a8d.3919e4","name":"","func":"msg.payload= Number(msg.payload)\nif (msg.payload>20 && msg.payload<45)\nmsg.payload = {\"temp\":msg.payload};\nif (msg.payload>=45)\nmsg.payload = {\"humidity\":msg.payload};\nif (msg.payload>=0 && msg.payload<20)\nmsg.payload = {\"mmwaterValue\":msg.payload};\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":80,"y":620,"wires":[[]]},{"id":"ca5e8bc7.33e968","type":"split","z":"41cc7a8d.3919e4","name":"","splt":"3\\n","spltType":"len","arraySplt":"3","arraySpltType":"len","stream":true,"addname":"","x":210,"y":660,"wires":[[]],"info":"data= Number(data)\nif (data>20 && data<45)\ndata = {\"temp\":data};\nif (data>=45)\ndata = {\"humidity\":data};\nif (data>=0 && data<20)\ndata = {\"mmwaterValue\":data};\nreturn data;"},{"id":"7e256050.c6ec9","type":"split","z":"41cc7a8d.3919e4","name":"","splt":"1\\n","spltType":"len","arraySplt":"1","arraySpltType":"len","stream":true,"addname":"","x":210,"y":460,"wires":[[]]},{"id":"92dc741a.aef448","type":"function","z":"41cc7a8d.3919e4","name":"","func":"var test = msg.payload;\ntag1 = {payload:test.temp[0].v};\ntag2 = {payload:test.humidity[1].v};\ntag3 = {payload:test.mmwaterValue[2].v};\nreturn [tag1,tag2,tag3];","outputs":1,"noerr":0,"initialize":"","finalize":"","x":80,"y":500,"wires":[[]]},{"id":"a91868c7.2d3368","type":"function","z":"41cc7a8d.3919e4","name":"","func":"var pl = msg.payload.trim();\nif(!pl) return null;\nvar parts = pl.trim().split(\",\");\nif(parts.length < 3) return null;\nvar msg1 = {\n    topic: \"humidity\",\n    payload: Number(parts[0])\n}\nvar msg2 = {\n    topic: \"temperature\",\n    payload: Number(parts[1])\n}\nvar msg3 = {\n    topic: \"water\",\n    payload: Number(parts[2])\n}\nreturn [[msg1, msg2, msg3]];","outputs":1,"noerr":0,"initialize":"","finalize":"","x":80,"y":460,"wires":[[]]},{"id":"80060cef.bb6e2","type":"function","z":"41cc7a8d.3919e4","name":"function LoRa","func":"var pl = msg.payload.trim();\nif(!pl) return null;\nvar parts = pl.split(\",\");\nif(parts.length < 3) return null;\n\nmsg.payload = {\n    humidity: Number(parts[0]),\n    temperature: Number(parts[1]),\n    mmwaterValue: Number(parts[2])\n}\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":340,"y":180,"wires":[["eb603aed.51e128","372cfca1.f56a74","c98e210f.88ff"]]},{"id":"eb603aed.51e128","type":"function","z":"41cc7a8d.3919e4","name":"Trigger Alarm","func":"var mmwaterValue=msg.payload;\nvar alarm_flag=context.get(\"alarm_flag\");\nif (typeof alarm_flag==\"undefined\")\nalarm_flag=false;\n\nif (mmwaterValue>3 && !alarm_flag)\n{\n    node.warn(\"debug1 - mmwaterValue=\" + mmwaterValue);\n    alarm_flag=true;\n    msg.alarm=1;\n    context.set(\"alarm_flag\",alarm_flag);\n    return msg;\n}\nif (mmwaterValue<=3 && alarm_flag)\n{\n    node.warn(\"debug1 - mmwaterValue=\" + mmwaterValue);\n    alarm_flag=false;\n    msg.alarm=0;\n    context.set(\"alarm_flag\",alarm_flag);\n    return msg;\n}\n","outputs":1,"noerr":0,"initialize":"","finalize":"","x":540,"y":320,"wires":[["372cfca1.f56a74","3d0a15e4.331e6a"]]},{"id":"372cfca1.f56a74","type":"function","z":"41cc7a8d.3919e4","name":"function email","func":"var mmwaterValue=msg.payload;\n\nvar d= new Date();\nvar message= \"\";\nif (msg.alarm==1)\n{   \n    msg.topic=\"High water level Alarm\";\n    message=\" High water level Alarm mmwaterValue= \";\n}\nelse\n{   \n    message=\" Water level now normal mmwaterValue= \";\n    msg.topic=\"Water level Alarm Reset\"\n}\nmsg.payload=\"time\" + d + message + msg.payload;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":640,"y":220,"wires":[["780f0cc1.6a1d94"]]},{"id":"2e4932cc.e055be","type":"email out","z":"41cc7a8d.3919e4","server":"smtp.gmail.com","port":"465","secure":true,"tls":true,"name":"alexandros2013@hotmail.com","dname":"","sendfailedcontrib":"","messagesentcontrib":"","x":790,"y":500,"wires":[[]]},{"id":"780f0cc1.6a1d94","type":"debug","z":"41cc7a8d.3919e4","name":"function email msg","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":930,"y":180,"wires":[]},{"id":"53c46c2.11c3294","type":"debug","z":"41cc7a8d.3919e4","name":"com5 msg","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":260,"y":100,"wires":[]},{"id":"c98e210f.88ff","type":"debug","z":"41cc7a8d.3919e4","name":"function LoRa msg","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":620,"y":120,"wires":[]},{"id":"3d0a15e4.331e6a","type":"debug","z":"41cc7a8d.3919e4","name":"trigger alarm msg","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":860,"y":320,"wires":[]},{"id":"a09a02e6.22d38","type":"serial-port","serialport":"COM2","serialbaud":"115200","databits":"8","parity":"none","stopbits":"1","waitfor":"","dtr":"none","rts":"none","cts":"none","dsr":"none","newline":"100","bin":"false","out":"time","addchar":"false","responsetimeout":""},{"id":"d5da3e9b.46abf","type":"ui_group","name":"DHT22","tab":"784aac14.5c2404","order":2,"disp":true,"width":"6","collapse":false},{"id":"784aac14.5c2404","type":"ui_tab","name":"Station","icon":"dashboard","order":1,"disabled":false,"hidden":false}]

No I only wanted to see the part you are dealing with, but that's ok, I can get rid of the rest.

What version of Node-red are you running?

Are you sure? If i can help you more tell me, and sorry by the way. So, i am running Node red version: v.1.2.8

Can you provide more debug information from what’s coming out of the COM file so I can try to reproduce it