Return Random Daily Gratitude from Daily Note Pages

Things I have tried

  • Creating a simple DataView list with one line selected at random from a specified inline field, but DataView does not appear to have a “random” function.
  • Using Dice Roller plugin to pull random lines, but it doesn’t read DataView tables when generating results.

What I’m trying to do

Every day, in my Daily Notes Page, I write down two things for which I am grateful using the custom field "Gratitude:: " e.g.

Gratitude:: I am grateful for the Obsidian forum community.
Gratitude:: I am also grateful for the hard work others have put in to making Obsidian what it is today.

What I would like to do, as a pick-me-up, is put a random "Gratitude:: " block in a note that serves as my homepage so I can have regular reminders to be grateful, selected at random from my own statements. When I open the page, the block might read “Remember, I am grateful for the Obsidian forum community” one day, but “Remember, I am also grateful for the hard work others have put in to make Obsidian what it is today.” another.

If there’s no way to get DataView to pull a random result, is there a way to export a DataView list or table to a new note so I could use the Dice Roller plugin?

I too am seeking similar functionality, did you manage to satisfy your search for a method to accomplish your requirements? I’d be grateful if you’d share as I am new to Obsidian right now but love it’s versatility.

Ooh, I feel like I’m getting closer! I found a dataviewjs snippet that I was able to modify to pull one random entry from a set of Daily Notes containing Gratitude::, but I can only get it to show me the link for the whole file rather than the specific line in question. So close!

I’ll post what I’ve got so far below, but it still isn’t a solution to the problem I’m having:

const limit = 1; 
const notes = dv.pages('"Journal"') 
	.where(p => "gratitude" in p)
	.sort(() => 0.5 - Math.random()) 
	.slice (0, limit) 
	.map (note => note.file.link); 
dv.list(notes);

If I knew more about javascript, I might have better luck, or if I knew more about dataview. As it is, I just keep logging my daily gratitude in the hopes that someday I’ll be able to look back on them.

Okay, here’s a totally functional solution that @Craig worked out, modified with the inline field Gratitude:: I’m using. See if it helps!

```dataviewjs
// List of gratitudes
let gratitudes = [];

// Extract gratitudes from pages that have them
dv.pages()
	.where(page => page.gratitude)
	.forEach(page => {
		dv.array(page.gratitude)
			.forEach(gratitude => {gratitudes.push(gratitude);})});

let greeting = gratitudes[Math.floor(Math.random()*gratitudes.length)] 

dv.paragraph(greeting);```

Now, because I’m greedy, I wish I could append a link to the source note each entry comes from (so it prints something like "I am grateful for the Obsidian forum community [[2022-05-18]]) but people have been so helpful that I’m not going to bother them with problems I should be solving myself.

Good luck with your quest!

Hahaha, I’m so glad my suggestion was helpful! :smile:

Adding the source page doesn’t take much extra code; instead of storing just the message, we store an object that contains both the message and the page it came from:

```dataviewjs
// List of gratitudes
let gratitudes = [];

// Extract gratitudes from pages that have them
dv.pages()
	.where(page => page.gratitude)
	.forEach(page => {
		dv.array(page.gratitude)
			.forEach(gratitude => {
				gratitudes.push({
					message: gratitude,
					page: page
				});
				})});

let greeting = gratitudes[Math.floor(Math.random()*gratitudes.length)] 

dv.paragraph(greeting.message + " - " + greeting.page.file.link);
```

The output looks like this:

image

2 Likes

It works like a charm, and it’s so good! Thank you very much!

I actually use two writing exercise as part of my daily journaling practice, one is to write down two things I’m grateful for at the beginning of the day and the other is to write down something awesome that I noticed by the end of the day. As much as I love gratitude journals and inspirational quotes, I felt like they would be much more meaningful coming from my own life, even if I’m not as eloquent as others who might write such prompts.

In any case, I was able to modify the code you provided (I think the page.link.file was where I was getting stuck) and I couldn’t be happier with the results. Now, if I want to revisit something awesome or a particular moment of gratitude, the link is right there! I’m sharing the snippet I use below in case other people find themselves wanting to do something similar.

```dataviewjs
// List of gratitudes
let gratitudes = [];

// Extract gratitudes from pages that have them
dv.pages()
	.where(page => page.gratitude)
	.forEach(page => {
		dv.array(page.gratitude)
			.forEach(gratitude => {
				gratitudes.push({
					message: gratitude,
					page: page
				});
				})});

// List of awesome things
let somethingawesomes = [];
// Extract awesome things from pages that have them
dv.pages()
	.where(page => page.somethingawesome)
	.forEach(page => {
		dv.array(page.somethingawesome)
			.forEach(somethingawesome => {
				somethingawesomes.push({
					message: somethingawesome,
					page: page
				});
				})});

let gratitudegreeting = gratitudes[Math.floor(Math.random()*gratitudes.length)] 
let awesomegreeting = somethingawesomes[Math.floor(Math.random()*somethingawesomes.length)]

dv.paragraph("*A random awesome thing:* " + awesomegreeting.message + " (" + awesomegreeting.page.file.link + ")" + "<br>" + "*Practice gratitude:* " + gratitudegreeting.message + " (" + gratitudegreeting.page.file.link + ")");
2 Likes

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.