Plugin for tracking streaks

It’d be nice to have a plugin that automatically tracks the number of days/weeks that we were able to maintain a habit and writes it in the daily note, possibly under a pre-defined header and in a pre-defined format. For example, in today’s daily note 2022-01-25, I could write two streaks like this:

Streaks

  • Jogging: day 1
  • No sugar: day 2

The plugin would automatically add 1 to the previous daily note’s streak, under the header Streaks of tomorrow’s daily note:

Note: 2022-01-26

Streaks

  • Jogging: day 2
  • No sugar: day 3
2 Likes

Obsidian tracker is pretty comprehensive

I second this. My current (not-ideal) fix is using embedded notes. Example:

file: jogging.md

Current_Streak:: 20 (last updated: [[2022.01.21]])
Longest_Streak:: 42 (last updated: [[2021.07.03]]) 
^093df1

file: health-tracker.md

![[jogging.md#^093df1]]

It’s not great, and has to be manually maintained, but it’s simple and works.

Ideally, the plugin would allow for quick and easy +1 of a streak. I bet something can be put together with MetaEdit. I’ve been meaning to tinker, but have yet to do so.

It doesn’t do what I’m asking :sweat_smile:

Still not the solution I’m looking for but an improvement on my previous suggestion: Using Dataview (and dataviewjs).

DateField:: 2024-03-10
This would live on the note you track on (and fill in the ‘Your Note Title Here’ below).

and the corresponding dataviewjs (that can be on any other note):

```dataviewjs
    // Fetch the target note using its title.
    const targetNote = dv.page('Your Note Title Here');

    // Check if the target note and the specified date field exist.
    if (targetNote && targetNote.DateField) {
        // Convert the date from the DateField to a JavaScript Date object.
        const targetDate = new Date(targetNote.DateField);

        // Validate the converted date.
        if (!isNaN(targetDate.getTime())) {
            // Calculate the number of days between the current date and the target date.
            const today = new Date();
            const timeDiff = today - targetDate; // Difference in milliseconds.
            const daysSinceDate = Math.floor(timeDiff / (1000 * 3600 * 24)); // Convert to days.

            // Output the result with a link to the target note and the calculated days.
            dv.paragraph(`- [[Your Note Title Here|Time Since Event]]: ${daysSinceDate} days`);
        } else {
            // Display an error message if the date in DateField is invalid.
            dv.paragraph("The 'DateField' contains an invalid date.");
        }
    } else {
        // Display an error message if the target note or DateField is missing.
        dv.paragraph("Note 'Your Note Title Here' not found or 'DateField' missing.");
    }
```

Make sure to replace 'Your Note Title Here' and 'DateField' with the appropriate values for your specific use case when sharing or using this script.

This is via chatgpt, but I have confirmed it is working.

I’m testing out the Habit tracker 21 currently, and I kind of like its approach to habit tracker, so check it out and see if that could suit you.

2 Likes

Could you tell me how you set it up? I installed the plugin, creates the folder with empty .md files, directed the code snippet to it – but nothing is ever shown.

Newbie here, and unclear what I’m doing wrong :confused:

It’s part of a larger setup, but it ends up as this code:

~~~habittracker
{ "path": "meta/Habits", "lastDisplayedDate": "2024-04-17"}
~~~

This with some files in the meta/Habits folder (within the vault of course), and the date at the end is inserted by Templater ends up displaying as:

image

This is shown either in live preview and reading mode, and in either case you can click on the day column of that row to indicate that you’ve done that habit on that day. And when repeated it looks like the image above showing that the top habit I’ve kept all throughout the period, but the bottom habit I skipped on the 17th and 24th.

Thank you so much! I used ``` instead of ~~~ to bracket the code, so nothing was ever triggered.

My new problem is that ALL files are shown, not just those of the directory I specify. Will investigate – but thanks to your help, to code is actually being executed :slight_smile:

Thanks!

Using backticks or tilfes shouldn’t matter, and it will show all files of that habit folder you specified. In other words it is best to have a dedicated folder for the habits you’re tracking. In addition remember to specify the full path from the root of your vault to that folder.

I have further refined my previous attempts (seen above at this). I didn’t want to distract this conversation too much so I started a new thread: Adding 'it has been x days since you last did y' to daily notes

Basically, check a box each day (or don’t) and see ‘it has been x days since last y’ on your daily notes.