Category Archives: geek

Using Emacs Org Mode tables to calculate doses to buy

I got tired of manually calculating how many I needed to buy based on a daily protocol and how many I had in stock, so I wrote a little bit of Emacs Lisp to figure it out. You can specify the type, daily dose, start and end dates (inclusive; defaults to the last specified date if blank), and how many you have in stock.

First, define a table of this form, and give it a name.

#+NAME: input
| Type         | Per day |      Start |        End | Stock |
| Medication A |       2 | 2015-06-09 | 2015-06-16 |     5 |
| Medication B |       1 |            |            |     0 |
| Medication C |     0.1 | 2015-06-12 | 2015-06-16 |   0.2 |
Type Per day Start End Stock
Medication A 2 2015-06-09 2015-06-16 5
Medication B 1 0
Medication C 0.1 2015-06-12 2015-06-16 0.2

To call the code from the bottom of this post, use something like:

#+CALL: calculate-meds-needed(meds=input) :hlines yes :colnames yes
Type Total In stock Needed
Medication A 16 5 11
Medication B 8 0 8
Medication C 0.5 0.2 1

Here’s the code that processes it:

#+name: calculate-meds-needed :var meds=meds :colnames yes :hlines yes
#+begin_src emacs-lisp
(let (start end)
   (list (list "Type" "Total" "In stock" "Needed"))
   (list 'hline)
   (sort (delq nil (mapcar
                    (lambda (row)
                      (unless (or (eq row 'hline) (string= (elt row 0) "Type"))
                        (let (total)
                          (setq start (if (string< "" (elt row 3)) (elt row 3) start)
                                end (if (string< "" (elt row 2)) (elt row 2) end)
                                total (* (elt row 1)
                                         (- (calendar-absolute-from-gregorian (org-date-to-gregorian start))
                                            (calendar-absolute-from-gregorian (org-date-to-gregorian end))
                           (elt row 0)
                           (elt row 4)
                           (max 0 (ceiling (- total (elt row 4))))))))
                    meds)) (lambda (a b) (string< (car a) (car b))))))

Adding calculations based on time to the Org Agenda clock report

Duplicating this answer on my blog in case StackOverflow goes away. =)

Leo asked:

I’m trying to make the Agenda Clockreport show how many pomodoros I’ve invested in a task. A Pomodoro is 25 minutes. For example, 1:15 hours of work is 3 pomodoros.

I’m trying to customize org-agenda-clockreport-paramater-plist, and I would like to extract “Time” and convert it to a pomodoro. I.e., (time in minutes / 25) = pomodoro.

I wrote:

This will create a column in your clocktable report that sums the hours from columns 3 and 4, and then another column that shows you the round number of pomodoros that took up.

(setq org-agenda-clockreport-parameter-plist
      '(:link t :maxlevel 2 :formula "$5=$3+$4;t::$6=ceil($5*60/25);N"))

If you don’t want in-between columns, here’s a totally hackish approach:

(defun my/org-minutes-to-clocksum-string (m)
  "Format number of minutes as a clocksum string.
Shows the number of 25-minute pomodoros."
  (format "%dp" (ceiling (/ m 25))))
(fset 'org-minutes-to-clocksum-string 'my/org-minutes-to-clocksum-string)

Alternatively, you can use :formatter, but the formatting function looks very long and annoying to change.

Leo eventually configured it with:

(setq org-agenda-clockreport-parameter-plist
 '(:fileskip0 t :link t :maxlevel 2 :formula "$5=($3+$4)*(60/25);t"))

(He didn’t mind the decimals, I guess! =) )

Recreating and enhancing my tracking interface by using Tasker and Javascript

I got tired of setting up Tasker scripts by tapping them into my phone, so I looked into how to create Tasker interfaces using Javascript. First, I created a folder in Dropbox, and I used Dropsync to synchronize it with my phone. Then I created a simple test.html in that folder. I created a Tasker scene with a WebView that loaded the file. Then I started digging into how I can perform tasks, load applications, and send intents to Evernote so that I can create notes with pre-filled text. I really liked being able to reorder items and create additional screens using Emacs instead of Tasker’s interface.

Here’s my code at the moment. It relies on other Tasker tasks I’ve already created, so it’s not a standalone example you can use right off the bat. Still, it might be useful for ideas.


        <title>Sacha's personal tracking interface</title>
        <style type="text/css">
         button { padding: 20px; font-size: large; width: 45%; display: inline-block  }
        <script type="text/javascript" src=""></script>
        <div id="feedback"></div>
        <!-- For making it easy to track things -->
        <div class="screen" id="main-screen">
        <button class="note">Do</button>
        <button class="note">Think</button>
        <button class="note">Note</button>
        <button class="note">Journal</button>
        <button class="switch-screen" data-screen="track-screen">Track</button>
        <button class="switch-screen" data-screen="play-screen">Play</button>
        <button class="switch-screen" data-screen="eat-screen">Eat</button>
        <button class="switch-screen" data-screen="energy-screen">Energy</button>
        <button id="reload">Reload</button>
        <div class="screen" id="play-screen">
            <button class="play">Persona 3</button>
            <button class="play">Ni No Kuni</button>
            <button class="play">Hobbit</button>
            <button class="switch-screen"
        <div class="screen" id="energy-screen">
            <button class="energy">5</button><br />
            <button class="energy">4</button><br />
            <button class="energy">3</button><br />
            <button class="energy">2</button><br />
            <button class="energy">1</button><br />
            <button class="switch-screen"
        <div class="screen" id="eat-screen">
            <button class="eat">Breakfast</button>
            <button class="eat">Lunch</button>
            <button class="eat">Dinner</button>
            <button class="switch-screen"
        <div class="screen" id="track-screen">
            <button class="update-qa">Routines</button>
            <button class="update-qa">Subway</button>
            <button class="update-qa">Coding</button>
            <button class="update-qa">E1 Gen</button>
            <button class="update-qa">Drawing</button>
            <button class="update-qa">Cook</button>
            <button class="update-qa">Kitchen</button>
            <button class="update-qa">Tidy</button>
            <button class="update-qa">Relax</button>
            <button class="update-qa">Family</button>
            <button class="update-qa">Walk Other</button>
            <button class="update-qa">Nonfiction</button>
            <button class="update-qa">Laundry</button>
            <button class="update-qa">Sleep</button>
            <button id="goToWeb">Web</button>
            <button class="switch-screen" data-screen="main-screen">Back</button>
         function updateQuantifiedAwesome(category) {
             performTask('Update QA', null, category);

         function showFeedback(s) {
         function switchScreen(s) {
             $('#' + s).show();

         $('.switch-screen').click(function() {
         function createEvernote(title, body) {
             sendIntent('com.evernote.action.CREATE_NEW_NOTE', 'activity',
                        '', '', 'none', '', '',
                        ['android.intent.extra.TITLE:' + (title || ''),
                         'android.intent.extra.TEXT:' + (body || '')]);
         $('.note').click(function() {
         $('.energy').click(function() {
             createEvernote('Energy', 'Energy ' + $(this).text() + ' ');
         $('#reload').click(function() {
             performTask('Reload Test');
         $('.update-qa').click(function() {
             updateQuantifiedAwesome($(this).attr('data-cat') || $(this).text());
             hideScene('Test View');
         $('#goToWeb').click(function() {
         $('.eat').click(function() {
         $('.play').click(function() {
             performTask('Play', null, $(this).text());



You can find the latest version at

Thinking about simplifying capture on my phone

I’ve been thinking about temporary information: things like where I put down something I was holding, the task I’m working on just in case I get interrupted, thoughts that I want to explore later on.

The seeds of a good system are there, if I learn how to use them more effectively. I usually have my phone handy. Evernote can record audio, pictures, or text, and the creation date is an automatic timestamp. I can export the notes and process them using Emacs. Google Now’s “Note to self” command can create a note in Evernote, or I can tap the Create Note icon.

How can I improve how I use these tools?

If I get used to starting my note lines with a special keyword, then it’ll be easier for me to extract those lines and process them. For example:

DO Actions to add to my to-do list
THINK Thoughts to explore
PLACE Putting things down

If I add custom commands to Google Now, or make Google Now the fallback for another command line, then I can use that for my tracking system as well. AutoShare and AutoVoice might be handy. I should probably learn how to get Tasker working with Javascript, too. Alternatively, I can use Evernote’s e-mail interface, although there might be a slight delay if I’m offline.

If I create a custom single-click interface that can start the note with a specified keyword, that would be even better.

I could also use a more systematic review process. For example:

  • All THINK items can be automatically added to the end of my as their own headings.

  • All DO items can be added to my uncategorized tasks.

Okay, let’s start by figuring out Javascript and Tasker, since that will make it easier for me to write actions that take advantage of intents.

First step is to save the JS library template from Tasker. The file is stored in /storage/emulated/0/Tasker/meta/tasker.js. Okay, I’ve created a Tasker scene that has a WebView component that loads the file that I synchronized with Dropsync.

The next step is to simplify development so that I can try things quickly. I want to be able to sync and reload my WebView scene by tapping a button. The Dropsync Tasker action returns immediately, so maybe I’ll just add a wait to it.

Hmm, maybe a good path might be:

  1. Set up easy sync/reload, so I can try things out quickly.
  2. Include JQuery.
  3. Run tasks.
  4. Launch apps.
  5. Send intents.
  6. Convert my current tracking menu to this format.
  7. Add buttons for creating notes (either e-mail-based or intent-based):
    • Do
    • Think
    • Place
  8. Add a command-line. Compare using it vs. using buttons.

On a related note, what kinds of things would I like my phone to be smart enough to do?

  • If I’m at home and I’m calling my cellphone from our home phone, set the ring volume to maximum, since that means I’m trying to find it.
  • If I say that I’m at a social event, ask me who I’m spending time with. Track that. Create a note with the person’s name as the title and “social” as the tag.
  • If I’m going to sleep, track that, then start tracking in Sleep as Android.
  • If I’m going to play a game, track that, then ask me what I’m going to do afterwards and how long I want to play. Load hints for the game (if I want). After the specified time, make a sound and remind me of what I was going to do.
  • If I’m going to read a book, show me the list of my checked-out books and let me pick one of them. Track that, then create a note with the title and author so that I can take notes.
  • NFC opportunities:
    • If I scan at the door, show me a menu: walk, subway, groceries, family, bike.
    • If I scan in at the kitchen table, track it as breakfast / lunch / dinner (as appropriate), then launch MyFitnessPal.
    • If I scan in at the table in front of the TV, show me a menu: relax, nonfiction, fiction, games.
    • If I scan in at my bedside table, treat it as going to sleep.

Mmm… Little things to tweak. =)

A deeper dive into absent-mindedness and misplacing things

I haven’t misplaced anything today, but I know I will at some point. This week? This month? Definitely this year, and probably more and more as the years go by. It got me thinking about misplacing things, and what I might be able to do about that.

When or why do I misplace things?

  • Active
    • Putting something down in one of many frequent places
    • Putting something down in an infrequent place
    • Getting distracted half-way and putting something down somewhere I don’t remember
    • Putting something down because my hands are full and I need to pick up something else
    • Putting something in a place that’s similar to but not the same as the place it should be, and not catching the mistake
    • Putting something somewhere near it should be instead of where it should be because that place is occupied or inaccessible
    • Putting something away for the long term, then forgetting where it is
    • Putting something away, then forgetting whether I have it or not
    • Putting aside something in progress or waiting for something else, then forgetting where it is or when I need to get back to it (ex: mismatched socks)
    • Shuffling things into similar things (ex: papers)
    • Making a mental note of where I put something, but not remembering it well enough
  • Passive
    • Someone moving or dislodging something from where I expect it to be
    • Forgetting to check for things that have accidentally fallen or been left behind (ex: gloves, scarves, things in pockets)
    • Leaving things in an opaque container for convenience, and then not taking them out and putting them away (ex: gloves)
    • Things falling out of pockets or through linings, un-noticed
    • Familiar tasks in familiar environments lead to automatic thinking and reduced attention
    • Forgetting to prepare or take something
    • Gaps when retracing steps
  • Retrieval
    • Skipping over something because something else is covering it or obstructing my view
    • Looking at something but not recognizing it
    • Limiting my field of view unnecessarily
    • Misremembering things that are similar to things I remember getting rid of, so I don’t look for them
    • Not searching in a systematic manner
    • Having a false memory of putting something away in a different place
      • Confusing with previous memory
      • Confusing plans with reality

What tools and tactics do people use to minimize the hassle of misplacing things?

  • Build automatic habits
    • Have one clearly defined place for each thing, or very few clearly defined places
    • Explicitly encode memories around picking things up or putting things down
      • Looking
      • Mental note
      • Note to self, out loud
      • Text note
      • Audio note, recorded
      • Picture
    • Have a handy holding place for in-between things or miscellaneous things, and review this frequently (ex: bin, belt bag)
  • Reduce retrieval costs
    • Regularly tidy with fresh eyes
    • Make lists of where things are
    • Label containers with their contents (ex: cabinets)
    • Keep things clear and tidy
  • Label
    • Label things so that in case they’re lost, someone might be able to return them to you
    • Offer rewards
  • Reduce the need for the item
    • Replace or supplement often-misplaced identification with always-present information or more frequently used devices (ex: biometrics, keycodes, smartphone)
    • Buy or budget for replacements (ex: pens)
    • Keep extra stock of items in multiple places (ex: pens)
    • Minimize the number of unneeded things you carry, and keep other things in a known place (ex: infrequently-used keys)
    • Eliminate the item entirely
  • Add alerts
    • Track location (ex: smartphones, parking)
    • Add proximity alerts (ex: smartphone-laptop Bluetooth proximity detection, tracking stickers)
  • Fill in gaps
    • Retrace steps
    • Ask someone else who might be able to look with fresh eyes or who might have different memories

When are these tools particularly useful?

From “External and internal memory aids: when and how often do we use them?” (Intons-Peterson and Fournier, 1986):

  • When intervening events may interfere
  • When there’s a long delay between encoding and retrieval
  • When accuracy is important
  • When information is difficult to remember
  • When there’s limited time to remember
  • When you want to avoid the effort of remembering

Based on these thoughts, what can I tweak about my life? Maybe I can pay closer attention to incidents of misplaced things and other action slips over the next few weeks so that I can see where the gaps are….

Building tools for myself: grocery receipt tracking

Today was another good day for writing code. I finally built that quick-feedback receipt item tracker I’ve been thinking about for a while. I’d built a simpler version into Quantified Awesome (not linked from the main interface, since it was very rough), but I found the browser roundtrip too disruptive. Today’s implementation uses Angular for faster responses. For good measure, I’ve got a NodeJS server proxying the requests to either my local development copy of Quantified Awesome or to my production version.

Here’s a screenshot:

2015-05-26 22_21_38-sachachua.com_8080_receipt

On the left side, I have a scanned receipt in Autodesk Sketchbook Pro (or a paper receipt on the side). On the right, my autocompleting tracking interface. It’s not pretty, but it fits what I have in my head. I like the way that typing in the first few characters of the receipt line item is often enough to uniquely identify the receipt item type and retrieve the price history. This means that as I track, I also get a sense of the price trends and what a good sale is.

Aside from keeping track of the prices, I’m also looking forward to analyzing our consumption by category on a more regular basis. I did a few analyses along those lines before (here’s a year of data), but it might be neat to have that kind of feedback on a daily basis. Entering my receipt archive was easy. I ended up typing in the receipts from here to January because it was fun. =)

Next up: fast categorization, some graphing… I’m also looking forward to making a quick price book interface. Hmm, if I dust off that grocery list tool I had started building into Quantified Aweome and I integrate the price book, that might be handy.