Update DateTime item from String with jython

Ok, i quit at the moment. I started to write rules with jython and i am learning a lot at the moment, but unfortunately i found a problem that i could not solve on my own.

Problem: I hav a date and time value as a string and i like to update a DateTime item with it. I try to use the postUpdate function from core.util, and tried different ways to transform the string into a Datetime value that could be used to update my item.

Please anybody help

What is the exact format you are trying to post to the DateTime Item? It should accept anything that is in ISO8601 format which usually looks like YYYY-MM-DDTHH:mm:SS.sss where:

  • YYYY is the 4 digit year
  • MM is the number of the month
  • DD is the day of the month
  • T is a divider and always there, it’s literally T
  • HH is the hour of the day
  • mm is the minute of the hour
  • SS is the second of the minute
  • sss is the milliseconds of the second

I think that sss is not required and I’m pretty certain the TZ information is not required also. But you can look up ISO 8601 and see how to format that if needed.

Assuming you have to parse and manipulate the original String to get into that format in the first place, you might be able to use ZonedDateTime and use the of method to build a ZonedDateTime Object that you can then call toString on to get ISO 8601 formatting. You could also use the with methods. Use ZonedDateTime to get now and modify the various fields using the with methods (e.g. withMonthOfYear(4)) to build up the Object and then call toString.

NOTE: Joda DateTime is being removed from OH in OH 3 so use ZonedDateTime instead of DateTime for stuff like this.


I suggest not using this function, which just wraps events.postUpdate. It was added when the lucid libraries were migrated back into the main helper libraries to make it easier for people to migrate their scripts that were using the lucid libraries. Just use the example here… https://openhab-scripters.github.io/openhab-helper-libraries/Guides/But%20How%20Do%20I.html#send-an-update-to-an-item.

events.postUpdate("Your_DateTimeItem_Name", your_date_time_value)

If you log out the value of a DateTimeItem’s state…


… you’ll see one of the accepted formats (2020-05-05T05:55:55.000-0400). You can find all of the accepted formats here. For example…

events.postUpdate("Your_DateTimeItem_Name", "2020-05-05T05:55:55")

org.joda.time.DateTime has been removed in OH 3.0. You could set it up as a third party library to use with Jython, but IMO it is best to use ZonedDateTime. Rather than importing java.time.ZoneDateTime, you can use the DateTimeType constructor and easily convert to ZonedDateTime. Then use the builder methods to build a ZonedDateTime programmatically. Use toString() at the end to convert to a string.

events.postUpdate("Your_DateTimeItem_Name", DateTimeType().zonedDateTime.withYear(2020).withMonth(5).withDayOfMonth(5).withHour(17).withMinute(55).withSecond(55).withNano(0).toString())

Too much trees to see the wood :slight_smile: I fixed it finally. The problem was the wrong format of the string and my to complicated mindset.

The solution was to exchange a Space with a ‘T’ in the date/time string and the use the events.postUpdate function

@rlkoshak Thanks for the note about JodaTime . Unfortunately i see not much Information about the upcomming version 3.0

@5iver Thanks for the hint with the postUpdate and the note about JodaTime