Timeconversion: Astro Binding (DateTime) to Date() javascript object

No, as I said, except for minor differences (e.g. like the need and how to import ZonedDateTime) the lines of code will be the same no matter what rules language you are using.

The code I typed above is the code you’d type into a JavaScript Script Action.

The editor is irrelevant. It’s irrelevant whether you are coding in text rules or in the UI. Those are the lines of code to use.

Rules DSL to compare the state of an Item to now

if(now.isAfter(MyDateItem.state.getZonedDateTime){
    // do something
}

Python

from java.time import ZonedDateTime

if ZonedDateTime.now().isAfter(items["MyDateItem"].getZonedDateTime():
    # do something

JavaScript

var ZonedDateTime = Java.type("java.time.ZonedDateTime");

if(ZonedDateTime.now().isAfter(items["MyDateItem"].getZonedDateTime())) {
    // do something
}

Notice, except for minor differences based on the specific syntax used, they are basically almost identical lines of code. And it’s possible to easily understand what each one does regardless of your preferred langauge.

Look at my examples and description again. It’s a dict of the Item’s states. The key is the Item’s name. The value is the Item’s state, not the Item. You don’t call getState() on an Item’s state. It’s already the Item’s state. If you want the state of Item “Foo”

`items["Foo"]`

That’s it. Nothing more.

I’m really not a proponent of trying to be “pure” in rules. When you go out of your way to use pure JavaScript stuff when there is an openHAB native way to do something (e.g. working with date times):

  • You end up needing to do extra conversion back and forth from the native OH Objects to the pure language version (e.g from OH’s DateTimeType to JavaScript Date and back again) any time you need to use that date time to interact with OH (e.g. createTimer, minimumSince, etc.).

  • You end up needing to learn two ways of working with DateTimes because there will be cases where you have to use the Java classes at some point because you can’t for example, schedule a Timer using a JavaScript Date Object. Consistency is a really useful way to reduce the complexity and make code easier to understand.

  • It’s harder to look at a Rules DSL or Python (or some other supported language) example and easily convert it to JavaScript and if you share any code it’s harder for others to look at and understand your rules if they are not JavaScript. As a result all these “pure to the language” implementations basically become siloed into a language specific communities who end up unable to mutually understand each other.

2 Likes