-
-
Notifications
You must be signed in to change notification settings - Fork 3.7k
[jdbc] SQLite: Fix format when persisting item with timestamp #18407
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Signed-off-by: Zhivka Dimova <zhivka.dimova@myforest.net>
This pull request has been mentioned on openHAB Community. There might be relevant details there: https://community.openhab.org/t/how-do-i-use-persistence-persit-in-javascript/161623/6 |
@lovery - thank you for the fix, and sorry for the long wait! I'm trying to understand this issue. I've checked this and my understanding now is that we're actually storing a formatted string (please correct me if I'm wrong). So to understand the difference in behavior, we need to look at this: Line 69 in 3b64968
vs. Lines 134 to 135 in 3b64968
So as far as I can see, your fix will do the same formatting on Java side as done on SQL side for the I'm wondering though if it would be safest to use the same |
Signed-off-by: Zhivka Dimova <zhivka.dimova@myforest.net>
Signed-off-by: Zhivka Dimova <zhivka.dimova@myforest.net>
Hi @jlaur, you've understood is correctly, a formatted string is stored. I re-wrote the code and used the |
Perhaps that's because it expects number of seconds rather than milliseconds?
Maybe you could use strftime('%Y-%m-%d %H:%M:%f', ?, 'unixepoch', 'localtime') Do you think that would work? |
...b.persistence.jdbc/src/main/java/org/openhab/persistence/jdbc/internal/db/JdbcSqliteDAO.java
Outdated
Show resolved
Hide resolved
Signed-off-by: Zhivka Dimova <zhivka.dimova@myforest.net>
@jlaur Changed to use |
...b.persistence.jdbc/src/main/java/org/openhab/persistence/jdbc/internal/db/JdbcSqliteDAO.java
Outdated
Show resolved
Hide resolved
Signed-off-by: Zhivka Dimova <zhivka.dimova@myforest.net>
Signed-off-by: Zhivka Dimova <zhivka.dimova@myforest.net>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changed to use getEpochSecond(), and also I added a method for getting the milliseconds, instead of using the substr.
Excellent, thanks for the simplification! We are almost there, just requesting one correction to comply with naming conventions (also reported by SAT). Please confirm that the new version is fully tested, and we are ready to merge. Thanks for your efforts!
...b.persistence.jdbc/src/main/java/org/openhab/persistence/jdbc/internal/db/JdbcSqliteDAO.java
Outdated
Show resolved
Hide resolved
Signed-off-by: Zhivka Dimova <zhivka.dimova@myforest.net>
@jlaur Fully tested the change, everything is good with it. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
…n item (openhab#18407) Signed-off-by: Zhivka Dimova <zhivka.dimova@myforest.net>
Description
Change the date and time format for
time
value which is inserted whenpersist
method of an item is called.Bellow is an example of a JSRule for testing the issue.
The default
time
format is'%Y-%m-%d %H:%M:%f'
- check it here.All other formats aren't supported by the HabPanel's graphics - the values don't appear on the graphic if the time is not in the upper format.
I tried one other solution, but since the format isn't specified anywhere, I didn't like it, because it could lead to the same problem as before:
Here is a link to a thread in OH community forum
Testing
Automatic built will be under the following folder:
https://openhab.jfrog.io/ui/native/libs-pullrequest-local/org/openhab/addons/bundles/
Here is also a link to a build that I made for OH 4.2 and 4.3
Correct existing data in SQL
In case someone wants to update their data in the SQLite, I am providing guidelines how to do that:
Create a copy of your database or at least of the tables you are going to edit
First you have to find the table in which the item is persisted in the SQLite with:
SELECT ItemId, ItemName FROM items WHERE ItemName = 'XXXXXXXX';
Check that you are getting the right id connected to your item.
Check that you have mixed time values:
SELECT time, value FROM itemXXXX;
Keep in mind that you may have to add leading zeros to the id you get, I had
ItemId
- 256 and I had to add one leading 0, your case could be different.All my values started with
17
, so I used this for in thewhere
clause of myupdate
statement like:UPDATE itemXXXX SET time = datetime(CAST(substr(time, 0, 11) as REAL), 'unixepoch', 'localtime') || '.' || substr(time, 11, 3) WHERE time LIKE '17%';
Fixes #18101