Formatting DateTime item into string in rule

I am on OH 4.0.0 and struggeling to get a rule to work;

I have a DateTime item which are being fed from an external datasource (through HTTP binding).
In addition - the same datasource feeds an item with a electricity price.
I now want to format a string to be published to a string item - where I have the price and the date formatted together.

I can get this to work with the current time - like this:

var String tmp
var String Timestamp
val date_formatter = java.time.format.DateTimeFormatter.ofPattern("dd.MM.YYYY HH:mm")

Timestamp = now.format(date_formatter)

tmp = ((Math::round(Elpris.floatValue*100.00)/100.00) + " kr pr. Kwh (" + Timestamp + ")")
postUpdate(ElpriserTestStringItem, tmp)

But I cannot get it to work with the contents of my DateTime item instead of the current time… This is my (non-workable) rule code:

var String tmp
var String Timestamp
val date_formatter = java.time.format.DateTimeFormatter.ofPattern("dd.MM.YYYY HH:mm")
		
Timestamp = (SpotPriceDKK_currentTime.state as DateTimeType).getZonedDateTime().format(date_formatter)

tmp = ((Math::round(Elpris.floatValue*100.00)/100.00) + " kr pr. Kwh (" + Timestamp + ")")
postUpdate(ElpriserTestStringItem, tmp)

I am missing something related to stringing DateTime items - but what ?

If you are using MainUI, it would be better to combine the two Items in one widget instead of using a rule and a separate Item.

In what way is it no working? Errors in the log? Wrong format?

Based on how it’s written I’m assuming this is a Rules DSL Script Action, correct?

Hmm. My bad: I could see my errors in the log, but thought I would quickly recreate the situation and copy the log messages freshly - and now my code works…

This is the working code:

var String tmp
var String Timestamp
val date_formatter = java.time.format.DateTimeFormatter.ofPattern("dd.MM.YYYY HH:mm")
		
Timestamp = (SpotPriceDKK_currentTime.state as DateTimeType).getZonedDateTime().format(date_formatter)

tmp = ((Math::round(Elpris.floatValue*100.00)/100.00) + " kr pr. Kwh (" + Timestamp + ")")
postUpdate(ElpriserTestStringItem, tmp)

Sorry for bothering you guys - but thanks for the suppport :slight_smile: