Thanks everyone posting it. and helping me here. Consider me a noob please.
I did few things already before so thought i was giving full functionality when i did not.
So, let me explain you my requirement, i send out a blank payload to a mqtt topic. if i dont get a response within 10 seconds as "ESP device connected" i want to log it in a file ,else as soon i get the message i want to again log in the file.
This is my flow which was missing earlier
[
{
"id": "8ce1e49d586df17b",
"type": "tab",
"label": "Flow 1",
"disabled": false,
"info": "",
"env": []
},
{
"id": "9b597adc373b3022",
"type": "debug",
"z": "8ce1e49d586df17b",
"name": "",
"active": true,
"tosidebar": true,
"console": false,
"tostatus": false,
"complete": "true",
"targetType": "full",
"statusVal": "",
"statusType": "auto",
"x": 2135.5,
"y": 40,
"wires": []
},
{
"id": "c5eb9860a71d9e45",
"type": "inject",
"z": "8ce1e49d586df17b",
"name": "",
"props": [
{
"p": "payload"
},
{
"p": "topic",
"vt": "str"
}
],
"repeat": "",
"crontab": "",
"once": false,
"onceDelay": "15",
"topic": "",
"payload": "",
"payloadType": "date",
"x": 615.5,
"y": 100,
"wires": [
[
"4ae50cef02d83df9",
"4f1bac3383f429de"
]
]
},
{
"id": "1a0b09a35e814c86",
"type": "switch",
"z": "8ce1e49d586df17b",
"name": "",
"property": "payload",
"propertyType": "msg",
"rules": [
{
"t": "eq",
"v": "ESP device connected",
"vt": "str"
}
],
"checkall": "true",
"repair": false,
"outputs": 1,
"x": 1555.5,
"y": 40,
"wires": [
[
"38c1608ec7fa7e0a",
"9b597adc373b3022",
"6a4808da5784d63e"
]
]
},
{
"id": "ae11d5b63f5fb551",
"type": "mqtt out",
"z": "8ce1e49d586df17b",
"name": "97244",
"topic": "PiServer/97244/Status",
"qos": "0",
"retain": "",
"respTopic": "",
"contentType": "",
"userProps": "",
"correl": "",
"expiry": "",
"broker": "50f2d76bef2a77d6",
"x": 1435.5,
"y": 20,
"wires": []
},
{
"id": "4ae50cef02d83df9",
"type": "function",
"z": "8ce1e49d586df17b",
"name": "function to send chip id",
"func": "var chip_array = flow.get(\"chip_arrays\") || [];\n\nmsg.topic = \"ESP/Status/97244\" ;//+ chip_array[0];\nmsg.payload = \"\";\nreturn msg;",
"outputs": 1,
"noerr": 0,
"initialize": "",
"finalize": "",
"libs": [],
"x": 895.5,
"y": 20,
"wires": [
[
"ae11d5b63f5fb551",
"2a26c6614d182915"
]
]
},
{
"id": "10c4dc307f533c99",
"type": "mqtt in",
"z": "8ce1e49d586df17b",
"name": "",
"topic": "",
"qos": "0",
"datatype": "auto-detect",
"broker": "50f2d76bef2a77d6",
"nl": false,
"rap": true,
"rh": 0,
"inputs": 1,
"x": 1255.5,
"y": 40,
"wires": [
[
"1a0b09a35e814c86"
]
]
},
{
"id": "38c1608ec7fa7e0a",
"type": "trigger",
"z": "8ce1e49d586df17b",
"name": "Watchdog",
"op1": "",
"op2": "timeout",
"op1type": "nul",
"op2type": "str",
"duration": "10",
"extend": true,
"overrideDelay": false,
"units": "s",
"reset": "ESP device connected",
"bytopic": "topic",
"topic": "topic",
"outputs": 1,
"x": 1735.5,
"y": 40,
"wires": [
[
"9b597adc373b3022",
"fb3d1ca8f074120c"
]
]
},
{
"id": "2a26c6614d182915",
"type": "change",
"z": "8ce1e49d586df17b",
"name": "",
"rules": [
{
"t": "set",
"p": "action",
"pt": "msg",
"to": "subscribe",
"tot": "str"
},
{
"t": "set",
"p": "topic1",
"pt": "msg",
"to": "true",
"tot": "bool"
}
],
"action": "",
"property": "",
"from": "",
"to": "",
"reg": false,
"x": 1095.5,
"y": 40,
"wires": [
[
"10c4dc307f533c99",
"fcc151793123976c"
]
]
},
{
"id": "4f1bac3383f429de",
"type": "function",
"z": "8ce1e49d586df17b",
"name": "function to send chip id",
"func": "var chip_array = flow.get(\"chip_arrays\") || [];\n\nmsg.topic = \"ESP/Status/11182\" ;//+ chip_array[0];\nmsg.payload = \"\";\nreturn msg;",
"outputs": 1,
"noerr": 0,
"initialize": "",
"finalize": "",
"libs": [],
"x": 875.5,
"y": 140,
"wires": [
[
"cbd3ae8e94815c04",
"ba461f3acfc6af5b"
]
]
},
{
"id": "cbd3ae8e94815c04",
"type": "mqtt out",
"z": "8ce1e49d586df17b",
"name": "11182",
"topic": "PiServer/11182/Status",
"qos": "0",
"retain": "",
"respTopic": "",
"contentType": "",
"userProps": "",
"correl": "",
"expiry": "",
"broker": "50f2d76bef2a77d6",
"x": 1435.5,
"y": 140,
"wires": []
},
{
"id": "ba461f3acfc6af5b",
"type": "change",
"z": "8ce1e49d586df17b",
"name": "",
"rules": [
{
"t": "set",
"p": "action",
"pt": "msg",
"to": "subscribe",
"tot": "str"
},
{
"t": "set",
"p": "topic2",
"pt": "msg",
"to": "true",
"tot": "bool"
}
],
"action": "",
"property": "",
"from": "",
"to": "",
"reg": false,
"x": 1075.5,
"y": 180,
"wires": [
[
"e812b981bcdddb73",
"b9af36d0695a4267"
]
]
},
{
"id": "e812b981bcdddb73",
"type": "mqtt in",
"z": "8ce1e49d586df17b",
"name": "",
"topic": "",
"qos": "0",
"datatype": "auto-detect",
"broker": "50f2d76bef2a77d6",
"nl": false,
"rap": true,
"rh": 0,
"inputs": 1,
"x": 1255.5,
"y": 180,
"wires": [
[
"dae944f275a91afe"
]
]
},
{
"id": "dae944f275a91afe",
"type": "switch",
"z": "8ce1e49d586df17b",
"name": "",
"property": "payload",
"propertyType": "msg",
"rules": [
{
"t": "eq",
"v": "ESP device connected",
"vt": "str"
}
],
"checkall": "true",
"repair": false,
"outputs": 1,
"x": 1555.5,
"y": 180,
"wires": [
[
"a1b9ec56b187657a",
"6a4808da5784d63e"
]
]
},
{
"id": "a1b9ec56b187657a",
"type": "trigger",
"z": "8ce1e49d586df17b",
"name": "Watchdog",
"op1": "",
"op2": "timeout",
"op1type": "nul",
"op2type": "str",
"duration": "10",
"extend": true,
"overrideDelay": false,
"units": "s",
"reset": "ESP device connected",
"bytopic": "topic",
"topic": "topic",
"outputs": 1,
"x": 1735.5,
"y": 180,
"wires": [
[
"930ac790eba97bab"
]
]
},
{
"id": "930ac790eba97bab",
"type": "debug",
"z": "8ce1e49d586df17b",
"name": "",
"active": true,
"tosidebar": true,
"console": false,
"tostatus": false,
"complete": "true",
"targetType": "full",
"statusVal": "",
"statusType": "auto",
"x": 1795.5,
"y": 220,
"wires": []
},
{
"id": "7d5c3adda621496d",
"type": "fs-file-lister",
"z": "8ce1e49d586df17b",
"name": "",
"start": "D:\\Development\\Home Automation\\Logs\\",
"pattern": "",
"folders": "*",
"hidden": true,
"lstype": "files",
"path": true,
"single": true,
"depth": 0,
"stat": true,
"showWarnings": false,
"x": 2355.5,
"y": 300,
"wires": [
[
"33e9cf808ccf1f06"
]
]
},
{
"id": "33e9cf808ccf1f06",
"type": "change",
"z": "8ce1e49d586df17b",
"name": "Get file content",
"rules": [
{
"t": "set",
"p": "payload",
"pt": "msg",
"to": "filecontent",
"tot": "msg"
}
],
"action": "",
"property": "",
"from": "",
"to": "",
"reg": false,
"x": 2555.5,
"y": 320,
"wires": [
[
"cda9028c7e4a0aaa"
]
]
},
{
"id": "cfa24e7e49ac00b2",
"type": "function",
"z": "8ce1e49d586df17b",
"name": "Filename generator",
"func": "// Get the current time and convert it to text\nvar now = new Date();\nvar yyyy = now.getFullYear();\nvar mm = now.getMonth() < 9 ? \"0\" + (now.getMonth() + 1) : (now.getMonth() + 1); // getMonth() is zero-based\nvar dd = now.getDate() < 10 ? \"0\" + now.getDate() : now.getDate();\nvar hh = now.getHours() < 10 ? \"0\" + now.getHours() : now.getHours();\nvar mmm = now.getMinutes() < 10 ? \"0\" + now.getMinutes() : now.getMinutes();\nvar ss = now.getSeconds() < 10 ? \"0\" + now.getSeconds() : now.getSeconds();\n\n// Generate out file name pattern\nmsg.fname = \"Logs_\"+ dd + mm+ yyyy+ \".log\";\n// Full filename with path for the file node later\nmsg.filename = \"D:\\\\Development\\\\Home Automation\\\\Logs\\\\\"+ msg.input_room+\"\\\\\"+msg.fname;\n\n// We save the current payload into a different place on the msg object\nmsg.filecontent = msg.payload;\n\n// We are passing the file name search pattern to fs node to tell us if the file exists or not\nmsg.payload = {\"pattern\":msg.fname};\n\n\nnode.status({ fill: \"blue\", shape: \"ring\", text: msg.filename});\n\nflow.set(\"Send_Bool\", \"false\");//to prevent loop of mqtt\n\nreturn msg;",
"outputs": 1,
"noerr": 0,
"initialize": "",
"finalize": "",
"libs": [],
"x": 2195.5,
"y": 320,
"wires": [
[
"7d5c3adda621496d"
]
]
},
{
"id": "b9af36d0695a4267",
"type": "trigger",
"z": "8ce1e49d586df17b",
"name": "Watchdog",
"op1": "",
"op2": "timeout",
"op1type": "nul",
"op2type": "str",
"duration": "10",
"extend": true,
"overrideDelay": false,
"units": "s",
"reset": "ESP device connected",
"bytopic": "topic",
"topic": "topic",
"outputs": 1,
"x": 1255.5,
"y": 240,
"wires": [
[
"930ac790eba97bab",
"5a8460f6c72dd963"
]
]
},
{
"id": "fcc151793123976c",
"type": "trigger",
"z": "8ce1e49d586df17b",
"name": "Watchdog",
"op1": "",
"op2": "timeout",
"op1type": "nul",
"op2type": "str",
"duration": "10",
"extend": true,
"overrideDelay": false,
"units": "s",
"reset": "ESP device connected",
"bytopic": "topic",
"topic": "topic",
"outputs": 1,
"x": 1235.5,
"y": 100,
"wires": [
[
"fcce8d59de94f1d4"
]
]
},
{
"id": "5a8460f6c72dd963",
"type": "function",
"z": "8ce1e49d586df17b",
"name": "Set Negative data",
"func": "var now = new Date();\nvar yyyy = now.getFullYear();\nvar m = now.getMonth() < 9 ? \"0\" + (now.getMonth() + 1) : (now.getMonth() + 1); // getMonth() is zero-based\nvar dd = now.getDate() < 10 ? \"0\" + now.getDate() : now.getDate();\nvar hh = now.getHours() < 10 ? \"0\" + now.getHours() : now.getHours();\nvar mm = now.getMinutes() < 10 ? \"0\" + now.getMinutes() : now.getMinutes();\nvar ss = now.getSeconds() < 10 ? \"0\" + now.getSeconds() : now.getSeconds();\nvar pd = msg.inputs;\nvar topic= msg.topic;\nvar top_room = topic.split(\"/Status/\")[1];\n\nif (top_room == \"97244\")\nmsg.input_room = \"room_1\";\n\nelse if (top_room == \"11182\")\nmsg.input_room = \"room_2\" ;\n\nvar pd_str= dd + \"//\" + m + \"//\" + yyyy + \" \"+hh + \":\" + mm + \":\" + ss+\": \";\nmsg.payload = pd_str + \"Room ESP \" + top_room+\" could not be connected\";\n// +\" : temperature: \" + \"1\";\n//msg.payload = msg.payload + \" ,humidity \" + \"2\"\nnode.status({ fill: \"red\", shape: \"ring\", text: msg.payload });\n\n\nreturn msg;",
"outputs": 1,
"noerr": 0,
"initialize": "",
"finalize": "",
"libs": [],
"x": 1675.5,
"y": 260,
"wires": [
[
"cfa24e7e49ac00b2"
]
]
},
{
"id": "6a4808da5784d63e",
"type": "function",
"z": "8ce1e49d586df17b",
"name": "Set Positive data",
"func": "var now = new Date();\nvar yyyy = now.getFullYear();\nvar m = now.getMonth() < 9 ? \"0\" + (now.getMonth() + 1) : (now.getMonth() + 1); // getMonth() is zero-based\nvar dd = now.getDate() < 10 ? \"0\" + now.getDate() : now.getDate();\nvar hh = now.getHours() < 10 ? \"0\" + now.getHours() : now.getHours();\nvar mm = now.getMinutes() < 10 ? \"0\" + now.getMinutes() : now.getMinutes();\nvar ss = now.getSeconds() < 10 ? \"0\" + now.getSeconds() : now.getSeconds();\nvar topic= msg.topic;\nvar top_room = topic.split(\"/Status/\")[1];\nif (top_room == \"97244\")\nmsg.input_room = \"room_1\";\n\nelse if (top_room == \"11182\")\nmsg.input_room = \"room_2\" ;\n\n\nvar pd_str= dd + \"//\" + m + \"//\" + yyyy + \" \"+hh + \":\" + mm + \":\" + ss+\": \";\nmsg.payload = pd_str + \"Room ESP \" + top_room+\" \"+msg.payload;\n// +\" : temperature: \" + \"1\";\n//msg.payload = msg.payload + \" ,humidity \" + \"2\"\nnode.status({ fill: \"red\", shape: \"ring\", text: msg.payload });\nnode.warn(msg.payload);\n\n\nreturn msg;",
"outputs": 1,
"noerr": 0,
"initialize": "",
"finalize": "",
"libs": [],
"x": 1975.5,
"y": 100,
"wires": [
[
"a07b45a2d1dea6f9",
"3f65b181443568ae"
]
]
},
{
"id": "a07b45a2d1dea6f9",
"type": "function",
"z": "8ce1e49d586df17b",
"name": "Filename generator",
"func": "// Get the current time and convert it to text\nvar now = new Date();\nvar yyyy = now.getFullYear();\nvar mm = now.getMonth() < 9 ? \"0\" + (now.getMonth() + 1) : (now.getMonth() + 1); // getMonth() is zero-based\nvar dd = now.getDate() < 10 ? \"0\" + now.getDate() : now.getDate();\nvar hh = now.getHours() < 10 ? \"0\" + now.getHours() : now.getHours();\nvar mmm = now.getMinutes() < 10 ? \"0\" + now.getMinutes() : now.getMinutes();\nvar ss = now.getSeconds() < 10 ? \"0\" + now.getSeconds() : now.getSeconds();\n\n// Generate out file name pattern\nmsg.fname = \"Logs_\"+ dd + mm+ yyyy+ \".log\";\n// Full filename with path for the file node later\nmsg.filename = \"D:\\\\Development\\\\Home Automation\\\\Logs\\\\\"+ msg.input_room+\"\\\\\"+msg.fname;\n\n// We save the current payload into a different place on the msg object\nmsg.filecontent = msg.payload;\n\n// We are passing the file name search pattern to fs node to tell us if the file exists or not\nmsg.payload = {\"pattern\":msg.fname};\n\n\nnode.status({ fill: \"blue\", shape: \"ring\", text: msg.filename});\n\nflow.set(\"Send_Bool\", \"false\");//to prevent loop of mqtt\n\nreturn msg;",
"outputs": 1,
"noerr": 0,
"initialize": "",
"finalize": "",
"libs": [],
"x": 2175.5,
"y": 240,
"wires": [
[
"7d5c3adda621496d"
]
]
},
{
"id": "3f65b181443568ae",
"type": "debug",
"z": "8ce1e49d586df17b",
"name": "",
"active": true,
"tosidebar": true,
"console": false,
"tostatus": false,
"complete": "true",
"targetType": "full",
"statusVal": "",
"statusType": "auto",
"x": 2175.5,
"y": 100,
"wires": []
},
{
"id": "fb3d1ca8f074120c",
"type": "function",
"z": "8ce1e49d586df17b",
"name": "Set Positive data",
"func": "node.warn(\"now\");\nreturn \"\";",
"outputs": 1,
"noerr": 0,
"initialize": "",
"finalize": "",
"libs": [],
"x": 1975.5,
"y": 60,
"wires": [
[]
]
},
{
"id": "fcce8d59de94f1d4",
"type": "function",
"z": "8ce1e49d586df17b",
"name": "Set Negative data",
"func": "var now = new Date();\nvar yyyy = now.getFullYear();\nvar m = now.getMonth() < 9 ? \"0\" + (now.getMonth() + 1) : (now.getMonth() + 1); // getMonth() is zero-based\nvar dd = now.getDate() < 10 ? \"0\" + now.getDate() : now.getDate();\nvar hh = now.getHours() < 10 ? \"0\" + now.getHours() : now.getHours();\nvar mm = now.getMinutes() < 10 ? \"0\" + now.getMinutes() : now.getMinutes();\nvar ss = now.getSeconds() < 10 ? \"0\" + now.getSeconds() : now.getSeconds();\nvar pd = msg.inputs;\nvar topic= msg.topic;\nvar top_room = topic.split(\"/Status/\")[1];\n\nif (top_room == \"97244\")\nmsg.input_room = \"room_1\";\n\nelse if (top_room == \"11182\")\nmsg.input_room = \"room_2\" ;\n\nvar pd_str= dd + \"//\" + m + \"//\" + yyyy + \" \"+hh + \":\" + mm + \":\" + ss+\": \";\nmsg.payload = pd_str + \"Room ESP \" + top_room+\" could not be connected\";\n// +\" : temperature: \" + \"1\";\n//msg.payload = msg.payload + \" ,humidity \" + \"2\"\nnode.status({ fill: \"red\", shape: \"ring\", text: msg.payload });\n\n\nreturn msg;",
"outputs": 1,
"noerr": 0,
"initialize": "",
"finalize": "",
"libs": [],
"x": 1635.5,
"y": 100,
"wires": [
[
"cfa24e7e49ac00b2"
]
]
},
{
"id": "cda9028c7e4a0aaa",
"type": "file",
"z": "8ce1e49d586df17b",
"name": "write to log file",
"filename": "filename",
"filenameType": "msg",
"appendNewline": true,
"createDir": false,
"overwriteFile": "false",
"encoding": "none",
"x": 2780,
"y": 320,
"wires": [
[]
]
},
{
"id": "50f2d76bef2a77d6",
"type": "mqtt-broker",
"name": "",
"broker": "192.168.0.6",
"port": "1881",
"tls": "b3b6ea0373156ff4",
"clientid": "",
"autoConnect": true,
"usetls": false,
"protocolVersion": "4",
"keepalive": "60",
"cleansession": true,
"birthTopic": "",
"birthQos": "0",
"birthPayload": "",
"birthMsg": {},
"closeTopic": "",
"closeQos": "0",
"closePayload": "",
"closeMsg": {},
"willTopic": "",
"willQos": "0",
"willPayload": "",
"willMsg": {},
"userProps": "",
"sessionExpiry": ""
},
{
"id": "b3b6ea0373156ff4",
"type": "tls-config",
"name": "",
"cert": "",
"key": "",
"ca": "",
"certname": "device ceritificate.crt",
"keyname": "private key.key",
"caname": "AmazonRootCA1.pem",
"servername": "AWS iot",
"verifyservercert": true,
"alpnprotocol": ""
}
]