More tolerant weight recording

You may remember the Weight Today shortcut I wrote about last month. I run it from my iPhone every morning to record my weight in both the Health app and a CSV file I use for graphing my progress (or lack thereof). It’s a simple shortcut, and yesterday it proved to be too simple.

I made a typo when entering my weight and tapped the Done button before I could stop myself. I opened up a text editor to fix the entry in the CSV file, but got distracted and forgot to fix the entry in Health. Not a big deal, but later that day I saw that a 3-mile walk had, according to my watch, burned only 1 calorie. I complained on Mastodon about the watch screwing up, and this morning Juande Santander-Vela (without knowing of my weight recording error) correctly deduced the cause:

maybe you’ve recorded a new weight and it is too small? I remember having had the opposite problem, where merely moving incurred a lot of calories, and it was because 80,2 kg had been recorded as 802 kg…

Upon reading this, I immediately opened the Health app and saw that it had my weight as 1.4 lbs. That explained the minuscule calorie count.

Shortly thereafter, April had a suggestion:

Can you incorporate some logic in your Shortcut to check for a minimum weight to avoid this?

Great idea. I’ll collect the most recent weight, compare it to the value just entered, and if the difference between the two is too much, I’ll ask for the weight again. I’ll put a while loop—or whatever Shortcuts calls a while loop—into the shortcut and that’ll take care of the problem.

Except I couldn’t find anything like a while loop in Shortcuts. I asked on Mastodon if I’d missed something, and the answer was no. Shortcuts can loop a specific number of times and it can loop through the elements of a list, but it can’t loop until a condition is met. Two workarounds were suggested:

  1. Use a Repeat loop with an unreasonably high count and stop the shortcut when the condition is met. This was suggested both by Tom and by Ian Bjorhovde,1 but I couldn’t bring myself to do it this way. Too ugly.
  2. Use recursion. This came from Leon Cowle, and that’s what I went with. Recursion seems kind of highfalutin for Shortcuts, and rewriting Weight Today that way made me think I was doing an exercise in SICP.

Here’s the new, more tolerant version of Weight Today:

StepActionComment
1 Weight Today Step 01 Get today’s date.
2 Weight Today Step 02 Format it as yyyy-mm-dd.
3 Weight Today Step 03 Get all the weights recorded in Health over the past 7 days, put them in reverse chronological order, and limit the list to the most recent.
4 Weight Today Step 04 Get the numerical value of the most recent weight.
5 Weight Today Step 05 Ask the user for today’s weight.
6 Weight Today Step 06 Calculate the difference (absolute value) between today’s weight and the most recent.
7 Weight Today Step 07 If the difference is more than 3…
8 Weight Today Step 08 Rerun the shortcut
9 Weight Today Step 09 If the difference is 3 or less…
10 Weight Today Step 10 Log today’s weight into the Health app.
11 Weight Today Step 11 Make a new CSV line.
12 Weight Today Step 12 Add the line to the CSV file.
13 Weight Today Step 13 Done.

As you can see, I chose 3 lbs as the cutoff for a realistic weight change. Anything more than that and I’m asked to try again. In the 6 months or so that I’ve been tracking my weight, I’ve never had a change of more than 2 lbs, so 3 lbs seemed like a decent choice. Of course, this doesn’t prevent all typos, just excessive ones.

I think the comments explain the shortcut pretty well. If you want to use something like it, but without the CSV stuff, just delete Steps 11 and 12. One last thing: I learned that the fabs function used in Step 6 was available in Shortcuts through a Reddit post by gluebyte.


  1. Whose uncle (I think it was uncle) Reidar Bjorhovde was a structural engineering researcher of some renown.