WatchDog Subflow

Over the last week, I have seen a few posts where a need to monitor the incoming data and to react when it stops.

I have seen a few examples of how this is addressed, and I have taken them and wrapped them up in a cute little subflow.

This subflow Node has the following feature set.

  • Settable Trigger Time
  • Settable Topic
  • Includes the last payload and it's time when the Watch Dog alerts you
  • Passes all messages without modification
  • Can be started in 2 ways
    • Immediately
    • After the first message
  • Outputs via the following
    • 2nd Output Pin
    • Or UDP Broadcast

2nd Output Pin

{
    payload: {
        "lastPayload": Unknown,
        "lastTopic": Unknown,
        "lastPayloadTime": Number
    },
    topic: string(as set on the node)
}

UDP Broadcast

{
    "lastPayload": Unknown,
    "lastTopic": Unknown,
    "lastPayloadTime": Number,
    "topic": string(as set on the node)
}

Anyway - it's nothing major, but maybe helpful for those that need this type of monitoring.
this wasn't a full project - just something I wanted to put together for re-use and fun

[{"id":"b17c7f38b23fb4de","type":"subflow","name":"Watch Dog","info":"","category":"","in":[{"x":40,"y":340,"wires":[{"id":"1b5262aaeb390dae"},{"id":"d83bcd9b3298eb9c"}]}],"out":[{"x":820,"y":400,"wires":[{"id":"9c0a44dc31ef8539","port":0}]},{"x":730,"y":540,"wires":[{"id":"9dd2a8d18e1f2c04","port":0}]}],"env":[{"name":"TOPIC","type":"str","value":"","ui":{"icon":"font-awesome/fa-tag","label":{"en-US":"Topic"},"type":"input","opts":{"types":["str"]}}},{"name":"NOW","type":"bool","value":"false","ui":{"icon":"font-awesome/fa-play","label":{"en-US":"Start immediately"},"type":"checkbox"}},{"name":"TIME","type":"num","value":"30000","ui":{"icon":"font-awesome/fa-clock-o","label":{"en-US":"Threshold (MS)"},"type":"input","opts":{"types":["num"]}}},{"name":"ACTION","type":"str","value":"PIN","ui":{"icon":"font-awesome/fa-gears","label":{"en-US":"Action"},"type":"select","opts":{"opts":[{"l":{"en-US":"2nd Pin Output"},"v":"PIN"},{"l":{"en-US":"UDP Broadcast"},"v":"UDP"}]}}},{"name":"BAddress","type":"str","value":"","ui":{"icon":"font-awesome/fa-server","label":{"en-US":"Broadcast Address"},"type":"input","opts":{"types":["str"]}}},{"name":"BPort","type":"num","value":"","ui":{"icon":"font-awesome/fa-plug","label":{"en-US":"Broadcast Port"},"type":"input","opts":{"types":["num"]}}}],"meta":{},"color":"#3FADB5","outputLabels":["Pass Through","Action"],"icon":"font-awesome/fa-user-secret","status":{"x":1120,"y":80,"wires":[{"id":"45e8d783f6c8e214","port":0},{"id":"b07d1819ab4a1950","port":0},{"id":"087a8f75fbb904fe","port":0}]}},{"id":"502e5cc53e846971","type":"inject","z":"b17c7f38b23fb4de","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":true,"onceDelay":"0.1","topic":"","payload":"NONE: Immediate Start","payloadType":"str","x":150,"y":120,"wires":[["9568cfafa35e3f22","69becc8e19e43e71"]]},{"id":"94de3913649870b1","type":"delay","z":"b17c7f38b23fb4de","name":"","pauseType":"delayv","timeout":"0","timeoutUnits":"milliseconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"allowrate":false,"outputs":1,"x":820,"y":260,"wires":[["087a8f75fbb904fe","46e9c074762666e1"]]},{"id":"9568cfafa35e3f22","type":"switch","z":"b17c7f38b23fb4de","name":"","property":"NOW","propertyType":"env","rules":[{"t":"false"},{"t":"true"}],"checkall":"true","repair":false,"outputs":2,"x":390,"y":120,"wires":[["923c5f88313eb057"],["3123e89c81a2c842","81b7e16132e8410d"]]},{"id":"3123e89c81a2c842","type":"change","z":"b17c7f38b23fb4de","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"{\"fill\":\"green\",\"shape\":\"dot\",\"text\":\"Monitoring\"}","tot":"json"}],"action":"","property":"","from":"","to":"","reg":false,"x":620,"y":120,"wires":[["45e8d783f6c8e214"]]},{"id":"923c5f88313eb057","type":"change","z":"b17c7f38b23fb4de","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"{\"fill\":\"yellow\",\"shape\":\"ring\",\"text\":\"Waiting To Start\"}","tot":"json"}],"action":"","property":"","from":"","to":"","reg":false,"x":620,"y":80,"wires":[["b07d1819ab4a1950"]]},{"id":"42a2aca8fbced4dd","type":"change","z":"b17c7f38b23fb4de","name":"","rules":[{"t":"set","p":"reset","pt":"msg","to":"true","tot":"bool"}],"action":"","property":"","from":"","to":"","reg":false,"x":380,"y":260,"wires":[["3123e89c81a2c842","94de3913649870b1"]]},{"id":"d1f13e8250d5817e","type":"delay","z":"b17c7f38b23fb4de","name":"","pauseType":"delay","timeout":"5","timeoutUnits":"milliseconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"allowrate":false,"outputs":1,"x":410,"y":340,"wires":[["81b7e16132e8410d"]]},{"id":"81b7e16132e8410d","type":"change","z":"b17c7f38b23fb4de","name":"","rules":[{"t":"set","p":"delay","pt":"msg","to":"$number($env('TIME'))","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":640,"y":340,"wires":[["94de3913649870b1"]]},{"id":"087a8f75fbb904fe","type":"change","z":"b17c7f38b23fb4de","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"{\"fill\":\"red\",\"shape\":\"dot\",\"text\":\"Triggered\"}","tot":"json"}],"action":"","property":"","from":"","to":"","reg":false,"x":980,"y":200,"wires":[[]]},{"id":"46e9c074762666e1","type":"link out","z":"b17c7f38b23fb4de","name":"link out 18","mode":"link","links":["0d6dfac1af842c67"],"x":945,"y":260,"wires":[]},{"id":"0d6dfac1af842c67","type":"link in","z":"b17c7f38b23fb4de","name":"link in 10","links":["46e9c074762666e1"],"x":105,"y":560,"wires":[["3a6d70c83f7464fe"]]},{"id":"3a6d70c83f7464fe","type":"switch","z":"b17c7f38b23fb4de","name":"","property":"ACTION","propertyType":"env","rules":[{"t":"eq","v":"PIN","vt":"str"},{"t":"else"}],"checkall":"true","repair":false,"outputs":2,"x":230,"y":560,"wires":[["9dd2a8d18e1f2c04"],["7f73a3e925609f12"]]},{"id":"69becc8e19e43e71","type":"change","z":"b17c7f38b23fb4de","name":"","rules":[{"t":"set","p":"lastPayload","pt":"flow","to":"payload","tot":"msg","dc":true},{"t":"set","p":"lastTopic","pt":"flow","to":"topic","tot":"msg","dc":true},{"t":"set","p":"lastPayloadTime","pt":"flow","to":"","tot":"date"}],"action":"","property":"","from":"","to":"","reg":false,"x":380,"y":200,"wires":[[]]},{"id":"d83bcd9b3298eb9c","type":"function","z":"b17c7f38b23fb4de","name":"Deep Copy MSG","func":"return {\n    msg:RED.util.cloneMessage(msg)\n}","outputs":1,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":190,"y":340,"wires":[["42a2aca8fbced4dd","d1f13e8250d5817e"]]},{"id":"9dd2a8d18e1f2c04","type":"function","z":"b17c7f38b23fb4de","name":"Notification","func":"return {\n    payload: {\n        lastPayload: flow.get('lastPayload'),\n        lastTopic: flow.get('lastTopic'),\n        lastPayloadTime: flow.get('lastPayloadTime')\n    },\n    topic: env.get('TOPIC'),\n}","outputs":1,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":450,"y":540,"wires":[[]]},{"id":"7f73a3e925609f12","type":"function","z":"b17c7f38b23fb4de","name":"Notification","func":"return {\n    payload: JSON.stringify({\n        lastPayload: flow.get('lastPayload'),\n        lastTopic: flow.get('lastTopic'),\n        lastPayloadTime: flow.get('lastPayloadTime'),\n        topic: env.get('TOPIC')\n    }),\n    ip: env.get('BAddress'),\n    port: env.get('BPort')\n}","outputs":1,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":450,"y":600,"wires":[["a8c37c11cbb16ba2"]]},{"id":"a8c37c11cbb16ba2","type":"udp out","z":"b17c7f38b23fb4de","name":"","addr":"","iface":"","port":"","ipv":"udp4","outport":"","base64":false,"multicast":"broad","x":730,"y":600,"wires":[]},{"id":"45e8d783f6c8e214","type":"junction","z":"b17c7f38b23fb4de","x":780,"y":120,"wires":[[]]},{"id":"b07d1819ab4a1950","type":"junction","z":"b17c7f38b23fb4de","x":780,"y":80,"wires":[[]]},{"id":"9c0a44dc31ef8539","type":"junction","z":"b17c7f38b23fb4de","x":200,"y":400,"wires":[["69becc8e19e43e71"]]},{"id":"1b5262aaeb390dae","type":"junction","z":"b17c7f38b23fb4de","x":120,"y":400,"wires":[["9c0a44dc31ef8539"]]},{"id":"12085eb00806f5fb","type":"subflow:b17c7f38b23fb4de","z":"7de832c8bdc79726","name":"","x":410,"y":260,"wires":[[],[]]}]
2 Likes

Nice, Added to my library, will doubtless use that.

I did change the node colour though back to the default for a sub-flow as the original colour makes it look like a D2 node! :slight_smile:

1 Like

I should state, there was little in the way of quality contol here :smiley:

But logically, should be sound.

Hey, its a foundation if nothing else. It is a common enough use-case and, like most of us I guess, I end up reinventing a flow each time.

1 Like

So the first output sends message every time a message is recieved
and the second output is triggered when it doesn't recieve a message within the threshold.
Is it possible to get another special message that communications has been re-established ? or should i add another few nodes with logic at output to give me a message when communication restore ?

Correct

The first Pin is nothing but a direct pass-through (nothing is changed on the message), so in affect, if something in your flow is triggered due to the 2nd output, you can silence the alarms so to speak, on the output of Pin 1.

Pin 2 will include the last message that was received (once triggered) -and of course what was output on Pin 1, before the alert itself

EDIT
You could also attach a status node to it.

  • Triggered (nothing received within the threshold)
  • Monitoring (everything is rosy)

Just a small feature update to this WatchDog subflow


I have now added a new Action -> Execute Terminal Command

This Action executes a terminal command, this can be useful to trigger actions outside of Node RED - say to reboot a device, restart a service or something else - what ever you want really.

The output of the command is sent to Pin 2.
It's output is very similar to that of the 2nd Output Pin option, just with some slightly altered format

{
  payload: <output of the command>,
  cause: {
    "lastPayload": Unknown,
    "lastTopic": Unknown,
    "lastPayloadTime": Number
  },
  topic: string(as set on the node)
}

Enjoy (Below)

[{"id":"b17c7f38b23fb4de","type":"subflow","name":"Watch Dog","info":"","category":"","in":[{"x":40,"y":340,"wires":[{"id":"1b5262aaeb390dae"},{"id":"d83bcd9b3298eb9c"}]}],"out":[{"x":820,"y":400,"wires":[{"id":"9c0a44dc31ef8539","port":0}]},{"x":810,"y":500,"wires":[{"id":"9dd2a8d18e1f2c04","port":0},{"id":"4aac61c07977a805","port":0}]}],"env":[{"name":"TOPIC","type":"str","value":"","ui":{"icon":"font-awesome/fa-tag","label":{"en-US":"Topic"},"type":"input","opts":{"types":["str"]}}},{"name":"NOW","type":"bool","value":"false","ui":{"icon":"font-awesome/fa-play","label":{"en-US":"Start immediately"},"type":"checkbox"}},{"name":"TIME","type":"num","value":"30000","ui":{"icon":"font-awesome/fa-clock-o","label":{"en-US":"Threshold (MS)"},"type":"input","opts":{"types":["num"]}}},{"name":"ACTION","type":"str","value":"PIN","ui":{"icon":"font-awesome/fa-gears","label":{"en-US":"Action"},"type":"select","opts":{"opts":[{"l":{"en-US":"2nd Pin Output"},"v":"PIN"},{"l":{"en-US":"Execute Terminal Command"},"v":"CMD"},{"l":{"en-US":"UDP Broadcast"},"v":"UDP"}]}}},{"name":"BAddress","type":"str","value":"","ui":{"icon":"font-awesome/fa-server","label":{"en-US":"Broadcast Address"},"type":"input","opts":{"types":["str"]}}},{"name":"BPort","type":"num","value":"","ui":{"icon":"font-awesome/fa-plug","label":{"en-US":"Broadcast Port"},"type":"input","opts":{"types":["num"]}}},{"name":"CMD","type":"str","value":"","ui":{"icon":"font-awesome/fa-terminal","label":{"en-US":"Command"},"type":"input","opts":{"types":["str"]}}}],"meta":{},"color":"#3FADB5","outputLabels":["Pass Through","Action"],"icon":"font-awesome/fa-user-secret","status":{"x":1120,"y":80,"wires":[{"id":"45e8d783f6c8e214","port":0},{"id":"b07d1819ab4a1950","port":0},{"id":"087a8f75fbb904fe","port":0}]}},{"id":"502e5cc53e846971","type":"inject","z":"b17c7f38b23fb4de","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":true,"onceDelay":"0.1","topic":"","payload":"NONE: Immediate Start","payloadType":"str","x":150,"y":120,"wires":[["9568cfafa35e3f22","69becc8e19e43e71"]]},{"id":"94de3913649870b1","type":"delay","z":"b17c7f38b23fb4de","name":"","pauseType":"delayv","timeout":"0","timeoutUnits":"milliseconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"allowrate":false,"outputs":1,"x":820,"y":260,"wires":[["087a8f75fbb904fe","46e9c074762666e1"]]},{"id":"9568cfafa35e3f22","type":"switch","z":"b17c7f38b23fb4de","name":"","property":"NOW","propertyType":"env","rules":[{"t":"false"},{"t":"true"}],"checkall":"true","repair":false,"outputs":2,"x":390,"y":120,"wires":[["923c5f88313eb057"],["3123e89c81a2c842","81b7e16132e8410d"]]},{"id":"3123e89c81a2c842","type":"change","z":"b17c7f38b23fb4de","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"{\"fill\":\"green\",\"shape\":\"dot\",\"text\":\"Monitoring\"}","tot":"json"}],"action":"","property":"","from":"","to":"","reg":false,"x":620,"y":120,"wires":[["45e8d783f6c8e214"]]},{"id":"923c5f88313eb057","type":"change","z":"b17c7f38b23fb4de","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"{\"fill\":\"yellow\",\"shape\":\"ring\",\"text\":\"Waiting To Start\"}","tot":"json"}],"action":"","property":"","from":"","to":"","reg":false,"x":620,"y":80,"wires":[["b07d1819ab4a1950"]]},{"id":"42a2aca8fbced4dd","type":"change","z":"b17c7f38b23fb4de","name":"","rules":[{"t":"set","p":"reset","pt":"msg","to":"true","tot":"bool"}],"action":"","property":"","from":"","to":"","reg":false,"x":380,"y":260,"wires":[["3123e89c81a2c842","94de3913649870b1"]]},{"id":"d1f13e8250d5817e","type":"delay","z":"b17c7f38b23fb4de","name":"","pauseType":"delay","timeout":"5","timeoutUnits":"milliseconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"allowrate":false,"outputs":1,"x":410,"y":340,"wires":[["81b7e16132e8410d"]]},{"id":"81b7e16132e8410d","type":"change","z":"b17c7f38b23fb4de","name":"","rules":[{"t":"set","p":"delay","pt":"msg","to":"$number($env('TIME'))","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":640,"y":340,"wires":[["94de3913649870b1"]]},{"id":"087a8f75fbb904fe","type":"change","z":"b17c7f38b23fb4de","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"{\"fill\":\"red\",\"shape\":\"dot\",\"text\":\"Triggered\"}","tot":"json"}],"action":"","property":"","from":"","to":"","reg":false,"x":980,"y":200,"wires":[[]]},{"id":"46e9c074762666e1","type":"link out","z":"b17c7f38b23fb4de","name":"link out 18","mode":"link","links":["0d6dfac1af842c67"],"x":945,"y":260,"wires":[]},{"id":"0d6dfac1af842c67","type":"link in","z":"b17c7f38b23fb4de","name":"link in 10","links":["46e9c074762666e1"],"x":65,"y":560,"wires":[["3a6d70c83f7464fe"]]},{"id":"3a6d70c83f7464fe","type":"switch","z":"b17c7f38b23fb4de","name":"","property":"ACTION","propertyType":"env","rules":[{"t":"eq","v":"PIN","vt":"str"},{"t":"eq","v":"CMD","vt":"str"},{"t":"else"}],"checkall":"true","repair":false,"outputs":3,"x":170,"y":560,"wires":[["9dd2a8d18e1f2c04"],["767263a1eff7ce2a"],["7f73a3e925609f12"]]},{"id":"69becc8e19e43e71","type":"change","z":"b17c7f38b23fb4de","name":"","rules":[{"t":"set","p":"lastPayload","pt":"flow","to":"payload","tot":"msg","dc":true},{"t":"set","p":"lastTopic","pt":"flow","to":"topic","tot":"msg","dc":true},{"t":"set","p":"lastPayloadTime","pt":"flow","to":"","tot":"date"}],"action":"","property":"","from":"","to":"","reg":false,"x":380,"y":200,"wires":[[]]},{"id":"d83bcd9b3298eb9c","type":"function","z":"b17c7f38b23fb4de","name":"Deep Copy MSG","func":"return {\n    msg:RED.util.cloneMessage(msg)\n}","outputs":1,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":190,"y":340,"wires":[["42a2aca8fbced4dd","d1f13e8250d5817e"]]},{"id":"9dd2a8d18e1f2c04","type":"function","z":"b17c7f38b23fb4de","name":"Notification","func":"return {\n    payload: {\n        lastPayload: flow.get('lastPayload'),\n        lastTopic: flow.get('lastTopic'),\n        lastPayloadTime: flow.get('lastPayloadTime')\n    },\n    topic: env.get('TOPIC'),\n}","outputs":1,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":450,"y":500,"wires":[[]]},{"id":"7f73a3e925609f12","type":"function","z":"b17c7f38b23fb4de","name":"Notification","func":"return {\n    payload: JSON.stringify({\n        lastPayload: flow.get('lastPayload'),\n        lastTopic: flow.get('lastTopic'),\n        lastPayloadTime: flow.get('lastPayloadTime'),\n        topic: env.get('TOPIC')\n    }),\n    ip: env.get('BAddress'),\n    port: env.get('BPort')\n}","outputs":1,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":450,"y":620,"wires":[["a8c37c11cbb16ba2"]]},{"id":"a8c37c11cbb16ba2","type":"udp out","z":"b17c7f38b23fb4de","name":"","addr":"","iface":"","port":"","ipv":"udp4","outport":"","base64":false,"multicast":"broad","x":730,"y":620,"wires":[]},{"id":"a1c53719a8c21a43","type":"exec","z":"b17c7f38b23fb4de","command":"","addpay":"payload","append":"","useSpawn":"false","timer":"","winHide":false,"oldrc":false,"name":"","x":490,"y":560,"wires":[["4aac61c07977a805"],[],[]]},{"id":"4aac61c07977a805","type":"function","z":"b17c7f38b23fb4de","name":"Output","func":"return {\n    payload: msg.payload,\n    topic: env.get('TOPIC'),\n    cause: {\n        lastPayload: flow.get('lastPayload'),\n        lastTopic: flow.get('lastTopic'),\n        lastPayloadTime: flow.get('lastPayloadTime')\n    },\n}","outputs":1,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":650,"y":560,"wires":[[]]},{"id":"767263a1eff7ce2a","type":"change","z":"b17c7f38b23fb4de","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"$env('CMD')","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":340,"y":560,"wires":[["a1c53719a8c21a43"]]},{"id":"45e8d783f6c8e214","type":"junction","z":"b17c7f38b23fb4de","x":780,"y":120,"wires":[[]]},{"id":"b07d1819ab4a1950","type":"junction","z":"b17c7f38b23fb4de","x":780,"y":80,"wires":[[]]},{"id":"9c0a44dc31ef8539","type":"junction","z":"b17c7f38b23fb4de","x":200,"y":400,"wires":[["69becc8e19e43e71"]]},{"id":"1b5262aaeb390dae","type":"junction","z":"b17c7f38b23fb4de","x":120,"y":400,"wires":[["9c0a44dc31ef8539"]]},{"id":"0c68929067bedbab","type":"subflow:b17c7f38b23fb4de","z":"2d7bf6e3.84c97a","name":"","x":290,"y":700,"wires":[[],[]]}]

Note: As this is a sub flow, one is free to change it as they see fit, as per @TotallyInformation says, it's a foundation - alter it to suit your needs