Extract labels & values from MQTT message

On a cloud node-RED instance I receive a MQTT message of power data (in the influx line protocol format);
grafana,device=diverter,type=energy grid=264,solar=0,divert=0,volts=242.71,accusage=0.7333333333333333,accsolar=0,accdivert=0,temp=10.87,usage=264,rssi=-68 1582828964661000000
and I would like to parse the data pairs into an object payload.

The string is split into 3 sections, with each section being defined by a space;
The first section (which I don't need) - grafana,device=diverter,type=energy
The second section (needed, but in a format that I can use) - grid=264,solar=0,divert=0,volts=242.71,accusage=0.7333333333333333,accsolar=0,accdivert=0,temp=10.87,usage=264,rssi=-68
and the final section ( a timestamp which I also need) - 1582828964661000000

So far, I have a function which breaks up the 3 sections;

const x = msg.payload.trim().split(" ");
const data = x[1];
const timestamp = x[2];

and which results in data =
and in timestamp = the timestamp 1582828964661000000 (I can sort the timestamp ok)

But how can I further progress the data?

Advice would be appreciated.

let arr = msg.payload.split(" ")
let pairs = arr[0].split(",")
let result = {}
function addToResult(ob){
    let a = ob.split("=")
    result[a[0]] = a[1] 
pairs.map(ob => addToResult(ob))
result.timestamp = parseInt(arr[1])/1000

msg.payload = result
return msg;

Many thanks, perfect :+1:

1 Like

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