The ‘Email export’ analysis provided by Tago is a simple way to automate export of data from a sensor for a defined period of time. However, the export file only contains the value without a timestamp. This makes the data practically useless. Is there a way to modify the analysis so that it adds a timestamp for each value exported?
Hi,
If you wanna get the timestamp when the data are created you can change this party of code for this:
// Create csv header
let csv = “Fuel Level, Created At”;
// For each record in the fuel_list, add the value in the csv text.
// Use \n to break the line.
for (const item of fuel_list) {
csv = `${csv},\n${item.value}, ${item.time}`;
}
Lines 38-44.
Excellent, many thanks!
Hi guys,
In this analysis did you get two columns with different variable in the same csv file?
I got it but with both the two variables in the same column.
Hi,
Do you want each variable to generate a new CSV file, or keep the variable values in the same column within the CSV? I didn’t understand what you want to do.
I want two variables in the same CSV file, but in diferent columm. Like in the photo
I gotcha, you can do that, change lines 36 to 45 and put this code.
const items = await device.getData({ variable: [“fuel_level”, “fuel_temperature”], qty: 5 });
const result = items.reduce((acc, item) => {
if (item.variable === “fuel_level” || item.variable === “fuel_temperature”) {
if (!acc[item.variable]) {
acc[item.variable] = ;
}
acc[item.variable].push(item.value);
}
return acc;
}, {});
// Create csv header
let csv = “Fuel Level, Fuel Temperature, Created At”;
for (let i = 0; i < result.fuel_level.length; i++) {
csv += `\n${result.fuel_level[i]}, ${result.fuel_temperature[i]}, ${items[i].time}`;
}
Thank you Mateus, this worked! You helped me a lot!
And another doubt. I want use this code more funtional.
Do you know how to take the data from a period, like one month?
Because to day in this code you define the quantity you want of the last data.
Gabriel, please refer to the documentation for the available parameters that can be used with the “sendData” method. You can find them here:
https://js.sdk.tago.io/classes/Device.html#getData
Click on “DataQuery” to view the applicable parameters.
This is a great script! Is there a way to output the time stamp in a specific format? It’s unusable in it’s current form of “Mon Jan 29 2024 15:50:11 GMT+0000 (Coordinated Universal Time)”. How would one format the timestamp to something like MM-DD-YYYY HH:MM?
For more complex date formatting, you might want to use a library like date-fns or moment.js. These libraries provide a lot of utility functions for working with dates.
Here’s an example of how you can format a date using date-fns:
import { format } from 'date-fns';
const date = new Date();
const formattedDate = format(date, 'MM-dd-yyyy HH:mm');
console.log(formattedDate); // Outputs date in the format "MM-dd-yyyy HH:mm"
And here’s an example using moment.js:
import moment from 'moment';
const date = moment();
const formattedDate = date.format('MM-DD-YYYY HH:mm');
console.log(formattedDate); // Outputs date in the format "MM-DD-YYYY HH:mm"
Remember to install these libraries using npm before using them.

