Function Node Make iOS Dynamic Notification Line Break

I am using node red to create an iOS notification. I would really like to define a line break in the notification for formatting issues. I stumbled across a website similar to this one. I found some code to create a new line but the problem is I can’t figure out how to insert my flow variable into it.

https://sebhastian.com/new-line-javascript/

I have two function nodes, one of them creating the message body, another one creating the message, which is then passed to the call service node.

To best of my knowledge it is javascript console.
Here is the code I have:

var a = '{"iOS_Notification" : "this is line 1\\nthis is line 2\\nthis is line 3\\nthis is line 4\\nthis is line 5 "}';
var b = JSON.parse(a);
msg.payload= b

return msg;

function node 2

var iOS_Notification = msg.payload.iOS_Notification
msg.payload =
{ 
    "data": {
    "message":  iOS_Notification
}
}
return msg;

The whole flow:

[{"id":"154c352b23a3a7f0","type":"tab","label":"Flow 1","disabled":false,"info":""},{"id":"eed7f3ed0810fab7","type":"inject","z":"154c352b23a3a7f0","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payloadType":"date","x":275,"y":460,"wires":[["e8923ef9a59046d9"]],"l":false},{"id":"e8923ef9a59046d9","type":"function","z":"154c352b23a3a7f0","name":"","func":"var a = '{\"iOS_Notification\" : \"this is line 1\\\\nthis is line 2\\\\nthis is line 3\\\\nthis is line 4\\\\nthis is line 5 \"}';\nvar b = JSON.parse(a);\nmsg.payload= b\n\nreturn msg;\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":315,"y":420,"wires":[["efc023215ddb677e"]],"l":false},{"id":"efc023215ddb677e","type":"function","z":"154c352b23a3a7f0","name":"","func":"var iOS_Notification = msg.payload.iOS_Notification\nmsg.payload =\n{ \n    \"data\": {\n    \"message\":  iOS_Notification\n}\n}\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":355,"y":380,"wires":[["269d10f277d180ad"]],"l":false},{"id":"269d10f277d180ad","type":"api-call-service","z":"154c352b23a3a7f0","name":"","server":"7acb3af6.49e9d4","version":3,"debugenabled":false,"service_domain":"notify","service":"our_iphones","entityId":"","data":"{}","dataType":"json","mergecontext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":395,"y":340,"wires":[[]],"l":false},{"id":"7acb3af6.49e9d4","type":"server","name":"Home Assistant","version":1,"legacy":false,"addon":true,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true,"cacheJson":true}]

I am sure this could be done better. I am not a programmer by day but just an enthusiast. I love learning about new things and would be grateful if someone could point out away to do this.

My objective is to create line breaks with dynamic iOS notifications. The code works well enough for static notifications.

If you are using JSON parse - you are just turning it in to an object. So you could literally just do this:

msg.payload.IOS_Notification = "this is line 1\nthis is line 2\nthis is line 3\nthis is line 4";
return msg;

If you want to use variables in it:

msg.payload.IOS_Notification = "this is some text\nand the temperature is " + msg.temperature + "\nwhat a lovely day it is";
return msg;

(msg.temperature would have been set in some other part of the flow, in this example - eg a get state node)

1 Like

Andrew, thank you so much for the solution. :+1: I was racking my brain trying to figure it out. It seems so easy. I was trying to make it much more difficult than it really was.

I have posted the working flow down here for people who come across this post later.

[{"id":"3e8d5b6e938cdabc","type":"tab","label":"Working Line Break ","disabled":false,"info":""},{"id":"59b623aa94c28d87","type":"inject","z":"3e8d5b6e938cdabc","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payloadType":"date","x":335,"y":380,"wires":[["b47ab403d773e12b"]],"l":false},{"id":"b47ab403d773e12b","type":"api-current-state","z":"3e8d5b6e938cdabc","name":"Gets Current Temp & Stores to msg.temperature","server":"7acb3af6.49e9d4","version":2,"outputs":1,"halt_if":"","halt_if_type":"num","halt_if_compare":"is","entity_id":"sensor.openweathermap_temperature","state_type":"str","blockInputOverrides":false,"outputProperties":[{"property":"temperature","propertyType":"msg","value":"","valueType":"entityState"},{"property":"data","propertyType":"msg","value":"","valueType":"entity"}],"override_topic":false,"state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","x":375,"y":340,"wires":[["ada68bc380ba8787"]],"icon":"font-awesome/fa-thermometer-3","l":false},{"id":"ada68bc380ba8787","type":"api-current-state","z":"3e8d5b6e938cdabc","name":"Gets Current Humidity and Stores to msg.humidity","server":"7acb3af6.49e9d4","version":2,"outputs":1,"halt_if":"","halt_if_type":"num","halt_if_compare":"is","entity_id":"sensor.openweathermap_humidity","state_type":"str","blockInputOverrides":false,"outputProperties":[{"property":"humidity","propertyType":"msg","value":"","valueType":"entityState"},{"property":"data","propertyType":"msg","value":"","valueType":"entity"}],"override_topic":false,"state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","x":415,"y":300,"wires":[["aab7fbdb14649c99"]],"icon":"font-awesome/fa-percent","l":false},{"id":"aab7fbdb14649c99","type":"function","z":"3e8d5b6e938cdabc","name":"Creates Notification","func":"var iOS_Notification = \"The Temperature is \" + msg.temperature + \"°C\"+ \"\\nThe Humidity is \" + msg.humidity  +\"%\" +\"\\nWhat a lovely day it is!\";\n\nmsg.payload =\n{ \n    \"data\": {\n    \"message\":  iOS_Notification\n}\n}\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":455,"y":260,"wires":[["608061a6682069b3"]],"l":false},{"id":"608061a6682069b3","type":"api-call-service","z":"3e8d5b6e938cdabc","name":"Sends Notification","server":"7acb3af6.49e9d4","version":3,"debugenabled":false,"service_domain":"notify","service":"our_iphones","entityId":"","data":"{}","dataType":"json","mergecontext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":495,"y":220,"wires":[["5d02bd95822d6039"]],"icon":"node-red/envelope.svg","l":false},{"id":"5d02bd95822d6039","type":"debug","z":"3e8d5b6e938cdabc","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":535,"y":180,"wires":[],"l":false},{"id":"7acb3af6.49e9d4","type":"server","name":"Home Assistant","version":1,"legacy":false,"addon":true,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true,"cacheJson":true}]

If you are creating the message much earlier in the flow, so it is gradually being built up as it goes through the flow, you also have the option of just putting in a placeholder like “~” or something instead of having to remember to do “\n” all the time. And then before you pass it to the service node in the final function, do:

var iOS_Notification = msg.payload.iOS_Notification
msg.payload =
{ 
    "data": {
    "message":  iOS_Notification.replace("~","\n");
}
}
return msg;

And it will replace all your “~” with “\n” for you.

1 Like

Thanks for the tip. :+1: I still have so much left to learn.