8000 GitHub - idodov/MusicTracker: This Python script is designed to track music played on Home Assistant media players. It stores track information in a SQLite database and provides various statistics
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

This Python script is designed to track music played on Home Assistant media players. It stores track information in a SQLite database and provides various statistics

Notifications You must be signed in to change notification settings

idodov/MusicTracker

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

50 Commits
 
 
 
 

Repository files navigation

🎵 Music Tracker & AI Insights for Home Assistant

Uncover the story of your musical taste with this powerful AppDaemon script for Home Assistant. Go beyond simple play counts and dive deep into your listening habits with automatically generated, beautiful, and insightful music charts.

This isn't just a tracker; it's your personal music historian, complete with an optional AI musicologist that provides stunning visual and analytical reports on what you've been listening to.

Music Charts Interface

Ever wonder what the real soundtrack to your life is? Go beyond simple play counts and discover the true story of your musical taste with Music Tracker & AI Insights, a powerful AppDaemon application for Home Assistant.

This isn't just a scrobbler; it's your personal music historian that passively logs your listening habits and transforms that data into beautiful, insightful, and dynamic charts. Plus, with an optional AI musicologist, you can receive stunning analytical reports, artist visualizations, and even music-themed mini-games based on your unique listening DNA.

Best of all, it's designed to be self-maintaining, automatically cleaning and optimizing its own database to stay fast and efficient forever.


✨ Key Features

  • ✅ Passive Music Tracking: Automatically logs every song you listen to across configured Home Assistant media players (Sonos, Volumio, Spotify, etc.).
  • 📊 Dynamic Charts: Generates daily, weekly, monthly, and yearly charts for your top songs, artists, albums, and even your favorite radio stations or playlists.
  • 📈 Historical Analysis: See how your top songs rise and fall with position change indicators (▲, ▼, NEW).
  • 🚀 Self-Maintaining Database:
    • Intelligently identifies and removes "skipped" tracks (songs played for less than a minute).
    • Automatically prunes old chart data to keep the database lean and fast.
    • Reclaims disk space by running VACUUM after cleanup. Set it and forget it!
  • 🧠 AI-Powered Insights (Optional):
    • Connect a Generative AI service (like the google_generative_ai_conversation integration).
    • Receive stunning, self-contained HTML reports with deep analysis, new music recommendations, and fun facts.
    • Get a unique, AI-generated image of your top artist and interactive music trivia games!
  • 📱 Modern Web Interface: All charts are rendered into a single, clean HTML page that looks great on both desktop and mobile.
  • 🌓 Light & Dark Mode: Automatically adapts to your system's theme with a manual override toggle.
  • ⚡ On-Demand Updates: Trigger chart refreshes instantly from the web interface via a webhook or directly from a Home Assistant automation.

AI-Generated Report


🚀 Installation Guide

Prerequisites

  1. A working Home Assistant installation.
  2. The AppDaemon Add-on installed and running.
  3. (Optional) A configured Generative AI integration (e.g., Google Generative AI) to enable the AI features.

Step 1: Add the Script

  1. Navigate to your AppDaemon configuration folder (e.g., /config/appdaemon/apps).
  2. Create a new file inside the apps folder named music_tracker.py.
  3. Copy the entire content of the music_tracker.py script from this repository and paste it into your new file.

Step 2: Configure apps.yaml

Open your apps.yaml file (in the /config/appdaemon/apps directory) and add the following block. Customize it to fit your setup.

music_tracker:
  module: music_tracker
  class: MusicTracker
  
  # --- Required Settings ---
  # A list of all media_player entities you want to monitor.
  media_players:
    - media_player.living_room_sonos
    - media_player.kitchen_display
    - media_player.bedroom_speaker
    
  # The full path to the database file. Using the /config/ folder is recommended.
  db_path: "/config/music_data_history.db"

  # The full path where the final HTML page will be saved.
  # This MUST be inside your Home Assistant 'www' directory.
  html_output_path: "/config/www/music_charts.html"
  
  # --- AI & Chart Generation ---
  # The service call for your Generative AI integration. Set to false to disable.
  ai_service: "google_generative_ai_conversation/generate_content"
  
  # Time of day (24-hour HH:MM:SS format) to automatically update the charts.
  update_time: "23:59:00"

  # --- Database Optimization (Highly Recommended!) ---
  # Schedule to run the cleanup process.
  # This example runs every Sunday at 3:05 AM.
  cleanup_schedule: "03:05:00"
  cleanup_day_of_week: "sun"
  
  # Enable this to actually delete/prune records.
  cleanup_execute_on_run: true
  
  # Enable this to shrink the database file size after cleanup.
  cleanup_vacuum_on_complete: true

  # --- Fine-Tuning ---
  # How long a song must play (in seconds) to be counted.
  duration: 30
  
  # How many unique songs from an album must be played for it to appear.
  min_songs_for_album: 4

  # Set to true if you want the "Update Charts" button in the web interface.
  # See Step 4 below to set up the required automation.
  webhook: true
  
  # This is the helper toggle the script listens to for manual or webhook updates.
  chart_trigger_boolean: "input_boolean.music_charts_update"
  
  # Run the chart generation immediately when AppDaemon starts.
  run_on_startup: True

Step 3: Configure Home Assistant configuration.yaml (CRITICAL STEP)

To allow Home Assistant to serve the music_charts.html page, you must explicitly whitelist the www directory.

  1. Open your main configuration.yaml file.

  2. Add the following lines. If you already have a homeassistant: block, add the allowlist_external_dirs line under it.

    homeassistant:
      # This line is essential for serving files from the www folder.
      allowlist_external_dirs:
        - /config/www
  3. Save the configuration.yaml file.

  4. Restart Home Assistant (not just AppDaemon) for this change to take effect. You can do this from Settings > System > Restart.

Step 4: Set Up Manual & Webhook Updates (Optional)

To trigger updates from Home Assistant or from the chart page itself, you need a helper toggle and an automation.

A. Create a Helper Toggle

This toggle is the "switch" that tells the script to run an update.

  1. In Home Assistant, go to Settings > Devices & Services > Helpers.
  2. Click Create Helper and select Toggle.
  3. Name it Music Charts Update. This creates an entity input_boolean.music_charts_update, which matches the chart_trigger_boolean in the config above.

You can now turn this toggle on anytime in Home Assistant to trigger an immediate update.

B. Create the Webhook Automation (For the Web Interface Button)

This automation connects the "Update Charts" button on the HTML page to the toggle you just created.

  1. Go to Settings > Automations & Scenes.
  2. Click Create Automation and choose Start with an empty automation.
  3. Switch to YAML mode by clicking the three-dots menu in the top right.
  4. Paste the following YAML code and save it:
alias: "Trigger Music Charts Update via Webhook"
description: "Listens for a webhook from the music chart page and turns on the update toggle."
trigger:
  - platform: webhook
    webhook_id: Update_Music_Charts # This ID must be exact
action:
  - service: input_boolean.turn_on
    target:
      entity_id: input_boolean.music_charts_update # The helper you created
mode: single

The update flow is now complete: Button on HTML Page → Webhook → Automation → Turns on Helper Toggle → AppDaemon Script Runs.


🖥️ Viewing Your Charts

Once the script has run and generated the file, you can access your personal music charts at:

http://<your-home-assistant-ip>:8123/local/music_charts.html

  • Replace <your-home-assistant-ip> with the actual IP address or hostname of your Home Assistant instance.
  • Bookmark this page, or add it as a webpage card to one of your dashboards for easy access!

🧰 Troubleshooting

  • I get a 404 Not Found error when trying to view the page:

    • This is the most common issue. You almost certainly missed or made a mistake in Step 3: Configure Home Assistant configuration.yaml.
    • Ensure allowlist_external_dirs: - /config/www is in your configuration.yaml.
    • Make sure you restarted Home Assistant after adding it.
    • Verify that the music_charts.html file actually exists inside your /config/www folder.
  • Charts are not updating:

    • Check the AppDaemon logs for any errors related to music_tracker. Errors are usually very descriptive.
    • Ensure your db_path and html_output_path are correct and that AppDaemon has permission to write to those locations.
    • Manually trigger an update by turning on your helper toggle in Developer Tools > States.
  • The "Update Charts" button doesn't work:

    • Confirm that webhook: True is set in your apps.yaml.
    • Double-check that your automation's Webhook ID is exactly Update_Music_Charts.
    • Open your browser's developer tools (F12) on the charts page, go to the Console tab, click the button, and see if any network errors appear.
  • AI Analysis is missing:

    • Verify your ai_service is correctly configured and that the integration is working in Home Assistant.
    • Check the AppDaemon logs. AI generation can sometimes fail or time out, and the logs will contain the error details.
    • Generating the report can take 30-120 seconds. Be patient after triggering an update.

About

This Python script is designed to track music played on Home Assistant media players. It stores track information in a SQLite database and provides various statistics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

0