I’ve been trying for 2 hours now to get this frustrating thing over and done with, to no avail
I have a timestamp in epoch seconds from my Ring doorbell that comes in via MQTT as a string, into an item. I want to convert this to a DateTime.
In Nashhorn I had:
var logger = Java.type("org.slf4j.LoggerFactory").getLogger("org.openhab.model.script.Rules.RingDevices");
scriptExtension.importPreset("default");
var myEpochMilliseconds = items["ringSnapshotTimestampEpoch"];
var resultDateTime = new Date(myEpochMilliseconds * 1000);
var isoDateTime = resultDateTime.toISOString();
events.postUpdate("ringSnapshotTimestampLocal", isoDateTime)
This worked well. I am now trying to convert all my rules to the ECMA 2021 format. This is as close as I got:
var myEpochMilliseconds = Number(items.getItem("ringSnapshotTimestampEpoch").state)*1000;
var instant = time.Instant.ofEpochMilli(myEpochMilliseconds).atZone(time.ZoneId.systemDefault());
console.log(instant)
//var dt = time.toZDT(instant);
//var dt = time.ZonedDateTime.ofInstant(instant, tz);
//var dt = time.toZDT(instant)
//console.log(dt)
items.getItem("ringSnapshotTimestampLocal").postUpdate(instant.toString())
You can see by the commented out lines, I tried a lot (and more variants). Nothing seems to work to get this into my DateTime item. This last variant is the closest (as in: produces the least errors) and comes up with the error:
State '2025-03-11T10:21:47+01:00[SYSTEM]' cannot be parsed for item 'ringSnapshotTimestampLocal'.
Maybe someone can lend me a helping hand with this. Read a lot of topics but can’t get it to work.