How to add double quotes to msg.payload

I have a function node that send msg.payload to a terminal program for communication with a 4G modem.
How can I put " " so that the outgoing 'meg.payload +text' looks like "value my text "
and not just like, value-my-text

The code below almost works, but I have to use - to concatenate the sentence into a 'word', but it doesn't work with negative values

  if (msg.payload < 0 && !context.sentAlert) {
       msg.payload = msg.payload.toFixed(1) +"temperature-below-freezing-point";
      context.sentAlert = true;
      return msg;
  } else if (msg.payload > 1.0) {
      context.sentAlert = false;
  return null;

JavaScript lets you use either ' or " for strings. So if you want the string to contain ", then use ' as the string delimiter:

msg.payload = '"' + msg.payload.toFixed(1) +'"temperature-below-freezing-point"';

Some other JavaScript ways of doing it:

msg.payload = `"${msg.payload.toFixed(1)} temperature-below-freezing-point"`;
msg.payload = "\"" + msg.payload.toFixed(1) +"\"temperature-below-freezing-point\"";

Thank you for your answer!

Have tested most combinations of this, without getting it to work with spaces or - characters from the temperature gauge.
Ended up with this solution which works satisfactorily, without having understood why this works.
Also works with negative values

msg.payload = "OutTemp," + msg.payload.toFixed(1) + "°C";

Hi @Aurgelme

I don't see how that relates to your original question about including double quotes in a string... but I'm glad you figured out a solution to the problem.

Double quotes disappear from the output in the function node with the first solution, but are intact with this solution

Output "OutTemp,-0.3°C"

@Aurgelme you are not adding " to your string.

If you are viewing this in the Debug sidebar, it is showing the " to indicate it is a string - the actual value doesn't contain them.

I'm a total noob so I don't understand the whole sequence, but I get "" these in the exec node now, and the message is sent to the phone, like this
/4G/send-sms.js 12345678 "text message"
"text message" is add msg.payload in exec-node

I really appreciate any corrections, so maybe there is hope for an old man to learn some programming :slight_smile:

@knolleary Thanks for setting me on the right track

I finally figured out the problem, firstly there was a mistake in your suggestions with one " character too much, which I should have seen myself. The second was that what I typed in the node-red editor was not correct even windows is set to English keyboard, so had to write the code line in notepad and paste the whole line into the node-red editor. (haven't found out why)
Starting msg.payload with a text string instead of a number also solved the problem with negative values. So end up with this option.

msg.payload = "\"Out Temp Warning " + msg.payload.toFixed(1) + "°C\"";

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