Conditional Statement with Counter?

Not for sure best way to handle this? (It's the counter parts that has me messed up).

I monitor two (2) "Ports" on a single IP address every 15 minutes and if BOTH ports fail I send out an alarm (Only if BOTH ports fail).

What I would like to do if set this up to monitor every 7 minutes 30 seconds and it both ports fail count up 1, wait another 7 minutes 30 seconds read ports again and if both ports fail again count up another 1 (Counter = 2) when counter = 2 then send out alarm. Once alarm has been sent out reset counter.

Any suggestions?

[{"id":"9538fbf50ee36265","type":"inject","z":"5b623fd31ca86a42","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"port1","payload":"pass","payloadType":"str","x":220,"y":375,"wires":[["3d8812f7af908cf9"]]},{"id":"0762f42705ccdb38","type":"inject","z":"5b623fd31ca86a42","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"port1","payload":"fail","payloadType":"str","x":220,"y":425,"wires":[["3d8812f7af908cf9"]]},{"id":"b56e394785ac8b5d","type":"inject","z":"5b623fd31ca86a42","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"port2","payload":"pass","payloadType":"str","x":220,"y":525,"wires":[["9db2f6b675ac27a5"]]},{"id":"b8490bfc0d99dde2","type":"inject","z":"5b623fd31ca86a42","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"port2","payload":"fail","payloadType":"str","x":220,"y":575,"wires":[["9db2f6b675ac27a5"]]},{"id":"3d8812f7af908cf9","type":"switch","z":"5b623fd31ca86a42","name":"","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"pass","vt":"str"},{"t":"eq","v":"fail","vt":"str"}],"checkall":"true","repair":false,"outputs":2,"x":410,"y":400,"wires":[["b7c48d66bfa85cc9"],["b83e387a399c5ad7"]]},{"id":"b7c48d66bfa85cc9","type":"change","z":"5b623fd31ca86a42","name":"","rules":[{"t":"set","p":"port1","pt":"flow","to":"pass","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":580,"y":375,"wires":[["ca02c5867bf6fc79"]]},{"id":"b83e387a399c5ad7","type":"change","z":"5b623fd31ca86a42","name":"","rules":[{"t":"set","p":"port1","pt":"flow","to":"fail","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":580,"y":425,"wires":[["ca02c5867bf6fc79"]]},{"id":"9db2f6b675ac27a5","type":"switch","z":"5b623fd31ca86a42","name":"","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"pass","vt":"str"},{"t":"eq","v":"fail","vt":"str"}],"checkall":"true","repair":false,"outputs":2,"x":410,"y":550,"wires":[["c85f9ce51449306a"],["8ea3c5a55d8dbe02"]]},{"id":"c85f9ce51449306a","type":"change","z":"5b623fd31ca86a42","name":"","rules":[{"t":"set","p":"port2","pt":"flow","to":"pass","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":580,"y":525,"wires":[["ca02c5867bf6fc79"]]},{"id":"8ea3c5a55d8dbe02","type":"change","z":"5b623fd31ca86a42","name":"","rules":[{"t":"set","p":"port2","pt":"flow","to":"fail","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":580,"y":575,"wires":[["ca02c5867bf6fc79"]]},{"id":"cd5e85b8f91282df","type":"cronplus","z":"5b623fd31ca86a42","name":"","outputField":"payload","timeZone":"","storeName":"","commandResponseMsgOutput":"output1","defaultLocation":"","defaultLocationType":"default","outputs":1,"options":[{"name":"schedule1","topic":"topic1","payloadType":"default","payload":"","expressionType":"cron","expression":" */450 * * * * *","location":"","offset":"0","solarType":"all","solarEvents":"sunrise,sunset"}],"x":215,"y":700,"wires":[["d42dbf4bce001ef0"]]},{"id":"d42dbf4bce001ef0","type":"switch","z":"5b623fd31ca86a42","name":"PORT1","property":"port1","propertyType":"flow","rules":[{"t":"eq","v":"fail","vt":"str"}],"checkall":"true","repair":false,"outputs":1,"x":410,"y":700,"wires":[["664df4889e07b326"]]},{"id":"664df4889e07b326","type":"switch","z":"5b623fd31ca86a42","name":"PORT2","property":"port2","propertyType":"flow","rules":[{"t":"eq","v":"fail","vt":"str"}],"checkall":"true","repair":false,"outputs":1,"x":560,"y":700,"wires":[["8eb7cb33b75f595c"]]},{"id":"8eb7cb33b75f595c","type":"change","z":"5b623fd31ca86a42","name":"Increment","rules":[{"t":"set","p":"topic","pt":"msg","to":"increment","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":715,"y":700,"wires":[["87cb6dd3a01b9929"]]},{"id":"87cb6dd3a01b9929","type":"function","z":"5b623fd31ca86a42","name":"Counter","func":"var count = context.get('count')||0;\n\nif (msg.topic == \"reset\") {\n    count = 0;\n    context.set('count',0);\n}\nelse if (msg.topic== \"increment\") {\n    count += 1;\n    context.set('count',count);\n}\nelse if ( (msg.topic == \"decrement\")) {\n        count -= 1;\n    context.set('count',count);\n}\n\n// make it part of the outgoing msg object\nmsg.count = count;\nreturn msg;","outputs":1,"timeout":"","noerr":0,"initialize":"","finalize":"","libs":[],"x":890,"y":700,"wires":[["c823ddb62bf1fe2a"]]},{"id":"e603d4c888a53abd","type":"change","z":"5b623fd31ca86a42","name":"Reset","rules":[{"t":"set","p":"topic","pt":"msg","to":"reset","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":725,"y":750,"wires":[["87cb6dd3a01b9929"]]},{"id":"c823ddb62bf1fe2a","type":"switch","z":"5b623fd31ca86a42","name":"","property":"count","propertyType":"msg","rules":[{"t":"gt","v":"1","vt":"num"}],"checkall":"true","repair":false,"outputs":1,"x":1050,"y":700,"wires":[["25e26e6fa6823f19","9330826bb3f8f958"]]},{"id":"25e26e6fa6823f19","type":"link out","z":"5b623fd31ca86a42","name":"link out 302","mode":"link","links":["0f74656a0c564a39"],"x":1140,"y":700,"wires":[]},{"id":"0f74656a0c564a39","type":"link in","z":"5b623fd31ca86a42","name":"link in 316","links":["25e26e6fa6823f19"],"x":610,"y":750,"wires":[["e603d4c888a53abd"]]},{"id":"9330826bb3f8f958","type":"debug","z":"5b623fd31ca86a42","name":"debug 2619","active":true,"tosidebar":false,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","statusVal":"$now()","statusType":"jsonata","x":1195,"y":625,"wires":[]},{"id":"ca02c5867bf6fc79","type":"change","z":"5b623fd31ca86a42","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"'Port1: '&$flowContext('port1')& ' ' &'Port 2: ' &$flowContext('port2')\t","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":840,"y":475,"wires":[["ffbf86141b860710"]]},{"id":"ffbf86141b860710","type":"debug","z":"5b623fd31ca86a42","name":"debug 2620","active":true,"tosidebar":false,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","statusVal":"payload","statusType":"auto","x":1030,"y":475,"wires":[]}]

I think this is a long way using flow contexts, but it should work,
watch this space for expert advices from pros.

The trigger, filter and switch nodes should be able to do this.
e.g

[{"id":"d0da769320e30ab5","type":"inject","z":"d1395164b4eec73e","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"true","payloadType":"bool","x":110,"y":80,"wires":[["cd85d1e2537cef96"]]},{"id":"cd85d1e2537cef96","type":"rbe","z":"d1395164b4eec73e","name":"","func":"rbe","gap":"","start":"","inout":"out","septopics":true,"property":"payload","topi":"topic","x":230,"y":120,"wires":[["a40e440b1477d123"]]},{"id":"aed3a57be05f4bc7","type":"inject","z":"d1395164b4eec73e","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"false","payloadType":"bool","x":110,"y":140,"wires":[["cd85d1e2537cef96"]]},{"id":"a40e440b1477d123","type":"switch","z":"d1395164b4eec73e","name":"","property":"payload","propertyType":"msg","rules":[{"t":"false"},{"t":"true"}],"checkall":"true","repair":false,"outputs":2,"x":350,"y":120,"wires":[["29ee7ae6729a8c10"],["29ee7ae6729a8c10","c8bafda789a856fb"]]},{"id":"29ee7ae6729a8c10","type":"trigger","z":"d1395164b4eec73e","name":"","op1":"","op2":"","op1type":"nul","op2type":"payl","duration":"7","extend":false,"overrideDelay":false,"units":"s","reset":"true","bytopic":"all","topic":"topic","outputs":1,"x":500,"y":120,"wires":[["c8bafda789a856fb"]]},{"id":"c8bafda789a856fb","type":"trigger","z":"d1395164b4eec73e","name":"","op1":"","op2":"","op1type":"nul","op2type":"payl","duration":"7","extend":false,"overrideDelay":false,"units":"s","reset":"true","bytopic":"all","topic":"topic","outputs":1,"x":500,"y":180,"wires":[["3436ae219ad7e966"]]},{"id":"3436ae219ad7e966","type":"debug","z":"d1395164b4eec73e","name":"debug 2561","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":710,"y":140,"wires":[]}]
2 Likes

Not sure if this is helpful or not, but I use this to keep track of my windows and doors. The input comes from a state machine.

[{"id":"fabc60bc0b7ed897","type":"group","z":"95b8ed7423780576","g":"be8d0f9120a180d2","style":{"stroke":"#000000","label":true,"fill":"#bfdbef","color":"#000000"},"nodes":["cbcf838f66d2a099","9b275a51a37b9da3","aaf00e24598f3eb2","6840164a03a1cede","cb728b879ee1ff71","f6e0391ac76d8140","95e51dedf0cdb41b","2450ae252e3f3350","1f4fe2fe346e7008","389770a46c019487","7cc2c8f8575f7421","719ec93620e09edb","410b07f62e90c334","bbf8ac29df22498d","4965edc59297e074"],"x":934,"y":3079,"w":1112,"h":242},{"id":"16ba13235838884b","type":"subflow","name":"Counter","info":"","category":"","in":[{"x":120,"y":120,"wires":[{"id":"31a77bafcac74c41"}]}],"out":[{"x":780,"y":80,"wires":[{"id":"31a77bafcac74c41","port":0}]}],"env":[{"name":"RESET_ON_START","type":"bool","value":"false"}],"meta":{},"color":"#DDAA99","status":{"x":780,"y":140,"wires":[{"id":"31a77bafcac74c41","port":1}]}},{"id":"31a77bafcac74c41","type":"function","z":"16ba13235838884b","name":"Accumulate based on topic","func":"var topic = msg.topic\nvar resetTopic = msg.hasOwnProperty(\"resetTopic\");\nvar reset = msg.hasOwnProperty(\"reset\");\nvar ret = [];\nvar varName = \"counter\";\n\nvar addend = msg.hasOwnProperty(\"addend\") ? msg.addend : 1 ;\n\nvar cntr = flow.get(varName);\nif (cntr === undefined || reset) {\n    cntr = {};\n}\n\nif (!reset) {\n    if (cntr.hasOwnProperty(topic) && !resetTopic) {\n        cntr[topic] = cntr[topic] + addend;\n        \n    } else {\n        // @ts-ignore\n        cntr[topic] = 1 - resetTopic;\n    }\n    msg.counter = cntr[topic];\n    \n    ret = [msg,{ \"payload\": { \"text\": topic + \": \" + cntr[topic].toString()}}];\n\n} else {\n\n    ret = [undefined,{ \"payload\":{\"text\": \"counters reset\"}}];\n    \n}\nflow.set(varName,cntr);\n\nreturn ret;","outputs":2,"timeout":"","noerr":0,"initialize":"","finalize":"","libs":[],"x":360,"y":120,"wires":[[],[]]},{"id":"cbcf838f66d2a099","type":"subflow:16ba13235838884b","z":"95b8ed7423780576","g":"fabc60bc0b7ed897","name":"","x":1620,"y":3160,"wires":[["cb728b879ee1ff71"]]},{"id":"9b275a51a37b9da3","type":"inject","z":"95b8ed7423780576","g":"fabc60bc0b7ed897","name":"Reset Counters","props":[{"p":"reset","v":"","vt":"date"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"AnyTopic","x":1400,"y":3200,"wires":[["410b07f62e90c334","bbf8ac29df22498d"]]},{"id":"aaf00e24598f3eb2","type":"change","z":"95b8ed7423780576","g":"fabc60bc0b7ed897","name":"Decrease Count","rules":[{"t":"set","p":"topic","pt":"msg","to":"window","tot":"str"},{"t":"set","p":"payload","pt":"msg","to":"","tot":"date"},{"t":"set","p":"addend","pt":"msg","to":"-1","tot":"num"}],"action":"","property":"","from":"","to":"","reg":false,"x":1400,"y":3160,"wires":[["410b07f62e90c334"]]},{"id":"6840164a03a1cede","type":"change","z":"95b8ed7423780576","g":"fabc60bc0b7ed897","name":"Increase Count","rules":[{"t":"set","p":"topic","pt":"msg","to":"window","tot":"str"},{"t":"set","p":"payload","pt":"msg","to":"","tot":"date"}],"action":"","property":"","from":"","to":"","reg":false,"x":1400,"y":3120,"wires":[["410b07f62e90c334"]]},{"id":"cb728b879ee1ff71","type":"change","z":"95b8ed7423780576","g":"fabc60bc0b7ed897","name":"Set statusReport.windows.openCount","rules":[{"t":"set","p":"statusReport.windows.openCount","pt":"global","to":"counter","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":1870,"y":3160,"wires":[[]]},{"id":"f6e0391ac76d8140","type":"link in","z":"95b8ed7423780576","g":"fabc60bc0b7ed897","name":"Counter Reset In","links":["d8a99c9d4a088896"],"x":1265,"y":3200,"wires":[["95e51dedf0cdb41b"]]},{"id":"95e51dedf0cdb41b","type":"change","z":"95b8ed7423780576","g":"fabc60bc0b7ed897","name":"Reset Counters","rules":[{"t":"set","p":"reset","pt":"msg","to":"","tot":"date"},{"t":"set","p":"topic","pt":"msg","to":"window","tot":"str"},{"t":"set","p":"reset","pt":"msg","to":"","tot":"date"},{"t":"set","p":"topic","pt":"msg","to":"door","tot":"str"},{"t":"set","p":"reset","pt":"msg","to":"","tot":"date"},{"t":"set","p":"topic","pt":"msg","to":"AnyTopic","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":1400,"y":3200,"wires":[["410b07f62e90c334","bbf8ac29df22498d"]]},{"id":"2450ae252e3f3350","type":"switch","z":"95b8ed7423780576","g":"fabc60bc0b7ed897","name":"Doors & Windows \\n Open/Closed","property":"payload","propertyType":"msg","rules":[{"t":"cont","v":"Window open","vt":"str"},{"t":"cont","v":"Window closed","vt":"str"},{"t":"cont","v":"Door open","vt":"str"},{"t":"cont","v":"Door closed","vt":"str"}],"checkall":"true","repair":false,"outputs":4,"x":1110,"y":3200,"wires":[["6840164a03a1cede"],["aaf00e24598f3eb2"],["1f4fe2fe346e7008"],["4965edc59297e074"]]},{"id":"1f4fe2fe346e7008","type":"change","z":"95b8ed7423780576","g":"fabc60bc0b7ed897","name":"Increase Count","rules":[{"t":"set","p":"topic","pt":"msg","to":"door","tot":"str"},{"t":"set","p":"payload","pt":"msg","to":"","tot":"date"}],"action":"","property":"","from":"","to":"","reg":false,"x":1400,"y":3240,"wires":[["bbf8ac29df22498d"]]},{"id":"389770a46c019487","type":"change","z":"95b8ed7423780576","g":"fabc60bc0b7ed897","name":"Set statusReport.doors.openCount","rules":[{"t":"set","p":"statusReport.doors.openCount","pt":"global","to":"counter","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":1880,"y":3240,"wires":[[]]},{"id":"7cc2c8f8575f7421","type":"subflow:16ba13235838884b","z":"95b8ed7423780576","g":"fabc60bc0b7ed897","name":"","x":1620,"y":3240,"wires":[["389770a46c019487"]]},{"id":"719ec93620e09edb","type":"delay","z":"95b8ed7423780576","g":"fabc60bc0b7ed897","name":"","pauseType":"delay","timeout":"1","timeoutUnits":"seconds","rate":"1","nbRateUnits":"5","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":true,"allowrate":false,"outputs":1,"x":975,"y":3200,"wires":[["2450ae252e3f3350"]],"l":false},{"id":"410b07f62e90c334","type":"junction","z":"95b8ed7423780576","g":"fabc60bc0b7ed897","x":1540,"y":3160,"wires":[["cbcf838f66d2a099"]]},{"id":"bbf8ac29df22498d","type":"junction","z":"95b8ed7423780576","g":"fabc60bc0b7ed897","x":1540,"y":3240,"wires":[["7cc2c8f8575f7421"]]},{"id":"4965edc59297e074","type":"change","z":"95b8ed7423780576","g":"fabc60bc0b7ed897","name":"Decrease Count","rules":[{"t":"set","p":"topic","pt":"msg","to":"door","tot":"str"},{"t":"set","p":"payload","pt":"msg","to":"","tot":"date"},{"t":"set","p":"addend","pt":"msg","to":"-1","tot":"num"}],"action":"","property":"","from":"","to":"","reg":false,"x":1400,"y":3280,"wires":[["bbf8ac29df22498d"]]}]

You probably hear this all the time, but that solution is very clever.

2 Likes

Thanks - Great example.

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