I get a json response which looks like: 2018-06-16T13:12:11+00:00
I other posts I have seen that I could separate each part of it (hour, minute, etc), but I assume that there is a better way like parsing strings to Number items!?
If so, I would appreciate an example how to do it.
Thanks
I actualyy get the JSON response on my request which looks like:
val String time = transform(“JSONPATH”, “$.trips[0].tripDetails[0].startTime”, json)
I would like to parse this to a DateTime item like
CarTripStart.postUpdate(time)
That looks like a standard ISO 8601 date time string.
I think there is a constructor on Joda DateTime that will take the string and parse it for you. I know you can send this string unchanged as an update to a DateTime Item as is. I also suspect there is a constructor on DateTimeType that will take this string and parse it for you.
you should be able to simply do the following in your rule:
val String time = transform(“JSONPATH”, “$.trips[0].tripDetails[0].startTime”, json) // Your original rule code
var DateTime JDT_time = parse(time.toString) // Parse JDT object from timestring
var DateTimeType DTT_time = new DateTimeType(JDT_time.toString) // Convert JodaDateTime to DateTimeType
CarTripStart.postUpdate(DTT_time) // This should now work without ".toString"
Please let me know if this works! I’m new to this, but want to start helping out. If that doesn’t work you may need to specify the format using parse(string, DateTimeFormatter formatter), which I’ve done but would have to brush up on.
Thanks, Phil.
I thought I did it by the following (simplest) approach, but I just get the following DateTime result: 2018-06-16 00:00:00.000 (so no time, just date)
val String time = transform("JSONPATH", "$.trips[0].tripDetails[0].startTime", json)
CarTripStart.postUpdate(time) // whereas CarTripStart is DateTime type
In the log it looks good, though: time: >2018-06-19T17:25:13+00:00<
However, I will try your suggestion and let you know.
Thanks for your help!
OK, so unfortunately you cannot send a Joda DateTime Object as a command to a DateTime Item. It must be either a DateTimeType Object or a String. So try:
After reading Rich’s last post I starting thinking I had forgot a line and I did! I added it above in my original post, but you need to convert JodaDateTime to DateTimeType:
var DateTimeType DTT_time = new DateTimeType(JDT_time.toString) // Convert JodaDateTime to DateTimeType
The code in my original post should work, but please check the variables, etc. It is very frustrating working with time types in these rules, I will admit.
2018-06-27 21:02:06.929 [ERROR] [g.eclipse.smarthome.core.persistence] - [org.eclipse.smarthome.core.persistence.internal.PersistenceManagerImpl(75)] The addPersistenceService method has thrown an exception
java.lang.ClassCastException: java.lang.String cannot be cast to java.sql.Timestamp
I’m not totally sure but I wonder if those issues aren’t caused by another rule somewhere. I actually went through quite a bit of effort to figure out the appropriate syntax to avoid the “The method getCalendar() from the type DateTimeType is deprecated” Error. And, my rule example never uses the getCalendar() method. I am running OpenHAB2.3.0 Release Build, and I just tested the following and it works with no errors. Since I do not know what binding, etc. is providing your JSON string time response, I just make a static val equal to your original time string from the top post in this thread, and commented out the line of code that gets your string from a JSON response. The following should work exactly as written:
Here are the applicable items in an items file:
// Testing Group and Variables
Group Test "Testing/Debug"
Switch TestSwitch "Test Switch [%s]" (Test)
DateTime TestTime "Test Time [%1$tH:%1$tM:%1$tS, %1$tm/%1$td/%1$tY]" <clock> (Test)
Here is the applicable rule in the rules file:
// ------------------- RULE -------------------
rule "Test Switch Changed"
when
Item TestSwitch received command
then
logInfo("TEST","RULE FIRED: Test Switch Changed")
// --- Test code for DateTime Parse functionality ---
if (TestSwitch.state == OFF) {TestTime.postUpdate(NULL)}
else {
val String time = "2018-06-16T13:12:11+00:00" // Original post time string
//val String time = transform(“JSONPATH”, “$.trips[0].tripDetails[0].startTime”, json) // Your original rule code
var DateTime JDT_time = parse(time.toString) // Parse JDT object from timestring
var DateTimeType DTT_time = new DateTimeType(JDT_time.toString) // Convert JodaDateTime to DateTimeType
TestTime.postUpdate(DTT_time) // CarTripStart item changed to TestTime for the purpose of this test
}
// ----------------------------------------
end
Then, add the TestSwitch to sitemap. I have a “Test” group and “TestSwitch” item permanently, as well as the “TestSwitch” on my sitemap and a “test.rules” file so that I can always test things quickly and easily, without possibly damaging another set of code.
When you activate the TestSwitch on the sitemap (to ON) you should see the following (turning it off returns the timestamp to NULL:
18:28:56.803 [INFO ] [smarthome.event.ItemStateChangedEvent] - TestTime changed from NULL to 2018-06-16T13:12:11.000+0000
Let us know if this works. If it does, try to apply this code as closely as possible to your specific case.
I really appreciate your endurance in supporting me in my case.
Actually I have a permanent Debug_Trigger switch for such kind of investigations.
I will try your code and let you know.
Maybe you are right, that it comes from another rule.
By the way, the json response comes from the volvo on call python stuff