---
title: "Using Obsidian for meal planning"
date: 2023-11-30T21:26:43
slug: using-obsidian-for-meal-planning

---
In my [App defaults post](https://lewisdale.dev/post/app-defaults), I mentioned I use [Obsidian](https://obsidian.md) for various tasks, one of them being meal planning, so I thought I'd share how I actually manage that.

## Requirements

Obsidian, obviously.

Secondly, it relies on having the [Dataview](https://github.com/blacksmithgu/obsidian-dataview) plugin installed. Dataview is a really neat plugin that provides a simple scripting language over your Obsidian vault, that you can use to query and display data from your files.

## Storing the data

My Obsidian vault is also where I share my recipes. Each recipe is stored under the `recipes/` directory, and when I add a new one I add tags that I can then query against. For example, my recipe for a Broccoli and Cheddar Soup is tagged with `#easy` and `#lunch`, because it's easy to make and I can take it with me for lunch. Truly, revolutionary stuff.

## Querying the data

I'll admit, I didn't come up with this query myself, instead I adapted it from [this Reddit comment](https://www.reddit.com/r/ObsidianMD/comments/zoyviu/comment/j0rw5zr). Here's the full query I use for generating my weekly meal plan:

```js
const tags = ["#recipe", "#lunch", "#easy"]

const recipes = dv.pages(tags.join(' and '));
const maxItems = Math.min(recipes.length, 2);
const randomNum = DateTime.now().toFormat("WW") % recipes.length;

const items = Array(maxItems).fill().map((_, i) => recipes[((randomNum + i) % recipes.length)])

dv.list(items.map((item) => (item.file.link)))
```

It's fairly straightforward. I query for every item that's tagged with `#recipe`, `#easy` and `#lunch`. Then, I pick a "random" number (it's not random - it's the modulo of the number of recipes by the week of the year). As the Reddit comment points out, this way I have repeatable results for each week of the year - otherwise my meal plan would change every time I opened it.

Then, I pick use that number as an index to select at most two of the recipes that match the tags, and then list them. Easy!

![Screenshot showing a heading that says "Meal Plan". The subheading lists two meals under "Lunch": Stir-fried chicken & vegetables, and Lemon & Garlic Butter Shrimp](./src/images/Screenshot-2023-11-30-at-21.24.16.png)

And that's more-or-less it. I'm only generating my lunch meal plan at the minute - I have one for dinner but I've not got many recipes, so unless I want to eat the same thing every night until the end of time it's best to ignore it.

