2023-05-13 Dieting with Org mode

Some time ago I started to weigh myself every day. Some people argue that this is a bad idea and you should weigh yourself once per week to avoid worrying about random fluctuations. I disagree – the way not to worry about random fluctuations is to look at moving averages instead, Hacker's Diet-style. Note: I do not necessarily endorse Hacker’s Diet as a whole, but the moving average approach is certainly a good one. For the sake of simplicity, I am going to use simple (unweighted) moving averages over one week.

I want to keep my weight data in an Org mode table (obviously). The first step is to create a capture template:

("W"
 "Weight"
 table-line
 (file+olp "/path/file.org" "Weight" "Data")
 "| %U | %? | "
 :unnarrowed t)

The next step is adding a column with a simple moving average.

| Date+time              | Weight |   MA |
|------------------------+--------+------|
| [2023-04-30 Sun 19:23] |   66.1 |    0 |
| [2023-05-01 Mon 05:13] |   66.2 |    0 |
| [2023-05-04 Thu 04:56] |   66.0 |    0 |
| [2023-05-05 Fri 05:08] |   65.8 |    0 |
| [2023-05-06 Sat 19:23] |   66.0 |    0 |
| [2023-05-07 Sun 05:13] |   65.9 |    0 |
| [2023-05-10 Wed 04:56] |   65.6 | 65.9 |
| [2023-05-12 Fri 05:15] |   65.7 | 65.9 |
| [2023-05-13 Sat 05:07] |   65.8 | 65.8 |
#+TBLFM: $3=vmean(@-6$-1..@0$-1);%.1f::@2$3=0::@3$3=0::@4$3=0::@5$3=0::@6$3=0::@7$3=0

(The data are in kilograms – this is Europe! – and completely fictitious, by the way.) Notice how I apparently forgot to weigh myself on last Thursday. I still do use a moving average over 7 last entries. While it is probably technically incorrect, I don’t care about it – for the past 2 months, there were only two days I didn’t weigh myself, for example, so I can live with data which is skewed just a tiny little bit.

By the way, the zeros in the first six data rows are there because Org table formulas cannot refer to places outside the table (and calculating the mean of 6 numbers and the word Weight doesn’t make sense, either). Of course, I could add cell formulas to put the averages of less than 7 rows in those cells, but why would I do that? As I said, I want to keep thing simple, and since I’ve been weighing myself (almost) every day for more than half a year now, I really don’t care about those first 6 datapoints.

Now I decided it would be great if I could add org-table-recalculate to some hook so that the table could be recalculated every time I capture my weight – but it turns out that Org mode already does that! Now that’s a pleasant surprise!

The last thing I’d like to have is to tell Beeminder that I weighed myself whenever I use Org capture to put a row in my table. Since Org 9.6, I can add several hooks to the capture template (which is great!) – previously I would have to put some function into org-capture-.*-finalize-hook and detect which capture template was used. So, let’s add this to my template:

:prepare-finalize (lambda ()
  (beeminder-submit-datapoint
    "weighing"
    1
    (beeminder-default-comment
      (time-to-seconds (beeminder-current-time)))))

And this is it for today. I’ve been using this code for several weeks now and it works very well for me. One more repetitive task automated!

CategoryEnglish, CategoryBlog, CategoryEmacs