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:
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()))
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, 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
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.
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
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