DateTime Conversion (openHAB 3.x)

My rule looks like this:

from core.rules import rule
from core.triggers import when
from core.actions import ScriptExecution
from java.time import ZonedDateTime as DateTime

@rule("Turn on ZWaveNode089FGS222DoubleRelaySwitch2X15KWSwitchBinary1 after motion sensor is triggered", description="Turn on Flood light after motion is detected", tags=["Tag 1", "Tag 2"])
@when("Item ZWaveNode104SP816SP816MotionSensorAlarmMotion changed")
def ZWaveNode104SP816SP816MotionSensorAlarmMotion(event):
    if items["Naplemente_Kapcsolo"] == ON and items["Overwind"] == OFF:
        if items["ZWaveNode104SP816SP816MotionSensorAlarmMotion"] == ON:
            events.sendCommand("ZWaveNode089FGS222DoubleRelaySwitch2X15KWSwitchBinary1", "ON")
            ScriptExecution.createTimer(DateTime.now().plusSeconds(120), lambda:(ZWaveNode104SP816SP816MotionSensorAlarmMotion_2(OFF)))

def ZWaveNode104SP816SP816MotionSensorAlarmMotion_2(event):
    if items["ZWaveNode104SP816SP816MotionSensorAlarmMotion"] == OFF:
        events.sendCommand("ZWaveNode089FGS222DoubleRelaySwitch2X15KWSwitchBinary1", "OFF")
    else: ZWaveNode104SP816SP816MotionSensorAlarmMotion(ON)

Hello @kovacsi2899 ,

Thanks for your input and for sharing your rule.

Best regards.

Hello, I am currently migrating from OpenHAB 2.5 to 3.0. I really like the semantic model but I am struggling with migrating from Jodatime to Java Time API. I am not a programmer and guess there is a trivial solution. How do I translate these few lines?

var Number hour = now.getHourOfDay
var Number day = now.getDayOfWeek
var Number month = now.getMonthOfYear

Thanks so much!

Hello,

Take a look at this post:

I think it will help with what you are looking for.

Best regards

Thanks, it works for

now.getMonthValue
now.getHour

what I am missing is the day of the week either as a number or name.

Take a look at this thread:

I think it can help you.

Best regards.

That does actually work for a ZonedDateTime, but returns an complex object. Try -

var Number day = now.getDayOfWeek.getValue

1 Like

I am finalizing a rule for OH3 and struggling with the conversion. Probably an easy one for the community to give me the right hint.

> rule "Biotonne Erinnerung"
> when
>    Time cron "0 30 9 * * ? *"
> then
> 
>  val ZonedDateTime zdt = ZonedDateTime.now()
>  val ZonedDateTime pickupDateCompare = zdt.toLocalDate().atStartOfDay(zdt.getOffset())
>  val GarbagePickup = (garbage_collection_bioabfall.state as DateTimeType).getZonedDateTime() 
>  val FuturePickupDate = pickupDateCompare.plusDays(2)
>  
> 
> if( FuturePickupDate > GarbagePickup ) {
> 	  val telegramAction = getActions("telegram","telegram:telegramBot:Telegramxxxxx")
> 	  telegramAction.sendTelegram(xxxxxxxxx, "Denke an die Biotonne!!! Die Abholung ist am " + garbage_collection_bioabfall.state.toString()+".")
> 	  logInfo ("Biotonne", "Denke an die Biotonne!!! Die Abholung ist am " + garbage_collection_bioabfall.state.toString()+"." ) 
>   
> 		}
> end

The rule is working but I am struggling to understand how to format the Date of the output to make it dd:mm:yyyy like in the UI. this is how the output currently looks like:

Denke an die Biotonne!!! Die Abholung ist am 2021-02-08T00:00:00.000+0100

displayState.toString is not supported here in the rule. I am getting an error message.

Thanks!

You’ve already got it as a ZonedDateTime, let’s use that
GarbagePickup.format(DateTimeFormatter.ofPattern("dd:MM:yyyy")

2 Likes

yes, thanks this works! Perfect.

Hi, your suggestion does not work for me:

val LocalDateTime testtime1 = LocalDateTime.now() 
val DateTimeType testdtt1 = new DateTimeType(testtime1)

is throwing an error on the second code line, whereas:

val ZonedDateTime testtime2 = ZonedDateTime.now() 
val DateTimeType testdtt2 = new DateTimeType(testtime2)

works fine.

Hello, now I’ve managed to get my holiday script to work. Now I get an error in the log
Script execution of rule with UID ‘holly-1’ failed: Text ‘2021-4-4T00: 00: 00.000Z’ could not be parsed at index 5 in holly
Please help

It’s not a standard datetime form. 4 / 04
Where does it come from? What is trying to parse it?

from the Holiday Script with the call
var LocalDate easterSunday = ZonedDateTime.parse(year + “-” + month +"-" + day + “T00:00:00.000Z”).toLocalDate()

There’s a solution in this earlier post

Thanks, then the script died.
In the meantime, I would like to have the Joda time back.
a call, and not such a complicated conversion for a script

Joda time will not parse ‘2021-4-4T00: 00: 00.000Z’ either.

These variables are strings, yes?
@m4rk method -

var year = "2021"
var day = "4"
var month = "4"
//Day
if (day.length == 1) {
   day = "0" + day
}
//Month
if (month.length == 1) {
   month = "0" + month
}
var easterSunday = ZonedDateTime.parse(year + "-" + month +"-" + day + "T00:00:00.000Z").toLocalDate
logInfo("date", "date {}", easterSunday)
   //outputs date 2021-04-04

Script execution of rule with UID ‘holly-1’ failed: ‘length’ is not a member of ‘int’; line 21, column 5, length 10 in holly

that was the call in OH 2 and the script ran
var org.joda.time.DateTime easterSunday = parse (year + “-” + month + “-” + day)

So they are not strings, but integers. It’s quite hard to fix rules that we can’t see and you do not tell us what the moving parts are.

If you would like to go back to using OH2, you can. It hasn’t gone away and it will not stop working.

So, if your values are integers we can use a completely different approach.

var year = 2021
var day = 4
var month = 4
var easterSunday=LocalDate.now.withYear(year).withMonthOfYear(month).withDayOfMonth(day)
logInfo("date", "date {}", easterSunday)
  //outputs date 2021-04-04

holiday.script.txt (3.7 KB)