# Timestamp before shutdown

**URL:** <https://community.openhab.org/t/timestamp-before-shutdown/165056>\
**Category:** Beginners\
**Tags:** openhab4-openhab5\
**Created:** [July 23, 2025, 1:13pm UTC](https://community.openhab.org/t/timestamp-before-shutdown/165056 "2025-07-23T13:13:02Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dirk\_Albrecht](https://community.openhab.org/user_avatar/community.openhab.org/dirk_albrecht/32/87128_2.png) [@Dirk\_Albrecht](https://community.openhab.org/u/Dirk_Albrecht)\
**Post date:** [July 23, 2025, 1:13pm UTC](https://community.openhab.org/t/timestamp-before-shutdown/165056/1 "2025-07-23T13:13:02Z")

</div>

I’m wondering whether there is a possibility, at startup, of openHAB, to get the timestamp of when openhab shut down. (or just before, obviously).

Reason for this: I’m using one of my openhab servers in area (in Spain), where frequent, short, and (infrequent) longer power cuts occur. openhab - obviously - restarts for all of them, when the power is back. However, there have been moments, where some of the cron-triggered rules have been missed. So I would like to determine - at startup - since when openHAB has been down, in order to do the right things then.

I guess, a good enough proxy would be to find out, when any Item was last changed.

---

<div class="post-metadata">

**Author:** ![rlkoshak](https://community.openhab.org/user_avatar/community.openhab.org/rlkoshak/32/75251_2.png) [@rlkoshak](https://community.openhab.org/u/rlkoshak)\
**Post date:** [July 23, 2025, 5:00pm UTC](https://community.openhab.org/t/timestamp-before-shutdown/165056/2 "2025-07-23T17:00:23Z")

</div>

By default, OH opens a new logger file every time it starts. So the timestamp of the newest log archive file will be the time that OH stopped.

The following shell script will extract the timestamp of the most recent archived version of events.log as epoch. This assumes a standard Linux install. The path will be different for manual or Docker installs, and obviously it won’t work with Windows installs.

```sh
#!/bin/bash

find /var/log/openhab/events.log.* -type f -exec ls -t1 {} + | head -1 | { read f ; zcat $f ; } | tail -1 | cut -d ' ' -f 1-2 | { read dt ; date -d "$dt" +%s ; }

```

I put this into a file at `/etc/openhab/misc/getStopTime.sh` and make sure to set execute permissions.

What this script does is (separated by the pipe symbols):

- iterate over all the files that start with “events.log.” in `/var/log/openhab`
- For each file, list them based on when they were created
- Get the first line
- Pass that as the file to open by `zcat` which is just like `cat` but it works with compressed files
- Get the last line
- Extract the datetime
- Convert the datetime to epoch

I use a shell script because I gave up trying to get find to work with executecommandLine directly and I’m not sure the pipes will work.

Then the following lines of JS will run the script and convert the epoch to a ZonedDateTime which can be used to update an Item or in your rule however you need it.

```JS
var epochStr = actions.Exec.executeCommandLine(time.Duration.ofSeconds(1), '/etc/openhab/misc/getStopTime.sh').trim();
var epoch = parseInt(epochStr);
var instant = time.Instant.ofEpochSecond(epoch);
var timestamp = time.ZonedDateTime.ofInstant(instant, time.ZoneId.systemDefault());

```

Note, I purposefully do not parse the String and convert the timestamp to epoch to avoid needing to import the locales.

---

<div class="post-metadata">

**Author:** ![rlkoshak](https://community.openhab.org/user_avatar/community.openhab.org/rlkoshak/32/75251_2.png) [@rlkoshak](https://community.openhab.org/u/rlkoshak)\
**Post date:** [July 23, 2025, 6:13pm UTC](https://community.openhab.org/t/timestamp-before-shutdown/165056/3 "2025-07-23T18:13:54Z")

</div>

I figured out how to do this using just `executeCommandLine`, so I created a rule template you can install instead of copy/paste from here.

Create a DateTime Item to store the timestamp.

Go to Add-on Store → Automation and find “Last Shutdown Timestamp” in the list under marketplace. Add it.

Next go to Settings → Rules → +. Enter an id, name and description and select “Get Last Shutdown Timestamp” as the template. Enter the path to the folder where your events.log files are found and select the Item you created above.

Save.

This rule will trigger at OH system start at runlevel 50. It pulls the timestamp from the last log entry of the most recent events.log archive file. That is going to be very close to the last timestamp of when OH was up.

---

<div class="post-metadata">

**Author:** ![justaoldman](https://community.openhab.org/letter_avatar_proxy/v4/letter/j/c68b51/32.png) [@justaoldman](https://community.openhab.org/u/justaoldman)\
**Post date:** [July 23, 2025, 8:23pm UTC](https://community.openhab.org/t/timestamp-before-shutdown/165056/4 "2025-07-23T20:23:58Z")

</div>

Hi so scrapping logs is a way to deal with it but ideally you manage the server so any shutdowns are controlled ie UPS and if the outage is extended you trigger a shutdown script that gracefully triggers the action you could then easily set a item with a event and timestamp of the shutdown and once the system comes backup read the value and adjust rule triggers

---

<div class="post-metadata">

**Author:** ![rlkoshak](https://community.openhab.org/user_avatar/community.openhab.org/rlkoshak/32/75251_2.png) [@rlkoshak](https://community.openhab.org/u/rlkoshak)\
**Post date:** [July 23, 2025, 8:49pm UTC](https://community.openhab.org/t/timestamp-before-shutdown/165056/5 "2025-07-23T20:49:52Z")

</div>

Even when managed with an UPS and such, stuff can go wrong. You can never eliminate 100% of crashes, yanking the plug, `kill -9`, etc. So I think it’s still a useful rule template even with power backups and such. You’ll get a timestamp pretty close to the last activity that took place in OH before it went down, whether it was shutdown nicely or crashed.

Although, it just occurred to me that users running with zram who just lose power will also lose the logs. I need to make a note of that in the rule template.

---

<div class="post-metadata">

**Author:** ![justaoldman](https://community.openhab.org/letter_avatar_proxy/v4/letter/j/c68b51/32.png) [@justaoldman](https://community.openhab.org/u/justaoldman)\
**Post date:** [July 23, 2025, 9:01pm UTC](https://community.openhab.org/t/timestamp-before-shutdown/165056/6 "2025-07-23T21:01:02Z")

</div>

I by no means was implying the solution or rule template was not useful. The OP stated he was addressing power outages as I read the problem statement So what I was pointing out was controlling the chaos a little better should be considered as a part of the total solution. Sometimes you have to add hardware because software is not the complete fix.
