How to check if message from MQTT hasnt been received for 10 seconds

Hello All,
Please help me develop a logic, where I want to trigger an action (i.e. create log files, that is handled) if message is not received from MQTT subscribe for atleast 10 seconds.
This is my logic so far which is not woriking, and there is a timestamp before it.

The change node is
image

Please let me know.

Regards,
Shariq

1 Like

Is it expected from MQTT to start the 10-second countdown?

You can use a trigger node after the MQTT-In

3 Likes

Hello ..i have used the watchlist already.. can you suggest a flow?

All you need, to trigger an action if no mqtt message arrives for 10 seconds, is a trigger node set up the way I showed you, after the mqtt in node. That's all. No flow, no functions, no watch list.
Have you tried it?

1 Like

i have done the same thing as you see

Except differently!

Look again.

In particular, look at the handling option with the big blue arrow pointing at it.

The trigger node will send "timeout" if there is a delay of 10 seconds after a message arrives.

If you need an alert after 10 seconds if no message arrives at all, then you will have to supplement it.
Maybe an inject node set to inject a dummy message at start-up will work for you. Alternatively take a look at the MQTT retained message feature.

Since it looks like you are using dynamic subscription, I assumed that you need to handle each topic seperately.
In that case you probably have to send "ESP device connected" to reset the trigger when you unsubscribe.

Hi ,yes i have an timestamp to request for a heart beat message from esp device every 10 seconds
on repeat

sorry i didnt upload the latest image, yes i have updated it but i dont see response for the positive case that is when the response is positively received

I'm afraid I can no longer tell what the problem is.

This is one I did the other day. Mines approx every 5 minutes.

[{"id":"b89c143adcde11c5","type":"mqtt in","z":"f89b0150.631a1","name":"MQTT frequencywired/mqtt","topic":"frequencywired/mqtt","qos":"1","datatype":"auto-detect","broker":"87d550ef.ae142","nl":false,"rap":false,"inputs":0,"x":160,"y":2240,"wires":[["7d689df13a227ad1"]]},{"id":"fd82587694e7d543","type":"function","z":"f89b0150.631a1","name":"Speak: MQTT failed","func":"var response = \"Attention! Frequency diverter MQTT failed\";\nmsg.payload = response;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1160,"y":2240,"wires":[["63475a9cf945f3fb","e62cabc0939bd095"]]},{"id":"7d689df13a227ad1","type":"change","z":"f89b0150.631a1","name":"on","rules":[{"t":"set","p":"payload","pt":"msg","to":"on","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":350,"y":2240,"wires":[["42cd4699f72323eb"]]},{"id":"42cd4699f72323eb","type":"mytimeout","z":"f89b0150.631a1","name":"11 minutes","outtopic":"mqtt","outsafe":"","outwarning":"","outunsafe":"off","warning":"","timer":"660","debug":false,"ndebug":false,"ignoreCase":false,"repeat":false,"again":false,"x":530,"y":2240,"wires":[["fd82587694e7d543","d0c6765acdf58983","771f320c446db23f"],[]]},{"id":"d0c6765acdf58983","type":"change","z":"f89b0150.631a1","name":"on","rules":[{"t":"set","p":"payload","pt":"msg","to":"on","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":510,"y":2320,"wires":[["e3dcee48386e9625"]]},{"id":"e3dcee48386e9625","type":"delay","z":"f89b0150.631a1","name":"","pauseType":"delay","timeout":"5","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"allowrate":false,"outputs":1,"x":640,"y":2320,"wires":[["42cd4699f72323eb"]]},{"id":"b968889334716e31","type":"mqtt out","z":"f89b0150.631a1","name":"RESET","topic":"frequency/reset","qos":"0","retain":"false","respTopic":"","contentType":"","userProps":"","correl":"","expiry":"","broker":"87d550ef.ae142","x":1100,"y":2280,"wires":[],"inputLabels":["1"]},{"id":"192f3415e3357b45","type":"change","z":"f89b0150.631a1","name":"true","rules":[{"t":"set","p":"payload","pt":"msg","to":"true","tot":"bool"}],"action":"","property":"","from":"","to":"","reg":false,"x":970,"y":2280,"wires":[["b968889334716e31"]]},{"id":"771f320c446db23f","type":"delay","z":"f89b0150.631a1","name":"","pauseType":"rate","timeout":"5","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"hour","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":true,"allowrate":false,"outputs":1,"x":830,"y":2280,"wires":[["192f3415e3357b45"]]},{"id":"87d550ef.ae142","type":"mqtt-broker","name":"Pi 196","broker":"192.168.3.196","port":"1883","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":{},"sessionExpiry":""}]

What is this?
image

2 Likes

Hi @Shariq - a number of people have tried to help you but I can see that for some reason you are not up and running with something that works for you. Let me try.

Firstly - your original message was unclear, which is why I believe you have had responses that have not given you what you need. Your original message doesn't show the configuration of the trigger node, and also it cuts off the left hand side, so we have no idea what is driving the 2 function nodes that then route into the MQTT outputs and branch off to the change nodes.

@jbudd nevertheless responded early-on in the thread with a good solution, but it doesn't matter, I plan to start from scratch to get you up and running with what you are asking for.

(I only raise this point because I don't want you to say that you already know how to do the thing I'm going to write. Maybe you do, but it's really unclear from your messages on this forum so far.)

The person above (@Tinbum1) provided you with a flow that you could import, which required an addon called "mytimeout" which isn't installed by default, and which you would need to install. People often do this in the forum, assuming that you understand you'd need to install any missing addons (contribs) that are shown in dotted lines. You absolutely do not need to install any addons to do what you require, as you can make it work with the default, builtin nodes in Node-RED. I'm not saying that @Tinbum1's solution isn't correct, but they are using an addon that maybe provides them with more flexibility.

Secondly, I haven't looked in depth at your original flow, or the solutions offered by others, but I can definitely say that what has been suggested so far - to use the trigger node - is the best way of doing what you need.

The thing you want to do is called a "watchdog" and it's a very common function in a lot of systems. For more info about what a "watchdog" is, you can google this, but essentially it's what you initially described: create some message when a certain thing has not happened within a given timeframe.

For example, I have a basement with a water level monitor that sends messages every 10 seconds. Maybe you can imagine - if that system fails without me noticing, there's a possibility that my basement will flood, and so it's really important that I receive a message (email or whatever) when I don't get an update after a certain amount of time. As my monitor sends messages every 10 seconds, it would be mad to set the watchdog timer to 10 seconds, because that gives zero headroom in case of network errors or other delays, but it would perhaps be sensible to set that watchdog timer to 20 seconds. If we've not received a signal within that time, I want to know about it.

Right so let's get started, we will strip everything back to the bare functionality of a watchdog, then hopefully you can adapt this for your specific needs. We will create this from scratch and I'll explain as I go along.

  1. In Node-RED admin, create an inject node, and leave it with the default options:
    image

  2. Now add a trigger node, and change the config as follows:

  • Set it to send "Nothing"
  • Wait for 10 seconds
  • Where it says "Then send", enter the words "oh no" or something like that
  • Click "extend delay if new message arrives"
    It should look like this:
    image
    And the configuration of the trigger node looks like this:
  1. Now wire in a debug node to the output, and switch the right-hand panel to show debug viewer:

  2. Now hit deploy, then press the button on the blue inject node, to inject a message. Wait about 5 seconds, and press it again. Keep doing this repeatedly.

On the debug panel sidebar, you will notice there is no message.

However, when you stop pressing the inject node every 5 seconds, e.g. you leave it for 12 seconds, you will receive a message in the debug panel on the right.

If at any stage you aren't getting the same as me, then please stop and re-read what I've done, or post back here.

Now you have a rudimentary working watchdog timer, which does what you said you need.

Notes:

  • This flow will ONLY send one message once it hasn't received something in the timeout period.
  • It is self-resetting, which means once you inject a message again, it will start that timeout again.
  • However if, after hitting deploy again in Node-RED, your system does NOT send a message within the timeout period, it will NOT notify you. You need to send that initial message at least once for it to commence the watchdog.

Now when you integrate this into your existing system, you may want to also add an inject node as the input, and set that inject node to automatically run, i.e. set the option shown below, to combat that third point about it not automatically starting when you re-deploy Node-RED:

There are many ways to improve upon a watchdog system, for example I have Node-RED show me the countdown in realtime in Admin, and format nice up / down notification messages which are sent through via different means depending on how long it's been down, etc:

But my hope is that if you start with the basic, stripped-back version I've walked you through above, then you do at the very least have something that works and can be extended upon.

8 Likes

That's an excellent explanation of, as you say, a very simple watchdog.

In fact the original post has trigger nodes labelled "Watchdog", though they are not wired up correctly to work as such.
He also posted a picture of a change node setting msg.action = "subscribe".

Although there is no evidence of the OP of unsubscribing and subscribing to a different topic, presumably that's implied by using dynamic subscription.

So the flow I posted [a picture of] prevents the watchdog barking 10 seconds after a topic is unsubscribed.
No idea if this is appropriate for the OP because his original image does not cover the whole flow and I am having trouble understanding his responses - no doubt language related.

1 Like

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": ""
    }
]

In order to make code readable and usable it is necessary to surround your code with three backticks (also known as a left quote or backquote ```)

``` 
   code goes here 
```

You can edit and correct your post by clicking the pencil :pencil2: icon.

See this post for more details - How to share code or flow json

1 Like

Can you answer a few questions please?

  1. The MQTT topic you send a message to is (for example) "PiServer/97244/Status" or "PiServer/11182/Status"?
  2. Are these the only ESP devices or are there others?
  3. The ESP should respond with "ESP device connected" on topic "ESP/Status/97244" or "ESP/Status/11182"?
  4. How often do you request status from each ESP?

I think you can achieve most of your aims with this simplified flow.
I have omitted writing to the logfile because I'm totally confused by your filename generator etc nodes! The link node "To log file" does nothing (yet).
Note that there is only one MQTT-out and one MQTT-in node. No dynamic subscriptions needed.
I'm pretty sure the two function nodes will need some changes, I just copied these from your flow.

[{"id":"f15d15d4b36ef0b0","type":"group","z":"8ce1e49d586df17b","name":"Send status request MQTT messages and start watchdog trigger for the device","style":{"label":true},"nodes":["dab063679b1bc77c","f04fd78109bef55a","3991d6d669466aeb"],"x":34,"y":499,"w":752,"h":122},{"id":"dab063679b1bc77c","type":"inject","z":"8ce1e49d586df17b","g":"f15d15d4b36ef0b0","name":"Request status 97244","props":[{"p":"payload"},{"p":"topic","vt":"str"},{"p":"replytopic","v":"ESP/Status/97244","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"PiServer/97244/Status","payload":"","payloadType":"str","x":180,"y":540,"wires":[["f04fd78109bef55a","2d2f6db8d569b553"]]},{"id":"f04fd78109bef55a","type":"mqtt out","z":"8ce1e49d586df17b","g":"f15d15d4b36ef0b0","name":"Request status using msg.topic","topic":"","qos":"0","retain":"","respTopic":"","contentType":"","userProps":"","correl":"","expiry":"","broker":"50f2d76bef2a77d6","x":630,"y":540,"wires":[]},{"id":"3991d6d669466aeb","type":"inject","z":"8ce1e49d586df17b","g":"f15d15d4b36ef0b0","name":"Request status 11182","props":[{"p":"payload"},{"p":"topic","vt":"str"},{"p":"replytopic","v":"ESP/Status/11182","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"PiServer/11182/Status","payload":"","payloadType":"str","x":180,"y":580,"wires":[["f04fd78109bef55a","2d2f6db8d569b553"]]},{"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":""},{"id":"d64d4a3fcc2461d1","type":"group","z":"8ce1e49d586df17b","name":"Subscribe to all ESP response topics using wildcard","style":{"label":true},"nodes":["2d2f6db8d569b553","f9c1edfe2ac2b0ca","8c808590555a275d","443933c1425ad646","58bb51869b5d301e","cf4e01d2ba7d102f","c02f4eb45f7def4f"],"x":34,"y":639,"w":972,"h":202},{"id":"2d2f6db8d569b553","type":"trigger","z":"8ce1e49d586df17b","g":"d64d4a3fcc2461d1","name":"Trigger 10s seperately for each device topic","op1":"","op2":"","op1type":"nul","op2type":"pay","duration":"10","extend":false,"overrideDelay":false,"units":"s","reset":"ESP device connected","bytopic":"topic","topic":"topic","outputs":1,"x":570,"y":720,"wires":[["58bb51869b5d301e","cf4e01d2ba7d102f"]]},{"id":"f9c1edfe2ac2b0ca","type":"mqtt in","z":"8ce1e49d586df17b","g":"d64d4a3fcc2461d1","name":"","topic":"ESP/Status/#","qos":"2","datatype":"auto-detect","broker":"50f2d76bef2a77d6","nl":false,"rap":true,"rh":0,"inputs":0,"x":130,"y":760,"wires":[["2d2f6db8d569b553","443933c1425ad646","c02f4eb45f7def4f"]]},{"id":"8c808590555a275d","type":"comment","z":"8ce1e49d586df17b","g":"d64d4a3fcc2461d1","name":"Response within 10s cancels trigger","info":"","x":540,"y":680,"wires":[]},{"id":"443933c1425ad646","type":"debug","z":"8ce1e49d586df17b","g":"d64d4a3fcc2461d1","name":"Valid response","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":860,"y":800,"wires":[]},{"id":"58bb51869b5d301e","type":"debug","z":"8ce1e49d586df17b","g":"d64d4a3fcc2461d1","name":"Failure to respond","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":870,"y":680,"wires":[]},{"id":"cf4e01d2ba7d102f","type":"function","z":"8ce1e49d586df17b","g":"d64d4a3fcc2461d1","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":870,"y":720,"wires":[["49b12a9ede613cfa"]]},{"id":"c02f4eb45f7def4f","type":"function","z":"8ce1e49d586df17b","g":"d64d4a3fcc2461d1","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":870,"y":760,"wires":[["49b12a9ede613cfa"]]},{"id":"c89298d79b26dd63","type":"group","z":"8ce1e49d586df17b","name":"To log file (details omitted)","style":{"label":true},"nodes":["49b12a9ede613cfa"],"x":1034,"y":699,"w":172,"h":82},{"id":"49b12a9ede613cfa","type":"link out","z":"8ce1e49d586df17b","g":"c89298d79b26dd63","name":"To log file","mode":"link","links":[],"x":1120,"y":740,"wires":[],"l":true}]

Please take a look at this, understand it and then test it.
Report back with your results. :grinning:

Hi @jbudd,

  1. both,
  2. for now only two
  3. on both
  4. 10 seconds interval

i will update you with the later part tomorrow, its late here already

but i got your point ill try and update you