How to assign lastUpdate to an item?

Hi,

I used the following Code to get lastUpdate from mysql:

MyItem_LastUpdate.postUpdate(DateTimeType::DATE_FORMATTER_WITH_TZ.format(MyItem.lastUpdate("mysql")))

But now this Code doesn’t work anymore.
I found https://github.com/openhab/openhab/issues/2945 where DATE_FORMATTER_WITH_TZ is removed in code, so it seems, I have to change the rule, but… :smile: how?

1 Like

Could you replace it with:

MyItem_LastUpdate.postUpdate(new DateTimeType(MyItem.lastUpdate("mysql"))

Finally, I found the time to test, but no luck :frowning:

The designer says: Incompatible types. Expected java.util.Calendar but was java.util.Date

Ooops, sorry. This might work:

import java.util.Calendar
// ...
var Calendar cal = Calendar::getInstance()
cal.setTime(MyItem.lastUpdate("mysql"))
MyItem_LastUpdate.postUpdate(new DateTimeType(cal))
2 Likes

Thanks, that’s working like a charm :grinning:

2 Likes

Hi everyone.
I am trying to do the same thing, but I am getting an error.

I have traced the error to this line: cal.setTime(MyItem.lastUpdate(“mysql”))

This is my rule.rules file:

rule "Update timestamp"
when
    Item LastSeenDevices received update
then
        LastSeenDevices.members.forEach[i |
                var Calendar cal = Calendar::getInstance()
                cal.setTime(i.lastUpdate("mysql"))
                // postUpdate("Label_" + i.name, new DateTimeType(cal))
                // logInfo( "HB_LastSeen", "Updating Last seen value for" + i.name)
        ]
end

and the error I am getting is:

00:55:14.579 [ERROR] [.script.engine.ScriptExecutionThread] - Error during the execution of rule 'Update timestamp': Could not invoke method: java.util.Calendar.setTime(java.util.Date) on instance: java.util.GregorianCalendar[time=1457495714566,areFieldsSet=true,areAllFieldsSet=true,lenient=true,zone=sun.util.calendar.ZoneInfo[id="America/Argentina/Cordoba",offset=-10800000,dstSavings=0,useDaylight=false,transitions=62,lastRule=null],firstDayOfWeek=2,minimalDaysInFirstWeek=4,ERA=1,YEAR=2016,MONTH=2,WEEK_OF_YEAR=10,WEEK_OF_MONTH=2,DAY_OF_MONTH=9,DAY_OF_YEAR=69,DAY_OF_WEEK=4,DAY_OF_WEEK_IN_MONTH=2,AM_PM=0,HOUR=0,HOUR_OF_DAY=0,MINUTE=55,SECOND=14,MILLISECOND=566,ZONE_OFFSET=-10800000,DST_OFFSET=0]

Could you please help me find the way to solve this.
Thanks, regards!

EDIT: Forgot to told you I am using OH2 and mysql persitence working fine:
±--------------------±------+
| Time | Value |
±--------------------±------+
| 2016-03-08 22:21:05 | OFF |
| 2016-03-08 22:32:03 | ON |
| 2016-03-08 22:33:03 | OFF |
| 2016-03-08 22:34:03 | ON |
| 2016-03-08 22:35:03 | OFF |
| 2016-03-08 22:36:03 | ON |

This is exactly the problem I have. wonder if someone solved this issue.

I’m pretty sure you can’t define the var over and over again. Please move the definition out of the rule:

var Calendar cal = null

...

rule...
... cal = Calendar::getInstance() ...
...end

just wondering, after two years is this still the preferred way?

EDIT: I somehow expected it. When implementing I got:

The constructor DateTimeType(Calendar) is deprecated

If talking about Time and Date, please take a look at

There are some other really good tutorials for Type conversion.
Maybe also interesting:

1 Like

awesome, now it’s as easy as:

postUpdate(myItem, now.toString)