Manipulation of a string

Hello
I get a payload which contains several pieces of information.
As an example in the debug window I get:
payload string

"ALERT Step 1 RUN shift address 1772784635 74646 7862, ABFF (next line symbol)
Step 9 RUN shift address 194600 87875 7862, ABFF (next line symbol)
System Time is 14:21(next line symbol)
ERROR Step 5 STOP move address 17384635 75767895 7862, ABFF (next linesymbol)
Step 5 STOP move address 1772784635 75765 785562, ABFF (next line symbol)
INFO Step 7 GO move address 1772784635 794646 7862, ABFF "
(the next lins symbol is a symbol I coud not past)

Sometimes there are 3 lines, other times up to 20 lines.
However, I only need the lines that start with ALERT, INFO or ERROR and then only the first first character and the first address.
The whole thing looked super easy, but I can't get it to work.
The first thing I'm trying to do is to break down the string by the next line and then look at each line individually to find my keywords. In the last step I would try to cut from the back. But I don't have an Idear yet because the information always looks a bit different.

Does anyone have an idea how to tame this jumble of data? I can't change anything about the source, because I simply read it.

Not sure how you want the output, but here is an example that will get the 3 items you are looking for. It will output first character? and first address on ERROR ALERT and INFO,.

[{"id":"5eb099e4.83f1b8","type":"inject","z":"9b3f9f31.c45298","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":130,"y":800,"wires":[["dc0d1260.d9c36"]]},{"id":"dc0d1260.d9c36","type":"template","z":"9b3f9f31.c45298","name":"","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"ALERT Step 1 RUN shift address 1772784635 74646 7862, ABFF \nStep 9 RUN shift address 194600 87875 7862, ABFF \nSystem Time is 14:21\nERROR Step 5 STOP move address 17384635 75767895 7862, ABFF \nStep 5 STOP move address 1772784635 75765 785562, ABFF \nINFO Step 7 GO move address 1772784635 794646 7862, ABFF \n","output":"str","x":290,"y":800,"wires":[["1d36d10c.2121af"]]},{"id":"1d36d10c.2121af","type":"function","z":"9b3f9f31.c45298","name":"","func":"msg.payload=msg.payload.match(/(ERROR|INFO|ALERT).*(?=\\n)/g);\nfor(let i=0; i<msg.payload.length; i++){\n    msg.payload[i] = {[msg.payload[i][0]]:msg.payload[i].split(\" \")[6]};\n}\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":490,"y":800,"wires":[["c2d9cbe7.35c678"]]},{"id":"c2d9cbe7.35c678","type":"debug","z":"9b3f9f31.c45298","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":610,"y":900,"wires":[]}]

This uses REGEX but you could also split on each line and then split each line again on " ", then check if array element 0 contains ERROR, ALERT or INFO. element 6 would contain the address.

1 Like

SUPER E1cid :clap:

now it is done with a few simple steps and I get the data displayed on my dashboard the way I wanted it.
And it is so short and compact. :grinning:
Can you please explain to me what the following syntax does?
( / (ERROR|INFO|ALERT).*(?=\n) /g) (the first slash belongs to the g)
SOLVED: RegExp g Modifier
and
RegExp ?= Quantifier I understood

Not understood is the .*
((ERROR|INFO|ALERT).*(?=\n)/g)

(ERROR|INFO|ALERT) - error or info or alert
.* - any character 0 or more times
(?=\n) - followed by a new line but do not capture the new line
g - match all matches not just first

1 Like

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