Upgrading from OH 2.x to 3x and have questions about DateTime vs. DateTimeType when/where to use each. In the URL below, it references both of them but doesn’t clearly tell you which to use when/where.
In my .items file, I have DateTime defined for items. Do I change these to DateTimeType ?
In my .rules file, I have variables defined as DateTime. Do I change these to DateTimeType ?
I have changed some of the variables over in my .rules file which resulted in the compiled errors going away.
var DateTimeType Cal_Date0 = zonedDateTime.now()
var DateTimeType Cal_Date1 = zonedDateTime.now().plusDays(1)
var DateTimeType Cal_Date2 = zonedDateTime.now().plusDays(2)
var DateTimeType Cal_Date3 = zonedDateTime.now().plusDays(3)
var DateTimeType Cal_Date4 = zonedDateTime.now().plusDays(4)
this works also
var Cal_Date0 = new DateTimeType().zonedDateTime.now().plusDays(0)
var Cal_Date1 = new DateTimeType().zonedDateTime.now().plusDays(1)
var Cal_Date2 = new DateTimeType().zonedDateTime.now().plusDays(2)
var Cal_Date3 = new DateTimeType().zonedDateTime.now().plusDays(3)
var Cal_Date4 = new DateTimeType().zonedDateTime.now().plusDays(4)
It’s actually pretty easy. DateTime is an Item Type. You’d only ever use it in a .items file.
DateTime type is the type for the State carried by a DateTime Item.
In rules in OH 3, there is no longer such a thing as DateTime. That came from a library that has been deprecated for years and was finally removed in OH 3. Instead of org.joda.time.DateTime you will use java.time.ZonedDateTime.
The two work mostly the same but there are some differences covered in the lists of breaking changes. There are also several postings on the forum showing how to use ZonedDateTime.
Well, the error is pretty clear. As far as that rule is concerned the Item myitemname doesn’t exist. Do you see any evidence that the .items file was reloaded after you made the change? Can you see the Item anywhere else in OH?
rule "PhoneAlarm Clock in DateTime umrechnen"
when
Item Phone_AlarmClock changed
then
if (Phone_AlarmClock.state != NULL) {
if (Phone_AlarmClock.state as Number != 0) {
var alarmtime = new DateTime((Phone_AlarmClock.state as Number).longValue)
Phone_AlarmClock_DT.postUpdate(alarmtime.toString)
}
}
end
I get following error:
DateTime cannot be resolved —> in line starting with var alarmtime …
There were a number of changes to icon handling on OH 4 to add support for alternative icon providers. I’m sure they tightened up the syntax checking as part of that. I don’t think quotes were ever really supported. The fact they worked was probably just lucky.
You are working in Rules DSL. All your date time operations should be using ZonedDateTime. There is no such thing as DateTime that is available by default. Though I’m pretty sure that was the case in OH 3 too.
What type of Item is Phone_AlarmClock? Why are you trying to cast it to a Number? What is populating that Item? Why isn’t it a DateTimeItem in the first place? Is this rule even needed?
Any time I see anyone messing with epoch, it’s a huge code smell to me.
rule "PhoneAlarm Clock in DateTime umrechnen"
when
Item Phone_AlarmClock changed
then
if (Phone_AlarmClock.state != NULL) {
if (Phone_AlarmClock.state as Number != 0) {
var alarmtime = ZonedDateTime.of(Instant.ofEpochMillis(Phone_AlarmClock.state as Number).longValue), ZonedId.systemDefault)
Phone_AlarmClock_DT.postUpdate(alarmtime.toString)
}
}
end
Phone_AlarmClock is coming from my openhabandroid app. I have checked the manuals agein. This must have been changed since i implemented it. Now i will get a DeteTime-Item from the app.
But some time ago i got a number (miliseconds since ???) and had to convert it to a a DateTime-Item first.
I used to use that too. You can update a DateTime Item with the Android App. Indeed you don’t need this rule at all.
I’ve since moved to using the Sleep as Android MQTT plug-in instead. I was seeing weird behaviors with the android app at the time and didn’t have time to diagnose where the problem was coming from.