Use until datetime on rule based on datetime item

Dear Colleagues,

As I’m not a programmer I’m facing some issues to create a basic rule and I need your support. I’m still using OH 3.4 as one non official binding was not tested on 4.

I’ve a rule to pick up datetime from when my alarm goes off: HouseAlarmOff.postUpdate(now.toLocalDateTime.toString()) and it’s working.

Now I’m trying to create a rule to check how many minutes passed from what I’ve into DateTime item HouseAlarmOff;

I tried:

var mins_between = HouseAlarmOff.zonedDateTime.until(new DateTimeType().zonedDateTime, ChronoUnit.MINUTES)

But it’s not working. May someone help me? I read that my I need to transform my DateTime Item to Zonedate to then execute “until” function but it did not work

You have to use JavaTime instead of DateTimeType (yes, it’s a bit confusing) - see

as an excelent reference.

Hi Udo,

sorry but I read and tried to cast datetime type to java but it did not work. I’m not sure if should then i created a string item and add date directly like java or if really is there a way to cast from datetime type to java.

Code:
val MyJavaTimeFromDateTimeItem = (HouseAlarmOff.state as DateTimeType).getZonedDateTime()
logInfo(“rules”, "minutes: "+ MyJavaTimeFromDateTimeItem)

Error:

==> /var/log/openhab/openhab.log <==

2023-12-07 17:30:50.858 [ERROR] [internal.handler.ScriptActionHandler] - Script execution of rule with UID ‘alarm-4’ failed: Could not cast 2023-12-07T16:51:39.910279 to org.openhab.core.library.types.DateTimeType; line 77, column 39, length 35 in alarm

And

It isn’t clear to me what HouseAlarmOff means, and what you want mins_between to calculate? Is it the difference in time between HouseAlarmOff and now?

If that’s what you want, then:

val mins_between = Duration.between(HouseAlarmOff.state.zonedDateTime, now).toMinutes

Depending on whether HouseAlarmOff is in the past or in the future, you may need to swap the order of the two arguments above.

HouseAlarmoff is DateTime Item and I can’t cast it to zonedate to use between or until option. I guess now the mainly problem is how to convert correct DateTimeType to Zonedate, or how to save my item value on Zonedateformat

If I try to use hyour suggest I’ve got the following error: ‘alarm-4’ failed: ‘zonedDateTime’ is not a member of ‘org.openhab.core.types.State’; line 81, column 41, length 33 in alarm

Try

val mins_between = Duration.between((HouseAlarmOff.state as DateTimeType).zonedDateTime, now).toMinutes

I hate RulesDSL’s primitively verbose type casting. It makes a very messy code that is harder to read.

Consider switching to JRuby. This is the syntax in JRuby:

mins_between = (Time.now - HouseAlarmOff.state).to_minutes

Date/time calculation and comparisons, as with almost everything else, are much more straight forward in JRuby.

Hello, It did not work, I will consider switch to Jruby but we still have some DSL rules. Does anyone know how to save in ZoneDAte or cast DataTimeType to Zone date?

Any error messages?

Hello i’m sorry, I’ve forget to answer you…my bad.

Script execution of rule with UID ‘alarm-4’ failed: Could not cast 2023-12-14T08:10:03.816260 to org.openhab.core.library.types.DateTimeType; line 57, column 41, length 35

It’s the same error that can’t cast

Is housealarmoff a datetime item or a string item?

It’s a datetime item. I’m wondering if we can save date time in a string instead? I guess then I can save it using java time not?

Based on the error message, it seems that your HouseAlarmOff is NOT a DateTimeItem.

Try adding this line before the offending code:

logInfo("XXXX", "HouseAlarmOff type: {}", HouseAlarmOff.class)

Or you can just look at the item through the MainUI to check its type. I’m guessing it’s a StringItem.

Hello Tim, i’ve updated to OH 4.0.1 and not it’s working properly

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