My first steps with OpenHab

Added an items file to create groups and items for last update time stamps for various sensors and stuff, as described here:

The groups and items were no problem (I hadn’t noticed that items have a “parent groups” configuration". The tricky bit was working out how to implement the rule in Javascript. After a lot of fiddling I came up with a helper function to extract the name of the item updated from the “input” parameter:

// Adapted from https://gitlab.com/RNTs_3/openhab-jsr223-javascript-helper/tree/master
function GetTriggerItemForEvent(input) 
{
  var ev = input.get("event")+"";
        
  // Splits into: 'gRecordLastUpdate,changed,from,7.96612745098039237,to,7.95362745098039237,through,BalconyAeotec_SensorTemperature' 
  var evArr = ev.split("'").join("").split("Item ").join("").split(" ");

  return evArr[7];
}

It seems that evArr[0] contains the group name, not the item name, when you’re capturing changes to all members of a group.

The rule looks like this, in case it helps somebody. I stuck three different triggers in there because I wasn’t sure which I needed. The last bit of the puzzle was working out that the date sent to sendCommand needed to be in ISO format (.toISOString).

JSRule
(
  {
    name: "Rule_LastUpdate",
    description: "Store last update stamps for all items that require them",
    triggers: 
    [ 
      UpdatedEventTrigger("gRecordLastUpdate"), // Any Member of gRecordLastUpdate received update, even if the value didn't change
      ChangedEventTrigger("gRecordLastUpdate"),
      ItemStateChangeTrigger("gRecordLastUpdate")
    ],
    execute: function( module, input)
    {
      // LastUpdate item has same name as item update, plus _LastUpdate
      var itemLastUpdate; // Item containing last update date

      // Get current date
      var dateNow = new Date();

      var sTriggerItemName = GetTriggerItemForEvent(input);

      // LastUpdate item has same name as item update, plus _LastUpdate
      itemLastUpdate = getItem(sTriggerItemName + "_LastUpdate");      

      sendCommand(itemLastUpdate, dateNow.toISOString());    
    }
  }
);