Glucose Readings in a Terminal Prompt

I'm a diabetic, and it sucks. But I'm also computery and like to break stuff.

Inspired by Scott Hanselman's post on his friend getting his continuous glucose monitor (CGM) readings in his terminal, I decided I also wanted to do this. My CGM is a Dexcom G7, and Dexcom has an API you can query directly for your glucose levels, taken every few minutes.

I didn't make this a Windows service like Scott. I use Windows 11 but spend most of my day-to-day working in WSL2, which in my case runs Ubuntu 22.04. What I did:

  1. Make a Python script to fetch my current blood glucose reading and current trend arrow (is the glucose rising, falling, or steady?) and write the result to a file.
  2. Make a systemd service and timer to run the Python script every 20 minutes.
  3. Put the contents of the written file in my terminal prompt.

A little Python

This was very simple thanks to Gage Benne's pydexcom package. I used pip3 to install and then wrote the script:

#!/usr/bin/env python3

from pydexcom import Dexcom

class ANSI_Compatible:
    END = '\x1b[0m'
    # If Foreground is False that means color effect on Background
    def Color(ColorNo, Foreground=True): # 0 - 255
        FB_G = 38 # Effect on foreground
        if Foreground != True:
            FB_G = 48 # Effect on background
        return '\x1b[' + str(FB_G) + ';5;' + str(ColorNo) + 'm'

dexcom = Dexcom("USERNAME", "PASSWORD")
bg = dexcom.get_current_glucose_reading()

if 80 <= bg.value <= 180:
    color = ANSI_Compatible.Color(120)
else:
    color = ANSI_Compatible.Color(196)

glucose_number = (color + str(bg.value) + ANSI_Compatible.END)

with open('/home/hhoover/.dexcom/bg_file.txt', 'w') as f:
    f.write('{}{}'.format(glucose_number,bg.trend_arrow))

As you can see, the script authenticates with Dexcom and pulls down my current reading information. If the reading is in range, the text color will be green, otherwise it is red. From that, I only really care about the number and trend, so that's what I write to a file, called bg_file.txt.

Automate the script

The Dexcom sensor I wear reads my blood glucose every 5 minutes, and the result is sent to an app on my phone. If I want/need an IMMEDIATE reading I can look at my phone. I don't need to query Dexcom for the value every five minutes in my terminal, but every 20 should be good enough. To get this done in my WSL2 instance (which runs systemd) I used a systemd timer and service. The service is a oneshot, just running the script and exiting:

$ cat ~/.config/systemd/user/dexcom.service
[Unit]
Description=Grabs current Dexcom reading
Wants=dexcom.timer

[Service]
Type=oneshot
ExecStart=/usr/bin/python3 /home/hhoover/.dexcom/dex.py

[Install]
WantedBy=default.target

The timer looks like this:

$ cat ~/.config/systemd/user/dexcom.timer
[Unit]
Description=Runs the dexcom script
Requires=dexcom.service

[Timer]
OnBootSec=0min
Unit=dexcom.service
OnCalendar=*:0/20

[Install]
WantedBy=timers.target

Once those were written, you need to perform a daemon-reload and enable the service:

$ systemctl --user daemon-reload
$ systemctl --user enable dexcom.service
$ systemctl --user enable dexcom.timer

Now, I have a script, I've automated the fetching of the data, now I need to put it in my terminal.

Powerlevel10k Customization

I use Oh My ZSH and Powerlevel10k, which makes terminal customization a snap. I edited my ~/.p10k.sh file in the following section:

typeset -g POWERLEVEL9K_LEFT_PROMPT_ELEMENTS=(
    custom_dexcom
    ...
  )
POWERLEVEL9K_CUSTOM_DEXCOM="cat /home/hhoover/.dexcom/bg_file.txt"

I then refreshed my terminal - source ~/.zshrc - and had a result:

Dexom reading in a terminal!

All in all a fun exercise!