I would like to request a state from my mariaDB at a certain time. In this case CarTripStart is a DateTime item:
val Number distDNAStart = CarDistDNA.historicState(CarTripStart.state)
but I get:
2018-07-06 12:05:56.167 [ERROR] [ntime.internal.engine.ExecuteRuleJob] - Error during the execution of rule 'Received car trip data': An error occurred during the script execution: Could not invoke method: org.eclipse.smarthome.model.persistence.extensions.PersistenceExtensions.historicState(org.eclipse.smarthome.core.items.Item,org.joda.time.base.AbstractInstant) on instance: null
But neither CarDistDNA is null nor CarTripStart is null
Does not work
2018-07-07 08:21:23.829 [ERROR] [ntime.internal.engine.ExecuteRuleJob] - Error during the execution of rule ‘Received car trip data’: ‘toDateTime’ is not a member of ‘org.eclipse.smarthome.core.library.types.DateTimeType’; line 67, column 54, length 31
Joda DateTime is the type of object that is required by calls to get historic states and when creating a Timer. The implicit variable now is a Joda DateTime.
A DateTime Item carries it’s state as a DateTimeType. DateTimeType is not a Joda DateTime object and therefore cannot be used in places that require a Joda DateTime. So you must first convert the DateTimeType to a Joda DateTime. There are lots of ways to do this but I find the easiest is to use the toString from the DateTimeType to create a new DateTime, as illustrated above.
Got it.
Thanks for the explanation.
I use now quite a bit like now.isAfter(now.withTimeAtStartOfDay.plusHours(8))
or if(!System_started.changedSince(now.minusMinutes(45),"jdbc")
I always wondered what this “AbstractInstant” e.g. in the persistence documentation supposed to mean
Now I have a much better clue
Unfortunately this does not work: 2018-07-10 18:27:13.243 [ERROR] [ntime.internal.engine.ExecuteRuleJob] - Error during the execution of rule 'Received car trip data': 'state' is not a member of 'org.openhab.core.persistence.internal.QueryablePersistenceServiceDelegate$1'; line 73, column 48, length 18
Thanks - I will try this today.
I finally have time to catch up with this thread again.
Damn travelling
So to summarize:To get the historic state from my DB I need an AbstractInstant (JodaTime type).
Your suggestion to get this JodaTime is:
val dt_carStartTime = new DateTime(CarTripStart.state.toString)
Question (to better understand):
In another thread I got help to convert a string to a “real” DateTimeItem:
// get string
val String time = transform("JSONPATH", "$.trips[0].tripDetails[0].startTime", json)
// get JodaTime from String
var DateTime JDT_time = parse(time.toString)
// convert Joda to DateTimeType
var DateTimeType DTT_time = new DateTimeType(JDT_time.toString)
So, DateTime in rules is always Joda and DateTimeType is always “real” DateTime?
If that’s true it’s really confusing for me is that DateTime in rules is not the same format like DateTime item and DateTime items are DateTimeType in rules.
There is no “real” date time time except epoch. Epoch if the number of milliseconds since 1970-01-01T00:00:00Z.
Joda DateTime is a class that provides a lot of useful helper methods for comparing then together, getting parts of the date (e.g. day off the month), etc.
All OH Items carry a State. Joda DateTime is not a State. So OH has the DateTimeType which is a State but it is not a Joda DateTime. And DateTimeType doesn’t have all the nice comparison and date manipulation helper methods on it. So I’m order to get the nice helper methods or to use a DateTimeType to schedule Timers, it must first be converted to a Joda DateTime.
In your example code, I might be possible to just use:
var DateTimeType DTT_time = new DateTimeType(time)
Or just
var DateTime JDT_time = new DateTime(time)
Assuming that the time String is in standard ISO 8601 (I think that’s the number) format.