I am trying to get epoch time of a datetime item so I can compare time date differences between the items etc.
The issue I see is the date/time returned is in microseconds and the date.Parse doesn’t seem to like that.
If I remove the microseconds from the datetime then it seems to work.
Am I doing it correctly or not?
var logger = Java.type('org.slf4j.LoggerFactory').getLogger('org.openhab.rule.' + ctx.ruleUID);
var datework = Date.parse("2022-10-27T13:12:36.299+1100"); //<<this has microseconds removed
var datenotwork = Date.parse("2022-10-27T13:12:36.299631+1100") ; //<<this is the device date time returned
var devicedate = (items["Zigbeesengledbulb_Zigbeesengledlastseen"]); //<<this is the returned value
logger.info("\n\r devicedate = " + devicedate + "\n\r datenotwork " + datenotwork + "\n\r datework " + datework);
The state of a DateTime Item is a DateTimeType which carries a java.time.ZonedDateTime. Since the purpose of rules is to interact with openHAB and all your interactions with openHAB will require a ZonedDateTime, why convert it? It’s usually better to keep it as a ZonedDateTime.
Yes. Assuming ECMAScript 5, stay in the java.time.* classes.
Thanks for the reply. I am using ECMAScript 11 and I have read those documents.
I am not a programmer. I just wanted an example that I could cut and paste and use but I am unable to find anything that works.
For example if I use:
var datework = time.toZDT().betweenTimes(‘10:00 PM’, ‘8:00 AM’)
I get error:
2022-10-28 08:49:35.543 [ERROR] [internal.handler.ScriptActionHandler] - Script execution of rule with UID ‘886b62c8a4’ failed: ReferenceError: “time” is not defined in at line number 7
Thanks again. I will try to find something that makes sense to me.
Are you? Did you change the settings? If you didn’t change the settings, you got that error because you are using ECMAScript 5.1. If you only have one choice it’s because you haven’t installed the JS Scripting add-on under automation.
OK…That I didn’t know about the JS addon. I assumed if I upgraded OH then the scripting was done as well.
I will delve into this later.
Now I know why nothing worked as I was looking et the ECMA 11 examples.