I'm a bit of a novice but have made progress on my Node-Red skills but I have one last hurdle I can't quite figure out. I have an IOT node that takes temperature readings every X amount of time, stores it and then after Y amount of time sends all the data as a Hex string to my node-red (current reading + 8 historical readings). I have a Javascript that parses the hex string and puts the values into an array. Each data set is an array as well (is that right?) An array of arrays? The data set is Hive, Temp & Time, repeated. I need to write each data set within the array to my Influx DB. (Hive is the tag)
Here is what the output of the Javascript function is.
I have the Influx database out node, which I'm using on other flows successfully.
Would someone be kinda enough to nudge me in the write direction?
On another flow, that only has a single reading, I use a Change node that looks like this.
Why don't you send each value as it is received, rather than saving them up?
You need to make an array of arrays, where each element is as you have shown for your single reading, with the addition of a time field which must contain the time value in (I think) the units specified for Time Precision (default is millisecs). So for the example you show it should be an array of 7 objects, where each element is an array of 2 objects, the first being the fields and the second being the tags. So each of the 7 elements will be something like
The example you show has the time as a string, it must be a number, and it looks like nanosecs, whereas I am pretty sure it should be msec as that is how you have configured the precision. I am not certain about that though as it may also be down to the database settings. If the times come out all wrong try nanosecs. Oddly the other example you show
1703981797 42.7 71.06 f866207058384979
appears to be in seconds.
Why don't you send each value as it is received, rather than saving them up?
The device is a cellular device and as a battery saving mechanism, it wakes up long enough to take a temperature reading, store the data, go back into sleep mode. Then every so often it does a batch upload of the data. This allows me to take frequent temperature readings without killing the battery to make a cellular connection.
So for the example you show it should be an array of 7 objects, where each element is an array of 2 objects, the first being the fields and the second being the tags.
I think that is where I am struggling, is how to create an array with two objects. Is there any sample javascript code that would demonstrate that?
Here is a snippet of code that is creating the array:
var imei = 12345678910
var array=[]
var bytes="0x"
for (var i=1;i<data1.length;i++){ //Loop through array to get data
var temperature=((parseInt(bytes+data1[i].substring(4,8))/10)*9/5)+32 // 4,5,6,7
var time1=parseInt(bytes+data1[i].substring(16,)) // 16 until end
var obj={
'hive':imei,
'TempLower':temperature,
'time':time1+'000000000'
}
array[i]=obj
}
msg.payload=[array]
am pretty sure it should be msec as that is how you have configured the precision.
Yea, I'm adding 0's so that InfluxDB doesn't complain. I probably didn't configure the InfluxDB correctly. It works for now. If I can get the data written, I can see about fixing time precision.
Thanks Colin. Your guidance got me in the right direction and it is working now. After creating the array of array with 2 objects and then feeding that into a SPLIT function, it is working!!