Solved - Blocky "now" block not working with date/time item?

  • Platform information:
    • Hardware: Raspberry Pi 4 Model B
    • openHAB version: 3.4.1 (openhabian image)

tried to create a “last seen” item based on state change of another item using blocky
the rule is triggered but the datetime item is not updated (see error message below)
It seems that the “now” block is not working with daytime item, i assume it is because of the square brackets
Have i done some wrong or is this a bug?

further information for debugging:

Error log:

[WARN ] [rnal.defaultscope.ScriptBusEventImpl] - State '2023-02-03T18:18:36.500876+01:00[Europe/Berlin]' cannot be parsed for item 'Xiaomi_Mijia_BT_HT_Medienzimmer_last_seen'.

the last seen item is a datetime configured this way:
image

blocky “code”:

complete code of the rule:

configuration: {}
triggers:
  - id: "1"
    configuration:
      itemName: Xiaomi_Mijia_BT_HT_Medienzimmer_hum
    type: core.ItemStateChangeTrigger
  - id: "4"
    configuration:
      itemName: Xiaomi_Mijia_BT_HT_Medienzimmer_tempc
    type: core.ItemStateChangeTrigger
conditions: []
actions:
  - inputs: {}
    id: "5"
    configuration:
      blockSource: <xml xmlns="https://developers.google.com/blockly/xml"><block
        type="oh_event" id="qA6u)ERLw^I3,68Pk%+[" x="628" y="273"><field
        name="eventType">postUpdate</field><value name="value"><shadow
        type="text" id="/~p}{@Q[*DmIjWClf_s}"><field
        name="TEXT">value</field></shadow><block type="oh_zdt_now"
        id="PB7r@p?`WVVQzIl}NuCj"></block></value><value name="itemName"><shadow
        type="oh_item" id="d%*[qdq?3OvZ}iNNub,U"><field
        name="itemName">Xiaomi_Mijia_BT_HT_Medienzimmer_last_seen</field></shadow></value></block></xml>
      type: application/javascript
      script: >
        var dtf = Java.type("java.time.format.DateTimeFormatter");


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


        function getZonedDateTime(datetime) {
          datetime = String(datetime).replace('T', ' ')
          var regex_time_min =         /^\d{2}:\d{2}$/;
          var regex_time_sec =         /^\d{2}:\d{2}:\d{2}$/;
          var regex_date =             /^\d{4}-\d{2}-\d{2}$/;
          var regex_date_time_min =    /^\d{4}-\d{2}-\d{2} \d{2}:\d{2}$/;
          var regex_date_time_sec =    /^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$/;
          var regex_date_time_sec_tz = /^\d{4}-\d{2}-\d{2}[T ]\d{2}:\d{2}:\d{2}[+-]\d{2}:\d{2}$/;
          var regex_date_time_ms =     /^\d{4}-\d{2}-\d{2}[T ]\d{2}:\d{2}:\d{2}\.\d{3}$/;
          var regex_date_time_us =     /^\d{4}-\d{2}-\d{2}[T ]\d{2}:\d{2}:\d{2}\.\d{6}$/;
          var regex_date_time_ms_tz =  /^\d{4}-\d{2}-\d{2}[T ]\d{2}:\d{2}:\d{2}\.\d{3}[+-]\d{2}:\d{2}$/;
          var regex_date_time_us_tz =  /^\d{4}-\d{2}-\d{2}[T ]\d{2}:\d{2}:\d{2}\.\d{6}[+-]\d{2}:\d{2}$/;
          var regex_oh =               /^\d{4}-\d{2}-\d{2}[T ]\d{2}:\d{2}:\d{2}\.\d{3}[+-]\d{4}$/;
          var now = zdt.now();
          var now_year = now.getYear();
          var now_month = now.getMonthValue();
          var now_day = now.getDayOfMonth();
          var today = '' + now_year;
          today += '-' + ('0' + now_month).slice(-2);
          today += '-' + ('0' + now_day).slice(-2)+' ';
          switch (true) {
            case regex_time_min.test(datetime): return zdt.parse(today + datetime + ':00+00:00', dtf.ofPattern('yyyy-MM-dd HH:mm:ssz'));
            case regex_time_sec.test(datetime): return zdt.parse(today + datetime + '+00:00', dtf.ofPattern('yyyy-MM-dd HH:mm:ssz'));
            case regex_date.test(datetime): return zdt.parse(datetime + ' 00:00:00+00:00', dtf.ofPattern('yyyy-MM-dd HH:mm:ssz'));
            case regex_date_time_min.test(datetime): return zdt.parse(datetime + ':00+00:00', dtf.ofPattern('yyyy-MM-dd HH:mm:ssz'));
            case regex_date_time_sec.test(datetime): return zdt.parse(datetime + '+00:00', dtf.ofPattern('yyyy-MM-dd HH:mm:ssz'));
            case regex_date_time_sec_tz.test(datetime): return zdt.parse(datetime, dtf.ofPattern('yyyy-MM-dd HH:mm:ssz'));
            case regex_date_time_ms.test(datetime): return zdt.parse(datetime + ' +00:00', dtf.ofPattern('yyyy-MM-dd HH:mm:ss.SSS z'));
            case regex_date_time_us.test(datetime): return zdt.parse(datetime + ' +00:00', dtf.ofPattern('yyyy-MM-dd HH:mm:ss.SSSSSS z'));
            case regex_date_time_ms_tz.test(datetime): return zdt.parse(datetime, dtf.ofPattern('yyyy-MM-dd HH:mm:ss.SSSSz'));
            case regex_date_time_us_tz.test(datetime): return zdt.parse(datetime, dtf.ofPattern('yyyy-MM-dd HH:mm:ss.SSSSSSSz'));
            case regex_oh.test(datetime): 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.postUpdate('Xiaomi_Mijia_BT_HT_Medienzimmer_last_seen', (zdt.now()));
    type: script.ScriptAction

You cannot send the zoneddatetime directly to your item because the send command requires a string. Use the following block to convert your time into string first:

Do you need to use Blockly? You might be able to achieve this with built in profiles

thank you, i missed that in the in the docu

thanks, I have tried this. But sadly there seems to be a bug that is preventing to apply any kind of formatting using state description, so i have to stick with my rules for now