Ping Network Devices From a List

Good day,

I am looking for a way to ping network devices (that I specify) from a list or database in Node-RED.

I am using the "ping" node at the moment but I have to use one ping node per device which is very time consuming when I want to look at 50 devices.

Can someone please assist me with a how to? Thank you for looking!

pingnode|217x500

Node-red-node-ping can send multiple pings from an array sent in msg.payload

1 Like

Just select triggered mode and feed it an array of addresses
e.g.

[{"id":"1cc498a0.86e8f7","type":"ping","z":"5a245aa1.510164","protocol":"Automatic","mode":"triggered","name":"","host":"","timer":"20","inputs":1,"x":470,"y":4060,"wires":[["ce512a59.9f92b8"]]},{"id":"3fa5e753.811c28","type":"inject","z":"5a245aa1.510164","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"[\"192.168.1.1\",\"192.168.1.12\",\"192.168.1.13\",\"192.168.1.14\"]","payloadType":"json","x":160,"y":4060,"wires":[["1cc498a0.86e8f7"]]},{"id":"ce512a59.9f92b8","type":"debug","z":"5a245aa1.510164","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":520,"y":3880,"wires":[]}]
1 Like

Hi Steve, thank guys for pointing me in the right direction.

could you please assist in how to add telegram to the following flow that i found which i think could work, just needs telegram integration

[{"id":"7c2d32d2.d0546c","type":"tab","label":"Flow 3","disabled":false,"info":""},{"id":"1e62b528.9d10bb","type":"exec","z":"7c2d32d2.d0546c","command":"ping -c 1","addpay":true,"append":"","useSpawn":"false","timer":"5","oldrc":false,"name":"","x":340,"y":360,"wires":[["341b3327.c9a5fc"],["b9a1be69.c25bd"],["cbe0d02.fccf63"]]},{"id":"b9073504.221c28","type":"split","z":"7c2d32d2.d0546c","name":"","splt":"\n","spltType":"str","arraySplt":1,"arraySpltType":"len","stream":false,"addname":"","x":550,"y":220,"wires":[["5b3729dc.f64948"]]},{"id":"341b3327.c9a5fc","type":"function","z":"7c2d32d2.d0546c","name":"update thisPing","func":"\nmsg.thisPing.timestamp = new Date();\nmsg.thisPing.message = msg.payload;\n\nreturn msg;","outputs":1,"noerr":0,"x":580,"y":320,"wires":[["fa53225b.adcc8","90351547.4c6bc8"]]},{"id":"b9a1be69.c25bd","type":"function","z":"7c2d32d2.d0546c","name":"update thisPing","func":"\nmsg.thisPing.timestamp = new Date();\nmsg.thisPing.message = msg.payload;\nmsg.thisPing.error = true;\nmsg.thisPing.state = 'ng'\n\n\n\nreturn msg;","outputs":1,"noerr":0,"x":580,"y":360,"wires":[["e923153f.97a418","90351547.4c6bc8"]]},{"id":"cbe0d02.fccf63","type":"function","z":"7c2d32d2.d0546c","name":"update thisPing","func":"msg.thisPing.timestamp = new Date();\nmsg.thisPing.result = msg.payload;\nmsg.thisPing.error = msg.payload.code !== 0;\nmsg.thisPing.state = msg.thisPing.error === false ? "ok" : "ng";\n\n\nreturn msg;","outputs":1,"noerr":0,"x":580,"y":400,"wires":[["da784992.367698","90351547.4c6bc8"]]},{"id":"5b3729dc.f64948","type":"function","z":"7c2d32d2.d0546c","name":"Init thisPing","func":"let pingResults = flow.get("pingResults") || {}\nmsg.thisPing = pingResults[msg.topic] || {};\n\nmsg.thisPing.name = msg.payload.name;\nmsg.thisPing.ip = msg.payload.ip;\nmsg.thisPing.timestamp = new Date();\nmsg.thisPing.result = '';\nmsg.thisPing.error = null;\nmsg.thisPing.state = 'pinging';\n\nmsg.topic = msg.thisPing.name;\nmsg.payload = msg.thisPing.ip;\n\npingResults[msg.topic] = msg.thisPing;//store ping obj in pingResults\nflow.set("pingResults",pingResults);//save pingResults in flow context\n\nreturn msg;","outputs":1,"noerr":0,"x":710,"y":220,"wires":[["1e62b528.9d10bb","8cb06fb6.e611"]]},{"id":"8cb06fb6.e611","type":"debug","z":"7c2d32d2.d0546c","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":930,"y":220,"wires":},{"id":"fa53225b.adcc8","type":"debug","z":"7c2d32d2.d0546c","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":930,"y":320,"wires":},{"id":"e923153f.97a418","type":"debug","z":"7c2d32d2.d0546c","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":930,"y":360,"wires":},{"id":"da784992.367698","type":"debug","z":"7c2d32d2.d0546c","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":930,"y":400,"wires":},{"id":"90351547.4c6bc8","type":"function","z":"7c2d32d2.d0546c","name":"Update flow context","func":"let pingResults = flow.get("pingResults") || {}\nlet thisPing = msg.thisPing;\n\n//store ping item\npingResults[thisPing.name] = thisPing;\n\n//save pingResults in flow context\nflow.set("pingResults",pingResults);\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":890,"y":460,"wires":[]},{"id":"b8ad89e6.0932d8","type":"template","z":"7c2d32d2.d0546c","name":"build html table","field":"html","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"\n<table width="100%">\n \n Name\n IP \n Status \n \n {{#tableData}}\n \n {{{name}}}\n {{ip}} \n {{{stateInfo}}} \n \n {{/tableData}}\n","x":720,"y":580,"wires":[["d9d3e9e2.bcee58","8f4e870e.6df048"]]},{"id":"d9d3e9e2.bcee58","type":"debug","z":"7c2d32d2.d0546c","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":930,"y":540,"wires":},{"id":"f10d5f20.f511f","type":"inject","z":"7c2d32d2.d0546c","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"30","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"[{"name":"router","ip":"192.168.1.1"},{"name":"NVR","ip":"192.168.1.30"},{"name":"cam1","ip":"192.168.1.31"},{"name":"bad device","ip":"192.168.2.99"}]","payloadType":"json","x":270,"y":580,"wires":[["f8bd43a0.cf052"]]},{"id":"f8bd43a0.cf052","type":"function","z":"7c2d32d2.d0546c","name":"convert pingResults to array","func":"let pingResults = flow.get("pingResults") || {}\n\nlet tableData = ;\nfor (let k in pingResults) {\n let r = pingResults[k];\n if(r.state == "ok"){\n r.rowclass = "good";\n r.stateInfo = '<i class="fa fa-thumbs-up" aria-hidden="true"> OK'\n } else if(r.state == "ng") {\n r.rowclass = "bad";\n r.stateInfo = '<i class="fa fa-thumbs-down" aria-hidden="true"> NG'\n } else {\n r.rowclass = "unknown";\n r.stateInfo = '<i class="fa question" aria-hidden="true"> Unknown'\n }\n tableData.push(r);\n}\n\nmsg.tableData = tableData\n\nreturn msg;","outputs":1,"noerr":0,"x":480,"y":580,"wires":[["b8ad89e6.0932d8"]]},{"id":"8f4e870e.6df048","type":"ui_template","z":"7c2d32d2.d0546c","group":"94ef3efa.b0b16","name":"","order":3,"width":"6","height":"5","format":"\ntable {\n color: #333;\n font-family: Helvetica, Arial, sans-serif;\n width: 100%;\n border-collapse: collapse;\n border-spacing: 0;\n}\ntd, th {\n border: 1px solid transparent;\n /* No more visible border /\n height: 30px;\n transition: all 0.3s;\n / Simple transition for hover effect /\n}\nth {\n background: #DFDFDF;\n / Darken header a bit /\n font-weight: bold;\n}\ntd {\n background: #FAFAFA;\n text-align: center;\n}\n\n/ Cells in even rows (2,4,6...) are one color /\n\ntr:nth-child(even) td {\n background: #F1F1F1;\n}\n\n/ Cells in odd rows (1,3,5...) are another (excludes header cells) /\n\ntr:nth-child(odd) td {\n background: #FEFEFE;\n}\ntr td:hover {\n background: #666;\n color: #FFF;\n}\n\n/ Hover cell effect! / \n\ntr.good {\n color: green;\n}\n\ntr.bad {\n color: red;\n}\n\ntr.unknown {\n color: orange;\n}\n\n \n\n<div ng-bind-html="msg.html">\n","storeOutMessages":true,"fwdInMessages":true,"templateScope":"local","x":940,"y":580,"wires":[[]]},{"id":"7356cc09.d71024","type":"inject","z":"7c2d32d2.d0546c","name":"** Setup devices to ping here ***","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"[{"name":"router","ip":"192.168.1.1"},{"name":"NVR","ip":"192.168.1.30"},{"name":"cam1","ip":"192.168.1.31"},{"name":"bad device","ip":"192.168.2.99"}]","payloadType":"json","x":270,"y":180,"wires":[["b9073504.221c28"]]},{"id":"94ef3efa.b0b16","type":"ui_group","name":"Default","tab":"6cc8c50b.445afc","order":1,"disp":true,"width":"6","collapse":false},{"id":"6cc8c50b.445afc","type":"ui_tab","name":"Home","icon":"dashboard","disabled":false,"hidden":false}]

2 things...

  1. Your flow is not valid due to not pasting it in a code block, in order to make code more readable and importable it is important to surround your code with three backticks
    ```
    like this
    ```
    You can edit and correct your post by clicking the pencil icon.
    See this post for more details - How to share code or flow json

  2. What does this have to do with pinging network devices from a list?

Thank you Steve.

I have about 50 ip cameras and I was searching for a way to ping them from a list as mentioned before. I then found the attached flow which will work great however I would like to send me a Telegram message when the status change, that will alert me do repair or look into the device that is offline. Thank again for the assisttane

[{"id":"83a23089.dc697","type":"tab","label":"Network Ping","disabled":false,"info":""},{"id":"e80b494.9f3ffb8","type":"exec","z":"83a23089.dc697","command":"ping -c 1","addpay":true,"append":"","useSpawn":"false","timer":"5","oldrc":false,"name":"","x":340,"y":540,"wires":[["dcebcf2c.162d9"],["87273839.35aad8"],["f37c6d51.6df29"]]},{"id":"4792d396.188f5c","type":"split","z":"83a23089.dc697","name":"","splt":"\\n","spltType":"str","arraySplt":1,"arraySpltType":"len","stream":false,"addname":"","x":550,"y":400,"wires":[["4faf6278.986aac"]]},{"id":"dcebcf2c.162d9","type":"function","z":"83a23089.dc697","name":"update thisPing","func":"\nmsg.thisPing.timestamp = new Date();\nmsg.thisPing.message = msg.payload;\n\nreturn msg;","outputs":1,"noerr":0,"x":580,"y":500,"wires":[["cf63f56.6746e08","bae00ed9.43d8e"]]},{"id":"87273839.35aad8","type":"function","z":"83a23089.dc697","name":"update thisPing","func":"\nmsg.thisPing.timestamp = new Date();\nmsg.thisPing.message = msg.payload;\nmsg.thisPing.error = true;\nmsg.thisPing.state = 'ng'\n\n\n\nreturn msg;","outputs":1,"noerr":0,"x":580,"y":540,"wires":[["caef9860.2a9ab8","bae00ed9.43d8e"]]},{"id":"f37c6d51.6df29","type":"function","z":"83a23089.dc697","name":"update thisPing","func":"msg.thisPing.timestamp = new Date();\nmsg.thisPing.result = msg.payload;\nmsg.thisPing.error = msg.payload.code !== 0;\nmsg.thisPing.state = msg.thisPing.error === false ? \"ok\" : \"ng\";\n\n\nreturn msg;","outputs":1,"noerr":0,"x":580,"y":580,"wires":[["1c70a1b5.b65a5e","bae00ed9.43d8e"]]},{"id":"4faf6278.986aac","type":"function","z":"83a23089.dc697","name":"Init thisPing","func":"let pingResults = flow.get(\"pingResults\") || {}\nmsg.thisPing = pingResults[msg.topic] || {};\n\nmsg.thisPing.name = msg.payload.name;\nmsg.thisPing.ip = msg.payload.ip;\nmsg.thisPing.timestamp = new Date();\nmsg.thisPing.result = '';\nmsg.thisPing.error = null;\nmsg.thisPing.state = 'pinging';\n\nmsg.topic = msg.thisPing.name;\nmsg.payload = msg.thisPing.ip;\n\npingResults[msg.topic] = msg.thisPing;//store ping obj in pingResults\nflow.set(\"pingResults\",pingResults);//save pingResults in flow context\n\nreturn msg;","outputs":1,"noerr":0,"x":710,"y":400,"wires":[["e80b494.9f3ffb8","e9e310aa.76a0d"]]},{"id":"e9e310aa.76a0d","type":"debug","z":"83a23089.dc697","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":930,"y":400,"wires":[]},{"id":"cf63f56.6746e08","type":"debug","z":"83a23089.dc697","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":930,"y":500,"wires":[]},{"id":"caef9860.2a9ab8","type":"debug","z":"83a23089.dc697","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":930,"y":540,"wires":[]},{"id":"1c70a1b5.b65a5e","type":"debug","z":"83a23089.dc697","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":930,"y":580,"wires":[]},{"id":"bae00ed9.43d8e","type":"function","z":"83a23089.dc697","name":"Update flow context","func":"let pingResults = flow.get(\"pingResults\") || {}\nlet thisPing = msg.thisPing;\n\n//store ping item\npingResults[thisPing.name] = thisPing;\n\n//save pingResults in flow context\nflow.set(\"pingResults\",pingResults);\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":890,"y":640,"wires":[["3945775b.f70428"]]},{"id":"ee978690.205da8","type":"template","z":"83a23089.dc697","name":"build html table","field":"html","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"\n<table width=\"100%\">\n  <tr>\n    <th>Name</th>\n    <th>IP</th> \n    <th>Status</th> \n  </tr>\n  {{#tableData}}\n  <tr class='{{rowclass}}'>\n    <td>{{{name}}}</td>\n    <td>{{ip}}</td> \n    <td>{{{stateInfo}}}</i></td> \n  </tr>\n  {{/tableData}}\n</table>","x":720,"y":760,"wires":[["7960265a.b5a7f8","3bd11951.e1ee46"]]},{"id":"7960265a.b5a7f8","type":"debug","z":"83a23089.dc697","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":930,"y":720,"wires":[]},{"id":"d038da8f.ce2a68","type":"inject","z":"83a23089.dc697","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"30","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"[{\"name\":\"router\",\"ip\":\"192.168.1.1\"},{\"name\":\"nvr\",\"ip\":\"192.168.1.30\"},{\"name\":\"cam1\",\"ip\":\"192.168.1.31\"},{\"name\":\"bad device\",\"ip\":\"192.168.2.99\"}]","payloadType":"json","x":270,"y":760,"wires":[["436755d5.3c93dc"]]},{"id":"436755d5.3c93dc","type":"function","z":"83a23089.dc697","name":"convert pingResults to array","func":"let pingResults = flow.get(\"pingResults\") || {}\n\nlet tableData = [];\nfor (let k in pingResults) {\n    let r = pingResults[k];\n    if(r.state == \"ok\"){\n        r.rowclass = \"good\";\n        r.stateInfo = '<i class=\"fa fa-thumbs-up\" aria-hidden=\"true\"></i> OK'\n    } else if(r.state == \"ng\") {\n         r.rowclass = \"bad\";\n        r.stateInfo = '<i class=\"fa fa-thumbs-down\" aria-hidden=\"true\"></i> NG'\n    } else {\n        r.rowclass = \"unknown\";\n        r.stateInfo = '<i class=\"fa question\" aria-hidden=\"true\"></i> Unknown'\n    }\n  tableData.push(r);\n}\n\nmsg.tableData = tableData\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":480,"y":760,"wires":[["ee978690.205da8"]]},{"id":"3bd11951.e1ee46","type":"ui_template","z":"83a23089.dc697","group":"94ef3efa.b0b16","name":"","order":3,"width":"6","height":"5","format":"<style>\ntable {\n    color: #333;\n    font-family: Helvetica, Arial, sans-serif;\n    width: 100%;\n    border-collapse: collapse;\n    border-spacing: 0;\n}\ntd, th {\n    border: 1px solid transparent;\n    /* No more visible border */\n    height: 30px;\n    transition: all 0.3s;\n    /* Simple transition for hover effect */\n}\nth {\n    background: #DFDFDF;\n    /* Darken header a bit */\n    font-weight: bold;\n}\ntd {\n    background: #FAFAFA;\n    text-align: center;\n}\n\n/* Cells in even rows (2,4,6...) are one color */\n\ntr:nth-child(even) td {\n    background: #F1F1F1;\n}\n\n/* Cells in odd rows (1,3,5...) are another (excludes header cells)  */\n\ntr:nth-child(odd) td {\n    background: #FEFEFE;\n}\ntr td:hover {\n    background: #666;\n    color: #FFF;\n}\n\n/* Hover cell effect! */    \n\ntr.good {\n    color: green;\n}\n\ntr.bad {\n    color: red;\n}\n\ntr.unknown {\n    color: orange;\n}\n\n    \n</style>\n<div ng-bind-html=\"msg.html\"></div>\n","storeOutMessages":true,"fwdInMessages":true,"templateScope":"local","x":940,"y":760,"wires":[[]]},{"id":"8614c5fd.bf5588","type":"inject","z":"83a23089.dc697","name":"*** Setup devices to ping here ***","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"[{\"name\":\"router\",\"ip\":\"192.168.1.1\"},{\"name\":\"nvr\",\"ip\":\"192.168.1.30\"},{\"name\":\"cam1\",\"ip\":\"192.168.1.31\"},{\"name\":\"bad device\",\"ip\":\"192.168.2.99\"}]","payloadType":"json","x":270,"y":360,"wires":[["4792d396.188f5c"]]},{"id":"b70e1388.f2246","type":"telegram sender","z":"83a23089.dc697","name":"","bot":"","haserroroutput":false,"outputs":1,"x":1270,"y":640,"wires":[[]]},{"id":"3945775b.f70428","type":"rbe","z":"83a23089.dc697","name":"","func":"rbe","gap":"","start":"","inout":"out","property":"payload.content","x":1090,"y":640,"wires":[["b70e1388.f2246"]]},{"id":"94ef3efa.b0b16","type":"ui_group","name":"Default","tab":"6cc8c50b.445afc","order":1,"disp":true,"width":"6","collapse":false},{"id":"6cc8c50b.445afc","type":"ui_tab","name":"Home","icon":"dashboard","disabled":false,"hidden":false}]

Have you got the telegrambot nodes installed?

Have you tried the built in examples? (CTRL+I, examples)

Have you created a bot and got your token yet?

If the answer is no to any of the above questions then give it a go first and when you get stuck pop back and ask a specific question.

Start here: https://flows.nodered.org/node/node-red-contrib-telegrambot

All installed, bot configured, just don know how to get the payload on ping results on telegram...?

I am looking to publish the "OK" or "NG" from the message payload but unsure how to post to telegram

i have the following, but will need to add the chat id etc


let pingResults = flow.get("pingResults") || {}
let thisPing = msg.thisPing;

//store ping item
pingResults[thisPing.name] = thisPing;

//save pingResults in flow context
flow.set("pingResults",pingResults);

return msg;

and here is another


let pingResults = flow.get("pingResults") || {}

let tableData = ;
for (let k in pingResults) {
let r = pingResults[k];
if(r.state == "ok"){
r.rowclass = "good";
r.stateInfo = ' OK'
} else if(r.state == "ng") {
r.rowclass = "bad";
r.stateInfo = ' NG'
} else {
r.rowclass = "unknown";
r.stateInfo = ' Unknown'
}
tableData.push(r);
}

msg.tableData = tableData

return msg;

I did something a bit sneaky... if the cameras are active on the network, sending data to some database, or NR, etc., you can sneak a look at the ARP cache of the destination device.

This avoids a static list. But if a camera does not send data for a long time, its address might disappear from the ARP cache. In my case, that was not an issue. The benefit, as I add something to the network, my flow adapts, without need of changing the static list.

You could even have ARP cache results, generate a static but update-able list, then if a camera disappears it still is in the list, and you could warn that such has happens.

That flow is a bit much for something simple. It is also out of date due to the node-red-node-ping now able to ping multiple devices.

This is a basic working demo...

image

...

  • Import the flow
  • adjust the ping list in the inject.
    • alternatively get this list from a database or other place - just ensure it is the same format.
  • select your bot in the telegram node

flow...

[{"id":"e9e310aa.76a0d","type":"debug","z":"83a23089.dc697","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":1610,"y":80,"wires":[]},{"id":"8614c5fd.bf5588","type":"inject","z":"83a23089.dc697","name":"*** Setup devices to ping here ***","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"[{\"name\":\"router\",\"host\":\"192.168.1.1\"},{\"name\":\"nvr\",\"host\":\"192.168.1.50\"},{\"name\":\"cam1\",\"host\":\"192.168.1.38\"},{\"name\":\"bad device\",\"host\":\"192.168.2.99\"}]","payloadType":"json","x":1230,"y":80,"wires":[["29069ffe.93c78"]]},{"id":"29069ffe.93c78","type":"ping","z":"83a23089.dc697","mode":"triggered","name":"","host":"","timer":"20","inputs":1,"x":1450,"y":80,"wires":[["e9e310aa.76a0d","9774ee36.6c7ff"]]},{"id":"9774ee36.6c7ff","type":"function","z":"83a23089.dc697","name":"Prepare telegram message (if bad)","func":"if(msg.payload === false) {\n    msg.payload = {};\n    msg.payload.type = 'message';\n    msg.payload.content = `Ping to '${msg.ping.name}' (${msg.ping.host}) failed`;\n    msg.payload.chatId = 11111111\n    return msg;\n}\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1380,"y":160,"wires":[["f275d6de.1e4fc8","8d95831d.c0745"]]},{"id":"f275d6de.1e4fc8","type":"debug","z":"83a23089.dc697","name":"to Telegram node","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1650,"y":160,"wires":[]},{"id":"8d95831d.c0745","type":"telegram sender","z":"83a23089.dc697","name":"","bot":"fa7a9376.321ce","haserroroutput":false,"outputs":1,"x":1650,"y":220,"wires":[[]]},{"id":"fa7a9376.321ce","type":"telegram bot","botname":"mybot","usernames":"","chatids":"","baseapiurl":"","updatemode":"polling","pollinterval":"","usesocks":false,"sockshost":"","socksport":"","socksusername":"","sockspassword":"","bothost":"","botpath":"","localbotport":"","publicbotport":"","privatekey":"","certificate":"","useselfsignedcertificate":false,"sslterminated":false,"verboselogging":false}]

PS: when you post code or flow - always put it in a code block

```
code goes between backticks otherwise it looks rubbish and actually doesn't work due to forum formatting!!!
```

1 Like

Thank you so much, Steve that worked great!

Now if I may one last request, can one add the ability to notify when that bad device comes back online? basically the reverse of what you have done?

pingbot

Store the bad result in a lookup object stored in flow or global context.

When the device comes back & it was previously bad, send another message then remove it from the lookup.

Do you know how to make a lookup object? Do you know how to store and receive values in context?

sorry, should added a bumper sticker "noob alert" I am learning as I go. your help is much appreciated

my answer to all your above questions is no I am afraid.

image

change the function to this...

const chatId = 11111111; // << update this

if (!msg.ping.host) return null; //ensure host has a value

var pingMonitor = flow.get("pingMonitor") || {};//get lookup object ||or|| a new empty object
var pingPrev = pingMonitor[msg.ping.host] || {};//this ping monitor object ||or|| a new empty object
var sendTelegram = false;

//if ping is good, you get a number in payload...
if (typeof msg.payload == "number" && pingPrev.status === "bad" ) {
    pingPrev.status = "good";
    msg.payload = {};
    msg.payload.type = 'message';
    msg.payload.content = `'${msg.ping.name}' (${msg.ping.host}) is now back on line`;
    msg.payload.chatId = chatId;
    sendTelegram = true;
} else if (msg.payload === false && (pingPrev.status === "good" || !pingPrev.status)) {
    pingPrev.status = "bad";
    msg.payload = {};
    msg.payload.type = 'message';
    msg.payload.content = `Ping to '${msg.ping.name}' (${msg.ping.host}) failed`;
    msg.payload.chatId = chatId;
    sendTelegram = true;
}

//update context
pingPrev.datetime = new Date();
pingPrev.host = msg.ping.host;
pingPrev.name = msg.ping.name;
pingMonitor[msg.ping.host] = pingPrev;
flow.set("pingMonitor", pingMonitor);

if (sendTelegram) return msg;

return null;

you will also see the status in flow context viewer...

1 Like

Brilliant, thank you Steve works like a charm! very thankful

Hey, I piosted the ARP trick.... where is it? Missing? Oh different thread! Well, since using the ARP cache can avoid statistic lists of devices, here is an example.

As for accessing the ARP cache, I just use an exec node to grab the ARP cache data from the Linux CLI., 'arp -a' will output device name (DNS resolved), device ip, and MAC address as well as the interface of the device that sees the other devices.

For example:

A.domain.org (192.168.1.161) at 50:02:91:d5:8d:95 [ether] on wlan0
B.domain.org (192.168.1.231) at 00:13:ef:60:08:ce [ether] on eth0
C.domain.org (192.168.1.137) at b8:27:eb:8a:b2:ff [ether] on eth0
D.domain.org (192.168.1.6) at on eth0
E.domain.org (192.168.1.168) at bc:dd:c2:53:e8:eb [ether] on eth0

Below is a simple flow using a persistent flow variable to maintain the device list, that is updated as the ARP cache changes, i.e. new devices added. Note my persistent flow variables are defined as 'persistent', any flow variable tagged with 'persistent' is such, and this environment specific, something you configure. See the end of this note for details on using persistent flow variables.

[{"id":"50ac89e1.0ecb","type":"inject","z":"cfa0d8c5.398578","name":"Make List","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":100,"y":40,"wires":[["881e4330.695288"]]},{"id":"c506450a.dc169","type":"split","z":"cfa0d8c5.398578","name":"Split Lines","splt":"\\n","spltType":"str","arraySplt":1,"arraySpltType":"len","stream":false,"addname":"","x":470,"y":40,"wires":[["d93f3fd9.e875a"]]},{"id":"aa6bb798.afacd","type":"debug","z":"cfa0d8c5.398578","name":"Message","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":260,"y":140,"wires":[]},{"id":"881e4330.695288","type":"exec","z":"cfa0d8c5.398578","command":"arp -a","addpay":false,"append":"","useSpawn":"false","timer":"","oldrc":false,"name":"Get ARP Cache","x":280,"y":40,"wires":[["c506450a.dc169"],[],[]]},{"id":"d93f3fd9.e875a","type":"function","z":"cfa0d8c5.398578","name":"Add To Array","func":"// Just Device Name...\nvar theName = msg.payload.split(' ')[0];\n\nvar theDevices = flow.get('devices', 'persistent') || [],\n    theDevice = theDevices.map(theDevice => theDevice).indexOf(theName);\n\nif (theDevice != -1) {\n\n    // Update Existing Entry?  Not Really Needed Right?\n\n} else {\n    \n    // Add Entry...\n    theDevices.push(theName);\n    \n    flow.set('devices', theDevices, 'persistent');\n    \n}\n\n\nreturn msg;","outputs":0,"noerr":0,"initialize":"","finalize":"","x":650,"y":40,"wires":[]},{"id":"19bcf5ec.4323da","type":"inject","z":"cfa0d8c5.398578","name":"Show List","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"#:(persistent)::devices","payloadType":"flow","x":100,"y":140,"wires":[["aa6bb798.afacd"]]}]

If you need details on persistence of flow variables...
https://discourse.nodered.org/t/a-guide-to-understanding-persistent-context/4115

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