I am very new to node-red. I am building an IOT-Project for collecting temperatures. I have a JSON format Payload, which i want to sort according to Hostname and its given values before i pass it to Influxdb.
{
Time: "14:13:34"
Temperature: "23.04"
Humidity: "50.62"
hostname: "host1"
}
I dont know how to sort the data. I would appreciate your help.
Thanks : )
What do you mean by sort it according to hostname. Have you got multiple values with different hostnames that you want to sort, or do you mean that you want a different order within the object, something like
Hey,
Sorry for the confusion.
No,I meant that i have got other IOT Devices with different hostname.
The other Host also sends the same data i wanted to arrange the data according to its hosts before it is sent to the Influxdb.
Thanks : )
Why?
You should have the hostname as a tag in the measurement, with temperature and humidity as fields. Assuming that the time is the current time then let influx put that in to the db for you.
If the example data you showed us is typical - ie a message contains the latest measurements for just one host, then you need to be aware that a Node-red function, indeed almost all nodes, deals with a single message as it arrives. It has no memory of the previous messages.
This seems counterintuitive when you are new to flow based programming, but you need to understand it.
So if you wanted to "save up" the messages until you had a batch of them, then sort them and send them to Influx, you would need some mechanism to store them. It could be a join mode to collect the latest readings from 2, 3, 5, ... n hostnames, or context storage in the function node.
But look, you have a database! The perfect place to save data!
Send each message to the database as soon as it arrives.
You can sort and filter them however you wish as you extract and use them.