Correct syntax for toToday()?

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

I’m on OH 4.1.0 Release Build
thanks

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.

yes, it is. I already tried to remove and add it via UI but that fails. it never uninstalls… any chance to do that on the console?


                           _   _     _     ____
   ___   ___   ___   ___  | | | |   / \   | __ )
  / _ \ / _ \ / _ \ / _ \ | |_| |  / _ \  |  _ \
 | (_) | (_) |  __/| | | ||  _  | / ___ \ | |_) )
  \___/|  __/ \___/|_| |_||_| |_|/_/   \_\|____/
       |_|       4.1.0 - Release Build

Use '<tab>' for a list of available commands
and '[cmd] --help' for help on a specific command.
To exit, use '<ctrl-d>' or 'logout'.

openhab> bundle:list | grep Add-ons
243 │ Active │  80 │ 3.4.1                  │ openHAB Add-ons :: Bundles :: worxlandroid Binding
275 │ Active │  80 │ 4.1.0                  │ openHAB Add-ons :: Bundles :: Automation :: JavaScript Scripting
276 │ Active │  80 │ 4.1.0                  │ openHAB Add-ons :: Bundles :: Amazon Echo Control Binding
277 │ Active │  80 │ 4.1.0                  │ openHAB Add-ons :: Bundles :: Astro Binding
278 │ Active │  80 │ 4.1.0                  │ openHAB Add-ons :: Bundles :: ChatGPT Binding
279 │ Active │  80 │ 4.1.0                  │ openHAB Add-ons :: Bundles :: Enigma2 Binding
280 │ Active │  80 │ 4.1.0                  │ openHAB Add-ons :: Bundles :: Exec Binding
281 │ Active │  80 │ 4.1.0                  │ openHAB Add-ons :: Bundles :: GPSTracker Binding
282 │ Active │  80 │ 4.1.0                  │ openHAB Add-ons :: Bundles :: HTTP Binding
283 │ Active │  80 │ 4.1.0                  │ openHAB Add-ons :: Bundles :: iCalendar Binding
284 │ Active │  80 │ 4.1.0                  │ openHAB Add-ons :: Bundles :: IpCamera Binding
285 │ Active │  80 │ 4.1.0                  │ openHAB Add-ons :: Bundles :: KM200 Binding
286 │ Active │  80 │ 4.1.0                  │ openHAB Add-ons :: Bundles :: Mail Binding
287 │ Active │  80 │ 4.1.0                  │ openHAB Add-ons :: Bundles :: Xiaomi Wifi devices (Mi IO) Binding
288 │ Active │  80 │ 4.1.0                  │ openHAB Add-ons :: Bundles :: MQTT Broker Binding
289 │ Active │  81 │ 4.1.0                  │ openHAB Add-ons :: Bundles :: MQTT EspMilightHub
290 │ Active │  81 │ 4.1.0                  │ openHAB Add-ons :: Bundles :: MQTT Things and Channels
291 │ Active │  82 │ 4.1.0                  │ openHAB Add-ons :: Bundles :: MQTT HomeAssistant Convention
292 │ Active │  82 │ 4.1.0                  │ openHAB Add-ons :: Bundles :: MQTT Homie Convention
293 │ Active │  82 │ 4.1.0                  │ openHAB Add-ons :: Bundles :: MQTT Ruuvi Gateway
294 │ Active │  80 │ 4.1.0                  │ openHAB Add-ons :: Bundles :: Network Binding
295 │ Active │  80 │ 4.1.0                  │ openHAB Add-ons :: Bundles :: NTP Binding
296 │ Active │  80 │ 4.1.0                  │ openHAB Add-ons :: Bundles :: Onkyo Binding
297 │ Active │  80 │ 4.1.0                  │ openHAB Add-ons :: Bundles :: OpenWeatherMap Binding
298 │ Active │  80 │ 4.1.0                  │ openHAB Add-ons :: Bundles :: Remote openHAB Binding
299 │ Active │  80 │ 4.1.0                  │ openHAB Add-ons :: Bundles :: Shelly Binding Gen1+2
300 │ Active │  80 │ 4.1.0                  │ openHAB Add-ons :: Bundles :: SqueezeBox Binding
301 │ Active │  80 │ 4.1.0                  │ openHAB Add-ons :: Bundles :: Systeminfo Binding
302 │ Active │  80 │ 4.1.0                  │ openHAB Add-ons :: Bundles :: WiFiLED Binding
303 │ Active │  80 │ 4.1.0                  │ openHAB Add-ons :: Bundles :: ZWave Binding
314 │ Active │  80 │ 4.1.0                  │ openHAB Add-ons :: Bundles :: IO :: openHAB Cloud Connector
315 │ Active │  80 │ 4.1.0                  │ openHAB Add-ons :: Bundles :: Persistence Service :: InfluxDB
316 │ Active │  80 │ 4.1.0                  │ openHAB Add-ons :: Bundles :: Persistence Service :: RRD4j
317 │ Active │  75 │ 4.1.0                  │ openHAB Add-ons :: Bundles :: Transformation Service :: JSonPath
318 │ Active │  75 │ 4.1.0                  │ openHAB Add-ons :: Bundles :: Transformation Service :: Map
319 │ Active │  75 │ 4.1.0                  │ openHAB Add-ons :: Bundles :: Transformation Service :: RegEx
320 │ Active │  75 │ 4.1.0                  │ openHAB Add-ons :: Bundles :: Transformation Service :: Scale
323 │ Active │  80 │ 4.1.0                  │ openHAB Add-ons :: Bundles :: Voice :: VoiceRSS Text-to-Speech
openhab>

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.

image

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:

can someone pls doublecheck on your environment if toToday is working there?

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).

For Rules DSL, see ZonedDateTime (Java SE 17 & JDK 17). You’ll probably have to do something like the following in Rules DSL.

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

There’s a typo.

var toToday = ZonedDateTime.of(now.toLocalDate, (entrydoormonitor_start.state as DateTimeType).ZonedDateTime.toLocalTime, now.ZoneId)

Almost all of this kind of thing is harder in Rules DSL than any of the other languages. It’s easy to make errors.

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

Try this:

val today_start = (entrydoormonitor_start.state as DateTimeType).zonedDateTime.with(LocalDate.now)
entrydoormonitor_start.postUpdate(today_start.toString)

FYI in JRuby, working with date/time/duration is more intuitive yet simplified, so that a lot of type castings are not needed.

today_start = entrydoormonitor_start.state.with(LocalDate.now)
entrydoormonitor_start.update(today_start)

You could even write a method to bring any datetime item to today

def to_today(item) = item.update(item.state.with(LocalDate.now))

to_today(entrydoormonitor_start)
to_today(entrydoormonitor_end)

Or even like this

class DateTimeItem
  def to_today = update(state.with(LocalDate.now))
end

entrydoormonitor_start.to_today
entrydoormonitor_end.to_today

How are you using these items in your rules? Perhaps you could simply ignore the date part instead of updating the date every day.

You could also look into Ephemeris (and update it with your school holidays) and only set the alarm during school days.

Another typo. Lower case “z”.

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.