Function String Extract

#1

Hello,
i want to read my out my electric meter over serial, and store it into a MySqlDatabase.
I get datas from the meter, but i dont now how i can seperate the values.
Serial output give me lines, the information that i need is in line 10,11,12 and 13.
I want to seperate it.

meter

Many Thanks,
Markus

0 Likes

#2

Hi Markus,
Just out of curiousity - what hardware did you use to get the reading out of the SMART meter?

0 Likes

#3

hi, a raspberry pi 3 and a usb infrared diode somthing like this
https://www.google.com/search?q=zähler+infrarot+auslesen&rlz=1C1GCEU_deAT821AT821&tbm=isch&source=lnms&sa=X&ved=0ahUKEwis8rCc5ezfAhUksqQKHdPCDekQ_AUI1AEoAQ&biw=1680&bih=977&safe=active&ssui=on#imgrc=GwiBPdiaEMdTaM:

0 Likes

#4

Well that looks like a string but it is hard to tell what is behind the red box. Please copy and post the debug data to a reply.

0 Likes

#5

you could try this. Add a change node after where the data comes out and select the jsonata option and add in $substring(payload,6,11) - note you may have to play with the offsets
33%20AM

0 Likes

#6

detail

0 Likes

#7

Oh after you extract the data, you may want to change it to a number - since it is a string - depending on how you want to use it.

0 Likes

#8

i need the value after "1.8.1" and "1.8.2" and "2.8.1" and "2.8.2" as number to an output
for example "1.8.2" (00005760.291) output 5760.291

thanks

0 Likes

#9

And did you try what I suggested?

0 Likes

#10

Function way:

var parts = msg.payload.split("(");
msg.key = parts[0];
msg.val = parseFloat(parts[1]);
return msg;

2 Likes

#11

Yes, this works sorry i have forget to set the "to" to expression.

the serial output give me lines, how can i count these lines to sort out what i want?

or better to find for example "1.8.1"

0 Likes

#12

Using @hotNipi function, take a look at the switch node and see if that will help.

0 Likes

#13

many thanks, to you (zenofmud and hotNipi)
final funktion
var parts = msg.payload.split("(");
msg.key = parts[0];
if (msg.key == "1.8.1")
{
msg.val = parseFloat(parts[1]);
return msg;
}

0 Likes

#14

This example is crying for a Regex solution, imo...

var parts = msg.payload.match(/([\d.]+)\(([\d.]+)\*(\w+)\)/);
msg.payload = {
    id: parts[1],
    value: parseFloat(parts[2]),
    units: parts[3]
};
return msg;

With the incoming data listed above, the output payload will be:

{
    "id": "1.8.2",
    "value": 5760.291,
    "units": "kWh"
}
0 Likes

#15

@shrickus regex makes my head hurt - I never spend enougth time with it so each time is like starting over again and again and ...

1 Like

#16

Heh heh, you are not alone... I've always suspected that I'm wired differently, but since my sweet-spots seem to be things like xslt, map-reduce, jsonata, and regex -- well, it's pretty obviously true!

BTW, so as not to disappoint -- here is the equivalent JSONata expression:

payload.$match(/([\d.]+)\(0*([\d.]+)\*(\w+)\)/) {
    "id": groups[0],
    "value": $number(groups[1]),
    "units": groups[2]
}
0 Likes

#17

Hi Steve,
I was waiting patiently for the JSONata to appear - and then, as if by magic, it did.

Happy New Year from Dynamic Dave in the UK

1 Like