What is wrong with my function? Javascript "if" doesn't seem to work


I admit I'm not javascript knowlegeable neither programmer.... I'm cutting/pasting pieces of code in order to make things work...

I'm trying to get a flow working but I can't understand what I'm doing wrong on this one....
Basically I'm using a function to perform some filtering based on "if" statements. While some of them work, there is one that is not working...

if(msg.device.ieeeAddr==="0x00124b00224350da")   // This one works
 if(msg.payload.contact==="false") // This one never works
  record_object="Kitchen Door Opened. Node 119. Contact value: " + msg.payload.contact;
  Timestamp = Timestamp=(new Date()).toISOString();
 else  // as alternative to the previous "if" statement, this one is always called.
  record_object="Kitchen Door Closed. Node 119. Contact value: " + msg.payload.contact;
  Timestamp=Timestamp=(new Date()).toISOString();
 record_string = Timestamp + "\n" + record_object;
     Node: "119",
     Bat: msg.payload_raw.battery

The function returns the message, which, by definition is wrong because if the contact value is "false", the message should be "Kitchen door Opened", not "Closed":

2021-01-25T21:42:56.578ZKitchen Door Closed. Node 119. Contact value: false




"description":"Contact sensor",

Any idea what am I doing wrong?

Hi @jabss,

I 'think' you should replace if(msg.payload.contact==="false") by if(msg.payload.contact=="false") or if(msg.payload.contact===false).

Because === means "equal and the same type". But in your input message the "contact":false means it is type boolean, while `msg.payload.contact==="false"``means that you expect a string "false".

If you simply use ==, it checks whether the value is equal but not the type.

P.S. I haven't tested my theory ...

1 Like


I had tried before with the two equals " == " but without success....
Haven't tried the true/false statement without the quotes ( " " ).
With two equals and the statement without quotes it works!


Thank you very much!

1 Like

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