How can I test for any text received?

Hi,

I want to test for any text received in my function, this will then turn on a relay until no text received.
I tried below, but does not work. What do I need to put in the if (msg.payload === "") part to match any text ?


if (msg.payload === "")
{
  msg.payload = "RLY = ON";
  return msg;
}

Thanks

Maybe check if payload is string and if payload.length > 0 .

Try

if (msg.payload && typeof msg.payload === "string")
{
  msg.payload = "RLY = ON";
  return msg;
}

Truthy Truthy - MDN Web Docs Glossary: Definitions of Web-related terms | MDN

typeof typeof - JavaScript | MDN

An array has length property to.

example

[{"id":"a443d98b41ad5fd3","type":"inject","z":"d1395164b4eec73e","name":"trina with length and truthy","props":[{"p":"payload"},{"p":"separater","v":"\\","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"a strina with length and truthy","payloadType":"str","x":450,"y":4740,"wires":[["3b9b60abd878fb12"]]},{"id":"3b9b60abd878fb12","type":"function","z":"d1395164b4eec73e","name":"function 153","func":"if (msg.payload && typeof msg.payload === \"string\")\n{\n  msg.payload = \"RLY = ON\";\n  return msg;\n}","outputs":1,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[{"var":"path","module":"path"}],"x":690,"y":4760,"wires":[["1f8ae278cd39375c"]]},{"id":"99adde23cb42603b","type":"inject","z":"d1395164b4eec73e","name":"stinng no length and falsey","props":[{"p":"payload"},{"p":"separater","v":"\\","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"str","x":450,"y":4800,"wires":[["3b9b60abd878fb12"]]},{"id":"1f8ae278cd39375c","type":"debug","z":"d1395164b4eec73e","name":"debug 2470","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":1010,"y":4740,"wires":[]}]

[edit] added missing example.

I used JeromeVISUALS suggestion as this is for outputting of speech synthesis so it could be text or numbers that are recieved. The relay turns on an amplifier to hear the speech sent.

if (msg.payload.length > 0 )
{
  msg.payload = "RLY = ON";
  return msg;
}
  

Thanks for your help both of you :+1:

If still not working use a debug node to show us what is in the payload when no text is received.