Help with separating Node Red msg strings

I’m Looking to Separate out the plates and its confidence
image_2024-05-05_135117934

you might want to put this on the node-red forum here: https://discourse.nodered.org/
there are some folks who use node-red here, so you might get lucky, but i’d guess most do not… and specially the more advanced users here i’d guess don’t use node-red to configure home assistant (not positive, but i’d wager)

I moved this over to Third Party Integrations - Node Red where some of our more advanced users who do use Node Red may be able to help :wink:

1 Like

thx!

Some weird characters in there - essentially what you want to use is the split node and put these into seperate records where you can then break them down further

something like msg.session.stdout.0 will then give you the list of records to look at

As a first step - feed this to a change node and get it to change msg.sessions.stdout.0 to msg.plates and put a debug on that and see what it gives you

Craig

Start by splitting on “plate”, which should remove plate and leave the rest on one line each.
Then split the lines on “:”, which should give you the plate number as the first value and the confidence as the last value.
You might have to do a .replace(“newline arrow”,“”) on the last value to get rid of the arrow. Replace the newline arrow in the text with the actual arrow.

Tricky, for several reasons.

Your string is at session.stdout.0, so the final field name as a number needs special referencing to get at it.

Your string contains the newline \n and tab \t characters. This is not necessarily a problem, but has to be taken into account.

First, this is going to be an array, so needs to be split into parts. Second, I assume that you want the plate number, the plate result ID, and the confidence, hence each part has to have these values picked out. Third, it would probably be nice to have the plate number and confidence as numbers, and some care needs to be taken to do this.

JSONata has the necessary tools to cope with all of this, and can be run in a simple change node. The first split can be done on the \np part of “plate” at the end-start between each part. Then use of $substringBefore() and $substringAfter() can pick out the plate number, the result string, and the confidence. All done using \n for the newline and \t for the tab (the strange arrow thingy is the tabulate character).

Once we have picked out the number part of the string, without any leading or trailing non-numeric characters, we can safely use $number() to turn a string into a number.

Then all it takes is a grouping operator to build an array of objects with the values you want.

I have tested this, but if it does not work for you then you will have to provide a copy of your real data. Typing in test data from a picture is tedious and prone to error.

Here is the JSONata code. I hope this helps!

(
    $x:=session.stdout.`0`;
    $a:=$split($x, "\np");
    $a.(
        $plate:=$number($substringBefore(":")~>$substringAfter("late"));
        $result:=$substringAfter("- ")~>$substringBefore("\t");
        $conf:=$substringAfter("dence:")~>$substringAfter(" ")~>$number();
        {"plate": $plate,
        "result": $result,
        "confidence": $conf}
    ); 
)
3 Likes