Neater way of checking ping results

Trying to get my head around Javascript and Node-Red this year and looking to use a bit more code rather than convoluted flows that stretch across the screen...

I am using the ping function from node-red-node-ping to check a list of devices for network connection and merging the resultant messages back into one using the join node as I know the number of messages returned.

This gives me a JSON object (using test URLs here) like this:

{"google.com":6.98,"microsoft.com":14.2}

If any system is not available I get a boolean false as the ping time:

{"google.com":7.01,"testsitetofail.com":false}

I need to be able to see if any have failed (not fussed which - any is fine as I do not need to know the site name just the 'fail' state) and I've been converting this to a string and using the switch node testing for the text ":false" but feel there is a better Javascript way!

Problem is I have zero idea what to code - in Python I would just scan this as a dictionary but my Javascript is very very weak :slight_smile: :blush:

I seen code using for / break or object.entries but unsure if these work within Node-Red and what is a better way to go.

Extra thanks if you can point me in the direction of a good intro to Javascript for Node-Red tutorial!

can you share a sample flow that presents this output?

Do you check an array of things or have individual ping nodes and individual results?

Again share your flow


To share flow, select the nodes of interest, press ctrl+e, click copy, paste the reply into a </> code block

```
paste flow between 3 backticks like this
```

Using your data

{"google.com":7.01,"testsitetofail.com":false}

and this flow

[{"id":"b0282b9b2d1118b4","type":"inject","z":"339501fde5391440","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"google.com\":7.01,\"testsitetofail.com\":false}","payloadType":"json","x":350,"y":120,"wires":[["7e523902c1d1a585"]]},{"id":"a9c28b6388aa36cf","type":"debug","z":"339501fde5391440","name":"Payload false","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"parts.key","targetType":"msg","statusVal":"","statusType":"auto","x":760,"y":100,"wires":[]},{"id":"7e523902c1d1a585","type":"split","z":"339501fde5391440","name":"","splt":"\\n","spltType":"str","arraySplt":1,"arraySpltType":"len","stream":false,"addname":"","x":470,"y":120,"wires":[["8ee65244ddc8670f"]]},{"id":"8ee65244ddc8670f","type":"switch","z":"339501fde5391440","name":"","property":"payload","propertyType":"msg","rules":[{"t":"false"},{"t":"else"}],"checkall":"true","repair":false,"outputs":2,"x":590,"y":120,"wires":[["a9c28b6388aa36cf"],["043137d32b541f24"]]},{"id":"043137d32b541f24","type":"debug","z":"339501fde5391440","name":"Valid payload","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"parts.key","targetType":"msg","statusVal":"","statusType":"auto","x":750,"y":140,"wires":[]}]

I get this, which might be useful, even without function nodes :smile:

If this is the only request, then a switch node with jsonata :

$contains($string($.*),"false") = true ? "fail" : "success"

will give you fail if there is at least one false in all the pings.

That seem like an overly complex JSONata expression

$not(false in $$.payload.*)

will return false if any false pings are returned

or Javascript equiverlent

msg.payload = !Object.values(msg.payload).some(val => val === false);

I knew that I was taking a risk in proposing a not so great jsonata method :wink:
The "only" merit of mine (I think), it is a bit less cryptic.
Sometimes (often ?) jsonata is cryptic. For me at least.

1 Like

Wow, thanks folk - lots of bits to try and get my head around :slight_smile:

A test flow (sorry should have included it) is:

[{"id":"a3c75a60826c7302","type":"tab","label":"Testing","disabled":false,"info":"","env":[]},{"id":"1a871f9f3426c836","type":"ping","z":"a3c75a60826c7302","protocol":"Automatic","mode":"triggered","name":"","host":"","timer":"20","inputs":1,"x":570,"y":320,"wires":[["f718fbc578ac622b"]]},{"id":"74925663b4591d50","type":"debug","z":"a3c75a60826c7302","name":"Raw Payload","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":810,"y":440,"wires":[]},{"id":"12131cadced372e8","type":"inject","z":"a3c75a60826c7302","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":200,"y":320,"wires":[["af20663a3ba2595d"]]},{"id":"af20663a3ba2595d","type":"function","z":"a3c75a60826c7302","name":"function 1","func":"msg.payload = [\n    {\n        \"host\": \"google.com\",\n        \"name\": \"external test point\",\n        \"timeout\": 10000,\n    },\n    {\n        \"host\": \"testsitetofail.com\",\n        \"name\": \"external fail point\",\n        \"timeout\": 10000,\n    }\n]\nreturn msg;\n","outputs":1,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":400,"y":320,"wires":[["1a871f9f3426c836"]]},{"id":"f718fbc578ac622b","type":"join","z":"a3c75a60826c7302","name":"","mode":"custom","build":"object","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":false,"timeout":"","count":"2","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":730,"y":320,"wires":[["74925663b4591d50","7ce0692d711772d4"]]},{"id":"7ce0692d711772d4","type":"json","z":"a3c75a60826c7302","name":"","property":"payload","action":"str","pretty":false,"x":910,"y":320,"wires":[["9329ce50b06d5bd4"]]},{"id":"40d03418faeccfc1","type":"debug","z":"a3c75a60826c7302","name":"Failed","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1250,"y":300,"wires":[]},{"id":"9329ce50b06d5bd4","type":"switch","z":"a3c75a60826c7302","name":"","property":"payload","propertyType":"msg","rules":[{"t":"cont","v":":false","vt":"str"},{"t":"else"}],"checkall":"true","repair":false,"outputs":2,"x":1070,"y":320,"wires":[["40d03418faeccfc1"],["35593195f92cb82a"]]},{"id":"35593195f92cb82a","type":"debug","z":"a3c75a60826c7302","name":"All OK","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1250,"y":340,"wires":[]}]

No need for JSON node
Here is example using JSONata in switch node.

[{"id":"12131cadced372e8","type":"inject","z":"a3c75a60826c7302","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":200,"y":320,"wires":[["af20663a3ba2595d"]]},{"id":"af20663a3ba2595d","type":"function","z":"a3c75a60826c7302","name":"function 1","func":"msg.payload = [\n    {\n        \"host\": \"google.com\",\n        \"name\": \"external test point\",\n        \"timeout\": 10000,\n    },\n    {\n        \"host\": \"testsitetofail.com\",\n        \"name\": \"external fail point\",\n        \"timeout\": 10000,\n    }\n]\nreturn msg;\n","outputs":1,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":400,"y":320,"wires":[["1a871f9f3426c836"]]},{"id":"1a871f9f3426c836","type":"ping","z":"a3c75a60826c7302","protocol":"Automatic","mode":"triggered","name":"","host":"","timer":"20","inputs":1,"x":570,"y":320,"wires":[["f718fbc578ac622b"]]},{"id":"f718fbc578ac622b","type":"join","z":"a3c75a60826c7302","name":"","mode":"custom","build":"object","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":false,"timeout":"","count":"2","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":730,"y":320,"wires":[["74925663b4591d50","9329ce50b06d5bd4"]]},{"id":"74925663b4591d50","type":"debug","z":"a3c75a60826c7302","name":"Raw Payload","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":810,"y":440,"wires":[]},{"id":"9329ce50b06d5bd4","type":"switch","z":"a3c75a60826c7302","name":"","property":"$not(false in $$.payload.*)","propertyType":"jsonata","rules":[{"t":"false"},{"t":"else"}],"checkall":"true","repair":false,"outputs":2,"x":1070,"y":320,"wires":[["40d03418faeccfc1"],["35593195f92cb82a"]]},{"id":"40d03418faeccfc1","type":"debug","z":"a3c75a60826c7302","name":"Failed","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1250,"y":300,"wires":[]},{"id":"35593195f92cb82a","type":"debug","z":"a3c75a60826c7302","name":"All OK","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1250,"y":340,"wires":[]}]

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