Automate my Marathon Training with Huginn

André Claaßen
4 min readFeb 13, 2016

--

Ok, this sounds silly and maybe it is, but I want to use my coding skills to automate repetive things in my life as far as possible. And I also want to participate in a marathon this year.

To prepare me for the marathon, I started a training plan on Strava, a platform for fitness enthusiasts. Strava sends me a daily instruction via email. But I wanted the instructions to appear in my google calendar, where I organize all my efforts through a day. So I started dealing with the open source platform Huginn to gain experience in automating things.

Huginn? What the heck is Huginn?

First at all, Huginn is one of the two ravens from the norse god Odin, which are traveling the world by day and tell Odin about all important things that are happening. Odin, who traded one of eyes to gain wisdom, is hopelessly dependent on his two brave ravens. Maybe the world goes dark when these pets doesn’t exist anymore.

Secondly, Huginn is an Open Source project about agents, which listen to the digital world for you and inform you about things. Think about an open source project which combines Yahoo Pipes with Zapier and IFTTT.

There is a very good introduction for huginn available as a screencast from the main contributer to this project.

How does Huginn work?

The main concept is very simple. Huginn is a host for agents. Agents are little programs which are watching the internet and / or doing things with events. An Event is a result or the input of an agent. All what you do is, select the right Agents for your internet observation and and connect the events with other Agents, which are doing things with the result

Here is a simple Example: Send me daily a event about trends I care about on Twitter .

For this recipe you need two Agents:

  1. One Twitter Agent for observing Twitter and generating Events with results and
  2. one Email Agent for sending me a digest with them via Email

Setting up my WorkFlow in Huginn

So I set up my personal Huginn Server and created my first workflow with three Agents.

  1. An email agent for watching my GMail Inbox for a new Strava Training Plan.
  2. A JavaScript Agent for converting the body of my email to the google calendar entry.
  3. A Google Publisher Agents which stamps the event right into my Google Calendar.

Now I want to explain the setup in detail. Things getting more nerdy

Step 1: Check my Gmail account for a new training plan

I use the IMAP Folder agent to check gmail several times a day for a new mail from Strava. The search condition or filter for my mail is under the condition section. Note, that you can separate your credentials (user/password) from the Configuration.

This agent generates an event payload with the important data of the fetched mail for my Google calendar entry : subject and body.

Step 2 – Create a google calendar entry out of the mail

Now the things become somewhat nasty. The needed information about the training is buried deep in the body of the mail. I needed to extract the information with regular expressions and must create a message structure, which is consumable from the Google Calendar Api. I admit, this stuff is somewhat more complicated

Welcome to the Javascript agent

Huginn has a nice JavaScript agent, a swiss army knife for the more complex requirements.

This code fetches the data out of the body of the email and create a data structure which is consumable from the Google Calendar Publisher Agent. By the way, I love the log statement provided by Huginn. It makes debugging very comfortable.

Step 3 — Create the Google Calendar Entry

I have to admit. The Google Calendar Publisher Agent is a tiny shell about the Google Calendar Api. You have to create a message structure with an agent (usually trigger agent) and the Google Publisher send it with your api token to the google interface:

I needed a long time, to get this configuration working. Gently hint: Please authorize the google service email account for modifying your calendar.

Finally — Every training day I have an event scheduled in my Google Calendar made by Huginn

Important Links

--

--

André Claaßen

Digital Coach with a fable for Technology, Modern Work and the beauty of Life