DateTime Conversion (openHAB 3.x)

Hello to all and good afternoon,

Yesterday migrated to OH3 and I am loving it. I stumbled across some things regarding ZonedDateTime. I elaborated a simle rule in jython just as an example to whta I want to do that is do a simple postUpdate to a DateTime item like this:

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

@rule("JythonLightControlTestRule", description="Jython Light Control Test Rule", tags=["admin"])
@when("Item SwitchLightsControlShortJython changed")
def jython_light_control_test_rule(event):
    now = DateTime.now()
    timeout = 10
    if event.itemState == ON:
        events.postUpdate(ir.getItem("DateTimeLightsControlLampJythonTimeoutControlLastUpdate"), str(now.plusSeconds(timeout)))

The issue is that the now.plusSeconds(timeout), if I print it out to the log it get’s printed with the timezone like this:

2021-01-30 15:51:26.017 [INFO ] [23.jython.JythonLightControlTestRule] - 2021-01-30T15:51:36.006681Z[Europe/Lisbon]

Afterwards I get an error:

IllegalArgumentException: java.lang.IllegalArgumentException: The argument 'state' must not be null.

This is for sure some simple conversion necessary, but from all the atemps I read I did not get there yet.

Could someone be so kind and give ma hand on this, please?

Thank you very much in advanced.

Hello again,

I found a way that work, do not know if it is the most correct one but it works, By adding .toLocalDateTime() in postUpdate it works now:

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

@rule("JythonLightControlTestRule", description="Jython Light Control Test Rule", tags=["admin"])
@when("Item SwitchLightsControlShortJython changed")
def jython_light_control_test_rule(event):
    now = DateTime.now()
    timeout = 10
    if event.itemState == ON:
        events.postUpdate(ir.getItem("DateTimeLightsControlLampJythonTimeoutControlLastUpdate"), str(now.plusSeconds(timeout).toLocalDateTime()))

Thanks.

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)