pushing your myfitnesspal data to MQTT

MyFitnessPal is a great online service we are using to track what we eat. It’s well integrated into our daily routine – it works!

Unfortunately MyFitnessPal is not well set-up to interface 3rd party applications with it. In fact it appears they are actively trying to make it harder for externals to utilize the data there.

To access your data there’s an open source project called “python-myfitnesspal” which allows you to interface with MyFitnessPal from the command line. This project uses web-scraping to extract the information from the website and will break everytime MyFitnessPal is changing the design/layout.

Since the output for this would be command line text output it is not of great use for a standardized system. What is needed is to have the data sent in a re-useable way into the automation system.

This is why I wrote the additional tool “myfitnesspal2mqtt“. It takes the output provided by python-myfitnesspal and sends it to an MQTT topic. The message then can be decoded, for example with NodeRed, and further processed.

As you can see in the image above I am taking the MQTT message coming from myfitnesspal2mqtt and decoding it with a bit of javascript and outputting it back to MQTT.

var complete = {};
var sodium = {};
var carbohydrates = {};
var calories = {};
var daydate = {};
var fat = {};
var sugar = {};
var protein = {};

var weight = {};
var bodyfat = {};

var goalsodium = {};
var goalcarbohydrates = {};
var goalcalories = {};
var goalfat = {};
var goalsugar = {};
var goalprotein = {};

var caloriesdiff = {};

var ttopic = msg.topic.toLowerCase();

var firstobject = Object.keys(msg.payload)[0];

complete.payload = msg.payload[firstobject].complete;
complete.topic = ttopic+'/complete';

sodium.payload = msg.payload[firstobject].totals.sodium;
sodium.topic = ttopic+'/total/sodium';
carbohydrates.payload = msg.payload[firstobject].totals.carbohydrates;
carbohydrates.topic = ttopic+'/total/carbohydrates';
calories.payload = msg.payload[firstobject].totals.calories;
calories.topic = ttopic+'/total/calories';
fat.payload = msg.payload[firstobject].totals.fat;
fat.topic = ttopic+'/total/fat';
sugar.payload = msg.payload[firstobject].totals.sugar;
sugar.topic = ttopic+'/total/sugar';
protein.payload = msg.payload[firstobject].totals.protein;
protein.topic = ttopic+'/total/protein';

weight.payload = msg.payload[firstobject].measurements.weight;
weight.topic = ttopic+'/measurement/weight';
bodyfat.payload = msg.payload[firstobject].measurements.bodyfat;
bodyfat.topic = ttopic+'/measurement/bodyfat';

goalsodium.payload = msg.payload[firstobject].goals.sodium;
goalsodium.topic = ttopic+'/goal/sodium';
goalcarbohydrates.payload = msg.payload[firstobject].goals.carbohydrates;
goalcarbohydrates.topic = ttopic+'/goal/carbohydrates';
goalcalories.payload = msg.payload[firstobject].goals.calories;
goalcalories.topic = ttopic+'/goal/calories';
goalfat.payload = msg.payload[firstobject].goals.fat;
goalfat.topic = ttopic+'/goal/fat';
goalsugar.payload = msg.payload[firstobject].goals.sugar;
goalsugar.topic = ttopic+'/goal/sugar';
goalprotein.payload = msg.payload[firstobject].goals.protein;
goalprotein.topic = ttopic+'/goal/protein';

caloriesdiff.payload = msg.payload[firstobject].goals.calories - msg.payload[firstobject].totals.calories;
caloriesdiff.topic = ttopic+'/caloriedeficit';

daydate.payload = firstobject;
daydate.topic = ttopic+"/date";

return [complete, sodium, carbohydrates, calories, fat, sugar, protein, weight, bodyfat, goalsodium, goalcarbohydrates, goalcalories, goalfat, goalsugar, goalprotein, daydate, caloriesdiff];

In the end it expands into a multitude of topics with one piece of information per MQTT topic.

And with just that every time the script is run (which I do in a docker container and with a cronjob) the whole lot of pieces of information about nutrition and health stats are being pushed and stored in the home automation system.

This way they are of course also available to the home automation system to do things with it.

Like locking the fridge.

health: tracking food

It had been mentioned before: regarding my own health until 2015 I did not have any structure and understanding when it came to food, sports, weight and all that is connected.

I used to be around 147kg (324lbs) when I made the decision to change that. Less than 8 months later I reached the weight that is considered “normal weight” for my body height.

Maybe more important than losing weight and going from class III obesity   to normal is to keep in the range offering the biggest health benefits.

2015 till today

Apart from sport and being more active the key to managing weight in general for me was to understand and keep learning about food from many different perspectives.

Like in good science to learn more and make progress you got to listen and take well notes. Some things only can be understood when there’s enough historical data available. Not only the amount of time logged but also what is being logged is quite important.

In a normal week I am used to one-meal-a-day which I am preparing myself together with my wife. We eat in the evening – this makes things so much easier as there’s one time and space where everything regarding food comes together during routine days.

Also this routine works well in the long run. If we only got 10 minutes to prepare we will be able to hit the nutrition targets with either much or not-so-much quantities of food. In the last 4 years we practiced and played and created recipes for all situations you can think of. In a way we have made our simple-and-healthy-recipes the fall-back position we are using when we otherwise would have eaten something unhealthy.

Most importantly: We have tracked everything we cooked and ate. On a gram scale. I even built our own wifi enabled and calibrated kitchen scales.

Where do we track? There are so many options but since 2015 we stick to MyFitnessPal.

4+ years constant tracking

We are still happy using this as the base for tracking as the app is bearable even on Android and the food database that it uses is offering a good-enough data detail level.

So after more than 4 years of doing this a lot of data as come together. As I am doing it in sync with my wife a lot of things happened…

We have not become vegans. We eat meat still and we still like it. It’s just that the quality of meat we eat has gotten much better and with this the number of times we eat meat have reduced to maybe once a week at most.

We have started to eat things and experiment cooking with ingredients we did not know a year ago. While we keep adding ingredients all the time we find that you can optimize and gain so much joy from just jumping in head first into new tastes and recipes.

We’ve developed a “body-feel”. Apart from the taste buds changing completely over time I could not have thought of how much food influences how you feel. Different nutritious values lead to very different feels afterwards. I would go as far to say that most of the headaches I very frequently had while being overweight could have been traced back to what I’ve eaten just before.

So what now? We will keep tracking. Maybe not on a cloud service but on our self-hosted service. Maybe you got a good hint towards such self-hosted solutions to enter and track nutrition over time.

Apple Health challenges are broken

We are using Apples smartwatch to measure some health stats during our workouts. And Apple Watch is doing a great job at that.

With all that polish one would expect better from what Apple has to offer in the software department…

Apple Watch has monthly challenges that get automatically generated from previous measurements. But seeing that an already much above average activities number would have to be doubled to complete the challenge is absurd. To a degree where challenges are arguably health risks…

active noise cancellation does not suck on your eardrums

If you ever traveled on a train or plane with good active noise cancellation headphones you might agree how much more pleasant the trip was with much less noise reaching your ears.

When I tried active noise cancellation for the first time I had that weird sensation as if the pressure around suddenly changed. Like being in a very fast elevator or going for a quick dive. It felt weird but luckily it went away and the aww of joy replaced it. Quietness. Bliss.

Now there seem to be people for whom that feeling won’t go away. They get headaches and cannot stand the feeling when using active noise cancellation.

I’ve never had any explanation to this phenomena – until now. I ran across an article on SoundStage describing that in fact the feeling is not caused by actual changes of pressure but…

According to the engineer, eardrum suck, while it feels like a quick change in pressure, is psychosomatic. “There’s no actual pressure change. It’s caused by a disruption in the balance of sound you’re used to hearing,” he explained. 

eardrum suck – the mystery solved

Aha! The brain gets confused by signals reaching your ears that naturally would not exists. Those signals make no sense so the brain tries to make sense of it. And voilá something is sucking your ear drum!