I’d like to update a datetime item every day to the current date and found toToday() as a new method:
alarm.postUpdate(time.toZDT(alarm).toToday());
what is the correct syntax in a rules DSL file?
2024-01-01 22:16:27.126 [ERROR] [internal.handler.ScriptActionHandler] - Script execution of rule with UID 'testrule2-1' failed: The name 'time' cannot be resolved to an item or type; line 12, column 15, length 4 in testrule2
Are you sure this is running the beer JS Scripting addon?
It’s complaining it doesn’t know what time is and that’s a core part of the helper library. There is nothing wrong with the line of code, assuming you are using the helper library. So something else must be off kilter.
I’m curious why do you need to do this? What’s the datetime item used for? I’m just wondering if there’s a better way of achieving your primary intention, e.g using a time only option on a datetime trigger
the use case is to monitor the main entrance door of our house. my little daughter sometimes arrives home early from school and we want to know when she’s home. I only want to get notifications during these times.
You can try to remove using the console but if it failed from the UI I have little expectation it’ll work better from the console.
So at this point your add-on is in an unknown state. Maybe it’s working, maybe it’s not. Try clearing the cache first. Verify you have the 2022+ add-on installed and not the Nashorn add-on. Make sure you have the default settings on the add-on, mainly to auto inject the helper library.
You can side step this whole issue using a condition that only lasts the rule run between those times. Then you don’t even need to check in the script action at all.
Under “but only if” add a time condition that is “inside a time range”.
You still need to figure out what’s going wrong with your add-on but this dude steps the problem for now.
sorry Rick, I need to ask again.
I did uninstall the JS Scripting add-on via the console. then I rebooted openhab and was able to install the add-on again.
I can run a JS script via UI rules which works as expected:
var entrystart = items.entrydoormonitor_start;
var entryend = items.entrydoormonitor_end;
entrystart.postUpdate(time.toZDT(entrystart).toToday());
entryend.postUpdate(time.toZDT(entryend).toToday());
console.info('Items entrydoormonitor_start and entrydoormonitor_end updated with actual date ');
2024-01-02 21:52:37.418 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item 'entrydoormonitor_start' changed from 2023-12-30T11:04:00.000+0100 to 2024-01-02T11:04:00.000+0100
2024-01-02 21:57:42.937 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item 'entrydoormonitor_end' changed from 2023-12-30T13:01:00.000+0100 to 2024-01-02T13:01:00.000+0100
so the JS add-on should work fine for my understanding?
again I tried to do the same in RulesDSL, but I’m still curious about the correct syntax. even VSC states that the field time is undefined:
I guess. It seems to be working now based on your logs.
Rules DSL is completely different. There is no time entity in Rules DSL. You can get now and then manipulate that. There is no toToday() in Rules DSL either. That only exists in JS Scripting with the helper library enabled (which is the default).
var toToday = new ZonedDateTime.of(now.toLocalDate, (entrydoormonitor_start.state as DateTimeType).zonedDateTime.toLocaTime, now.zoneId)
Each rule language is completely different. You can’t use a concept from one in the others. Always look at the docs in the language you are using for how to do anything.
it’s getting frustrating.
even with the documentation I’m lost:
var toToday = new ZonedDateTime.of(now.toLocalDate, (entrydoormonitor_start.state as DateTimeType).ZonedDateTime.toLocalTime, now.ZoneId)
2024-01-02 23:21:06.388 [ERROR] [internal.handler.ScriptActionHandler] - Script execution of rule with UID 'testrule2-1' failed: An error occurred during the script execution: Cannot invoke "org.eclipse.xtext.common.types.JvmType.eIsProxy()" because "type" is null in testrule2
I agree, but I thought it would be easier to maintain only one language.
2024-01-02 23:40:45.309 [ERROR] [internal.handler.ScriptActionHandler] - Script execution of rule with UID 'testrule2-1' failed: 'ZonedDateTime' is not a member of 'org.openhab.core.library.types.DateTimeType'; line 9, column 53, length 60 in testrule2
val today_start = (entrydoormonitor_start.state as DateTimeType).zonedDateTime.with(LocalDate.now)
entrydoormonitor_start.postUpdate(today_start.toString)
var toToday = new ZonedDateTime.of(now.toLocalDate, (entrydoormonitor_start.state as DateTimeType).zonedDateTime.toLocalTime, now.ZoneId)
It’s a lot easier in JS Scripting too. time.toZDT() can be called with just about anything that can become a date time including Items, States, numbers, ISO8601 duration strings, etc. and get back a ZonedDateTime.