Detecting type of payload

Hi guys.

I am playing with a ping node. It returns a message payload of type boolean if its not visible.

If it gets a response it returns a message payload of type number.

So can someone help with a pice of code to use in a function block that I can use to return simply a true or false. True when its a number and false if its a boolean.

I am having a mental block…

There is typeof

Something like:

if (typeof msg.payload ===  'boolean') {
  newMsg.payload = true
} else if (typeof msg.payload === 'number')  {
  newMsg.payload = false
}
return newMsg

I havent test this, might need some adjustment

Thank you. I will give it a go later today.

Ok I have been playing around. I have a lot on my plate at the moment and ost likely its a small mistake but I cant get it to work

I ended up with

var newMsg = true;

if (typeof msg.payload === ‘boolean’) {

newMsg.payload = true

} else if (typeof msg.payload === ‘number’) {

newMsg.payload = false

}

return newMsg

but I keep getting in the debug window
“Function tried to send a message of type boolean”

Not sure what I have done but as I said earlier my mind is mush at the moment

Maybe try declare the empty object first.

var newMsg = {};

I Forgot about this thread. I’ll try to test it later.

Here is a simple flow

[{"id":"b1d2e08.d89182","type":"inject","z":"5db76bc2.b15b94","name":"","topic":"","payload":"20","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":230,"y":500,"wires":[["d37f18fe.13dc18"]]},{"id":"d37f18fe.13dc18","type":"function","z":"5db76bc2.b15b94","name":"","func":"var newMsg = {};\nif (typeof msg.payload === 'boolean') {\n    newMsg.payload = true\n} else if (typeof msg.payload === 'number') {\n    newMsg.payload = false\n}\nreturn newMsg;","outputs":1,"noerr":0,"x":426.0173645019531,"y":511.0104064941406,"wires":[["57bf5a9f.0435e4"]]},{"id":"57bf5a9f.0435e4","type":"debug","z":"5db76bc2.b15b94","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","x":590,"y":500,"wires":[]},{"id":"a69fe205.87415","type":"inject","z":"5db76bc2.b15b94","name":"","topic":"","payload":"true","payloadType":"bool","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":230,"y":560,"wires":[["d37f18fe.13dc18"]]}]

This would satisfy the requirement of:

if (typeof msg.payload === 'boolean')
 { msg.payload = false; }
else
 { msg.payload = true; }
return msg;

Thanks heaps. I will check it all out.

I feel I am asking a lot of questions and you all are helping out. Here is a link to what I am up to