Parsen von einem bestimmten Teil eines Strings (Parsing of a certain part of a string)

Hallo,

ich logge den Stromverbrauch mit einem Raspberry Pi und möchte die Daten in SQLite bzw. in Grafana weiterverarbeiten.

Nun lassen sich in SQLite bzw. Grafana String werte nicht so ohne weiteres verarbeiten, weshalb ich hier fragen möchte ob es eine Möglichket gibt den Wert für StromZ1 im CSV Format als Float auszugeben bzw. zu speichern?

Ich hoffe ich konnte mein Problem Verständlich erklären und freue mich auf Ratschläge.

Gruß

Patrick

var StromZ1 = msg.payload["1-0:1.8.1*255"].values[0].value; 

StromZ1 = (StromZ1);


d = new Date(),
dformat = [d.getMonth()+1,
    d.getDate(),
    d.getFullYear() + (";")].join('-')+''+
    
    [d.getHours(),
    d.getMinutes(),].join(':') + ";" + 
    Math.round(d.getTime() /1000);
    
    

    
var log = dformat +  ";" +StromZ1; flow.set('StromZ1',StromZ1,);


return[
{payload: log},

];

Translation:


Hello,

I log the power consumption with a Raspberry Pi and would like to process the data in SQLite or Grafana.

Now in SQLite or Grafana String values do not process so easily, which is why I would like to ask here if there is a possibility to output or save the value for StromZ1 in CSV format as a float?

I hope I was able to explain my problem in an understandable way and look forward to advice.


@Epsylon, can you show us the payload that you receive, and what value in said payload, you like to convert to a float?

Please provide the payload between 3 backticks (```)

```
Incoming JSON payload here.
```

Ich habe mal einen "Screenshot" gemacht in der Hoffnung das dies weiterhilft.

ich verstehe nicht ganz was Sie mit "Please provide the payload between 3 backticks (```)" meinen,
Payload

Sorry.

Sorry, ich will den Wert 11047.839 als Float und nicht als String speichern sofern das im CSV-Format überhaupt geht.

To provide a valuable solution, we need to see the data that you are receiving.
Whatever Node is sending you the power consumption information, attach it to a debug node.

Then you can copy (and paste here) the raw data payload that you see in the debug pane.

Screenshot 2023-12-21 at 10.02.18

We can use the csv node downstream - but we need a payload to use to ensure a workable solution is provided.

Wenn ich nach Ihrer Beschreibung vorgehe erhalte ich diesen Wert:

12-21-2023;11:18;1703153940;11048.0782

ich vermute mal das ist nicht das was Sie brauchen um eine Lösung vorzuschlagen, da dieser Wert im Prinzip schon im vorherigen Post angezeigt wurde.

Ich bitte um Entschuldigung für mein Unwissen.

Hey, no worries.

Instead, in your code do this...
and it will print to the debug pane - and then send us what is printed (paste it not screenshot)

```
Here
```
var StromZ1 = msg.payload["1-0:1.8.1*255"].values[0].value; 

node.warn(msg.payload) /* <----- Add this line */

StromZ1 = (StromZ1);


d = new Date(),
dformat = [d.getMonth()+1,
    d.getDate(),
    d.getFullYear() + (";")].join('-')+''+
    
    [d.getHours(),
    d.getMinutes(),].join(':') + ";" + 
    Math.round(d.getTime() /1000);
    
    

    
var log = dformat +  ";" +StromZ1; flow.set('StromZ1',StromZ1,);


return[
{payload: log},

];

{"129-129:199.130.3255":{"medium":129,"channel":129,"measurement":199,"measureType":130,"tariffRate":3,"previousMeasurement":255,"values":[{"value":"EMH","unit":""}]},"1-0:0.0.9255":{"medium":1,"channel":0,"measurement":0,"measureType":0,"tariffRate":9,"previousMeasurement":255,"values":[{"value":"0901454d48000074fde5","unit":""}]},"1-0:1.8.0255":{"medium":1,"channel":0,"measurement":1,"measureType":8,"tariffRate":0,"previousMeasurement":255,"values":[{"value":11048.1934,"unit":"kWh"}]},"1-0:1.8.1255":{"medium":1,"channel":0,"measurement":1,"measureType":8,"tariffRate":1,"previousMeasurement":255,"values":[{"value":11048.1934,"unit":"kWh"}]},"1-0:1.8.2255":{"medium":1,"channel":0,"measurement":1,"measureType":8,"tariffRate":2,"previousMeasurement":255,"values":[{"value":0,"unit":"kWh"}]},"1-0:16.7.0255":{"medium":1,"channel":0,"measurement":16,"measureType":7,"tariffRate":0,"previousMeasurement":255,"values":[{"value":232.4,"unit":"W"}]},"129-129:199.130.5*255":{"medium":129,"channel":129,"measurement":199,"measureType":130,"tariffRate":5,"previousMeasurement":255,"values":[{"value":"a41f72728c7ae1a4b83318244eeb162d4860f819be3a9f524b873a705af2a5040641e3411f0ac4fd266c537068c0a246","unit":""}]}}

So the one I have highlighted is what you need the info on?
I ask, as I can not see 1-0:1.8.1*255 in the list - so I'm just guessing?

or do you need all values?

Screenshot 2023-12-21 at 10.42.48

Ja, dieser Wert soll als float ausgegeben bzw. gespeichert werden. Andere Werte wie das Datum und den Zeitstempel sollen dann hier

var log = dformat +  ";" +StromZ1; flow.set('StromZ1',StromZ1,);

zusammengeführt werden, sodass ich alle werte in eine "Tabelle" einfügen kann.

is it always 1-0:1.8.1255?

Ja, der Wert kommt immer von 1-0:1.8.1255.

Give me a few minutes

Ok,

This should get you going in the right direction. it:

  • Appends the readings to a flow variable (readings)
  • Passes the object through, that has just been appended to readings
    • from here, you do what you need.

If you need all readings at any given time, use the flow variable (readings)

Use this in place of your code, you can attach this function node to a csv node for example.

Object example:

{
  "date": "12-21-2023",
  "time": "11:17",
  "timeCode": 1703157439,
  "kwh": 11048.1934
}

Readings Example

[
  {
    "date": "12-21-2023",
    "time": "11:17",
    "timeCode": 1703157439,
    "kwh": 11048.1934
  },
  {
    "date": "12-21-2023",
    "time": "11:17",
    "timeCode": 1703157464,
    "kwh": 11048.1934
  }
]

Code

const Target = msg.payload["1-0:1.8.1255"]

let table =  flow.get("readings") || []
flow.set("readings",table)
table =  flow.get("readings")

const date = new Date();

const row = {
    date: `${(date.getMonth()+1).toString().padStart(2,"0")}-${(date.getDate()).toString().padStart(2,"0")}-${ date.getFullYear()}`,
    time:  `${date.getHours().toString().padStart(2,"0")}:${date.getMinutes().toString().padStart(2,"0")}`,
    timeCode: Math.round(date.getTime() /1000),
    kwh: parseFloat(Target.values[0].value)
}

table.push(row)

return {payload:RED.util.cloneMessage(row)}

Das sieht sehr vielversprechend aus. Leider habe ich aber gleich noch einen Termin weshalb ich die Lösung nicht direkt testen kann.

Ich werde Sie auf jeden Fall bei Gelegenheit ausprobieren und nochmals hier berichten.

Ich danke Ihnen vielmals für Ihre Geduld und Ihre wertvolle Zeit und wünsche Ihnen Frohe Weihnachten und ein gutes Neues Jahr!

Viele Grüße

Patrick

1 Like

Frohe Weihnachten für Dich

So, habe das ganze einmal getestet und hatte noch ne kleine Fehlermeldung. Ich musste noch "1-0:1.8.1255" in "1-0:1.8.1*255" umbenennen da sonst keine Werte ausgeslesen werden konnten.

Nochmals vielen Dank für die Hilfe!

1 Like

For what it is worth :wink:

I use another approach for reading out the P1 port.
A transistor, an esp01 an ams1117 step down power regulator and a few resistors and capacitors read the data. Using the dsmr-library written by Matthijs Kooijman I decode the data and send it every 10 seconds in a mqtt-message to a broker. Node Red listens and puts all in graphs, tables and database.

I’ll be happy to give details if somebody wants :slight_smile:

1 Like

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