# Change a $tring of letters to have a full stop and a space

**URL:** <https://discourse.nodered.org/t/change-a-tring-of-letters-to-have-a-full-stop-and-a-space/72191>\
**Category:** General\
**Created:** [14 December 2022 13:51 UTC](https://discourse.nodered.org/t/change-a-tring-of-letters-to-have-a-full-stop-and-a-space/72191 "2022-12-14T13:51:58Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![Linhey](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/linhey/32/73679_2.png) [@Linhey](https://discourse.nodered.org/u/Linhey)\
**Post date:** [14 December 2022 13:51 UTC](https://discourse.nodered.org/t/change-a-tring-of-letters-to-have-a-full-stop-and-a-space/72191/1 "2022-12-14T13:51:58Z")

</div>

I am collecting some flight server data in Node Red and sending the data as a text string to Home Assistant to use with Google TTS speech. The issue I have is that the Callsign part is a string of letters such as RYR1234G, which I want to be spoken as characters not a word. I know that a space and/or a full-stop between each character may do the trick (R .Y .R .1 .2 .3 .4 .G ), but my solution is ugly, due to a lack of knowledge and although it sort of works, I don't know how do this with a random length of characters.  
Please can someone show me a better way of doing this or should I be doing something with the Google TTS coding?

```auto
[{"id":"da8a6ef0b3c9a5c8","type":"tab","label":"Flow 1","disabled":false,"info":"","env":[]},{"id":"ef9f06be11be1595","type":"inject","z":"da8a6ef0b3c9a5c8","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":true,"onceDelay":0.1,"topic":"","payload":"RYR123G","payloadType":"str","x":350,"y":140,"wires":[["c25bd685916af4ca"]]},{"id":"777ae9cd4da78bd3","type":"debug","z":"da8a6ef0b3c9a5c8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":1150,"y":140,"wires":[]},{"id":"c25bd685916af4ca","type":"function","z":"da8a6ef0b3c9a5c8","name":"","func":"var call = msg.payload;\nlet one = call.substr(0,1);\nlet two = call.substr(1,1);\nlet three = call.substr(2,1);\nlet four = call.substr(3,1);\nlet five = call.substr(4,1);\nlet six = call.substr(5,1);\nlet seven = call.substr(6,1);\nlet eight = call.substr(7,1);\n\n\n//return {payload:{one}};\nreturn {payload:{one:one,two:two,three:three,four:four,five:five,six:six,seven:seven,eight:eight}}\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":560,"y":140,"wires":[["0fb13606396dd088"]]},{"id":"0fb13606396dd088","type":"change","z":"da8a6ef0b3c9a5c8","name":"","rules":[{"t":"set","p":"payload.all","pt":"msg","to":"$string(payload.one) & \" .\" & $string(payload.two) & \" .\" & $string(payload.three) & \" .\" & $string(payload.four) & \" .\" & $string(payload.five) & \" .\" & $string(payload.six) & \" .\" & $string(payload.seven) & \" .\" & $string(payload.eight) & \" .\" ","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":750,"y":140,"wires":[["9442164bb3990b3f"]]},{"id":"9442164bb3990b3f","type":"change","z":"da8a6ef0b3c9a5c8","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"payload.all","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":970,"y":140,"wires":[["777ae9cd4da78bd3"]]},{"id":"3da6785dbcb2f779","type":"comment","z":"da8a6ef0b3c9a5c8","name":"Splitup a code with .'s","info":"Splitup a code with .'s","x":360,"y":80,"wires":[]}]

```

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [14 December 2022 14:00 UTC](https://discourse.nodered.org/t/change-a-tring-of-letters-to-have-a-full-stop-and-a-space/72191/2 "2022-12-14T14:00:15Z")

</div>

```auto
[{"id":"f38450d4577c05fb","type":"inject","z":"da8a6ef0b3c9a5c8","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":true,"onceDelay":0.1,"topic":"","payload":"RYR123G","payloadType":"str","x":390,"y":270,"wires":[["8ba6492f2ca8fe19"]]},{"id":"8ba6492f2ca8fe19","type":"function","z":"da8a6ef0b3c9a5c8","name":"","func":"msg.payload = msg.payload.split('').join('.');\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":570,"y":270,"wires":[["65548732bd8ae057"]]},{"id":"65548732bd8ae057","type":"debug","z":"da8a6ef0b3c9a5c8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":760,"y":270,"wires":[]}]

```

---

<div class="post-metadata">

**Author:** ![Linhey](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/linhey/32/73679_2.png) [@Linhey](https://discourse.nodered.org/u/Linhey)\
**Post date:** [14 December 2022 14:07 UTC](https://discourse.nodered.org/t/change-a-tring-of-letters-to-have-a-full-stop-and-a-space/72191/3 "2022-12-14T14:07:01Z")

</div>

I am slightly confused your solution is exactly the same as mine. Maybe I am wrong but it looks like my code was still in your clipboard?

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [14 December 2022 14:10 UTC](https://discourse.nodered.org/t/change-a-tring-of-letters-to-have-a-full-stop-and-a-space/72191/4 "2022-12-14T14:10:24Z")

</div>

Sorry - yes - re-edited above... should just be a function with

```auto
msg.payload = msg.payload.split('').join('.');
return msg;

```

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [14 December 2022 14:14 UTC](https://discourse.nodered.org/t/change-a-tring-of-letters-to-have-a-full-stop-and-a-space/72191/5 "2022-12-14T14:14:06Z")

</div>

And in a single change node with a JSONata expression

```auto
$join( $split( $$.payload, ""), ". ") & "."

```

Plus adding final period

---

<div class="post-metadata">

**Author:** ![Linhey](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/linhey/32/73679_2.png) [@Linhey](https://discourse.nodered.org/u/Linhey)\
**Post date:** [14 December 2022 14:17 UTC](https://discourse.nodered.org/t/change-a-tring-of-letters-to-have-a-full-stop-and-a-space/72191/6 "2022-12-14T14:17:03Z")

</div>

Phew!!! I thought I was going mad. Thank you so much, that is so simple and exactly what I need. Somehow I seem to always go for the complicated overworked solution.

---

<div class="post-metadata">

**Author:** ![Linhey](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/linhey/32/73679_2.png) [@Linhey](https://discourse.nodered.org/u/Linhey)\
**Post date:** [14 December 2022 14:20 UTC](https://discourse.nodered.org/t/change-a-tring-of-letters-to-have-a-full-stop-and-a-space/72191/7 "2022-12-14T14:20:31Z")

</div>

Brilliant!!!. I need to do a bit more reading I think, it's just a complete lack of knowledge on my part, trouble is when you get old you get stuck with what you think you know.

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [14 December 2022 14:22 UTC](https://discourse.nodered.org/t/change-a-tring-of-letters-to-have-a-full-stop-and-a-space/72191/8 "2022-12-14T14:22:58Z")

</div>

You should also be able to use the JSONata expression in your HA node directly with no need for a function or a change node.

---

<div class="post-metadata">

**Author:** ![Linhey](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/linhey/32/73679_2.png) [@Linhey](https://discourse.nodered.org/u/Linhey)\
**Post date:** [14 December 2022 14:30 UTC](https://discourse.nodered.org/t/change-a-tring-of-letters-to-have-a-full-stop-and-a-space/72191/9 "2022-12-14T14:30:32Z")

</div>

Thanks, the whole thing collects bearing, destination, origin, speed etc... whenever an aircraft is within 10km of my location.  
I want those individual items shown as entities, which is all fine. Then I thought afterwards I would send the useful parts of that to my phone for plane spotting as speech. Barmy idea I know and I obviously have too much time on my hands, but it's one way of trying to keep my very old brain active.

---

<div class="post-metadata">

**Author:** ![Buckskin](https://avatars.discourse-cdn.com/v4/letter/b/b9e5f3/32.png) [@Buckskin](https://discourse.nodered.org/u/Buckskin)\
**Post date:** [14 December 2022 18:51 UTC](https://discourse.nodered.org/t/change-a-tring-of-letters-to-have-a-full-stop-and-a-space/72191/10 "2022-12-14T18:51:46Z")

</div>

> [@E1cid](#):
>
> `$join( $split( $$.payload, ""), ". ") & "."`

Question, why do you use `$$.payload` as it works fine just using `payload`?

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [14 December 2022 19:06 UTC](https://discourse.nodered.org/t/change-a-tring-of-letters-to-have-a-full-stop-and-a-space/72191/11 "2022-12-14T19:06:04Z")

</div>

It's a convention of mine. It reminds me where in the context the property i want to use is. It does not matter so much in simple expressions, but to me it saves time when in more complex expressions. As in a map of an array of objects you would add $$ if you wanted to address the msg context.

---

<div class="post-metadata">

**Author:** ![hotNipi](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/hotnipi/32/383_2.png) [@hotNipi](https://discourse.nodered.org/u/hotNipi)\
**Post date:** [14 December 2022 19:09 UTC](https://discourse.nodered.org/t/change-a-tring-of-letters-to-have-a-full-stop-and-a-space/72191/12 "2022-12-14T19:09:05Z")

</div>

JSONata is fancy and many of multistep functionalities look like breeze with it. But the danger is if you'll need to read and understand the syntax let's say a year after tomorrow.

I'd recommend to use plain JavaScript not just for readability but also wider online support and available documentation.

Double dollar is root level selector.

---

<div class="post-metadata">

**Author:** ![Buckskin](https://avatars.discourse-cdn.com/v4/letter/b/b9e5f3/32.png) [@Buckskin](https://discourse.nodered.org/u/Buckskin)\
**Post date:** [14 December 2022 19:32 UTC](https://discourse.nodered.org/t/change-a-tring-of-letters-to-have-a-full-stop-and-a-space/72191/13 "2022-12-14T19:32:08Z")

</div>

Thank you, both of you. I have only used JSONata for simple operations, mainly to see if I could, as I much prefer Javascript & function nodes (Not the correct Node-red way I know).

I have also checked, using msg.payload also works and is a lot more obvious to a simple sole like myself (although so far I have only ever used 'payload')

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [14 December 2022 19:43 UTC](https://discourse.nodered.org/t/change-a-tring-of-letters-to-have-a-full-stop-and-a-space/72191/14 "2022-12-14T19:43:22Z")

</div>

> [@Buckskin](#):
>
> (Not the correct Node-red way I know).

it is there to be used... if you're happy - use it.  
(I know I do 🙂 )

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [14 December 2022 20:01 UTC](https://discourse.nodered.org/t/change-a-tring-of-letters-to-have-a-full-stop-and-a-space/72191/15 "2022-12-14T20:01:03Z")

</div>

> [@Buckskin](#):
>
> I have also checked, using msg.payload also works

That puts JSONata into compatibility mode, which i find more confusing and i had issues there, can not recall the issues, been a while.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [12 February 2023 20:01 UTC](https://discourse.nodered.org/t/change-a-tring-of-letters-to-have-a-full-stop-and-a-space/72191/16 "2023-02-12T20:01:29Z")

</div>

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