#And while i have a listening ear on this

1 messages ยท Page 1 of 1 (latest)

uncut solstice
#

It is possible, I did it too. But... there is no standard way to do it and it is hard. Since you say you're a dba, I assume you know your way around in sql. If you don't, you should really not even try this, as you will very likely corrupt your new HA installation....
If you still want to go ahead, I suggest you install the Terminal & SSH Add-on first, that will give you a terminal window from which you can run sqlite3 to execute your statements.

elfin galleon
#

at your own risk*

torn blade
#

๐Ÿ™‚ after almost 3 decades working with SQL / data, i think i have a fairt chance ๐Ÿ™‚

uncut solstice
#

In the configuration page for the Terminal & SSH add-on add sqlite to the Packages list so that you do not have to install the sqlite package every time.

torn blade
#

SQLite web looks like it uses standard T-SQL 92 compatible code, nice !

uncut solstice
#

It's over a year ago for me now, but I do have some of my notes and scripts I used still lying around. You can have those to get you started, but I will not be able to assist you a lot.

torn blade
#

i would really appreciate any and all docs you can share

elfin galleon
#

dont forget to create backups before modifying things ^^
(also keep in mind that you can use more powerful software to edit the db as you can copy it on and off system)

torn blade
#

Half the fun is tinekering around and figuring out stuff

uncut solstice
#

What I did was to attach a 2nd database in my domoticz machine, then select and insert all data I wanted to transfer into that transfer database. Copy the transfer database over into the home assistant machine, and from there do the inverse, copy the data from the transfer db into the HA database.

torn blade
#

You did not export the data in CSV format ?

elfin galleon
#

oh another thing, make sure ha doesnt actually run while modifying / changing out the db

uncut solstice
#

Correct, I did not. I used a sqlite db to transfer the data. This 1st script contains the statements I used in the domoticz instance. As I said, use them as an example. DO NOT RUN THEM blindly, and do NOT RUN THE ENITRE SCRIPT IN ONE GO...

uncut solstice
#

Making proper backups is very sound advice though ๐Ÿ˜‰

torn blade
#

Making backups is THE advice if you ask a DBA ๐Ÿ˜‰

elfin galleon
#

sqlite doesnt really work like a dedicated sql server. its simply a file thats being read and written to with possibly some in memory caching.
adding a second process to access it may be enough of a disturbance to kill it. at best ha notices and dumps it as corrupt and you can simply copy it back and its fine. at worst you may leave the file in a corrupt enough state for the data to be lost

torn blade
#

Ahh... your just mounting a temp 2nd DB in HA for the transfer of the data

#

Nice.... looks like there is even code to correction of the datetime datatype

uncut solstice
#

That 1st script contains statements I used in the domoticz machine to create the transfer database. You can try that multiple times.

torn blade
#

Check !

torn blade
elfin galleon
#

moving stuff around like that will be fine, id just turn off the ha core container for a moment while you do the moving

torn blade
#

Use the domotics sqllite enginge to generate insert scripts you can run by using sqlite web on HA

torn blade
#

HA assumes its the only process working in the DB

elfin galleon
#

pretty much yes

uncut solstice
#

What I'm doing is to insert the data from DZ before the data set already created in HA. Since HA stores the energy data in a cumulative format (Long Term Statistics or LTS), there will be a point between those two sets where the sequence is broken. I did not try to adjust this break myself. Instead I used the developer tool 'STATISTICS' to find the break and adjust only the 1st data point of the HA data to restore the sequence. You'll see what I mean once you've used the STATISTICS tool once.

torn blade
#

so, its not just raw data that gets compressed to large timeslots when it gets older ?

uncut solstice
#

Correct, HA doesn't play very nice when you run long running statements, but it still applies basic locking mechanisms, so as long as you keep the batches small, and possibly avoid running larger DDL around the :00 times, you'll have no issues at all.

elfin galleon
#

ha stores both the change and an absolute value in the history

#

depending on what is being displayed that helps to keep processing times down

uncut solstice
#

Indeed. statistics.state has the absolute value at the time of measurement, statistics.sum has a running total of the change between this and the last row.

elfin galleon
#

im not quite sure how the technical side of the modify statistic works in regards to either field, but it effectively adds a static offset to all of the following sets of data

#

the adjustment will be dropped once it hits the long term side of things if i recall, not sure on that anymore

uncut solstice
#

But only for those rows where the statistics_meta table has a has_sum = 1.

torn blade
uncut solstice
#

The Enegry dashboard (and other things using the LTS) only look at the statistics.sum column

torn blade
#

so rows with has_sum = 1, describe a datetime range, and has_sum = 0 is absolute data?

uncut solstice
#

Almost. has_sum = 1 means state and sum columns are filled; these are created from entities that have a state_class = total_increasing or state_class = total attribute.
entities that have a state_class = measurement will result in a statistic_meta row that has has_mean = 1. The rows in statistics for these statistics will get their min, max and mean columns filled.
As every entity can have only 1 attribute called state_class, the has_sum = 1 and has_mean = 1 are mutually exclusive. i.e. rows in statistics will either get their state + sum columns populated or their min, max and mean columns.

torn blade
#

hmmm... i think i'm beginning to understand he <says optimistically>

uncut solstice
torn blade
#

it takes some time to understand a new datamodel.

#

ow? its that simple to attach .db files to sqlite ?

uncut solstice
#

The HA database model isn't overly complicated. It's just that it is not very well normalized and not all data is available in the database sadly. It's been designed apparently mostly by developers, not a dba ๐Ÿ˜‰

elfin galleon
#

the file is effectively the database. attach is just the command to open a "connection" to it (read the file)

torn blade
uncut solstice
#

are you familiar with sqlite or do you use another sql dialect normally?

torn blade
#

its been far too long ago that i've played around with stuff like this ๐Ÿ™‚

I thank you guys for giving me a flying start, truly much apprecianted!

uncut solstice
#

i.e. do you need hints on how to read and write dates/datetimes?

torn blade
#

nah, i'm famliar with unix epoch to UTC conversions in my own projects

uncut solstice
#

excellent. I normally work in T-SQL (MS SQL), so this was another big hurdle for me ๐Ÿ™‚

torn blade
#

Same here, MSSQL DBA since version 6.5

uncut solstice
#

same

torn blade
#

wow... just read that HA even has support for Jupyter

uncut solstice
#

I'm looking to see if I can find some more queries I used, because that last one falls a bit short...

torn blade
#

i will report back if i can make it work, andwe can exchange notes ! ๐Ÿ™‚

#

Queries are pretty fast, considering it's only running on a RPI 5

uncut solstice
#

One side note: HA keeps one row in the LTS for each hour. Domoticz however has one row per day. i.e. you best insert that at 00:00 each day, so that the energy dashboard has at least one row for each day. It will look at bit strange when you look on a per day view, but that's all the data we have, so nothing to do about that. I made an error somewhere when inserting the data, probably due to Daylight saving time, that makes mine show up as 02:00, but this still works.. ๐Ÿ™‚

#

For example

torn blade
#

ey, thats a good tip....

#

And the reason why i always use UTC (because i have data coming in from all timezones)

uncut solstice
#

Right, I love UTC too. But sadly I don't always get to have a say in this...

torn blade
#

well... sometimes you must make space for dev's to fubar, and then swoop in to save the day.

#

๐Ÿ˜‰

#

again, thanks for the tips.

I'm off to bed, need to work @ 10 tomorrow.

uncut solstice
#

good night

torn blade