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

**URL:** https://discourse.nodered.org/t/parsen-von-einem-bestimmten-teil-eines-strings-parsing-of-a-certain-part-of-a-string/83904
**Category:** General
**Tags:** csv, float
**Created:** [21 December 2023 09:00 UTC](https://discourse.nodered.org/t/parsen-von-einem-bestimmten-teil-eines-strings-parsing-of-a-certain-part-of-a-string/83904 "2023-12-21T09:00:55Z")
**Posts on this page:** 19
**Page:** 1

<div class="post-metadata">

### Author: ![Epsylon](https://avatars.discourse-cdn.com/v4/letter/e/48db29/32.png) [@Epsylon](https://discourse.nodered.org/u/Epsylon)
#### Post date: [21 December 2023 09:00 UTC](https://discourse.nodered.org/t/parsen-von-einem-bestimmten-teil-eines-strings-parsing-of-a-certain-part-of-a-string/83904/1 "2023-12-21T09:00:55Z")

</div>

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

```auto
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},

];

```

---

<div class="post-metadata">

### Author: ![marcus-j-davies](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/marcus-j-davies/32/103435_2.png) [@marcus-j-davies](https://discourse.nodered.org/u/marcus-j-davies)
#### Post date: [21 December 2023 09:41 UTC](https://discourse.nodered.org/t/parsen-von-einem-bestimmten-teil-eines-strings-parsing-of-a-certain-part-of-a-string/83904/2 "2023-12-21T09:41:51Z")

</div>

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 (```)

````auto
```
Incoming JSON payload here.
```

````

---

<div class="post-metadata">

### Author: ![Epsylon](https://avatars.discourse-cdn.com/v4/letter/e/48db29/32.png) [@Epsylon](https://discourse.nodered.org/u/Epsylon)
#### Post date: [21 December 2023 09:49 UTC](https://discourse.nodered.org/t/parsen-von-einem-bestimmten-teil-eines-strings-parsing-of-a-certain-part-of-a-string/83904/3 "2023-12-21T09:49:58Z")

</div>

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](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/3/e/3e5b8d510a3c535b96037a6061d5149646b4905c.jpeg)

Sorry.

---

<div class="post-metadata">

### Author: ![Epsylon](https://avatars.discourse-cdn.com/v4/letter/e/48db29/32.png) [@Epsylon](https://discourse.nodered.org/u/Epsylon)
#### Post date: [21 December 2023 09:52 UTC](https://discourse.nodered.org/t/parsen-von-einem-bestimmten-teil-eines-strings-parsing-of-a-certain-part-of-a-string/83904/4 "2023-12-21T09:52:12Z")

</div>

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

---

<div class="post-metadata">

### Author: ![marcus-j-davies](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/marcus-j-davies/32/103435_2.png) [@marcus-j-davies](https://discourse.nodered.org/u/marcus-j-davies)
#### Post date: [21 December 2023 10:04 UTC](https://discourse.nodered.org/t/parsen-von-einem-bestimmten-teil-eines-strings-parsing-of-a-certain-part-of-a-string/83904/5 "2023-12-21T10:04:49Z")

</div>

> [@Epsylon](#):
>
> 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,

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](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/e/8/e8f32ed1282ec953ef4b041514ef27e1233731d5.png)

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

---

<div class="post-metadata">

### Author: ![Epsylon](https://avatars.discourse-cdn.com/v4/letter/e/48db29/32.png) [@Epsylon](https://discourse.nodered.org/u/Epsylon)
#### Post date: [21 December 2023 10:24 UTC](https://discourse.nodered.org/t/parsen-von-einem-bestimmten-teil-eines-strings-parsing-of-a-certain-part-of-a-string/83904/6 "2023-12-21T10:24:42Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![marcus-j-davies](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/marcus-j-davies/32/103435_2.png) [@marcus-j-davies](https://discourse.nodered.org/u/marcus-j-davies)
#### Post date: [21 December 2023 10:33 UTC](https://discourse.nodered.org/t/parsen-von-einem-bestimmten-teil-eines-strings-parsing-of-a-certain-part-of-a-string/83904/7 "2023-12-21T10:33:13Z")

</div>

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)

````auto
```
Here
```

````

```auto
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},

];

```

---

<div class="post-metadata">

### Author: ![Epsylon](https://avatars.discourse-cdn.com/v4/letter/e/48db29/32.png) [@Epsylon](https://discourse.nodered.org/u/Epsylon)
#### Post date: [21 December 2023 10:39 UTC](https://discourse.nodered.org/t/parsen-von-einem-bestimmten-teil-eines-strings-parsing-of-a-certain-part-of-a-string/83904/8 "2023-12-21T10:39:53Z")

</div>

> [@marcus-j-davies](#):
>
> `Here`

{"129-129:199.130.3_255":{"medium":129,"channel":129,"measurement":199,"measureType":130,"tariffRate":3,"previousMeasurement":255,"values":[{"value":"EMH","unit":""}]},"1-0:0.0.9_255":{"medium":1,"channel":0,"measurement":0,"measureType":0,"tariffRate":9,"previousMeasurement":255,"values":[{"value":"0901454d48000074fde5","unit":""}]},"1-0:1.8.0_255":{"medium":1,"channel":0,"measurement":1,"measureType":8,"tariffRate":0,"previousMeasurement":255,"values":[{"value":11048.1934,"unit":"kWh"}]},"1-0:1.8.1_255":{"medium":1,"channel":0,"measurement":1,"measureType":8,"tariffRate":1,"previousMeasurement":255,"values":[{"value":11048.1934,"unit":"kWh"}]},"1-0:1.8.2_255":{"medium":1,"channel":0,"measurement":1,"measureType":8,"tariffRate":2,"previousMeasurement":255,"values":[{"value":0,"unit":"kWh"}]},"1-0:16.7.0_255":{"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":""}]}}

---

<div class="post-metadata">

### Author: ![marcus-j-davies](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/marcus-j-davies/32/103435_2.png) [@marcus-j-davies](https://discourse.nodered.org/u/marcus-j-davies)
#### Post date: [21 December 2023 10:44 UTC](https://discourse.nodered.org/t/parsen-von-einem-bestimmten-teil-eines-strings-parsing-of-a-certain-part-of-a-string/83904/9 "2023-12-21T10:44:38Z")

</div>

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](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/1/d/1de0139cbb0e94a95e87086a7850f0e759370810.png)

---

<div class="post-metadata">

### Author: ![Epsylon](https://avatars.discourse-cdn.com/v4/letter/e/48db29/32.png) [@Epsylon](https://discourse.nodered.org/u/Epsylon)
#### Post date: [21 December 2023 10:49 UTC](https://discourse.nodered.org/t/parsen-von-einem-bestimmten-teil-eines-strings-parsing-of-a-certain-part-of-a-string/83904/10 "2023-12-21T10:49:25Z")

</div>

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

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

```

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

---

<div class="post-metadata">

### Author: ![marcus-j-davies](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/marcus-j-davies/32/103435_2.png) [@marcus-j-davies](https://discourse.nodered.org/u/marcus-j-davies)
#### Post date: [21 December 2023 10:52 UTC](https://discourse.nodered.org/t/parsen-von-einem-bestimmten-teil-eines-strings-parsing-of-a-certain-part-of-a-string/83904/11 "2023-12-21T10:52:26Z")

</div>

is it always `1-0:1.8.1255`?

---

<div class="post-metadata">

### Author: ![Epsylon](https://avatars.discourse-cdn.com/v4/letter/e/48db29/32.png) [@Epsylon](https://discourse.nodered.org/u/Epsylon)
#### Post date: [21 December 2023 10:53 UTC](https://discourse.nodered.org/t/parsen-von-einem-bestimmten-teil-eines-strings-parsing-of-a-certain-part-of-a-string/83904/12 "2023-12-21T10:53:17Z")

</div>

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

---

<div class="post-metadata">

### Author: ![marcus-j-davies](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/marcus-j-davies/32/103435_2.png) [@marcus-j-davies](https://discourse.nodered.org/u/marcus-j-davies)
#### Post date: [21 December 2023 10:58 UTC](https://discourse.nodered.org/t/parsen-von-einem-bestimmten-teil-eines-strings-parsing-of-a-certain-part-of-a-string/83904/13 "2023-12-21T10:58:15Z")

</div>

Give me a few minutes

---

<div class="post-metadata">

### Author: ![marcus-j-davies](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/marcus-j-davies/32/103435_2.png) [@marcus-j-davies](https://discourse.nodered.org/u/marcus-j-davies)
#### Post date: [21 December 2023 11:19 UTC](https://discourse.nodered.org/t/parsen-von-einem-bestimmten-teil-eines-strings-parsing-of-a-certain-part-of-a-string/83904/14 "2023-12-21T11:19:03Z")

</div>

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:

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

```

Readings Example

```auto
[
  {
    "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

```auto
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)}

```

---

<div class="post-metadata">

### Author: ![Epsylon](https://avatars.discourse-cdn.com/v4/letter/e/48db29/32.png) [@Epsylon](https://discourse.nodered.org/u/Epsylon)
#### Post date: [21 December 2023 11:25 UTC](https://discourse.nodered.org/t/parsen-von-einem-bestimmten-teil-eines-strings-parsing-of-a-certain-part-of-a-string/83904/15 "2023-12-21T11:25:52Z")

</div>

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

---

<div class="post-metadata">

### Author: ![marcus-j-davies](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/marcus-j-davies/32/103435_2.png) [@marcus-j-davies](https://discourse.nodered.org/u/marcus-j-davies)
#### Post date: [21 December 2023 11:27 UTC](https://discourse.nodered.org/t/parsen-von-einem-bestimmten-teil-eines-strings-parsing-of-a-certain-part-of-a-string/83904/16 "2023-12-21T11:27:38Z")

</div>

Frohe Weihnachten für Dich

---

<div class="post-metadata">

### Author: ![Epsylon](https://avatars.discourse-cdn.com/v4/letter/e/48db29/32.png) [@Epsylon](https://discourse.nodered.org/u/Epsylon)
#### Post date: [22 December 2023 10:14 UTC](https://discourse.nodered.org/t/parsen-von-einem-bestimmten-teil-eines-strings-parsing-of-a-certain-part-of-a-string/83904/18 "2023-12-22T10:14:27Z")

</div>

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!

---

<div class="post-metadata">

### Author: ![BarryMas](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/barrymas/32/38363_2.png) [@BarryMas](https://discourse.nodered.org/u/BarryMas)
#### Post date: [27 December 2023 14:38 UTC](https://discourse.nodered.org/t/parsen-von-einem-bestimmten-teil-eines-strings-parsing-of-a-certain-part-of-a-string/83904/19 "2023-12-27T14:38:49Z")

</div>

For what it is worth 😉

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 🙂

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)
#### Post date: [25 February 2024 14:39 UTC](https://discourse.nodered.org/t/parsen-von-einem-bestimmten-teil-eines-strings-parsing-of-a-certain-part-of-a-string/83904/20 "2024-02-25T14:39:07Z")

</div>

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