Hi guys…
actually I’m trying to convert my last Rules DSL rules to JSRules. but sometimes it get’s a little bit hard for me to sort out the problems…
this is one of the rules I’m trying to convert:
rule "TimeOfDay - Write DayStart Time and Set SleepState"
when
System started or
Item BedtimeStart changed or
Item ManualOverride changed
then
rescheduleTimer?.cancel
rescheduleTimer = createTimer(now, [ |
// get values from BedtimeStart for calculation
val awake = (BedtimeStart.state as DateTimeType).getZonedDateTime().plusDays(1).minusHours(16).minusMinutes(15)
val asleep = (BedtimeStart.state as DateTimeType).getZonedDateTime().plusMinutes(15)
val squeezestop = (BedtimeStart.state as DateTimeType).getZonedDateTime().plusDays(1).minusHours(14).minusMinutes(15)
val DateTimeType asp = new DateTimeType(asleep)
val DateTimeType awk = new DateTimeType(awake)
val DateTimeType sqstp = new DateTimeType(squeezestop)
val bedtime = Bedtime.state
// send values to Items
MyDayStart.postUpdate(awk)
MySleepStart.postUpdate(asp)
SqueezeStop.postUpdate(sqstp)
// Calculate the SleepState
switch true {
case now.isAfter(awake) && bedtime == OFF : curr2 = "AWAKE"
case now.isAfter(asleep) && now.isBefore(awake) && bedtime == OFF : curr2 = "AWAKE"
case now.isAfter(asleep) && now.isBefore(awake) && bedtime == ON : curr2 = "ASLEEP"
}
if ( SleepState.state.toString != curr2 ) {
SleepState.sendCommand(curr2)
logInfo("DayStart", "SleepState = "+curr2)
}
rescheduleTimer.reschedule(now.plusMinutes(5))
])
end
my main problem is that I can’t figure out how to work with zonedDateTime in java.
this is my approach to convert to JSRule:
const {log, rules, triggers, time} = require("openhab");
rules.JSRule({
name: "ECMA2021Script - Sleep State",
description: "toggle sleep state when bedtime/daystart changes",
triggers: [
triggers.SystemStartlevelTrigger(100),
triggers.ItemStateChangeTrigger('BedtimeStart'),
triggers.ItemStateChangeTrigger('ManualOverride')
],
execute: data => {
const sleepstate = items.getItem("SleepState");
const bedtimestart = items.getItem("BedtimeStart");
const mydaystart = items.getItem("MyDayStart");
const mysleepstart = items.getItem("MySleepStart");
const squeezestop = items.getItem("SqueezeStop");
const bedtime = items.getItem("Bedtime");
const bedtimeStartTime = time.ZonedDateTime.of(
bedtimestart.year,
bedtimestart.month,
bedtimestart.day,
bedtimestart.hour,
bedtimestart.minute,
bedtimestart.second,
bedtimestart.nano,
bedtimestart.zone
);
// Create ZonedDateTime objects from the number of milliseconds
var awake = bedtimeStartTime.plus(time.Duration.ofDays(1)).minus(time.Duration.ofHours(16)).minus(time.Duration.ofMinutes(15));
var asleep = bedtimeStartTime.plus(time.Duration.ofMinutes(15));
var sqzstop = bedtimeStartTime.plus(time.Duration.ofDays(1)).minus(time.Duration.ofHours(14)).minus(time.Duration.ofMinutes(15));
var now = time.ZonedDateTime.now();
var curr = "UNKNOWN";
mydaystart.postUpdate(awake);
mysleepstart.postUpdate(asleep);
squeezestop.postUpdate(sqzstop);
if ( now.isAfter(awake) && bedtime.state === "OFF" ) {
curr = "AWAKE";
console.log("SleepState ",curr);
}
if (now.isAfter(asleep) && now.isBefore(awake) && bedtime.state === "OFF") {
curr = "AWAKE";
console.log("SleepState ", curr);
}
if (now.isAfter(asleep) && now.isBefore(awake) && bedtime.state === "ON") {
curr = "ASLEEP";
console.log("SleepState ", curr);
}
if (sleepstate.state !== curr) {
sleepstate.sendCommand(curr);
console.log("SleepState ", curr);
}
// Reschedule timer to run every 5 minutes
setTimeout((data) => rules.JSRule(data), 300000, data);
},
tags: [],
});
I tried to figure out what’s the problem and it seems like I’m having problems telling java to work corectly
with my string items (e.g. bedtimestart) that hold the DateTime data.
this is the error I get from the logs when manually firing the rule via UI:
==> /var/log/openhab/openhab.log <==
2022-12-29 09:37:22.490 [ERROR] [tion.script.file.rules_sleepstate.js] - Failed to execute rule ECMA2021Script---Sleep-State-f2bd3f2b-a45f-4f0e-977e-0de2498f6d96: NullPointerException: year must not be null: NullPointerException: year must not be null
at JsJodaException (webpack://openhab/./node_modules/@js-joda/core/dist/js-joda.esm.js?:76)
at requireNonNull (webpack://openhab/./node_modules/@js-joda/core/dist/js-joda.esm.js?:298)
at LocalDate (webpack://openhab/./node_modules/@js-joda/core/dist/js-joda.esm.js?:11303)
at of (webpack://openhab/./node_modules/@js-joda/core/dist/js-joda.esm.js?:12008)
at ofNumbers (webpack://openhab/./node_modules/@js-joda/core/dist/js-joda.esm.js?:12766)
at of (webpack://openhab/./node_modules/@js-joda/core/dist/js-joda.esm.js?:12756)
at of8 (webpack://openhab/./node_modules/@js-joda/core/dist/js-joda.esm.js?:10516)
at of (webpack://openhab/./node_modules/@js-joda/core/dist/js-joda.esm.js?:10500)
at execute (rules_sleepstate.js:18)
seems like the conversion approach is completely wrong, but I’m a little bit lost, because the more I read about ZDT in java, the more I get confused.
I also tried to replace this definition
const bedtimeStartTime = time.ZonedDateTime.of(
bedtimestart.year,
bedtimestart.month,
bedtimestart.day,
bedtimestart.hour,
bedtimestart.minute,
bedtimestart.second,
bedtimestart.nano,
bedtimestart.zone
);
with
const bedtimeStartTime = time.ZonedDateTime.parse(bedtimestart.state);
but then I get the following error:
2022-12-29 09:48:44.769 [ERROR] [tion.script.file.rules_sleepstate.js] - Failed to execute rule ECMA2021Script---Sleep-State-1ab88ac8-aa36-410c-a92a-32999c0412a2: DateTimeParseException: Text '2022-12-29T01:12:41.206118+0100' could not be parsed at index 26: 2022-12-29T01:12:41.206118+0100, at index: 26: DateTimeParseException: Text '2022-12-29T01:12:41.206118+0100' could not be parsed at index 26: 2022-12-29T01:12:41.206118+0100, at index: 26
at JsJodaException (webpack://openhab/./node_modules/@js-joda/core/dist/js-joda.esm.js?:76)
at _parseToBuilder (webpack://openhab/./node_modules/@js-joda/core/dist/js-joda.esm.js?:7742)
at parse2 (webpack://openhab/./node_modules/@js-joda/core/dist/js-joda.esm.js?:7701)
at parse (webpack://openhab/./node_modules/@js-joda/core/dist/js-joda.esm.js?:7676)
at parse (webpack://openhab/./node_modules/@js-joda/core/dist/js-joda.esm.js?:10658)
at execute (rules_sleepstate.js:18)
at doExecute (webpack://openhab/./node_modules/openhab/rules/rules.js?:242)
my skills in datatype conversions are very limited I think
maybe someone can help me understand more what to look out for in plain words?
thanks!