Alexa time on timer

Hi I am new to Openhab and I wanted to have an Item that displays the Hours:Minutes:Seconds on an Alexa Timer.For this I created the Alexa_Tobias_to_Timer item in my model.Than I created a rule that runs every 5 seconds and runs a script that relables the Item automatically
(Sorry for my bad English)

configuration: {}
triggers:
  - id: "1"
    configuration:
      cronExpression: "0/5 * * * * * * "
    type: timer.GenericCronTrigger
conditions: []
actions:
  - inputs: {}
    id: "2"
    configuration:
      type: application/javascript
      script: >
        var string_Tobias, Alexa_Tobias, hour_Tobias, minute_Tobias,
        seconds_Tobias, Alexa_o_Tobias;


        var chronoUnit = Java.type("java.time.temporal.ChronoUnit");


        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);
        }



        Alexa_Tobias = itemRegistry.getItem('AlexaTobias_NachsterTimer').getState();

        if(Alexa_Tobias !="UNDEF"){
          string_Tobias = '';

        hour_Tobias = (chronoUnit.HOURS.between(Alexa_Tobias,zdt.now().plusSeconds(0)));

        minute_Tobias = (chronoUnit.MINUTES.between(Alexa_Tobias,zdt.now().plusSeconds(0)));

        seconds_Tobias = (chronoUnit.HOURS.between(Alexa_Tobias,zdt.now().plusSeconds(0)));

        string_Tobias += String(hour_Tobias);

        string_Tobias += ':';

        string_Tobias += String(minute_Tobias);

        string_Tobias += ':';

        string_Tobias += String(seconds_Tobias);

        Alexa_o_Tobias = itemRegistry.getItem('Alexa_Tobias_to_timer');

       Alexa_o_Tobias.setLabel(string_Tobias)

        }
    type: script.ScriptAction

But it does not re label and I got an Error in my log

[e.automation.internal.RuleEngineImpl] - Failed to execute rule 'a91537d4f3': Fail to execute action: 2

Update:

var string_Tobias, Alexa_Tobias, hour_Tobias, minute_Tobias, seconds_Tobias, Alexa_o_Tobias;

var chronoUnit = Java.type("java.time.temporal.ChronoUnit");

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);
}


Alexa_Tobias = itemRegistry.getItem('AlexaTobias_NachsterTimer').getState();
if(Alexa_Tobias !="UNDEF"){
  string_Tobias = '';

hour_Tobias = (chronoUnit.HOURS.between(Alexa_Tobias,zdt.now().plusSeconds(0)));
minute_Tobias = (chronoUnit.MINUTES.between(Alexa_Tobias,zdt.now().plusSeconds(0)));
seconds_Tobias = (chronoUnit.HOURS.between(Alexa_Tobias,zdt.now().plusSeconds(0)));
string_Tobias += String(hour_Tobias.format(dtf.ofPattern('hh')));
string_Tobias += ':';
string_Tobias += String(minute_Tobias.format(dtf.ofPattern('mm')));
string_Tobias += ':';
string_Tobias += String(seconds_Tobias.format(dtf.ofPattern('ss')));
Alexa_o_Tobias = itemRegistry.getItem('Alexa_Tobias_to_timer');
Alexa_o_Tobias.setLabel(string_Tobias)
}

Does not work to

Have a look at the market place. You maybe don’t need to reinvent the wheel…

1 Like

Thank you so much

Unfortunately, I always get the following error when no timer is running.

Script execution of rule with UID '48f5d55b65' failed: TypeError: alarm.getZonedDateTime is not a function in <eval> at line number 3

Unfortunately, the user does not answer me

Based on script language and oh version datetime handling is different.
Maybe search the forum for any similar datetime issue

I do Not found anything about this Error on the forum