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
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
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
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?
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