japhias
(Jan)
September 30, 2022, 3:06pm
1
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?
Example:
I want to save a ZonedDateTime.now() 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?
japhias
(Jan)
September 30, 2022, 8:18pm
3
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', (zdt.now()));
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?
japhias
(Jan)
September 30, 2022, 9:09pm
5
I have found a solution here:
Banging my head against the wall here.
Javascript v11
The Item:
DateTime CalendarEventEnd_CCMain (gCalendarTstat,gHVACZONE_CCMain){channel=“icalendar:eventfilter:feed_community:result_0#end”}
The javascript rule ECMAv11 in the UI rule editor:
var triggeringItem = items.getItem(triggeringName); // WORKS
var v1 = time.ZonedDateTime.now().plusSeconds(10); // WORKS
var nowplus = new DateTimeType(v1); // ERROR
triggeringItem.sendCommand(nowplus); //ERROR
I can’t figure o…
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:
https://www.openhab.org/javadoc/latest/constant-values.html#org.openhab.core.library.types.DateTimeType.DATE_PATTERN_WITH_TZ
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
Stefan
1 Like