Screen Time Alerts from Activity Watch
Tags: productivity, python
Published: 2024-06-22 13:17:08

---
title: Screen Time Alerts from Activity Watch
published: true
date: 2024-05-01 01:29:27 UTC
tags: productivity,python
canonical_url: https://avilpage.com/2024/05/screen-time-alerts-from-activity-watch.html
---

#### Introduction

<iframe src="https://www.youtube.com/embed/41A0r2H-EXs" allowfullscreen>
</iframe>

Activity Watch<sup id="fnref:activity_watch"><a href="https://avilpage.com/2024/05/screen-time-alerts-from-activity-watch.html#fn:activity_watch">1</a></sup> is a cross-platform open-source time-tracking tool that helps us to track time spent on applications and websites.

![Activity Watch](https://avilpage.com/images/activity-watch-alerts.png)

At the moment, Activity Watch doesn't have any feature to show screen time alerts. In this post, we will see how to show screen time alerts using Activity Watch.

#### Python Script

Activity Watch provides an API to interact with the Activity Watch server. We can use the API to get the screen time data and show alerts.

```
import json
import os
from datetime import datetime

import requests

def get\_nonafk\_events(timeperiods=None):
    headers = {"Content-type": "application/json", "charset": "utf-8"}
    query = """afk\_events = query\_bucket(find\_bucket('aw-watcher-afk\_'));
window\_events = query\_bucket(find\_bucket('aw-watcher-window\_'));
window\_events = filter\_period\_intersect(window\_events, filter\_keyvals(afk\_events, 'status', ['not-afk']));
RETURN = merge\_events\_by\_keys(window\_events, ['app', 'title']);""".split("\n")
    data = {
        "timeperiods": timeperiods,
        "query": query,
    }
    r = requests.post(
        "http://localhost:5600/api/0/query/",
        data=bytes(json.dumps(data), "utf-8"),
        headers=headers,
        params={},
    )
    return json.loads(r.text)[0]

def main():
    now = datetime.now()
    timeperiods = [
        "/".join([now.replace(hour=0, minute=0, second=0).isoformat(), now.isoformat()])
    ]
    events = get\_nonafk\_events(timeperiods)

    total\_time\_secs = sum([event["duration"] for event in events])
    total\_time\_mins = total\_time\_secs / 60
    print(f"Total time: {total\_time\_mins} seconds")
    hours, minutes = divmod(total\_time\_mins, 60)
    minutes = round(minutes)
    print(f"Screen Time: {hours} hours {minutes} minutes")

    # show mac notification
    os.system(f"osascript -e 'display notification \"{hours} hours {minutes} minutes\" with title \"Screen TIme\"'")

if \_\_name\_\_ == "\_\_main\_\_":
    main()

```

This script<sup id="fnref:github"><a href="https://avilpage.com/2024/05/screen-time-alerts-from-activity-watch.html#fn:github">2</a></sup> will show the screen time alerts using the Activity Watch API. We can run this script using the below command.

```
$ python screen_time_alerts.py

```

![Screen Time Alerts](https://avilpage.com/images/activity-watch-alerts2.png)

We can set up a cron job to run this script every hour to show screen time alerts.

```
$ crontab -e
0 * * * * python screen_time_alerts.py

```

We can also modify the script to show alerts only when the screen time exceeds a certain limit.

#### Conclusion

Since Actvity Watch is open-source and provides an API, we can extend its functionality to show screen time alerts. We can also use the API to create custom reports and dashboards.

* * *

1. [Activity Watch](https://activitywatch.net/) [↩](https://avilpage.com/2024/05/screen-time-alerts-from-activity-watch.html#fnref:activity_watch "Jump back to footnote 1 in the text")

2. [Screen Time Alerts Script](https://github.com/ChillarAnand/avilpage.com/blob/master/scripts/activity_watch_alerts.py) [↩](https://avilpage.com/2024/05/screen-time-alerts-from-activity-watch.html#fnref:github "Jump back to footnote 2 in the text")