Flow consolidation help

Hi All, I'm pretty new to nodered and have slowly been chipping away at getting my automations setup on the platform.

I understand that similar to writing code if you're repeating the same code again and again, there is a better way of doing things, iteration for example. I say this because I have designed a flow that as it stands does what it needs to do, but I feel could be consolidated/made more efficient, I am in the process of doing so but was interested to see if anyone with more experience with nodered could offer some suggestions.

The flow explanation, at 5pm weekdays the flow is triggered and basically checks if my work PC is online via a ping, if it gets a ping it starts the loop again, when it gets a no response on the ping, it repeats the same check 4 more times to basically make sure that the PC is off before turning off the powerboard the PC runs from, once this is done it then moves to the second part where it basically runs the same flow on a raspberry pi, but this time sending a shutdown command if the pi is still on, and keeps checking until its off to then turn its powerboard off too

Hopefully that makes sense, I have attached the flow so that you can see what I mean about consolidation/efficiency. I was playing with counter nodes but was unable to get them to reset when the count got to 5 or if there was a ping received indicating the PC or the pi is still on

[{"id":"74bb0eea.2be9d8","type":"tab","label":"Test Area","disabled":false,"info":""},{"id":"69f39eea.cb7d","type":"api-call-service","z":"74bb0eea.2be9d8","name":"","server":"9ee45713.52df98","version":1,"debugenabled":false,"service_domain":"light","service":"turn_off","entityId":"light.40357238a4cf12e1be73","data":"","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":550,"y":1140,"wires":[[]]},{"id":"949f9457.3aad18","type":"inject","z":"74bb0eea.2be9d8","name":"end of work day","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"00 17 * * 1,2,3,4,5","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"str","x":170,"y":60,"wires":[["677ba92e.a555e8"]]},{"id":"5069793d.0aa608","type":"function","z":"74bb0eea.2be9d8","name":"convert boolean to integer","func":"msg.original = msg.payload;\nif (msg.payload === \"false\" || msg.payload === false) {\n msg.payload = 0;\n} else {\n msg.payload = 1;\n}\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":540,"y":120,"wires":[["a014c0ca.248ad"]]},{"id":"a014c0ca.248ad","type":"switch","z":"74bb0eea.2be9d8","name":"","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"0","vt":"num"},{"t":"eq","v":"1","vt":"num"}],"checkall":"false","repair":false,"outputs":2,"x":110,"y":180,"wires":[["12a07d7a.965af3"],["c38bca0a.05f4b8"]]},{"id":"677ba92e.a555e8","type":"ping","z":"74bb0eea.2be9d8","mode":"triggered","name":"WORK PC","host":"WORK_PC","timer":"20","inputs":1,"x":450,"y":60,"wires":[["5069793d.0aa608"]]},{"id":"c38bca0a.05f4b8","type":"delay","z":"74bb0eea.2be9d8","name":"","pauseType":"delay","timeout":"1","timeoutUnits":"minutes","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":120,"y":100,"wires":[["677ba92e.a555e8"]]},{"id":"1287fb6d.dd0c05","type":"switch","z":"74bb0eea.2be9d8","name":"","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"0","vt":"num"},{"t":"eq","v":"1","vt":"num"}],"checkall":"false","repair":false,"outputs":2,"x":110,"y":240,"wires":[["916d746c.16d618"],["c38bca0a.05f4b8"]]},{"id":"40b15524.39682c","type":"switch","z":"74bb0eea.2be9d8","name":"","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"0","vt":"num"},{"t":"eq","v":"1","vt":"num"}],"checkall":"false","repair":false,"outputs":2,"x":110,"y":300,"wires":[["d63068a0.7a52d8"],["c38bca0a.05f4b8"]]},{"id":"5eb3ba19.abc6f4","type":"switch","z":"74bb0eea.2be9d8","name":"","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"0","vt":"num"},{"t":"eq","v":"1","vt":"num"}],"checkall":"false","repair":false,"outputs":2,"x":110,"y":360,"wires":[["d6f8bf8a.41ad1"],["c38bca0a.05f4b8"]]},{"id":"f0c7eddd.9953f","type":"switch","z":"74bb0eea.2be9d8","name":"","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"0","vt":"num"},{"t":"eq","v":"1","vt":"num"}],"checkall":"false","repair":false,"outputs":2,"x":110,"y":420,"wires":[["9ce38df3.e87b7"],["c38bca0a.05f4b8"]]},{"id":"12a07d7a.965af3","type":"ping","z":"74bb0eea.2be9d8","mode":"triggered","name":"WORK PC","host":"WORK_PC","timer":"20","inputs":1,"x":320,"y":180,"wires":[["b6197aa9.471cf8"]]},{"id":"b6197aa9.471cf8","type":"function","z":"74bb0eea.2be9d8","name":"convert boolean to integer","func":"msg.original = msg.payload;\nif (msg.payload === \"false\" || msg.payload === false) {\n msg.payload = 0;\n} else {\n msg.payload = 1;\n}\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":540,"y":180,"wires":[["1287fb6d.dd0c05"]]},{"id":"916d746c.16d618","type":"ping","z":"74bb0eea.2be9d8","mode":"triggered","name":"WORK PC","host":"WORK_PC","timer":"20","inputs":1,"x":320,"y":240,"wires":[["17751e60.ea9152"]]},{"id":"17751e60.ea9152","type":"function","z":"74bb0eea.2be9d8","name":"convert boolean to integer","func":"msg.original = msg.payload;\nif (msg.payload === \"false\" || msg.payload === false) {\n msg.payload = 0;\n} else {\n msg.payload = 1;\n}\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":540,"y":240,"wires":[["40b15524.39682c"]]},{"id":"d63068a0.7a52d8","type":"ping","z":"74bb0eea.2be9d8","mode":"triggered","name":"WORK PC","host":"WORK_PC","timer":"20","inputs":1,"x":320,"y":300,"wires":[["c5c48c09.3d702"]]},{"id":"c5c48c09.3d702","type":"function","z":"74bb0eea.2be9d8","name":"convert boolean to integer","func":"msg.original = msg.payload;\nif (msg.payload === \"false\" || msg.payload === false) {\n msg.payload = 0;\n} else {\n msg.payload = 1;\n}\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":540,"y":300,"wires":[["5eb3ba19.abc6f4"]]},{"id":"d6f8bf8a.41ad1","type":"ping","z":"74bb0eea.2be9d8","mode":"triggered","name":"WORK PC","host":"WORK_PC","timer":"20","inputs":1,"x":320,"y":360,"wires":[["b3d9af42.17512"]]},{"id":"b3d9af42.17512","type":"function","z":"74bb0eea.2be9d8","name":"convert boolean to integer","func":"msg.original = msg.payload;\nif (msg.payload === \"false\" || msg.payload === false) {\n msg.payload = 0;\n} else {\n msg.payload = 1;\n}\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":540,"y":360,"wires":[["f0c7eddd.9953f"]]},{"id":"9ce38df3.e87b7","type":"ping","z":"74bb0eea.2be9d8","mode":"triggered","name":"WORK PC","host":"WORK_PC","timer":"20","inputs":1,"x":320,"y":420,"wires":[["f173c41.4afb538"]]},{"id":"f173c41.4afb538","type":"function","z":"74bb0eea.2be9d8","name":"convert boolean to integer","func":"msg.original = msg.payload;\nif (msg.payload === \"false\" || msg.payload === false) {\n msg.payload = 0;\n} else {\n msg.payload = 1;\n}\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":540,"y":420,"wires":[["80972c11.ee9b8"]]},{"id":"58152b98.6b9284","type":"ping","z":"74bb0eea.2be9d8","mode":"triggered","name":"CCTV PI","host":"CCTV_PI","timer":"20","inputs":1,"x":300,"y":540,"wires":[["94ad1387.c9b27"]]},{"id":"94ad1387.c9b27","type":"function","z":"74bb0eea.2be9d8","name":"convert boolean to integer","func":"msg.original = msg.payload;\nif (msg.payload === \"false\" || msg.payload === false) {\n msg.payload = 0;\n} else {\n msg.payload = 1;\n}\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":540,"y":540,"wires":[["fe0c9a65.6b7b78"]]},{"id":"fe0c9a65.6b7b78","type":"switch","z":"74bb0eea.2be9d8","name":"","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"0","vt":"num"},{"t":"eq","v":"1","vt":"num"}],"checkall":"false","repair":false,"outputs":2,"x":730,"y":540,"wires":[["57fff0d7.8b273"],["c282e750.8434c8"]]},{"id":"c282e750.8434c8","type":"exec","z":"74bb0eea.2be9d8","command":"ssh pi@CCTV_PI sudo shutdown","addpay":false,"append":"","useSpawn":"false","timer":"","oldrc":false,"name":"shutdown pi","x":130,"y":660,"wires":[["1759c870.86c4d8"],["3e11903a.cc9a7"],["3a53d1bc.c68f7e"]]},{"id":"57fff0d7.8b273","type":"api-call-service","z":"74bb0eea.2be9d8","name":"turn_off_home_office_cube","server":"9ee45713.52df98","version":1,"debugenabled":false,"service_domain":"switch","service":"turn_off","entityId":"switch.33050284d8f15bb44dd9","data":"","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":860,"y":1160,"wires":[[]]},{"id":"94cee003.d2ab7","type":"ping","z":"74bb0eea.2be9d8","mode":"triggered","name":"CCTV PI","host":"CCTV_PI","timer":"20","inputs":1,"x":300,"y":480,"wires":[["2b8dd9d2.4dc396"]]},{"id":"2b8dd9d2.4dc396","type":"function","z":"74bb0eea.2be9d8","name":"convert boolean to integer","func":"msg.original = msg.payload;\nif (msg.payload === \"false\" || msg.payload === false) {\n msg.payload = 0;\n} else {\n msg.payload = 1;\n}\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":540,"y":480,"wires":[["fccd1687.2014d8"]]},{"id":"fccd1687.2014d8","type":"switch","z":"74bb0eea.2be9d8","name":"","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"0","vt":"num"},{"t":"eq","v":"1","vt":"num"}],"checkall":"false","repair":false,"outputs":2,"x":730,"y":480,"wires":[["57fff0d7.8b273"],["58152b98.6b9284"]]},{"id":"80972c11.ee9b8","type":"switch","z":"74bb0eea.2be9d8","name":"","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"0","vt":"num"},{"t":"eq","v":"1","vt":"num"}],"checkall":"false","repair":false,"outputs":2,"x":110,"y":480,"wires":[["94cee003.d2ab7","1e874747.e8c309"],["c38bca0a.05f4b8"]]},{"id":"3d9390f2.90967","type":"Tasmota Switch","z":"74bb0eea.2be9d8","broker":"b657dc9c.d2cca","device":"POWERBOARD_HOME_OFFICE","name":"","outputs":1,"uidisabler":false,"fullTopic":"","cmndPrefix":"","statPrefix":"","telePrefix":"","x":380,"y":580,"wires":[[]]},{"id":"1e874747.e8c309","type":"change","z":"74bb0eea.2be9d8","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"off","tot":"str"},{"t":"set","p":"topic","pt":"msg","to":"switch0","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":140,"y":580,"wires":[["3d9390f2.90967","1119e5b0.0111ca"]]},{"id":"3e11903a.cc9a7","type":"debug","z":"74bb0eea.2be9d8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":310,"y":640,"wires":[]},{"id":"1759c870.86c4d8","type":"debug","z":"74bb0eea.2be9d8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":310,"y":680,"wires":[]},{"id":"f2ae11b7.da0c7","type":"mqtt in","z":"74bb0eea.2be9d8","name":"","topic":"SENSORHUB/BLE13/CONTACT","qos":"2","datatype":"auto","broker":"b657dc9c.d2cca","x":1150,"y":80,"wires":[[]]},{"id":"9e66fcf7.8448d","type":"mqtt in","z":"74bb0eea.2be9d8","name":"","topic":"SENSORHUB/BLE13/BATTERY","qos":"2","datatype":"auto","broker":"b657dc9c.d2cca","x":1150,"y":140,"wires":[[]]},{"id":"14a18ef1.be0a51","type":"function","z":"74bb0eea.2be9d8","name":"convert boolean to integer","func":"msg.original = msg.payload;\nif (msg.payload === \"false\" || msg.payload === false) {\n msg.payload = 0;\n} else {\n msg.payload = 1;\n}\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":560,"y":800,"wires":[["f0f25cf4.79e4c"]]},{"id":"f0f25cf4.79e4c","type":"switch","z":"74bb0eea.2be9d8","name":"","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"0","vt":"num"},{"t":"eq","v":"1","vt":"num"}],"checkall":"false","repair":false,"outputs":2,"x":130,"y":860,"wires":[["6668f41e.d6407c"],["7034d9c.3e47c28"]]},{"id":"3a53d1bc.c68f7e","type":"ping","z":"74bb0eea.2be9d8","mode":"triggered","name":"CCTV PI","host":"CCTV_PI","timer":"20","inputs":1,"x":320,"y":740,"wires":[["14a18ef1.be0a51"]]},{"id":"7034d9c.3e47c28","type":"delay","z":"74bb0eea.2be9d8","name":"","pauseType":"delay","timeout":"15","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":140,"y":780,"wires":[["3a53d1bc.c68f7e"]]},{"id":"6b0c9cad.4d2fa4","type":"switch","z":"74bb0eea.2be9d8","name":"","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"0","vt":"num"},{"t":"eq","v":"1","vt":"num"}],"checkall":"false","repair":false,"outputs":2,"x":130,"y":920,"wires":[["c361fa4a.e4aae8"],["7034d9c.3e47c28"]]},{"id":"bd83c5a3.5ac148","type":"switch","z":"74bb0eea.2be9d8","name":"","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"0","vt":"num"},{"t":"eq","v":"1","vt":"num"}],"checkall":"false","repair":false,"outputs":2,"x":130,"y":980,"wires":[["ad6f800d.4d878"],["7034d9c.3e47c28"]]},{"id":"1f1efaf.fbc9a05","type":"switch","z":"74bb0eea.2be9d8","name":"","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"0","vt":"num"},{"t":"eq","v":"1","vt":"num"}],"checkall":"false","repair":false,"outputs":2,"x":130,"y":1040,"wires":[["7df95821.7a3848"],["7034d9c.3e47c28"]]},{"id":"68877baa.8197b4","type":"switch","z":"74bb0eea.2be9d8","name":"","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"0","vt":"num"},{"t":"eq","v":"1","vt":"num"}],"checkall":"false","repair":false,"outputs":2,"x":130,"y":1100,"wires":[["2dced927.689636"],["7034d9c.3e47c28"]]},{"id":"6668f41e.d6407c","type":"ping","z":"74bb0eea.2be9d8","mode":"triggered","name":"CCTV PI","host":"CCTV_PI","timer":"20","inputs":1,"x":320,"y":860,"wires":[["805d7b1d.e58a08"]]},{"id":"805d7b1d.e58a08","type":"function","z":"74bb0eea.2be9d8","name":"convert boolean to integer","func":"msg.original = msg.payload;\nif (msg.payload === \"false\" || msg.payload === false) {\n msg.payload = 0;\n} else {\n msg.payload = 1;\n}\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":560,"y":860,"wires":[["6b0c9cad.4d2fa4"]]},{"id":"c361fa4a.e4aae8","type":"ping","z":"74bb0eea.2be9d8","mode":"triggered","name":"CCTV PI","host":"CCTV_PI","timer":"20","inputs":1,"x":320,"y":920,"wires":[["68735457.3775fc"]]},{"id":"68735457.3775fc","type":"function","z":"74bb0eea.2be9d8","name":"convert boolean to integer","func":"msg.original = msg.payload;\nif (msg.payload === \"false\" || msg.payload === false) {\n msg.payload = 0;\n} else {\n msg.payload = 1;\n}\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":560,"y":920,"wires":[["bd83c5a3.5ac148"]]},{"id":"ad6f800d.4d878","type":"ping","z":"74bb0eea.2be9d8","mode":"triggered","name":"CCTV PI","host":"CCTV_PI","timer":"20","inputs":1,"x":320,"y":980,"wires":[["c0eaf60c.2b96e8"]]},{"id":"c0eaf60c.2b96e8","type":"function","z":"74bb0eea.2be9d8","name":"convert boolean to integer","func":"msg.original = msg.payload;\nif (msg.payload === \"false\" || msg.payload === false) {\n msg.payload = 0;\n} else {\n msg.payload = 1;\n}\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":560,"y":980,"wires":[["1f1efaf.fbc9a05"]]},{"id":"7df95821.7a3848","type":"ping","z":"74bb0eea.2be9d8","mode":"triggered","name":"CCTV PI","host":"CCTV_PI","timer":"20","inputs":1,"x":320,"y":1040,"wires":[["c37cecfd.1abb9"]]},{"id":"c37cecfd.1abb9","type":"function","z":"74bb0eea.2be9d8","name":"convert boolean to integer","func":"msg.original = msg.payload;\nif (msg.payload === \"false\" || msg.payload === false) {\n msg.payload = 0;\n} else {\n msg.payload = 1;\n}\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":560,"y":1040,"wires":[["68877baa.8197b4"]]},{"id":"2dced927.689636","type":"ping","z":"74bb0eea.2be9d8","mode":"triggered","name":"CCTV PI","host":"CCTV_PI","timer":"20","inputs":1,"x":320,"y":1100,"wires":[["5f689564.56473c"]]},{"id":"5f689564.56473c","type":"function","z":"74bb0eea.2be9d8","name":"convert boolean to integer","func":"msg.original = msg.payload;\nif (msg.payload === \"false\" || msg.payload === false) {\n msg.payload = 0;\n} else {\n msg.payload = 1;\n}\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":560,"y":1100,"wires":[["2d8b82bf.2e63ee"]]},{"id":"2d8b82bf.2e63ee","type":"switch","z":"74bb0eea.2be9d8","name":"","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"0","vt":"num"},{"t":"eq","v":"1","vt":"num"}],"checkall":"false","repair":false,"outputs":2,"x":130,"y":1160,"wires":[["57fff0d7.8b273"],[]]},{"id":"1119e5b0.0111ca","type":"api-call-service","z":"74bb0eea.2be9d8","name":"","server":"9ee45713.52df98","version":1,"debugenabled":false,"service_domain":"light","service":"turn_off","entityId":"light.40357238a4cf12e1be73","data":"","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":650,"y":580,"wires":[[]]},{"id":"17039f0b.98c481","type":"inject","z":"74bb0eea.2be9d8","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":120,"y":720,"wires":[["c282e750.8434c8"]]},{"id":"9ee45713.52df98","type":"server","name":"Home Assistant","legacy":false,"addon":false,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true,"cacheJson":true},{"id":"b657dc9c.d2cca","type":"mqtt-broker","name":"mosquitto","broker":"mosquitto","port":"1883","clientid":"","usetls":false,"compatmode":false,"keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","closeTopic":"","closeQos":"0","closePayload":"","willTopic":"","willQos":"0","willPayload":""}]

Why bother to convert the output of the ping to a 1/0? Since are sending that to a switch node why not test the value there? that would eliminate a lot of function nodes.
Screen Shot 2021-02-09 at 9.14.45 AM

I think that was left over from my attempt at getting the counter function to work, and then I copied and pasted to get the desired number of loops, I'll tweak the flow to remove the function nodes now, thanks for the tip

Can you comment to the repetition of loops at all as that's the main part of the flow I'm looking to streamline, my attempt with the counter either got the flow to count to 5 once, and then start from 6 or the reset would kick in at 5 but then would also act as a trigger and mean the script would never stop trying to run

It will be a bit before I can look any more. Been invaded by my 4 yr old granddaughter and we are making muffins

For the first part, rather than looping, I would repeatedly fire the ping at some rate, maybe once every 5 seconds, and feed that into a Trigger node, set to send nothing initially, then after 30 seconds (or whatever, send the message on to the next part of the flow, but with the Trigger set to reset when it receives a good ping message. Then all the time good pings are received the trigger will keep getting reset, but if 30 seconds of bad pings are received then it sends the message on.
For firing the ping I might use an Inject node set to fire every 5 seconds in the relevant time period.
Once that is working, then in I would use basically the same technique for the pi, but maybe use a node-red-contrib-simple-gate to block or pass the messages from the inject node, so that initially they are blocked, but once the first part of your flow is complete that would open the gate and allow the triggers through.

Edit: So conceptually something like this

Thanks Colin, this is definitely a lot sleeker than my original, I've tested it out and there is an issue in that the trigger node is expecting a single digit number to reset, but the ping always outputs a number to 2 decimal places so the trigger always sends its 0 regardless of any payload its getting from the ping, I have tried to re-introduce the convert to integer function node which gets the output back to a 1 or a 0 but then the trigger node isn't seeing anything for some reason (but the debug node attached to the same output shows the 1), I thought it could be that the function node was changing the message type but it seems that's not the case, as testing with a switch and a change node does the same thing, all receiving and sending number messages

But you have definitely given me some more ideas for the flow

edit: Think I found the problem, there needs to be a priming 0 sent before a reset 1 can be seen by the trigger node

Show us what the ping node sends and how you have configured the Trigger node