Skip to main content

Track working time using systemd login information

Project description

WorkTracker

A Python-based tool for tracking daily working time using systemd login information. WorkTracker automatically monitors your active session and logs working time, excluding periods when your system is suspended, locked, or hibernated. Also, this tool is able to sync the data to your Home Assistant using MQTT.

Features

  • Automatic Time Tracking: Monitors active working time using systemd login session data
  • Smart Exclusion: Automatically excludes suspend, locked, and hibernation periods
  • Daily Logs: Stores daily active time summaries in a simple SQLite database
  • Systemd Integration: Runs as a systemd user timer, updating every minute
  • Simple CLI: Easy-to-use command-line interface
  • Lightweight: Minimal dependencies, no external services required
  • Home Assistant: Integrates to Home Assistant using MQTT

AI Disclaimer

This project has been developed using a technique known as "vibe coding". I have actually written less than 1% of the lines on this repository, as my work was giving instructions to the AI agent. To be honest, this disclaimer was written by myself.

However, I do have large experience in Python and Linux, so everything was reviewed and the "AI slop" is reduced due to the proper and detailed instructions I provided.

There are few weird implementations but I have decided to keep them in since it is well documented and does not affect performance (which is not a problem here).

Requirements

  • Python 3.10+
  • systemd: Linux system with systemd-logind (most modern Linux distributions)
  • loginctl: Command-line tool for querying systemd login manager (usually pre-installed)

Installation

From PyPI

pip install worktracker

Usage

Initial Setup

Install and start tracking:

worktracker install

This command will:

  • Initialize the SQLite database in ~/.worktracker/worktracker.db
  • Create and install a systemd user timer (worktracker.timer)
  • Enable and start the timer to begin tracking
  • The timer will automatically start when you log in

View current tracking status and today's summary:

worktracker status

If needed, you can uninstall:

worktracker uninstall

This removes the systemd timer and service files. Note: The database files in ~/.worktracker/ are not removed automatically. To completely remove all data, manually delete the ~/.worktracker/ directory.

Home Assistant Integration

WorkTracker can publish daily time tracking data to Home Assistant via MQTT, allowing you to monitor your working time directly in your Home Assistant dashboard.

Setup

  1. Configure MQTT in WorkTracker:

    During installation, WorkTracker creates a default MQTT configuration file at ~/.worktracker/mqtt_config.toml. Edit this file to set your MQTT broker details:

    broker = "192.168.1.100"  # Your MQTT broker IP address
    port = 1883
    username = ""  # Optional: MQTT username
    password = ""  # Optional: MQTT password
    topic_prefix = "worktracker"
    update_interval = 60  # Publish updates every 60 seconds
    
  2. Generate Home Assistant YAML Configuration:

    worktracker mqtt yaml
    

    This command generates the Home Assistant YAML configuration with your hostname automatically filled in.

  3. Add Configuration to Home Assistant:

    • Copy the generated YAML configuration
    • Paste it into your Home Assistant configuration.yaml file
    • Restart Home Assistant or reload the MQTT integration
  4. Start the MQTT Publisher: To run the publisher as a service:

    worktracker mqtt start service
    

    This should be done only once and it should work automatically on next reboots. Or, to run it in the terminal:

    worktracker mqtt start local
    

    This allows you to simply CTRL-C and the publisher stops.

The integration creates a single sensor that displays your daily total active time formatted as hours and minutes (e.g., "2h 30m" or "45m"). The sensor updates automatically as WorkTracker publishes new data.

MQTT Commands

  • worktracker mqtt start <mode> - Start the MQTT publisher daemon
  • worktracker mqtt stop - Stop the MQTT publisher
  • worktracker mqtt status - Show MQTT configuration status
  • worktracker mqtt publish - Manually publish status (for testing)
  • worktracker mqtt uninstall - Disable the service and delete the publisher file.
  • worktracker mqtt yaml - Generate Home Assistant YAML configuration

How It Works

WorkTracker uses a systemd user timer that runs every minute. Each minute, it:

  1. Checks if your session is active using loginctl
  2. Verifies the session is not locked
  3. Confirms the system is not suspended/hibernated
  4. If all conditions are met, adds 60 seconds to today's total active time

The tracking is based on systemd login session state, which provides accurate information about:

  • Session activity (active/inactive)
  • Screen lock status
  • System power state

Limitations

  • Linux Only: Requires systemd, which is primarily available on Linux
  • User Sessions Only: Tracks only graphical/login sessions, not SSH sessions
  • No Historical Data Import: Cannot import time data from other sources
  • Manual Database Access: No built-in query interface; use SQLite tools for advanced queries

Contributing

Contributions are welcome! Please ensure:

  • All tests pass (pytest)
  • Code coverage remains above 80%
  • Code follows PEP 8 style guidelines
  • New features include appropriate tests

License

MIT License - see LICENSE file for details.

Version

Current version: 0.1.0

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

workpulse-0.1.0.tar.gz (18.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

workpulse-0.1.0-py3-none-any.whl (3.9 kB view details)

Uploaded Python 3

File details

Details for the file workpulse-0.1.0.tar.gz.

File metadata

  • Download URL: workpulse-0.1.0.tar.gz
  • Upload date:
  • Size: 18.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for workpulse-0.1.0.tar.gz
Algorithm Hash digest
SHA256 dcbe4e629fe4c1297cd710f31262cd46cd03afa96730d651d480f8e3bbb6ef93
MD5 84c7e264b4d8df18c414a8dfae8ed2d5
BLAKE2b-256 9d4a160a31c06cbd2a474517c85f911813e190b0897772f073a484725b362628

See more details on using hashes here.

Provenance

The following attestation bundles were made for workpulse-0.1.0.tar.gz:

Publisher: python-publish.yml on fdcavalcanti/workpulse

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file workpulse-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: workpulse-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 3.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for workpulse-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 aff89339f14ac3b3f044668a42321377e4020a40c83809f72845cd35941eaf29
MD5 88472e73dc9be8c141637704d2d2130b
BLAKE2b-256 02906bb30e9b456bd0667b1c6c17a5dd2562fb8c956b5028d5896712a8a1a330

See more details on using hashes here.

Provenance

The following attestation bundles were made for workpulse-0.1.0-py3-none-any.whl:

Publisher: python-publish.yml on fdcavalcanti/workpulse

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page