ping notification - cflurin/node-red-contrib-dsm GitHub Wiki

node-red-node-ping nodes can be used to watch the availability of devices (rpi's, pc's, AP's, cameras, sensors etc.) in a network.

The dsm ping notification manages for each device the status and a counter to filter single failures. For example you can set the limit to 3, so when the counter reaches the limit of 3 a notification is sent once. After the ping is ok again an ok notification is sent.

The number of devices (ping nodes) are unlimited.

Note: Please, install the latest dsm version.

ping

Configuration

{
    "inputTrigger": "cmd",
    "outputState": "current",
    "currentState": "ok",
        "states": {
            "ok": {
                "ok": "ok",
                "timeout": "failed"
            },
            "failed": {
                "ok": "ok",
                "timeout": "failed"
            }           
        },
    "data": {
        "limit": 3,
        "count": {}
    },
    "methods": {
        "default": [
            "sm.ip = msg.topic;",
            
            "if (typeof sm.data.count[sm.ip] === 'undefined') {",
            "   sm.data.count[sm.ip] = 0;",
            "};",
                        
            "if (!msg.payload) {",
            "   resume('timeout', msg);",
            "} else {",
            "   resume('ok', msg);",
            "}",
            "output = false;"
        ],
        "ok": [
            "sm.text = sm.ip + ' ok';",
            "if (sm.data.count[sm.ip] >= sm.data.limit) {",
            "   msg.payload = sm.text;",
            "   msg.topic = 'ping ok';",
            "} else {",
            "   output = false;",
            "};",
            "sm.data.count[sm.ip] = 0;"
        ],
        "timeout": [
            "sm.data.count[sm.ip]++;",
            
            "sm.text = sm.ip + ' failed, count ' + sm.data.count[sm.ip];",
            "if (sm.data.count[sm.ip] === sm.data.limit) {",
            "   msg.payload = sm.text;",
            "   msg.topic = 'ping failed';",           
            "} else {",
            "   output = false;",
            "};"
        ],
        "status": {
            "fill": {
                "get": "sm.currentState === 'ok' ? 'green': 'red';"
            },
            "shape": "dot",
            "text": {
                "get": "sm.text;"
            }
        }
    }
}

Flow

[{"id":"d4a9edca.cbb66","type":"dsm","z":"6ff0723.8c6b78c","name":"ping notification","sm_config":"{\n    \"inputTrigger\": \"cmd\",\n    \"outputState\": \"current\",\n    \"currentState\": \"ok\",\n        \"states\": {\n            \"ok\": {\n                \"ok\": \"ok\",\n                \"timeout\": \"failed\"\n            },\n            \"failed\": {\n                \"ok\": \"ok\",\n                \"timeout\": \"failed\"\n            }           \n        },\n    \"data\": {\n        \"limit\": 3,\n        \"count\": {}\n    },\n    \"methods\": {\n        \"default\": [\n            \"sm.ip = msg.topic;\",\n            \n            \"if (typeof sm.data.count[sm.ip] === 'undefined') {\",\n            \"   sm.data.count[sm.ip] = 0;\",\n            \"};\",\n                        \n            \"if (!msg.payload) {\",\n            \"   resume('timeout', msg);\",\n            \"} else {\",\n            \"   resume('ok', msg);\",\n            \"}\",\n            \"output = false;\"\n        ],\n        \"ok\": [\n            \"sm.text = sm.ip + ' ok';\",\n            \"if (sm.data.count[sm.ip] >= sm.data.limit) {\",\n            \"   msg.payload = sm.text;\",\n            \"   msg.topic = 'ping ok';\",\n            \"} else {\",\n            \"   output = false;\",\n            \"};\",\n            \"sm.data.count[sm.ip] = 0;\"\n        ],\n        \"timeout\": [\n            \"sm.data.count[sm.ip]++;\",\n            \n            \"sm.text = sm.ip + ' failed, count ' + sm.data.count[sm.ip];\",\n            \"if (sm.data.count[sm.ip] === sm.data.limit) {\",\n            \"   msg.payload = sm.text;\",\n            \"   msg.topic = 'ping failed';\",           \n            \"} else {\",\n            \"   output = false;\",\n            \"};\"\n        ],\n        \"status\": {\n            \"fill\": {\n                \"get\": \"sm.currentState === 'ok' ? 'green': 'red';\"\n            },\n            \"shape\": \"dot\",\n            \"text\": {\n                \"get\": \"sm.text;\"\n            }\n        }\n    }\n}","x":430,"y":2620,"wires":["309b1d09.872ee2"](/cflurin/node-red-contrib-dsm/wiki/"309b1d09.872ee2")},{"id":"309b1d09.872ee2","type":"debug","z":"6ff0723.8c6b78c","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":430,"y":2700,"wires":[]},{"id":"247c4181.790a5e","type":"inject","z":"6ff0723.8c6b78c","name":"","topic":"192.168.0.99","payload":"false","payloadType":"bool","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":170,"y":2700,"wires":["d4a9edca.cbb66"](/cflurin/node-red-contrib-dsm/wiki/"d4a9edca.cbb66")},{"id":"e378436.936dfc","type":"inject","z":"6ff0723.8c6b78c","name":"","topic":"192.168.0.99","payload":"456","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":180,"y":2740,"wires":["d4a9edca.cbb66"](/cflurin/node-red-contrib-dsm/wiki/"d4a9edca.cbb66")},{"id":"71bd852f.10080c","type":"comment","z":"6ff0723.8c6b78c","name":"send a notification e.g. to pushover","info":"","x":380,"y":2580,"wires":[]},{"id":"83e55553.d92628","type":"ping","z":"6ff0723.8c6b78c","name":"","host":"192.168.0.33","timer":"30","x":190,"y":2620,"wires":["d4a9edca.cbb66"](/cflurin/node-red-contrib-dsm/wiki/"d4a9edca.cbb66")},{"id":"651841d2.3094b","type":"ping","z":"6ff0723.8c6b78c","name":"","host":"192.168.0.34","timer":"30","x":190,"y":2660,"wires":["d4a9edca.cbb66"](/cflurin/node-red-contrib-dsm/wiki/"d4a9edca.cbb66")}]