DSL Rule DateTime formatting?

I have the output of an API in json in which dates are associated to prices. I am parsing the dates but trying to format them, but I get the following error:
An error occurred during the script execution: Could not invoke method: org.openhab.core.library.types.DateTimeType.format(java.lang.String) on instance: 2022-12-27T00:00:00.000+01:00

My code is the following:

rule "Testing date conversion"
when
    Time cron "0 30 15 1/1 * ? *"
then
	var String response_json = ""
	var int hour
	var DateTimeType startsAt
	var Number total
	
	response_json = pricesfortoday.state.toString()
	logInfo("TestDateConvert", "response_json = {} from API, length {}", response_json, response_json.length())

	for (hour=0; hour < 24; hour++) {
		logInfo("TestDateConvert", "hour = {}", hour)
		startsAt = transform("JSONPATH", "$[" + hour + "].startsAt", response_json)
		total = transform("JSONPATH", "$[" + hour + "].total", response_json)

		logInfo("TestDateConvert", "startsAt = {}", startsAt)
		logInfo("TestDateConvert", "total = {}", total)
		
		val String MyStringFromDateTimeType = startsAt.format("%1$td.%1$tm.%1$ty %1$tH:%1$tM")
		logInfo("TestDateConvert", "MyStringFromDateTimeType = {}", MyStringFromDateTimeType)
	}
end

The full log results:

2022-12-26 23:52:35.588 [INFO ] [ab.core.model.script.TestDateConvert] - response_json = [{"startsAt":"2022-12-27T00:00:00.000+01:00","total":0.1213},{"startsAt":"2022-12-27T01:00:00.000+01:00","total":0.0906},{"startsAt":"2022-12-27T02:00:00.000+01:00","total":0.0859},{"startsAt":"2022-12-27T03:00:00.000+01:00","total":0.0842},{"startsAt":"2022-12-27T04:00:00.000+01:00","total":0.1115},{"startsAt":"2022-12-27T05:00:00.000+01:00","total":0.13},{"startsAt":"2022-12-27T06:00:00.000+01:00","total":0.2063},{"startsAt":"2022-12-27T07:00:00.000+01:00","total":0.3055},{"startsAt":"2022-12-27T08:00:00.000+01:00","total":0.2913},{"startsAt":"2022-12-27T09:00:00.000+01:00","total":0.3456},{"startsAt":"2022-12-27T10:00:00.000+01:00","total":0.3324},{"startsAt":"2022-12-27T11:00:00.000+01:00","total":0.2709},{"startsAt":"2022-12-27T12:00:00.000+01:00","total":0.2614},{"startsAt":"2022-12-27T13:00:00.000+01:00","total":0.2173},{"startsAt":"2022-12-27T14:00:00.000+01:00","total":0.2271},{"startsAt":"2022-12-27T15:00:00.000+01:00","total":0.2317},{"startsAt":"2022-12-27T16:00:00.000+01:00","total":0.2364},{"startsAt":"2022-12-27T17:00:00.000+01:00","total":0.2485},{"startsAt":"2022-12-27T18:00:00.000+01:00","total":0.255},{"startsAt":"2022-12-27T19:00:00.000+01:00","total":0.2467},{"startsAt":"2022-12-27T20:00:00.000+01:00","total":0.2364},{"startsAt":"2022-12-27T21:00:00.000+01:00","total":0.2159},{"startsAt":"2022-12-27T22:00:00.000+01:00","total":0.1931},{"startsAt":"2022-12-27T23:00:00.000+01:00","total":0.1703}] from API, length 1438
2022-12-26 23:52:35.588 [INFO ] [ab.core.model.script.TestDateConvert] - hour = 0
2022-12-26 23:52:35.589 [INFO ] [ab.core.model.script.TestDateConvert] - startsAt = 2022-12-27T00:00:00.000+01:00
2022-12-26 23:52:35.589 [INFO ] [ab.core.model.script.TestDateConvert] - total = 0.1213
2022-12-26 23:52:35.590 [ERROR] [internal.handler.ScriptActionHandler] - Script execution of rule with UID 'power_price-2' failed: An error occurred during the script execution: Could not invoke method: org.openhab.core.library.types.DateTimeType.format(java.lang.String) on instance: 2022-12-27T00:00:00.000+01:00 in power_price

Any thoughts?

This is not a JavaScript rule so your thread title is a bit confusing.

These formatting require a ZonedDateTime to work on. All you have is a String. Luckily it’s in ISO8601 format so you should be able to simply pass it to a ZonedDateTime.

val startsAtDT = ZonedDateTime.parse(startsAt, DateTimeFormatter.ISO_OFFSET_DATE_TIME)
logInfo("TestDateConvert", "ZonedDateTime from String Formatted = {%1$td.%1$tm.%1$ty %1$tH:%1$tM}", startsAtDT)-

Well… I have added:

	val startsAtDT = ZonedDateTime.parse(startsAt, DateTimeFormatter.ISO_OFFSET_DATE_TIME)
	logInfo("TestDateConvert", "ZonedDateTime from String Formatted = {%1$td.%1$tm.%1$ty %1$tH:%1$tM}", startsAtDT)

But that doesn’t work either:

2022-12-29 09:44:24.341 [INFO ] [ab.core.model.script.TestDateConvert] - startsAt = 2022-12-29T23:00:00.000+01:00
2022-12-29 09:44:24.341 [INFO ] [ab.core.model.script.TestDateConvert] - total = 0.1155
2022-12-29 09:44:24.341 [INFO ] [ab.core.model.script.TestDateConvert] - ZonedDateTime from String Formatted = {%1$td.%1$tm.%1$ty %1$tH:%1$tM}

I have then added the format to the variable:

		val startsAtDT = ZonedDateTime.parse(startsAt, DateTimeFormatter.ISO_OFFSET_DATE_TIME)
		logInfo("TestDateConvert", "ZonedDateTime from String Formatted = {}", startsAtDT.format("%1$td.%1$tm.%1$ty %1$tH:%1$tM"))

Same issue as before, but now with java.time.ZonedDateTime.format(java.time.format.DateTimeFormatter):

2022-12-29 09:48:20.504 [INFO ] [ab.core.model.script.TestDateConvert] - startsAt = 2022-12-29T00:00:00.000+01:00
2022-12-29 09:48:20.504 [INFO ] [ab.core.model.script.TestDateConvert] - total = 0.0746
2022-12-29 09:48:20.506 [ERROR] [internal.handler.ScriptActionHandler] - Script execution of rule with UID 'power_price-2' failed: An error occurred during the script execution: Could not invoke method: java.time.ZonedDateTime.format(java.time.format.DateTimeFormatter) on instance: 2022-12-29T00:00+01:00 in power_price

Note that I did try every option mentioned here, but still couldn’t figure it out:

Regards,

Nika.

In the top of your rule, shouldn’t you declare

var DateTimeType startsAt

as

var String startsAt

Then in your “for”-loop (after you have extracted the JSON for “startsAt”):

val DateTimeType startsAtDT = DateTimeType.valueOf(startsAt)

and change

val String MyStringFromDateTimeType = startsAt.format("%1$td.%1$tm.%1$ty %1$tH:%1$tM")

to

val String MyStringFromDateTimeType = startsAtDT.format("%1$td.%1$tm.%1$ty %1$tH:%1$tM")

Had a similar issue a couple of weeks back. Had to convert from java time (now) to DateTimeType, before I could convert to day of the week with .format(“%1$tA”)

Yes, thanks Rich and Cor for this, it works as a charm after defining & converting it from String to DT and back :slight_smile:

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.