Hi, I’m new to Obsidian and I’ve never learned to program. So far I’ve been inspired by bits of code I found on the Obsidian Super Community forum. If I am asking your help today, it is because I have not found a solution to my problem and I would be delighted if one of you could help me.
What I’d like to do:
1. Count the number of tasks done each day as well as the number of tasks created, and display the numbers on the daily note via Dataview plugin.
exemple in the daily note : #NOTD:2#NOTC:3
2. Monitor the evolution of its numbers in a chart on the weekly note with Tracker plugin.
exemple in the weekly note : (chart of the tasks created and done each day of the week)
The problem:
Here are the formulas I use to count the number of tasks created and done each day. This formula displays the right number but the Tracker plugin I use for the chart does not recognize the formula as a number. (note: my tasks are individual notes with the task in it)
I had a very similar issue. My solution was to use the API of the community plugin MetaEdit to set the fields in my daily note rather than having dataview do it directly. So the fields themselves were just numbers and elsewhere on the page I had a dataviewjs code block which used MetaEdit’s update function to update them with the values.
Hi @scholarInTraining, thank you for your answer! I have never used meta edit yet but your solution is interesting. Would you have some of the code you used to update the fields please?
Then here is the part of updateTodayFields.js where I first count finished tasks on the page that I passed as input. Then I use metaedit to update a DV inline field on that page that currently reads Tasks Done Today:: 0. I have my dataview settings for automatic view refreshing at an interval of every 30 seconds, so if I check off a task on my daily notes page I should see the count increase on that day within 30 seconds. (The default refreshing rate is much faster, but that was causing annoying screen flickering for me, so I reduced it.)
You’ll probably want to change the calculation of howMany to match your very nice queries above. Just note that if you change what you pass in as the second argument to dv.view then the value of input will change in the script file and that is used both for calculation and in the last line of code to figure out which page’s field to update!
Good luck!
Thank you very much for this information, I tried to follow your advice but I still have some small problems. I try to create two variables in the dataviewjs block and then send them to the updateTodayFields function that would update them but it doesn’t work. I’m sorry to bother you with this.
Very nice! You just want to change slightly the format of your call to dv.view. When sending it multiple arguments, it wants them inside an object. Also, is the file with the fields that you want to update dv.current() in your dataviewjs block? If not, you’ll want to change.
Does this make sense? The dataview documentation is really sparse here! Also, if anything doesn’t work, you can add lines to console.log(...) your different variables in either portion of your code to make sure everything is what you expect.
Yes, thanks @scholarInTraining it makes sense. I’ve put your advice into practice but it still doesn’t work. I looked in the console with (CTRL + SHIFT + I ) but I don’t see any error message. How can I use consol.log() to find the cause of the problem?
No error messages is definitely a good thing! The first thing I would do would be in your dataviewjs code, before you even call out to your script with dv.view, console.log("dataviewjs noct: " + noct); and the same for notd. This’ll double-check that your numbers are being calculated correctly. Then inside the JS script I’d put in some similar log statements checking the stuff inside input, including the path. Hopefully, that’ll give you more information about what is happening!
Hi @scholarInTraining, I tried to combine the java script file with the dataview block and it works! Is there a particular reason why the js file is preferred over the two code conbination ?
Well I have no idea why you were getting those results but I am glad you got something working! I have it in a separate file in case I decide later that I want to do any additional calculation then I won’t have to go update the code in every daily note. Not so relevant for counting tasks done but one of the other fields I am updating I am less sure of my code for counting it. I think you are quite right that you can just go with a dataviewjs block in your case. Sorry for not suggesting that: I mixed your question up with another at first when I was sharing my code.