Loop: Dynamic Carb Absorption

I’m super excited to share Loop’s latest development…it’s a big one! Dynamic Carb Absorption.

So, one of the things that has been most difficult for most new (or little kid) users of Loop app has been how to enter carb absorption times for meals. There’s lots of different strategies I’ve seen employed…breaking meals down into several different carb entries for a single meal, changing carb ratio/ISF settings, or using “fake carbs”. My personal experience was that we had used the default carb absorption times and found we had some significant post-prandial lows due to early high temping.

In the old Loop app, if you entered a longer carb absorption time and had earlier/stronger than expected increase in BGs…Loop would early high temp to cover the “unexpected” spike. Loop also assumed that the later carbs you’d entered were still coming. Basically, Loop was unable to assume that perhaps the carbs were simply acting quicker, and therefore decay them quicker. Conversely the same was true…if you entered a short carb absorption time, but the carbs were actually taking longer, you would likely see some early suspensions and have a BG spike later in the meal.

We figured out that, for us, a default carb absorption time of 90 minutes was working pretty well for our average meals. Fatty and higher carb meals needed some extra attention. We split-bolused a lot and it involved a fair degree of attention to how we entered the carbs (time of entry and carb absorption time) in order to make sure the suggested boluses were also reasonable.

We also had to keep the max basal rate fairly low (about 2.5-3 times the highest basal rate), just in case we had a situation where the carbs absorbed quicker than we’d expected. Didn’t want to let Loop overly correct in those situations.

After a bit of testing and tweaking, we got into a groove…but it wasn’t what I’d call an easy hand-off for a care giver or kid to operate. We still had a lot of “hey mom, how should I bolus for this pizza?” and “Mom, what time do I enter for the carbs?” So, while Anna was good at estimating total carbs for a meal…her actual entry of carbs into Loop app was still difficult by herself.

This difficulty led me to try OpenAPS, especially because of friends who were having troubles with school nurses and caregivers having difficulty in properly entering carbs. OpenAPS was more forgiving of a carb entry and more straight-forward (familiar) for school nurses. They seemed used to “normal” pump bolus wizard entries.

Enter Pete Schwamb. He took on the carb absorption challenge and came up with Dynamic Carb Absorption. You can read more about the idea behind it in Github here. A really good read…highly recommended.

Basically, what Pete’s done is allowed Loop to look at how BGs are responding based on a term called InsulinCounteractionEffect (ICE for short). To quote Pete:

To make an attempt to see how carb absorption is actually progressing, we can take the observed changes in glucose, subtract changes modeled from insulin delivery, and look at the resulting difference, called insulin counteraction effect.

The effects represented by this term are more than just carb effects. It includes exercise, sensitivity changes, and even errors in insulin settings such as basal rate, ISF, etc. However, since the effect of consuming carbohydrates is relatively large, we can still make some useful adjustments to Loop’s carb model by assuming that the effect is mainly carb absorption in the period following recorded meal entries.

What does this mean for us in plain English?  A more forgiving Loop if you get your carb absorption estimate wrong.

Dynamic Carb Absorption version of Loop has been available for development testing since July 1st.  We tried to use it when it was first released, but we were in the midst of trying to cram for how to deal with diabetes camp July 13-19th.  We were trying to figure out if Anna was going to loop, and if so how much and with which system.  She used Loop from September-December 2016 and OpenAPS from January-July 2017.  She’s not keen on change…so trying out a new system right before camp was just not happening.

However, once she came home from camp, we decided to give it a try.  We had a couple days of tweaking her settings after camp, which is typical.  She is quite active and at high altitude for camp, so basals and ISF need a couple days to settle down again.  About 5 days ago, we finally had things dialed in again and I could fairly confidently say that any errors in looping behavior would’ve “not been the fault of bad settings”.  So, it was time to really test.


First test was two large donuts; one chocolate bar and one maple bar.  Typically we have split this bolus up into at least two, sometimes three, boluses.  Not my favorite thing to do.  I keep hoping for the day that Anna will know how to split her boluses herself, but we aren’t there yet.  She still asks for help on about 80% of her higher carb meals that need splits.  Sometimes I get distracted about the timing of the bolus or the eating, and she will go low early after the donuts are eaten because we’ve given too long of a pre-bolus.

So, I did something I thought I’d never do.  I told Anna just to enter 110g of carbs @ carb absorption time of 4 hours, postdate it for 20 minutes for her prebolus time, and give whatever the Loop app recommended for bolus.  It recommended bolusing for 60g of the 110g.  AND THAT WAS THE ONLY BOLUS WE GAVE.

I knew there was some inaccuracy in this entry upfront.  Obviously there are quicker carbs in there as some fraction of the overall carb count…so I knew there was going to be somewhat of a spike after eating.

But, my goal here was more than just BG control.  My goal was “How much can Loop simplify my daughter’s interaction with difficult meals and still maintain decent BGs?”  In other words, can I finally be less involved with my teen’s t1d without risking poor bolusing effects?

SUPER PLEASED with results.  Short version, she did not go above 180 mg/dl and landed after the meal around 100 mg/dl (target is 90 mg/dl).


Do I know that we could do better if I’d manually added a second bolus earlier?  Or if I’d added quicker carbs in addition to the slower carbs?  Sure.  But this accomplished something that I’ve been wanting to see for a long time…a very simplified, safe way my teen could bolus a sugary/fatty meal without danger independently and still get reasonably good (or better) BG results.  She’s not always home.  She’s not always wanting my input on bolusing.  Independence is an important aspect we are trying to nurture.  This experiment gets a big WIN checkmark from me.


Now you’re probably wondering what that new screen is above?  That’s the new carbohydrates effect graph that pops up when you click on the carbs graph.  The graph shows, basically, the expected ICE (insulin counteraction effects) compared to carb absorption effects in grey.  Pete does a great job of explaining the graph in the earlier link, so I won’t repeat it all here.  Suffice to say, I’ve actually found quite a bit of utility in this graph for non-carb stuff too.  For example, the first two days back from camp, Anna’s basals were still too low and our ICE was leading to additional carbs being “counted” at the end of each meal.  These were for meals we had been pretty certain of the carb amounts for.  It was pretty easy to see that we weren’t coming back to target after meal AND that the BG-resistence was being picked up as “additional carbs”.  Knowing that the carb counts were correct, it was pretty easy to hone in on lack of basals as our problem still.  Once we got basals dialed in a little better, the carb counts have been fairly close again between what we enter and what ends up being counted in the ICE at the end of the meal.

IN-N-OUT Cheeseburger

Another favorite meal that we’d previously split and prebolused for.  This time we did all 60g upfront @ carb absorption of 2 hours, and let Loop do the rest.  We started the meal at 115 mg/dl so I figured the lack of split would be ok this time…Loop agreed and provided the whole bolus upfront (vs giving a portion like in the donuts scenario).

About 2 hours after eating, we hit the high point.  Finger sticks showed a max of about 145 mg/dl.  Clearly the meal wasn’t done at the 2 hours we’d initially estimated.  In previous Loop versions, this would’ve meant a need to manually correct to come back to range easier.


By the time the meal ended (over an hour after I’d estimated the meal would take), we settled nice back to target range and didn’t have to manually intervene at all.  All off a single bolus entry.



How about a mixed meal?  Fruit cup, spoonfuls of peanut butter, and leftover fajitas meat.  Yum…the things that teenagers eat on their own while left alone during summer mornings.  Anna estimated 25g @ 90 minutes for this meal.  Peanut butter has always been a favorite of hers, but not so much for me.  Makes the carb absorption times kind of variable.  Some of the quick carbs make it through, but then we get a later rise too.

The dynamic carbs version somehow knew that all those carbs hadn’t ended when Anna had suggested.  By keeping them in the queue, the Loop was more responsive, even while BGs were dropping with IOB, and picked up the later BG rise quite well.  Left us in perfect shape after the meal.


So after 6 days of use, and 4 or 5 of those with our basals fairly well set, here’s how we are looking on Loop DCA version.  We’ve done zero spilt boluses, and only one manual correction (last night her basals doubled in middle of night from hormones).


Anna and I are both really loving the Loop DCA.  She’s enjoying bolusing from phone.  We are still using IFTTT that we’d setup from OpenAPS in order to track site and sensor changes.  I’d say I miss the range of the edison/explorer board that could get us several rooms away…but Anna has easily picked up her old habits of stashing the RileyLink in a pocket again.  We are going to test out tonight how well using multiple RileyLinks in the house may help if she walks around with phone/receiver only.

After testing this out, we’ve felt safe to give our max basal rate room to run a little more for boluses like the two donuts.  We have made our carb ratios just a touch stronger, but I think that’s unrelated to Loop DCA, but rather some hormonal changes that we saw even before we started this system.

All in all, I think this update is going to get a lot of deserved attention and appreciation.  Makes use for kids a heck of a lot easier and safer.  School nurses won’t have nearly the same ability to mess up your kid’s day by a careless carb absorption entry.

Hormones #2

Post-lunch BG climbs to 195, which I knew was unusual given what she’d packed for lunch.  Didn’t notice anything with a crazy carb count on her NS.  But those SMBs were really such a nice visual that my loop and I were on the same page about what to do about it.


I ventured a quick text to check in.  I hate doing that, but she was home from school and I wouldn’t be interrupting class…so I did.  I asked.


Woah.  Wasn’t expecting that one.  We are still a little variable on when that pesky thing is coming…so this caught me off guard.  Three weeks…not four.  Ok.

Things had been so steady for the last several days (*except on beach party with cupcakes, teenagers, and suspended pumps for extended times a couple days ago).  I mean, look…so steady, right?  That was yesterday.

Screen Shot 2017-06-05 at 4.46.05 PM

And then today….with the same food basically.  But that red arrow was The Change.


If I could offer a hug to an inanimate object, it may be going to autosens.  I took a peek to see if things really had been *that steady* or if we had actually been getting a lot of help that I wasn’t aware of.

My suspicions were correct.  What had looked like a calm day, was actually the result of increasing ISF and Basal adjustments, as well as auto lower temp targets, behind the scenes.  From yesterday to today…basals went from 0.8 to 1.05. ISF went from 40 to 30.3.  The storm was held at bay until this afternoon when the big hormones hit just after lunch.


We adjusted her basals to 1.3 (based on where we had to adjust them to last month) and autosens is predicting we may need more…we will let it sit here for a little bit if we need to nudge in that direction in a few hours.

I can’t say how nice it is to take what is normally a horrible time (hormone swings) and make it limited to just a few hours of pain-in-the-butt.  The old way (non-autosens and non-looping) was days of pain.

Under the hood of OpenAPS

(Pull up a chair, this is a bit of an involved post)

UAM is “unannounced meals” feature in oref1 for OpenAPS.  You can read about UAM features and its development in Dana’s blog post here.

Here’s how my thought process has gone on UAM since I first read about it:

1st thought: Oh, I won’t need that since we always enter our carbs and prebolus.  UAM won’t offer me any improvements since we always “announce” our meals.

2nd thought: Well, there are times (ahem, school snack bar) where Anna or I have some hard carb counts.  I wonder if maybe when we were 20% off on a carb count, perhaps UAM might help then?  Because if we are 20% off on a carb count, that would sort of be like having 20% of an unannounced meal, right?  Hmmmm…let’s try enabling it and see.

3rd thought: Wow, that worked really well.  But WHY did it work well?  How is it different?  Oh piss…I’d better roll up my sleeves and look at code.

And that is how I ended up learning more about oref0’s logic for setting temp basal and (now with oref1) setting SMBs.

Side Note: If you care to do the same as I’ve done…the logic for how oref0 sets temp basal and gives SMBs is laid out in a file called determine-basal.js in GitHub.  This same file is pulled into your rig when you build your loop.  I’m not a code person, but a slow and methodical read of the file can provide some insight into the logic for how temp basals are set.  Plus, there’s certain lines in that file that start with “//”…those are plain english notes to help readers understand the code that follows the note.  If you start with the // lines, that may help anchor some of your read of the file.

When I went to the first endo appointment after diagnosis, I was shocked that they didn’t have a flow chart to tell us how to dose insulin.  Like those decisions flow charts that say “if answer is yes, go to this next square…if the answer is no, go to the other square.”  As I came to learn the hard way, the answer to “How much should I dose for a BG of 250 mg/dl?” will have about 18 questions that follow it before you can give an answer.  What’s her ISF?  What’s her IOB?  Is she exercising?  Is her BG rising or falling?  Did she eat recently?  How long ago did she eat?  How much did she eat?  How confident are you in the carb count?  Is she stressed/excited/nervous?   And so on.  It’s no wonder finding a babysitter for a T1D kid is almost impossible.

The same complications exist for loop logic.  Depending on whether BGs are rising/falling/steady, how fast BGs are rising, whether food is at play, and whether temp targets…how does your loop take those into account?   I’d previously heard OpenAPS developer Scott Leibrand refer to “blending” things like this into his loop, but I didn’t quite understand the term until I really looked into the determine-basal.js code.

So how does that blending work?  Traditionally, you’d start loop math using just insulin and carbs to predict future BGs.  You’d approximate how the insulin would behave based on published insulin effectiveness curves (like peaking insulin activity around 60-90 minutes and then tailing off) and you’d approximate carb absorption (like the Loop system does using the model from Think Like A Panceas).  You’d add the downward effect of insulin and the upward effect of carbs and voila…a predicted BG curve blending insulin and carb effects.

But, a good loop will go beyond that simple approximation to try to help with things that are happening outside the perfect-world scenario.  Things like “retrospective correction” and “BG momentum” in Loop system attempt to incorporate the “wtf is happening now? I’m not sure.” part of diabetes into a better BG prediction.

OpenAPS does this refinement through the use of several different predicted BG curves.  Ever wonder WHY OpenAPS has all those purple lines showing in Nightscout?  This is the reason why…those lines are the drivers of the logic.  They help blend the various scenarios about rising/falling/steady BGs, recent food, and activity to come up with a better chosen predicted BG curve.  And a better predicted BG curve should yield a better loop decision…avoiding lows and quicker recovery from high BGs.

Ready for this?  OpenAPS has the following types of “basic” predicted BG curves:

  • IOBpredBGs: based on insulin only
  • COBpredBGs: carbs and insulin together
  • UAMpredBGs: “floating carbs” and insulin together

That’s not the exhaustive list of curves used though…depending on the situation (rising BGs or falling BGs, for example), OpenAPS also does other calculations using the predictedBG curves (for example, minPredBG and avePredBG) to help provide for safe looping.

Basically, determine-basal.js has a whole bunch of “if this (is happening), then do that (use THAT particular predBG curve to determine the delivery of insulin)”.  For example, IF carbs are on-board and UAM and SMB are enabled, and BG is rising but not as much as expected, and IOB is negative, and prediction is above target…THEN use the XXXpredBG curve to calculate how much insulin to deliver.  If you change any one of those IF inputs…like BG is instead dropping and faster than expected…then OpenAPS is going to make a different decision about how to adjust insulin delivery and may use a different predBG curve to do that.

So, determine-basal.js is that complicated set of 18 questions involving “IF this and this and this and this but not that or that, THEN that” that we all used to go through before giving a dose of insulin.  It even has an IF-term called “Deviation” which is basically a measure of how far off expected BG behavior you are.  Looking at the deviation term is a big part of the OpenAPS decision IF-logic.  We’ve all used it before…”dang it, she SHOULD be dropping right now, but she isn’t!”

That’s where UAM and floating carbs come into play in a new way.  Previously, looping was constrained mathematically around meal-times by the amount of carbs entered.  The COBpredBG curve could only go so far in its prediction of BGs because it was limited to the carbs entered.

UAMpredBG is not restricted to the carb entry you’ve provided.  In a way, it doesn’t “trust” your carb entry as much.  It fact-checks your carb entry by looking at deviation comparisons.   More mathematically stated, UAM is predicting the future BGs based on the slope (rate of change in BG) of your actual BGs during meals.  [“during meals” is user-defined as either having carbs on board, an eating soon temp target, and/or a bolus given].  So, if your BGs are rising during a meal and they continue to rise beyond what was predicted on carbs alone (strong deviations happening), UAM is going to carry that prediction forward…regardless of the carbs you entered.  And it will carry that forward until the slope stops and things settle down.  Without UAM, that continued BG prediction up would’ve been restricted shortly after the meal was eaten by the fact that the carbs would’ve decayed ( COB=0 ) fairly quickly based on the rise in BG.

So, do you ever find that perhaps you under-counted carbs?  You’ve watched a meal’s BG climb after unknown sauces and unidentified ingredients were clearly playing a part in the meal?  The recovery to target BG in that situation can be slowed by the fact that the carbs were eventually all “used up” by the loop logic to explain the rise in BG.  Any additional rise the loop had no explanation for, and tended to treat delicately (slowly).

With UAM, the curve says “I can see these deviations were not expected.  I will help.  Even if the deviations go beyond simple explanation of what COB would expect.”  So that mathematical freedom helps for poor carb counting.  (It’s not unlimited freedom, there are still safety mechanisms in place.)

Case in point…that mini McMuffin of unknown carb count.


See how the UAMpredBG is headed to 294, while the other predictions have lower eventual BG predictions?  UAM is taking into account that Deviation of 67 and COB of 16g…saying “wow, you have NOT shown the BG behavior that I would’ve expected at this point in time for the carbs and bolus you’ve told me about.  I’m gonna trust your carb entry less and instead trust the deviations more.  If my predictions are right, you will need 1.85 additional units of insulin to control this unexpected deviation.  But, predictions can change.  So let’s give you a SMB of 0.4 units to help with that right now.  If I’ve overshot my estimate, I can suspend enough basal later to fix. But that 0.4 is a good help right now for your deviations I’m seeing.  Let’s try that, we will regroup with the next BG and see where you are.”

The next BG reading comes in and loop has some new information to consider.  BG has slowed down and there’s more insulin on board from the 0.4 SMB.

Screen Shot 2017-05-22 at 11.16.08 AM

Deviation is now a smaller number (the slope of the rising BGs has decreased), we are moving closer to what the expected behavior should be.  In fact, the UAMpredBG curve now predicts an eventual BG of 104 and the insulin required is now -0.12 units.  So, loop will suspend basals and wait for the next BG to come in…and go through all of its IFs and THENs again before deciding what to do with insulin.  The loop is beginning to be able to trust cob again; it has delivered enough insulin to cover the carbs that we undercounted and, through SMBs help too, we are going to spend less time out of range.

What about situations where you OVER counted carbs?  Will UAM be able to help there too?  Yes.  When UAM is not seeing enough carb absorption compared to what you’ve told it to expect (“hey, you promised me 25g carbs and I’m only seeing enough BG impact to trust that you ate 10g of carbs”), then the loop will hold off on basal insulin and dosing until the BGs catch up.  (See…it’s comparing those COBpredBG curves with the UAMpredBG curves to make decisions.  All those little purple lines and deviation values are part of the logic sequence to deliver insulin.)

Because UAMpredBG curve is not limited so much by your actual carb entry, it will tend to have a much more variable prediction after each new BG entry.  It will predict very high eventual BG if your BG is strongly rising, and then it will predict lower BG if your BG is dropping.  So, if and when, you decide to enable UAM…don’t be surprised by seeing an ever-changing UAM curve, that’s an expected result since it isn’t quite so restricted by your carb entry.

As I understand it, it took several iterations for Scott and Dana to figure out the proper blending (IFs and THENs) of UAM into the oref0 determine-basal.js code.  Then there’s been many early users that have put it to the test to validate or improve the blending.

Where I’d previously thought that UAM had very little value, because we always enter our meals and prebolus…turns out I was wrong.  I’m not a carb-counting expert.  I get it wrong sometimes.  And UAM helps mop-up my harder carb counts, in both directions.  When I’m right on carb counts, UAM plays almost no role…but with UAM I’m not going to be nearly as BG-impacted when I do get things wrong.



OpenAPS and UAM

Now that we’ve had some time with SMBs enabled, I was ready to consider trying the other new feature called UAM (unannounced meals).  I wasn’t previously interested in this feature much, because (I’m lucky) Anna has only forgotten about 2 boluses in her 2+ years of mostly independent bolusing.  So meals generally don’t go unannounced.  I didn’t imagine there would be much value in a feature named “unannounced meals”.

But, then on some random school mornings…I am reminded that I should keep my options open.  School “nutrition” break is at 10:10am.  By 10:15am, I see a bolus come in on Nightscout.  And I think to myself “Huh, I wonder what that will be because I know she didn’t pack a snack today.”  By 10:30am, I see the BGs starting to climb and I wonder how it will go.

Nutrition break is never about quality food.  The things sold by the school for nutrition break usually involve chip bags, mini donuts, twizzlers, and the like.  These snacks are problematic because  (1) we don’t have a lot of practice for these foods because we don’t generally have lots of these particular foods around the house and (2) they are being eaten at a time where she doesn’t have a lot of time or interest in babysitting her BGs or IOB.  Also problematic is that pesky independence that Anna is really quite interested in.  She doesn’t want to tell me what she’s eating, nor does she want to have me telling her how to bolus things much these days.  She will tend to still ask me for input on new food boluses when it’s just the family around…but when she’s around her high school buddies at school?  Forget about it.  So, iteratively improving her bolus techniques for these foods is hard to do given the lack of communication on these snacks.

Anyways, I turned on UAM on Sunday late night with the thought that maybe Anna and I would have some help during those nutrition snacks.  My theory wasn’t that she isn’t bolusing or entering carbs…but that perhaps the carb counting on those snacks was particularly difficult and our desire to keep her independent probably wasn’t going to allow for much improvement on proper carb count/bolus techniques for the snacks. I really do value her growing self-confidence and want to cultivate it by not hovering on every new food and new bolus…but those nutrition snacks test those limits.

I didn’t tell Anna about the new feature, but she must have had that sixth sense because on Monday’s nutrition break…she decided to test it.  My pebble started to alert me around 10:45pm that her BGs had climbed above 130.  When I pulled up NS, there was 50g of bolused carbs entered.  BGs were climbing, but SMBs were started.  I sat patiently as I could.  But, eventually the curiosity got to me when she got to about 180 mg/dl.

Screen Shot 2017-05-22 at 11.16.08 AM

So nice of her to be testing the new feature with exactly the part that I thought it might help with…some random new food that she was completely guessing the carb count on (without me involved).

Things went awesome…I was so impressed.  Somehow the rapid rise from that mini-McMuffin was halted pretty well and the decline back to target was looking smooth.

Screen Shot 2017-05-22 at 11.51.51 AMSadly though…that experiment would have to end early.  Her rig’s battery came loose and looping stopped.  She couldn’t see that the wire had come loose so we just lived out the rest of the day old-school…no looping. (but look at how well those BGs slid in close to target of 90 even after 43 minutes of no looping.  UAM/SMBs sure did estimate that needed amount of suspension pretty accurately from a fair bit ahead of time)

Screen Shot 2017-05-22 at 12.33.42 PM

When she came home on Monday and I suggested that maybe she could run the experiment again…she was more than happy to oblige.  So, today she got the mini McMuffin again and did the same bolus and same carb entry.  Started the meal looking pretty similar to Monday.


The UAM/SMBs worked together to control that peak BG as best as possible…looked good.  I like how even though clearly the sandwich may have been a little different carb count (or her prebolus time was a little longer today?), the loop was reacting well to the BG behavior rather than simply the carb count.


But then I got a little leery when I saw this…was the bottom falling out?


My worries were unfounded.  The loop’s math was solid.  We gently landed pretty much at our target of 90 mg/dl.


I’d say the experiment has been a solid success.  One of the biggest things I notice is the way the loop successfully managed this same meal without prejudice.  What do I mean by that?  It reacted to the ACTUAL BGs that it was seeing really well rather than seeming overwhelmed by strictly the carbs entered.  At the peak BG on Monday, Anna was carrying about 5.34 units of IOB.  At the peak BG on Tuesday, Anna was carrying about 3 units of IOB.  To have both of those peaks resolve close to target without overshooting or needing a low treatment or intervention is pretty impressive.

The question is “If it was UAM that did this, HOW did UAM do this?”  I spent a good portion of the day exploring the answer to that question.  And the answer probably deserves its own post.

There’s a line in the movie Labyrinth where the main character Sarah has an epiphany and says to the evil Goblin King “You have no power over me.”  I feel like I’ve just gotten to say to the snack bar “You have no power over me.”

Doritos are still devilish on BGs compared to a low-carb muffin, but at least the BGs are being handled better than if I stared at them and tried to micromanage with 8 text messages and distractions.  Anna comes home and tells me about the things she did with friends and her homework…no need to discuss how to do the snack bar “better” the next day.  What a freedom.