How do I use persistence.persit in javascript?

I’m trying to write data into the jdbc / sqlite persistence in a javascript rule but it doesn’t seem to work correctly.
This is what I try:

var ZDT = (ZDT === undefined) ? Java.type("java.time.ZonedDateTime") : ZDT;
test = items.getItem('test');

today = ZDT.now();
test.sendCommand(-16)
test.persistence.persist(time.toZDT('2025-01-01T12:00'), '-10', 'jdbc')
test.persistence.persist(today, '-15', 'jdbc')

And this is what I get in the sqlite database:

sqlite> SELECT time, value from  test_0173;
┌─────────────────────────┬───────┐
│          time           │ value │
├─────────────────────────┼───────┤
│ 2025-01-11 18:23:54.490 │ -16.0 │
│ 1735729200000           │ -10.0 │
│ 1736616614191           │ -15.0 │
└─────────────────────────┴───────┘

The first line from openhab has a correct datetime but the two others don’t.
How can I set the datetime correctly?

You are mixing and matching java.time.ZonedDateTime and the JS native joda-js ZonedsDateTime.

You should stick to the JS native. When interacting with the API provided by openhab-js a JS ZDT is always expected.

test = items.getItem('test');

test.sendCommand(-16)
test.persistence.persist(time.toZDT('2025-01-01T12:00'), '-10', 'jdbc')
test.persistence.persist(time.toZDT(), '-15', 'jdbc')

The other two are stored as epoch I think which is a correct date time, It’s just not formatted nicely. The last line represents January 11, 2025 5:30:14.191 PM GMT and the middle line represents Wednesday, January 1, 2025 11:00:00 AM GMT.

I don’t know why it’s different between the built in persistence and using the .persist command but all the entries have a correct timestamp, just in two different formats.

Unfortunately even if they are valid timestamps, openhab can’t properly deal with them. I run this script for a new item (which is configured for jdbc persistence):

test = items.getItem('testPersistence');

test.persistence.persist(time.toZDT().minusMinutes(2), '1', 'jdbc')
test.persistence.persist(time.toZDT().minusMinutes(1), '2', 'jdbc')
test.persistence.persist(time.toZDT(), '3', 'jdbc')
test.sendCommand(4)

Then I tried to make a chart with the analyzer:



So the timestamp seems to be interpreted as a very old value.

I experience the same problem @chaoshome, did you find a solution to it?

Unfortunately no.

Hi @chaoshome ,

I’ve made a fix and PR for this problem.
Here is a link to a jar file I build for OH 4.2 and 4.3

And in the PR there are instructions how to correct your data in the SQLite, so you can see it in the HABPanel’s graphics.

Best wishes, Zhivka.