If a value is false for x time

Gents

i need some help i tried thorthel (count and Time) and trigger but i dd get it done.

i need de following i measure the internet with ping. when the internet is not there a false is send. the ping is every 60sec. when the internet is down for 10 minutes a action is taken. when the internet is back within the t0 minutes no action is taken. (Its a kind of watchdog function)

can some help me how to solve this

Chris

This should work.

ping every minute using exec node, check exit code (0 = ok, 2 = failed), if 0 send msg.reset to trigger node, if 2 trigger 10 minute wait, every minute it is still exit code 2 after 10 minutes, "internet is down" message will be send

Example flow

[{"id":"85c840d1.872178","type":"inject","z":"53bda012.37199","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"60","crontab":"","once":true,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":118,"y":144,"wires":[["1f758d38.6ae383"]]},{"id":"1f758d38.6ae383","type":"exec","z":"53bda012.37199","command":"ping -c 1 google.com","addpay":false,"append":"","useSpawn":"false","timer":"","oldrc":false,"name":"","x":316,"y":144,"wires":[["ff00b989.accf8"],[],[]]},{"id":"ff00b989.accf8","type":"switch","z":"53bda012.37199","name":"rc.code","property":"rc.code","propertyType":"msg","rules":[{"t":"eq","v":"0","vt":"num"},{"t":"eq","v":"2","vt":"num"}],"checkall":"true","repair":false,"outputs":2,"x":540,"y":144,"wires":[["89931d7b.77b188"],["1c389f0a.e8bfc9"]]},{"id":"1c389f0a.e8bfc9","type":"trigger","z":"53bda012.37199","name":"","op1":"","op2":"internet is down","op1type":"nul","op2type":"str","duration":"10","extend":false,"overrideDelay":false,"units":"min","reset":"","bytopic":"all","topic":"topic","outputs":1,"x":766,"y":168,"wires":[["761884bf.a7852c"]]},{"id":"89931d7b.77b188","type":"change","z":"53bda012.37199","name":"reset","rules":[{"t":"set","p":"reset","pt":"msg","to":"true","tot":"bool"}],"action":"","property":"","from":"","to":"","reg":false,"x":650,"y":96,"wires":[["1c389f0a.e8bfc9"]]},{"id":"761884bf.a7852c","type":"debug","z":"53bda012.37199","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":914,"y":168,"wires":[]}]
1 Like

I think that can be simplified a bit, as the Trigger node can be configured to reset on a particular payload value, so I don't think you need the reset node. In fact maybe the rc.code node isn't needed either, it may be possible to feed the ping node straight into the trigger.

1 Like

What action is taken?
I use a flow to monitor the internet status, and if it's consistently poor, it runs a script to reboot my Nighthawk router which has an api (That script isn't included in the flow below).
I'm using flow context to create a running status value, which determines the order in which the different functions operate.

[{"id":"a5d6a34.5c3ed6","type":"ping","z":"b3b413d1.05b1b","mode":"timed","name":"","host":"1.1.1.1","timer":"120","inputs":0,"x":150,"y":1940,"wires":[["db50e292.a466b"]]},{"id":"db50e292.a466b","type":"function","z":"b3b413d1.05b1b","name":"","func":"let router = flow.get('nighthawk');\n    if (router === undefined){\n    flow.set('nighthawk',{\"status\":0,\"reset\":false});\n    router = {\"status\":0,\"reset\":false};\n    }\nlet acc=router.status,reset=router.reset,ping=msg.payload;\nvar msg1,msg2,msg3;\n\n// Increment or decrement acc according to ping result\nif (ping === false){\n    acc=(acc+5);\n    node.status({fill:\"red\",shape:\"dot\",text:\"ping = \"+ping+\", status = \"+acc})\n    }\n    else if (ping >= 60){\n        if (acc>0){acc=(acc+3);}\n        else {acc=(acc+1);}\n        node.status({fill:\"grey\",shape:\"dot\",text:\"ping = \"+ping+\", status = \"+acc})\n        }\n    else if (ping < 60) {\n        if (acc < 0){acc++;}\n        else if (acc > 0){acc--;}\n        node.status({fill:\"green\",shape:\"dot\",text:\"ping = \"+ping+\", status = \"+acc});\n        }\n    else {\n        // If not false or a number, then exit\n        node.status({fill:\"red\",shape:\"dot\",text:\"Unreadable input msg: \"+ping});\n        msg3 = {payload: \"Unreadable input msg: \"+ping,loglevel: \"ERROR\"};\n        return [null,null,msg3];\n        }\n\n// When preset level is reached, reboot router\nif (acc >= 20){\n    flow.set('nighthawk.reset',true);\n    acc=-20;\n    msg1 = {payload: \"reboot\"};\n    msg2 = null;\n    node.status(\"Rebooting router\");\n    msg3 = {payload: \"Rebooting Router, status set to -20\",loglevel: \"WARN\"};\n}\n\n// Once internet has stabilised - send push message\nif ((reset === true)&&(acc>=-5)){\n    msg1 = null;\n    msg2 = {payload:\"Following internet failure, the router was rebooted.\"};\n    flow.set('nighthawk.reset',false);\n    node.status(\"Sending push message\");\n    msg3 = {payload: \"Sending push message\",loglevel: \"INFO\"};\n    }\n    \n// Update log\nif ((acc>=-3)&&(acc<20)&&(acc!=0)){\n    msg3 = {payload: \"ping = \"+ping+\", status = \"+acc,loglevel: \"INFO\"};\n    }\n    \n//flow.set('router[0]', acc);\nflow.set('nighthawk.status', acc);\n\nreturn [msg1,msg2,msg3];","outputs":3,"noerr":0,"initialize":"","finalize":"","x":300,"y":1940,"wires":[["7d358f2c.373e3"],["baa47935.195a68"],["c9298053.3101d"]]},{"id":"baa47935.195a68","type":"debug","z":"b3b413d1.05b1b","name":"Output to send push msg - e.g Pushover","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":575,"y":1940,"wires":[]},{"id":"7d358f2c.373e3","type":"debug","z":"b3b413d1.05b1b","name":"Output to run script to reboot my router","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":570,"y":1900,"wires":[]},{"id":"c9298053.3101d","type":"debug","z":"b3b413d1.05b1b","name":"Output to `node-red-contrib-flogger-now` node","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":595,"y":1980,"wires":[]}]

Output 1 Runs a script to reboots my router, which then picks up a different IP/Route and almost always resolves the problem.

Output 2 connects to a pushover node to send a push message to alert me (once connectivity has been restored :wink:)

Output 3 connects to node-red-contrib-flogger-now which writes selected entries to a logfile.

1 Like

If you did want to use the trigger node then this would probably do it

[{"id":"610055a1.add5dc","type":"ping","z":"ba9351cf.2e3e1","mode":"timed","name":"","host":"1.1.1.1","timer":"20","inputs":0,"x":90,"y":460,"wires":[["42e6daab.416b64"]]},{"id":"42e6daab.416b64","type":"change","z":"ba9351cf.2e3e1","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"$boolean(payload)","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":280,"y":460,"wires":[["1270ecbd.b045e3"]]},{"id":"1270ecbd.b045e3","type":"trigger","z":"ba9351cf.2e3e1","name":"","op1":"","op2":"No Internet","op1type":"nul","op2type":"str","duration":"10","extend":false,"overrideDelay":false,"units":"min","reset":"true","bytopic":"all","topic":"topic","outputs":1,"x":470,"y":460,"wires":[["15915b77.1aea85"]]},{"id":"15915b77.1aea85","type":"debug","z":"ba9351cf.2e3e1","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":650,"y":460,"wires":[]}]

1 Like

All thanks for your reaction this was help full and triggered me to some ideas. I solved on the following way
I check when the outcome of the ping is number this will reset stop timer.
When there is no number then and the timer expires it will trigger to close the roof of the observatory

my flow now

[{"id":"19e735e5.72fb2a","type":"link out","z":"7fa6be5b.eea368","name":"Watchdog-close-roof","links":["7bb68b00.56ea2c"],"x":995,"y":280,"wires":[]},{"id":"27ddda0e.ad3bc6","type":"ping","z":"7fa6be5b.eea368","mode":"timed","name":"internet 86.95.29.46","host":"8.8.8.8","timer":"60","inputs":0,"x":210,"y":380,"wires":[["8d157863.d0261","6d107a66.ebe14c","15fe46d7.4a2cd9"]]},{"id":"8d157863.d0261","type":"ui_text","z":"7fa6be5b.eea368","group":"cf41b0a7.a0c09","order":8,"width":4,"height":1,"name":"","label":"INTERNET:","format":"<font color= {{msg.color}} > {{msg.payload}} </font>","layout":"row-left","x":430,"y":320,"wires":[]},{"id":"6d107a66.ebe14c","type":"mqtt out","z":"7fa6be5b.eea368","name":"","topic":"/schoener/obs/monitoring/network/internet","qos":"","retain":"","broker":"d5b54be8.188b68","x":510,"y":380,"wires":[]},{"id":"b604171c.8646e8","type":"switch","z":"7fa6be5b.eea368","name":"Context based routing","property":"state","propertyType":"flow","rules":[{"t":"eq","v":"1","vt":"num"}],"checkall":"true","repair":false,"outputs":1,"x":840,"y":280,"wires":[["19e735e5.72fb2a"]]},{"id":"15fe46d7.4a2cd9","type":"switch","z":"7fa6be5b.eea368","name":"Is num ?","property":"payload","propertyType":"msg","rules":[{"t":"istype","v":"number","vt":"number"}],"checkall":"true","repair":false,"outputs":1,"x":420,"y":280,"wires":[["53106e23.359ce8"]]},{"id":"53106e23.359ce8","type":"stoptimer","z":"7fa6be5b.eea368","duration":"30","units":"Minute","payloadtype":"num","payloadval":"1","name":"","x":580,"y":280,"wires":[[],["b604171c.8646e8"]]},{"id":"cf41b0a7.a0c09","type":"ui_group","name":"Measurements","tab":"dd962aa5.a268f8","order":2,"disp":true,"width":"8","collapse":true},{"id":"d5b54be8.188b68","type":"mqtt-broker","name":"Remote","broker":"83.81.59.92","port":"18888","clientid":"","usetls":false,"compatmode":false,"keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","closeTopic":"","closeQos":"0","closePayload":"","willTopic":"","willQos":"0","willPayload":""},{"id":"dd962aa5.a268f8","type":"ui_tab","name":"OCS","icon":"dashboard","disabled":false,"hidden":false}]

Thanks can for your support

Chris

You didn't like the idea of using a Trigger node then? Generally it is better to use core nodes rather than contrib ones unless there is a good reason.
The stoptimer node appears to do basically the same thing as a Trigger node (rather less actually), but it hasn't been updated in four years and it hasn't got a github repository linked so there is no way of knowing whether it has any outstanding issues. Also if you look at the packages the author has produced you will see none of them has been updated in the last three years, which is not a good sign.

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