Check for property in payload

Hello

I have the following payload:

image

I want to check if "battery" is present in the payload. I have tried this (sometimes it is not there if I lose connection with the sensor):

if (Object.prototype.hasOwnProperty.call(msg.payload, "battery:")) {

msg.payload = "TRUE";

return msg;

}

But I cannot get it to report true, is there something I am missing?

There are multiple ways to do this.

If you want to use the 'pure' node-red way - I would recommend to use a switch node:


then followed with a change node where you set msg.payload to true.

or in a function node:

if("battery" in msg.payload){
 ... 
}


if(msg.payload.hasOwnProperty("battery")){
...
}
1 Like

Ok thank you I will try that. Just curious why the way in my original post doesn’t work?

Because the propery is not called battery: its battery

Lol oh dear! thank you very much :slight_smile:

Just checking where help is needed (unanswered posts, drive by etc etc)

And it took me much longer then it should have to realise what on earth you were on about - then I saw it :face_with_peeking_eye:

1 Like