i use a mqtt node to receive multiple topics (pv-inverter/#) from an mqqt brocker (sensor data from microconverter).
pv-inverter/ (14 topics)
pv-inverter/HM800/ (6 Topics)
pv-inverter/HM800/ch1 (7 Topics)
pv-inverter/HM800/ch2 (7 Topics)
pv-inverter/HM800/ch0 (21 Topics)
pv-inverter/HM800/alarm (5 Topics)
pv-inverter/status (1 Topics)
pv-inverter/status/total (4 Topics)
Now i want to write all sensor data with the same timestamp from the different messages into one influxdb mesurement with one field for every single sensor data/mqtt message.
Example - this three messages comes at the same time.
To inject this sensor data to influxdb i want to use a single msg
what is the right/shortest way?
Welcome to the forum @speerwerfer
Why do you want to do that? Why not just write them to separate measurements as you receive them?
Thank you for the welcome
My idea is, all sensor data reported at the same time by a device should also have the same timestamp in the influxdb.
If I have for example a weather station, then I thought so far that one collects these sensor data all in a mesurment.
The Bucket is Haussensorik (House sensors)
The Mesurement is for example Wetterstation (weatherstation)
Does the inverter publish them to MQTT directly?
in principle, yes. there is a receiving unit that receives the data from the microinverter via a proprietary protocol and passes it on to the brocker via mqtt. Unfortunately I can not modify the structure of the topics.
You can use a Join node in key/value mode to get them all into one message. Then you can reorganise the contents for influxdb.
See this article in the cookbook for an example of how to join messages into one object.
Is there an easy way to shorten the names of the elements? Here is the complete mqtt topic now.
Or should I change the names via a function node?
See the Result:
"pv-inverter/HM800/ 14190547764/name": "HM800",
"pv-inverter/HM-800/114190547764/device/fwbuilddatetime": "2021-11-01 08:42:00",
If you want the fields to be name, bootloaderversion, etc then in a function node you can iterate the keys using
Object.keys(obj).forEach() as described in Iterating Through an Object with `forEach()` - Mastering JS, then for each one use split("/") on the property name and take the last element of the array as the new key name. Build a new object with the new field names and then replace the payload with the new one.
Should not several of those be tags rather than fields in influx? Also make sure you change the ones that are numbers from Strings to Numbers, though I am surprised that the MQTT node does not do that for you if you tell it to auto-detect the type.
Once again, thank you!
Yes, I still need to think about the database/table design.
Yes, if I use auto-detect, then only strings are generated. If I set "a parsed JSON Object", then the numbers are recognized. With the strings there are then errors. I can process them with Catch
I don't understand what you mean by that.
sorry, english is not my first language
When i use in the mqtt node "output: auto-detect" then all messages contain the data type string.
When i use "output: a parsed JSON Object" then I receive only the messages that contain a number. Messages containing a string are not forwarded. The messages with a string as payload cause an error. I can then catch this error with a "Catch Node" and convert it to a correct message with a string as payload.
This is the Output from the "Catch Node"
"payload": "2021-11-01 08:42:00",
"message": "Failed to parse JSON string",
"type": "mqtt in",
This is probably the option you want
If you have not got that option then perhaps you are on an old version of node-red.
This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.