# Check if ping fails 3 times

**URL:** <https://discourse.nodered.org/t/check-if-ping-fails-3-times/54267>\
**Category:** General\
**Created:** [25 November 2021 11:52 UTC](https://discourse.nodered.org/t/check-if-ping-fails-3-times/54267 "2021-11-25T11:52:42Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ketpa](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ketpa/32/42496_2.png) [@ketpa](https://discourse.nodered.org/u/ketpa)\
**Post date:** [25 November 2021 11:52 UTC](https://discourse.nodered.org/t/check-if-ping-fails-3-times/54267/1 "2021-11-25T11:52:42Z")

</div>

I send ping to hosts and check if fails, and I have a lot of alarms.  
How to check if ping false 3 times. When only 2 times or 1 times reset counter to 0.

```auto
[{"id":"f7fb5a53.530398","type":"inject","z":"1842eb59.625d05","name":"array of objects with host ip and name","topic":"","payload":"[{\"name\":\"T1\",\"host\":\"10.0.0.71\",\"opis_ip_alarm\":\"ip alarm 1\"},{\"name\":\"T2\",\"host\":\"10.0.0.72\",\"opis_ip_alarm\":null}]","payloadType":"json","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":210,"y":440,"wires":[["fa19be08.bdf9b8"]]},{"id":"dee73236.1b36f","type":"debug","z":"1842eb59.625d05","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":650,"y":440,"wires":[]},{"id":"fa19be08.bdf9b8","type":"ping","z":"1842eb59.625d05","mode":"triggered","name":"","host":"","timer":"20","inputs":1,"x":470,"y":440,"wires":[["dee73236.1b36f"]]}]

```

for tests I create something like this

```auto
[{"id":"482675f2.756a44","type":"inject","z":"1842eb59.625d05","name":"","topic":"10.0.0.1","payload":"false","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":130,"y":220,"wires":[["bb3f6ca3.d3fae8"]]},{"id":"bb3f6ca3.d3fae8","type":"function","z":"1842eb59.625d05","name":"","func":"\nreturn msg;","outputs":1,"noerr":0,"x":290,"y":280,"wires":[["d252ecf8.8d0358","df2ce6ec.8cbf28"]]},{"id":"6ba48bec.f5a0ec","type":"inject","z":"1842eb59.625d05","name":"","topic":"10.0.0.2","payload":"1","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":120,"y":340,"wires":[["bb3f6ca3.d3fae8"]]},{"id":"1fae7899.5f01af","type":"inject","z":"1842eb59.625d05","name":"","topic":"10.0.0.1","payload":"1","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":120,"y":260,"wires":[["bb3f6ca3.d3fae8"]]},{"id":"be9a5e97.c1c1a8","type":"inject","z":"1842eb59.625d05","name":"","topic":"10.0.0.2","payload":"false","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":130,"y":300,"wires":[["bb3f6ca3.d3fae8"]]},{"id":"d252ecf8.8d0358","type":"debug","z":"1842eb59.625d05","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":470,"y":200,"wires":[]},{"id":"df2ce6ec.8cbf28","type":"function","z":"1842eb59.625d05","name":"","func":"var arr = flow.get('arr1') || [];\nvar counter = 0;\nvar isExist = 0;\n\nif (!arr.length){\n arr.push({\n \"ip\": msg.topic, \n \"state\": msg.payload,\n \"counter\": counter\n });\n flow.set(\"arr1\", arr);\n}\n\n\nfor (var i = 0; i < arr.length; i++) {\n \nif(arr[i].counter==3 & arr[i].ip == msg.topic & arr[i].state == msg.payload )\n{\n arr[i].counter =0;\n \n} \n \nif(arr[i].counter<3 & arr[i].ip == msg.topic & arr[i].state == msg.payload)\n{\n arr[i].counter=arr[i].counter +1\n \n}\n\n// zeruje jezeli state > 0\nif(arr[i].counter>0 & arr[i].ip == msg.topic & msg.payload >0)\n{\n arr[i].counter=0;\n arr[i].state = msg.payload;\n \n}\n\n}\n\n\n\nvar exists = arr.some(c => c.ip === msg.topic);\nif (!exists){\n arr.push({\n \"ip\": msg.topic, \n \"state\": msg.payload,\n \"counter\": counter\n });\n \n}\nmsg.exists = exists;\n\nflow.set(\"arr1\", arr);\n\n\nmsg.arr = flow.get(\"arr1\");\n\n\nmsg.payload = arr;\nreturn msg;","outputs":1,"noerr":0,"x":470,"y":280,"wires":[["fd6791de.6738e8"]]},{"id":"fd6791de.6738e8","type":"debug","z":"1842eb59.625d05","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":630,"y":280,"wires":[]}]

```

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [25 November 2021 13:48 UTC](https://discourse.nodered.org/t/check-if-ping-fails-3-times/54267/2 "2021-11-25T13:48:46Z")

</div>

Do you need it to fail a certain number of times or would it be good enough if it failed for a particular length of time? The latter is very easy to accomplish using a Trigger node set to Send Nothing then wait for the appropriate time then Send the alarm message. Then arrange for a good ping to Reset the trigger node (you can specify a payload value to do that in the Trigger node, so that if a good ping appears in that time then it will cancel the alarm message.

---

<div class="post-metadata">

**Author:** ![cflurin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cflurin/32/29_2.png) [@cflurin](https://discourse.nodered.org/u/cflurin)\
**Post date:** [26 November 2021 01:23 UTC](https://discourse.nodered.org/t/check-if-ping-fails-3-times/54267/3 "2021-11-26T01:23:46Z")

</div>

As an option have look at the dsm Wiki:

> **[ping notification · cflurin/node-red-contrib-dsm Wiki](https://github.com/cflurin/node-red-contrib-dsm/wiki/ping-notification)**
>
> A dynamic state machine node based on the Finite state machine. - ping notification · cflurin/node-red-contrib-dsm Wiki

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [25 January 2022 01:24 UTC](https://discourse.nodered.org/t/check-if-ping-fails-3-times/54267/4 "2022-01-25T01:24:11Z")

</div>

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