The right code for JavaScript rules or how to save ZonedDateTime into an item

Hi all,
I start to develop my rules in JavaScript (filebased). I’m little bit confused to about the code that I find in the forum, some is DSL, some is JavaScript but not filebased, some filebased? How can I distinguish between what I can reuse?

I want to save a to a DateTime item. But I can’t reuse the code in this post:

One way you can easily learn the right Javascripts is by creating the rule with blocklies and then look in the Code section what has been created.

What do you want to save it for? What do you want to do then with the datetime object?

Grundsätzlich eine tolle Idee, leider komme ich damit in diesem Fall nicht weiter, da mir Blockly folgendes gibt:

var dtf = Java.type("java.time.format.DateTimeFormatter");

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

/* Try to detect the format based on its length */
function getZonedDateTime(datetime) {
  datetime = String(datetime).replace('T', ' ')
  switch (datetime.length) {
    case 10: return zdt.parse(datetime + ' 00:00:00+00:00', dtf.ofPattern('yyyy-MM-dd HH:mm:ssz'));
    case 16: return zdt.parse(datetime + ':00+00:00', dtf.ofPattern('yyyy-MM-dd HH:mm:ssz'));
    case 19: return zdt.parse(datetime + '+00:00', dtf.ofPattern('yyyy-MM-dd HH:mm:ssz'));
    case 25: return zdt.parse(datetime, dtf.ofPattern('yyyy-MM-dd HH:mm:ssz'));
    case 23: return zdt.parse(datetime + ' +00:00', dtf.ofPattern('yyyy-MM-dd HH:mm:ss.SSS z'));
    case 26: return zdt.parse(datetime + ' +00:00', dtf.ofPattern('yyyy-MM-dd HH:mm:ss.SSSSSS z'));
    case 29: return zdt.parse(datetime, dtf.ofPattern('yyyy-MM-dd HH:mm:ss.SSSSz'));
    case 32: return zdt.parse(datetime, dtf.ofPattern('yyyy-MM-dd HH:mm:ss.SSSSSSSz'));
    case 28: return zdt.parse(datetime.slice(0,26) + ':' + datetime.slice(26,28), dtf.ofPattern('yyyy-MM-dd HH:mm:ss.SSSSz'));
    default: return zdt.parse(datetime);

function createZonedDateTime(year, month, day, hour, minute, second, nano, offsetString, timezoneString) {
  stringToParse = '' + year;
  stringToParse += '-' + ('0' + month).slice(-2);
  stringToParse += '-' + ('0' + day).slice(-2);
  stringToParse += 'T' + ('0' + hour).slice(-2);
  stringToParse += ':' + ('0' + minute).slice(-2);
  stringToParse += ':' + ('0' + second).slice(-2);
  stringToParse += '.' + nano + offsetString + '[' + timezoneString + ']';
  return zdt.parse(stringToParse, dtf.ISO_ZONED_DATE_TIME);

events.sendCommand('GA_Wallbox_openWB_LP1_PlugIn_Time', (;

In Blockly finde ich keinen cast zwischen ZDT und DateTime.

Which brings me back to my question: what exactly do you want to do and why do you need the cast?

I have found a solution here:

Thought, there is a more elegant way

You see, this is why it is always important to tell what you want to achieve. Here is the solution in Blockly:

The actual issue behind your problem is that you need to know what the format needs to be that you want to send to the datetime item.

See DateTimeType (openHAB Core 3.4.0-SNAPSHOT API) to know what types are expected. Now look up these constants:

and you’ll see that it requires a string that contains the “T” in the datetime string:


and you can achieve this by providing the OH-Time to the item.

Hope that helps

1 Like