How to extract a substring from a payload and feed it into a input_text in home assistant?

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
":soccer: 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!

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

1 Like

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

$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

[{"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":[]}]
1 Like

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!

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