I’ve been successfully importing long term statistics from energy sockets that I’m about to ‘retire’ into a newly created template sensor that I’ve created beforehand.
The new template sensor records the energy consumption of a new energy socket, but I wanted to retain a long term historical overview as well for what’s essentially a metric of the same device.
So being able to export the data for the old energy socket and then importing it into the new template sensor was exactly what I was looking for.
As far as the time zones are concerned, I decided to take the UTC values that the exported data had and turn those into new timestamps
Here is the first row of the exported CSV data in its original format:
entity_id,state,last_changed
sensor.sensor_name,5578.458,2023-12-31T23:00:00.000Z
I uploaded it into Google Drive, which turns it into a spreadsheet with three columns.
Due to the fact that I’m in UTC+1, that datestamp 2023-12-31T23:00:00.000Z is actually the very first hour of 2024.
I added 2 new columns between columns A and B for ‘unit’ and ‘start’. Next I added another new column between D and D, for ‘sum’. That also means the timestamp column ends up in column F.
In column F, replace the “T” with a space in all timestamps using Search & Replace, and also replace the Z with a “”. The timestamp now looks like “2023-12-31 23:00:00.000”
Now change the headings to
statistic_id unit start state sum last_changed
In column A (statistic_id), I did a search & replace to show the name of the new template sensor.
In column B (unit) I pasted the value “kWh” in all rows because this happens to be an energy sensor. Adjust accordingly for other data types.
In cell C2 (for start) I pasted this formula in all rows:
=TEXT(DATEVALUE(F2)+TIMEVALUE(F2)+(0.04166666666),"DD.MM.YYYY HH:MM")
This adds 1 hour to the value of the timestamp in column F and then formats it as the integration expects it. I then copied and pasted the formula to all rows below.
In E2 (for sum) I simply entered =D2 and then copied that to all the rows underneath.
Column F can be left in place, the integration will simply ignore that.
Now it looks like:
statistic_id unit start state sum last_changed
sensor.template_sensor_name kWh 01.01.2024 00:00 5578.458 5578.458 2023-12-31 23:00:00
Next I use the file → download → as CSV to export the spreadsheet to a csv file that can be used in the integration.
I’ve got the Google Sheet specified as English (UK) so the column separator turns out as a , (comma) and the decimal separator as a . (period).
Hopefully this is useful for others who are trying to do something similar.