Date format Rule doesn't working on OH2

rules
Tags: #<Tag:0x00007fe066354fc8>

(Simon Zsolt) #1

I have a following rule what worked perfectly on OH1.x, but since I updated to OH2, the rule doesn’t working.
This rule calculate the heating time and format it to: eg. 25.3 min (19:27)
rule:
rule “Heater Stopped” // Calculate heating time
when
Item STAT5 changed to OFF
then
var SimpleDateFormat datf = new SimpleDateFormat( “HH:mm” )
var long nowMsec = now.millis
var long wsMsec = whenStarted.millis
var long timeSinceStart = nowMsec - wsMsec
var double minutesSinceStart = (timeSinceStart/1000.0/60.0).doubleValue
postUpdate(HeatingTime, minutesSinceStart)
tempr = (Math::round(minutesSinceStart.floatValue*10.0)/10.0) + " min (" + datf.format(HeatingTime.lastUpdate(“mysql”)) + ")"
postUpdate(HeatingTimeString, tempr)
end

Error message: Rule ‘Heater Stopped’: Cannot format given Object as a Date
I think it’s wrong with the “datf.format(…)”, but I do not know how to fix it.

Many thnaks for help.


(Rich Koshak) #2

First, How to use code fences

I’m surprised this worked in OH 1.x. lastUpdate doesn’t return a java.util.Date object, but using VSCode I see there is a toDate method.

Try

datf.format(HeatingTime.lastUpdate("mysql").toDate)