Hello,
I've been running a volkszähler database for around 2 years now and just started a few weeks ago to MQTT.
To get the MQTT messages Node-RED is used.
The push server delivers data via websockets to Node-RED.
In Node-RED, I have a flow that converts the data and publishes it to the MQTT broker.
And then I took an ESP8266 and a display and subscribe to the MQTT broker, parse the MQTT message and put it on the display.
That works so far.
Admittedly, the flow in Node-RED is just copied together and I could not write that myself. No matter, as long as it works, it's fine for me.
Now I've discovered that there are also MQTT client apps, and then comes the desire to use this too.
These apps need the MQTT messages, but in a slightly different format.
So far, the MQTT message has the following content: "timestamp = 1537392772928 value = 2.5819666840232"
Since then the apps are also displayed exactly as "timestamp = 1537392772928 value = 2.5819666840232"
This is of course nonsense, because it is supposed only to see "2.5819666840232".
Example electricity meter:
The push server delivers via websockets "{"version":"0.3","data":{"uuid":"xxxxxxxx-1111-xxxx-xxxx-xxxxxxxxxxxx","tuples":[[1537387482345,479.24790029688,1]]}}"
Now I would like to have the following format:
To be sent to the topic „Haus/Strom/WärmepumpeHaupttarif/Timestamp“
The content of the message should be "1537387482345".
To be sent to the topic „Haus/Strom/WärmepumpeHaupttarif/Value“
The content of the message should be "479.24790029688".
Example temperature:
The push server delivers via websockets "{"version":"0.3","data":{"uuid":"xxxxxxxx-2222-xxxx-xxxx-xxxxxxxxxxxx","tuples":[[1537387448162,22.187,1]]}}"
Now I would like to have the following format:
Should be sent to the topic „Haus/Temperatur/T01Aussen/Timestamp“
The content of the message should be "1537387448162".
To be sent to the topic „Haus/Temperatur/T01Aussen/Value“
The content of the message should be "22.187".
Example water:
The push server delivers via websockets "{"version":"0.3","data":{"uuid":"xxxxxxxx-3333-xxxx-xxxx-xxxxxxxxxxxx","tuples":[[1537387660470,585.1755526658,1]]}}"
Now I would like to have the following format:
Sent to the topic „Haus/Wasser/Timestamp“
The content of the message should be "1537387660470".
To be sent to the topic „Haus/Wasserverbrauch/Value“
The content of the message should be "585.1755526658".
This example I found in the internet at http://flows.nodered.org/flow/dc9754a796fac49c918ab5a94f0af58a
[{
"id": "8b5844a8.98ceb",
"type": "websocket-client",
"z": "f6a99b89.ee40b8",
"path": "ws://192.168.178.8:8082/socket",
"wholemsg": "false"
},
{
"id": "da27812f.a869e",
"type": "mqtt-broker",
"z": "f6a99b89.ee40b8",
"broker": "192.168.178.8",
"port": "1883",
"clientid": "node_red",
"usetls": false,
"verifyservercert": true,
"compatmode": false,
"keepalive": "60",
"cleansession": true,
"willTopic": "",
"willQos": "0",
"willRetain": null,
"willPayload": "",
"birthTopic": "",
"birthQos": "0",
"birthRetain": null,
"birthPayload": ""
},
{
"id": "2a0bf3da.86486c",
"type": "debug",
"z": "f6a99b89.ee40b8",
"name": "",
"active": true,
"console": "false",
"complete": "payload",
"x": 470,
"y": 180,
"wires": []
},
{
"id": "c96f99e5.7e0f68",
"type": "mqtt out",
"z": "f6a99b89.ee40b8",
"name": "Push to Mosquitto",
"topic": "",
"qos": "0",
"retain": "false",
"broker": "da27812f.a869e",
"x": 190,
"y": 260,
"wires": []
},
{
"id": "46a539c8.f9ef08",
"type": "debug",
"z": "f6a99b89.ee40b8",
"name": "",
"active": false,
"console": "false",
"complete": "payload",
"x": 390,
"y": 100,
"wires": []
},
{
"id": "92d0eee7.2a6508",
"type": "function",
"z": "f6a99b89.ee40b8",
"name": "Format payload for influxdb",
"func": "/* Parses Json and formats data to be send to influxdb via \n* telegraf mqtt plugin\n* topic = topic used for mqtt\n* measurement = measurement in influxdb\n* tagX = tag information in influxdb\n*/\n\nvar uuidMap = {\n 'XXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX':{\n topic:'/power/sml/Leistung',\n\t\tmeasurement:'power_sml',\n\t\ttags:{\n\t\t\ttag1:'Type=sml',\n\t\t\ttag2:'Location=Leistung'\n\t\t}\n },\n 'XXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX':{\n topic:'/power/sml/Bezug',\n\t\tmeasurement:'power_sml',\n\t\ttags:{\n\t\t\ttag1:'Type=sml',\n\t\t\ttag2:'Location=Bezug'\n\t\t\ttag3:'bla'\n\t\t}\n },\n 'XXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX':{\n topic:'/power/sml/Lieferung',\n\t\tmeasurement:'power_sml',\n\t\ttags:{\n\t\t}\n },\n 'XXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX':{\n topic:'/power/s0/pv',\n\t\tmeasurement:'power_s0',\n\t\ttags:{\n\t\t\ttag1:'',\n\t\t\ttag2:''\n\t\t}\n },\n 'XXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX':{\n topic:'/power/s0/og',\n\t\tmeasurement:'power_s0',\n\t\ttags:{\n\t\t\ttag1:'Type=s0',\n\t\t\ttag2:'Location=og'\n\t\t}\n },\n 'XXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX':{\n topic:'/power/s0/eg',\n\t\tmeasurement:'power_s0',\n\t\ttags:{\n\t\t\ttag1:'Type=s0',\n\t\t\ttag2:'Location=eg'\n\t\t}\n },\n 'XXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX':{\n topic:'/power/s0/ug',\n\t\tmeasurement:'power_s0',\n\t\ttags:{\n\t\t\ttag1:'Type=s0',\n\t\t\ttag2:'Location=ug'\n\t\t}\n }\n}; \n\n// Parse JSON \nvar myJsonObj = [];\nvar myJsonObj = JSON.parse(msg.payload);\n\n// Get value, timestam and UUID\nvar myUuid = myJsonObj.data.uuid;\nvar myTimestamp = myJsonObj.data.tuples[0][0];\nvar myValue = myJsonObj.data.tuples[0][1];\n\n// Dynamically read tags from uuidMap\nvar myTags =[];\nfor(var i in uuidMap[myUuid]['tags'])\n{\n\tif (uuidMap[myUuid]['tags'][i].length > 0) \n\t{\n\t\tmyTags.push(uuidMap[myUuid]['tags'][i]);\n\t}\n}\nvar myTagLine = myTags.join(\",\");\n\n// Create output payload\t\nvar myOutput = {};\nmyOutput.topic = uuidMap[myUuid]['topic'];\nmyLine1 = uuidMap[myUuid]['measurement'] + (myTagLine.length==0? '':','+ myTagLine);\nmyLine2 = \"value=\" + myValue + \" \" + (myTimestamp*1000000);\nmyOutput.payload = myLine1 + \" \" + myLine2;\nreturn myOutput;\n\t",
"outputs": 1,
"noerr": 6,
"x": 220,
"y": 180,
"wires": [["2a0bf3da.86486c",
"c96f99e5.7e0f68"]]
},
{
"id": "69806b97.448f4c",
"type": "websocket in",
"z": "f6a99b89.ee40b8",
"name": "Connect to VZ push-server",
"server": "",
"client": "8b5844a8.98ceb",
"x": 150,
"y": 100,
"wires": [["46a539c8.f9ef08",
"92d0eee7.2a6508"]]
}]
And this was adapted to, which works like described abouve.
[{ var uuidMap = {
'xxxxxxxx-1111-xxxx-xxxx-xxxxxxxxxxxx': {
topic: 'volkszaehler/Wasser/Verbrauch',
}
};
// Wasser xxxxxxxx-1111-xxxx-xxxx-xxxxxxxxxxxx
// Wasser Push-Server xxxxxxxx-2222-xxxx-xxxx-xxxxxxxxxxxx
// Parse JSON
var myJsonObj = [];
var myJsonObj = JSON.parse(msg.payload);
// Get UUID, timestamp and value
var myUuid = myJsonObj.data.uuid;
var myTimestamp = myJsonObj.data.tuples[0][0];
var myValue = myJsonObj.data.tuples[0][1];
if (uuidMap[myUuid] !== undefined) {
// Create output payload
var myOutput = {};
myOutput.topic = uuidMap[myUuid]['topic'];
myOutput.payload = "timestamp=" + (myTimestamp) + " value=" + myValue;
return myOutput;
}
}]
How can I build the Node-RED Flow to distribute the data of each UUID to the corresponding MQTT topics?
Thanks and best regards,
Chris