# How to extract a substring from a payload and feed it into a input\_text in home assistant?

**URL:** https://discourse.nodered.org/t/how-to-extract-a-substring-from-a-payload-and-feed-it-into-a-input-text-in-home-assistant/81502
**Category:** General
**Tags:** home-assistant
**Created:** [23 September 2023 01:14 UTC](https://discourse.nodered.org/t/how-to-extract-a-substring-from-a-payload-and-feed-it-into-a-input-text-in-home-assistant/81502 "2023-09-23T01:14:24Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![marcor78](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/marcor78/32/83123_2.png) [@marcor78](https://discourse.nodered.org/u/marcor78)
#### Post date: [23 September 2023 01:14 UTC](https://discourse.nodered.org/t/how-to-extract-a-substring-from-a-payload-and-feed-it-into-a-input-text-in-home-assistant/81502/1 "2023-09-23T01:14:24Z")

</div>

Hello,  
it's driving me nuts and surely there's a simple answer that I just can't see right now...  
I have a payload like this  
"⚽ 90+1' Gol! 0 - [1] Canotto L."  
and I want to extract the result (which in this case is 0 - 1) and feed it into an input\_text in home assistant, by Call Service Node.  
Is there a simple way to do it? I guess with some regex and change node, but it's way far from my skills...  
any help will be appreciated!

---

<div class="post-metadata">

### Author: ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)
#### Post date: [23 September 2023 04:42 UTC](https://discourse.nodered.org/t/how-to-extract-a-substring-from-a-payload-and-feed-it-into-a-input-text-in-home-assistant/81502/2 "2023-09-23T04:42:55Z")

</div>

Regex is probably the way to go.

If you can provide a sample set of strings in all their varying formats, we will likely be able to generate a single regex that works for every combination.

Please also describe each part of a string and whether any parts are optional etc

---

<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: [23 September 2023 05:28 UTC](https://discourse.nodered.org/t/how-to-extract-a-substring-from-a-payload-and-feed-it-into-a-input-text-in-home-assistant/81502/3 "2023-09-23T05:28:07Z")

</div>

Not a HA user, but i do know in HA nodes you can use JSONata `J:`  
so you could use something similar to

```auto
(
$parts := $split($$.payload, " ");
$substringBefore($substringAfter($parts[3], "["), "]") &
"-" &
$substringBefore($substringAfter($parts[5], "["), "]")
)

```

directly in the HA node, no need for a change node.

Example in a change node as i do not have HA installed

```auto
[{"id":"bde50f1e49b8d03c","type":"inject","z":"b9860b4b9de8c8da","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":":soccer: 90+1' Gol! 0 - [1] Canotto L.","payloadType":"str","x":270,"y":120,"wires":[["3f8ffb44d2555609"]]},{"id":"3f8ffb44d2555609","type":"change","z":"b9860b4b9de8c8da","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"(\t$parts := $split($$.payload, \" \");\t$substringBefore($substringAfter($parts[3], \"[\"), \"]\") &\t \"-\" &\t $substringBefore($substringAfter($parts[5], \"[\"), \"]\")\t)","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":440,"y":120,"wires":[["206ef230aa8a551e"]]},{"id":"206ef230aa8a551e","type":"debug","z":"b9860b4b9de8c8da","name":"debug 338","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":690,"y":120,"wires":[]}]

```

---

<div class="post-metadata">

### Author: ![marcor78](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/marcor78/32/83123_2.png) [@marcor78](https://discourse.nodered.org/u/marcor78)
#### Post date: [23 September 2023 08:35 UTC](https://discourse.nodered.org/t/how-to-extract-a-substring-from-a-payload-and-feed-it-into-a-input-text-in-home-assistant/81502/4 "2023-09-23T08:35:34Z")

</div>

> [@E1cid](#):
>
> ```auto
> (
> $parts := $split($.payload, " ");
> $substringBefore($substringAfter($parts[3], "["), "]") &
> "-" &
> $substringBefore($substringAfter($parts[5], "["), "]")
> )
> 
> ```

Just perfect! Works like a charm! I ended up setting a change node and then put the payload into the ha node. You saved my day!

---

<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: [22 November 2023 08:36 UTC](https://discourse.nodered.org/t/how-to-extract-a-substring-from-a-payload-and-feed-it-into-a-input-text-in-home-assistant/81502/5 "2023-11-22T08:36:05Z")

</div>

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