String compare not working when string contains ()

I have a msg payload where:

msg.payload = "(INP 8)"

The switch node is a simple:

msg.property != "(INP 8)" --> 1

Even though msg.payload is "(INP 8)", the switch node sends the message on. I tried the same thing with state on the Home Assistant node, and it never did the comparison correctly there either.

Here is the debug output showing the value of msg.payload.
Screenshot 2022-11-23 at 5.38.02 PM

What gives? It's just a simple string. I do string caparisons in switches elsewhere without a problem. Same for state.

For grins, I setup an inject node to send msg.payload = "(INP 8)". With that, it works. My guess now is there are either hidden characters in the state I'm receiving from my device, or there is a character set issue. How can I see that?

I was able to make it work by adding a function node:

msg.payload = msg.payload.replace(/[^a-zA-Z0-9() ]/g, "")
return msg;

Must be some hidden characters in there. My question still stands. How would I see those since nothing is showing in the debug window.

If I just wanted to check that particular string, I'd send it to a Write file node and then display the contents with od -bc filename (print as octal chars and printable/escape chars)

They must be unprintable control characters.

One clue in the debug side was the string[50] - telling you it believed the string to be 50 characters long.

We do replace some control characters to make them more obvious, such as newline and tab. It may be there's more to be done.

That’s what I figured. It was a bit of a head scratcher, but now that I have the function node removing any special characters all is good.

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