uid: rules_tools:getLastShutdownTimestamp label: Get Last Shutdown Timestamp description: Only works on a POSIX enviornment with bash. Pulls the last timestamp from the most recent events.log archive as the shutdown timestamp. configDescriptions: - name: path type: TEXT label: Logs folder. required: true description: Path to the folder containing events.log and it's archives. - name: item type: TEXT context: item filterCriteria: - name: type value: DateTime label: Shutdown Timestamp Item required: true description: Item that holds the timetstamp of the last shutdown. triggers: - id: "1" configuration: startlevel: 50 type: core.SystemStartlevelTrigger conditions: [] actions: - inputs: {} id: "2" configuration: type: application/javascript script: >+ // Version 0.2 // Imports var File = Java.type('java.io.File'); var FileInputStream = Java.type('java.io.FileInputStream') var GZipInputStream = Java.type('java.util.zip.GZIPInputStream'); var InputStreamReader = Java.type('java.io.InputStreamReader'); var BufferedReader = Java.type('java.io.BufferedReader'); // Properties var srcDirectory = new File('{{path}}'); var timestampItem = '{{item}}'; // Get all the events.log.X.gz files var listFiles = srcDirectory.listFiles(); var file = epoch = listFiles.filter(f => f.getName().startsWith("events.log.")) .sort(( a,b) => b.lastModified() - a.lastModified())[0] console.debug(file); try { // Open the file for reading const fis = new FileInputStream(file); const gzis = new GZipInputStream(fis); const reader = new InputStreamReader(gzis); const breader = new BufferedReader(reader); // Get the last line let currLine = breader.readLine(); let lastLine = null; while(currLine != null) { lastLine = currLine; currLine = breader.readLine(); } // Extract the timestamp and convert it to an ISO8601 let timestamp = time.toZDT(lastLine.substring(0, 22).replace(' ', 'T')); // Log a message and update the Item console.info('openHAB last shutdown at ' + timestamp); items[timestampItem].postUpdate(timestamp); } catch(e) { console.error(e); } type: script.ScriptAction