Trivial text formatting (new line) in a function node

5,398 views
Skip to first unread message

Bobo

unread,
May 30, 2017, 1:22:18 AM5/30/17
to Node-RED
I have been trying to format some text by creating a new line in a function node, but no luck. I have tried just about every combo of \n, \\n, etc I can think of.

Can someone please explain my error?

Thanks

if (ptime != "false") {msg.payload = msg.payload + "\n - Last ping time was on " +
  new Date().toString();}

Colin Law

unread,
May 30, 2017, 2:37:45 AM5/30/17
to node...@googlegroups.com
In what way does that not work?

Colin
> --
> http://nodered.org
>
> Join us on Slack to continue the conversation: http://nodered.org/slack
> ---
> You received this message because you are subscribed to the Google Groups
> "Node-RED" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to node-red+u...@googlegroups.com.
> To post to this group, send email to node...@googlegroups.com.
> Visit this group at https://groups.google.com/group/node-red.
> To view this discussion on the web, visit
> https://groups.google.com/d/msgid/node-red/527b9ad4-aef0-4469-a82d-2cbe6f9b2bfb%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Bobo

unread,
May 30, 2017, 2:51:55 AM5/30/17
to Node-RED
\n has no effect.
\\n causes one \n to appear in the text.

Should also say this is for output to a dashboard text node, which is where I want the new line to appear.

Colin Law

unread,
May 30, 2017, 3:16:02 AM5/30/17
to node...@googlegroups.com
When you say \n has no effect I presume you mean that when it is
displayed you see no new line. Putting the \n in the text *does* have
the effect of putting a newline in the text. I suspect that your
problem is that in html a newline is effectively ignored. If you want
to see a newline in the text you need <br> in the html text. However
if you just put this in the text I suspect the text node will escape
the string and show <br>. I will have to leave it to others to tell
you how to get round this problem

Colin
> --
> http://nodered.org
>
> Join us on Slack to continue the conversation: http://nodered.org/slack
> ---
> You received this message because you are subscribed to the Google Groups
> "Node-RED" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to node-red+u...@googlegroups.com.
> To post to this group, send email to node...@googlegroups.com.
> Visit this group at https://groups.google.com/group/node-red.
> To view this discussion on the web, visit
> https://groups.google.com/d/msgid/node-red/0401b5a6-291d-40c6-ad03-0dd2d7718576%40googlegroups.com.

Zenofmud

unread,
May 30, 2017, 5:00:51 AM5/30/17
to node...@googlegroups.com
Where are you trying to display the line of text? In a UI? Are you sending it to an email? Twitter? In a debug node?

a little more about what you are trying to do and the flow would be helpful.

--
http://nodered.org
 
Join us on Slack to continue the conversation: http://nodered.org/slack
---
You received this message because you are subscribed to the Google Groups "Node-RED" group.
To unsubscribe from this group and stop receiving emails from it, send an email to node-red+u...@googlegroups.com.
To post to this group, send email to node...@googlegroups.com.
Visit this group at https://groups.google.com/group/node-red.

Bobo

unread,
May 30, 2017, 5:13:19 AM5/30/17
to Node-RED
As previously stated, this is for output to a dashboard text node, which is where I want the new line to appear.

So input is a ping node --> function node --> text node. That is it, nothing complicated at all.

Thanks

Colin Law

unread,
May 30, 2017, 5:42:34 AM5/30/17
to node...@googlegroups.com
Looking at the info for the text node it says it can contain valid
html, so this should work

msg.payload = msg.payload + "<br> - Last ping time was on " + new
Date().toString();

Colin
> https://groups.google.com/d/msgid/node-red/47359802-1b21-41a8-a72c-98e1e4a6cbc4%40googlegroups.com.

Bobo

unread,
May 30, 2017, 5:55:45 AM5/30/17
to Node-RED
Colin, it does indeed. Thank you for your assistance.

Colin Law

unread,
May 30, 2017, 7:03:25 AM5/30/17
to node...@googlegroups.com
Bobo

Glad to be of help. Please make sure you understand why that works.

Colin
> https://groups.google.com/d/msgid/node-red/636fecbe-bc5e-4d6f-84ec-fee70cc54a05%40googlegroups.com.

Bobo

unread,
May 30, 2017, 7:55:04 AM5/30/17
to Node-RED
First off, RTFM and then read https://www.w3schools.com/tags/tag_br.asp

Peace out.

Colin Law

unread,
May 30, 2017, 8:23:39 AM5/30/17
to node...@googlegroups.com
Do you mean *I* should RTFM?

Colin
> --
> http://nodered.org
>
> Join us on Slack to continue the conversation: http://nodered.org/slack
> ---
> You received this message because you are subscribed to the Google Groups
> "Node-RED" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to node-red+u...@googlegroups.com.
> To post to this group, send email to node...@googlegroups.com.
> Visit this group at https://groups.google.com/group/node-red.
> To view this discussion on the web, visit
> https://groups.google.com/d/msgid/node-red/c139c988-ad38-470e-b883-ca6dcff5cf9b%40googlegroups.com.

Bobo

unread,
May 30, 2017, 9:16:44 AM5/30/17
to Node-RED
Only if you think you should :-)

I was referring to myself. Sometimes its easy to fall into the trap of ignoring what is right under your nose, as I did. It states quite clearly in the text node info panel that it accepts html. If I had read that first I wouldn't have wasted your time.

Thanks again.

Colin Law

unread,
May 30, 2017, 9:23:42 AM5/30/17
to node...@googlegroups.com
OK, understood.

Colin
> --
> http://nodered.org
>
> Join us on Slack to continue the conversation: http://nodered.org/slack
> ---
> You received this message because you are subscribed to the Google Groups
> "Node-RED" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to node-red+u...@googlegroups.com.
> To post to this group, send email to node...@googlegroups.com.
> Visit this group at https://groups.google.com/group/node-red.
> To view this discussion on the web, visit
> https://groups.google.com/d/msgid/node-red/2deb3247-b501-420d-97c6-c05d7a80f55b%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages