Please add diabetes specific medical units like "International Unit (IU)", mmol/L and mg/dL as a units of measure

Hi There,

I’m integrating my diabetes systems into HA, and noticed that International Unit (IU), Millimoles Per Litre (mmol/L) and Milligrams Per Decilitre (mg/dL) as a units of measure are missing.

IU is a unit of measure for certain types of supplements or drugs like insulin - Converting Units Of Insulin To Milligrams And Milliliters | HelloPharmacist.

Millimoles Per Litre (mmol/L) is the measure used to understand a blood glucose reading values used in the EU and AU. This is also used for other pathology based results like cholesterol levels etc so may benefit others trying to integrate health systems. - Millimoles Per Liter(mmol/L) - Concentration of Mass Conversion - Unit Converter

Milligrams Per Decilitre (mg/dL) is the measure used to understand a blood glucose reading values used in primarily in the USA . This is also used for other pathology based results like cholesterol levels etc. - Milligrams Per Deciliter(mg/dL) - Concentration of Mass Conversion - Unit Converter

My understanding is that these units of measure are defined in this file - core/homeassistant/const.py at d7ac4bd65379e11461c7ce0893d3533d8d8b8cbf · home-assistant/core · GitHub

However I’m unsure if it’s as simple as adding this:

# Medical units
INTERNATIONAL_UNIT = "unit" #unit is the measure generally used for insulin instead of IU even though IU is the correct way to represent it.
MILLIMOLES_PER_LITRE = "mmol/L"
MILLIGRAMS_PER_DECILITRE = "mg/dL"

You can define your entity units however you like.

Welcome by the way. where/how are you getting your diabetes related entities into HA?

2 Likes

Pulling it in via rest api from Nightscout.

I use Dexcom G6 and xDrip+ to push data into Nightscout.

The Official Nightscout App is lacking many of the datapoints you can pull via the API so doing it manually now instead.

Here is the code I’m using, it’s a WIP and has issues specifically one of the REST fields only shows up when a note is added, but is not there the rest of the time so HA complains that it’s not there when it starts up, and I’ve not found a way to make it ignore a missing value yet.

rest:
  - resource: https://[email protected]/api/v1/entries/current.json
    scan_interval: 150
    sensor:
      - name: Blood Sugar
        value_template: "{{ (value_json[0].sgv/18)|round(1)}}"
        unit_of_measurement: mmol/L
        icon: mdi:diabetes
      - name: "Blood Sugar Delta"
        value_template: "{{ (value_json[0].delta/18)|round(2)}}"
        unit_of_measurement: mmol/L
      - name: Blood Sugar Trend
        value_template: "{{ value_json[0].direction}}"

  - resource: https://[email protected]/api/v1/treatments.json
    scan_interval: 150
    sensor:
      - name: "Treatment - Event Type"
        value_template: >
          {% if value_json is defined %}
            {{ value_json[0].eventType }}
          {% else %}
            {{ "None" }}
          {% endif %}
      - name: "Treatment - Notes"
        value_template: >
          {% if value_json is defined %}
            {{ value_json[0].notes }}
          {% else %}
            {{ "None" }}
          {% endif %}
      - name: "Treatment - Insulin"
        unit_of_measurement: units
        icon: mdi:diabetes
        value_template: >
          {% if value_json is defined %}
            {{ value_json[0].insulin }}
          {% else %}
            {{ "0" }}
          {% endif %}
      - name: "Treatment - Insulin Injections"
        value_template: >
          {% if value_json is defined %}
            {{ value_json[0].insulinInjections }}
          {% else %}
            {{ "None" }}
          {% endif %}
      - name: "Treatment - Carbs"
        unit_of_measurement: g
        value_template: >
          {% if value_json is defined %}
            {{ value_json[0].carbs }}
          {% else %}
            {{ "0" }}
          {% endif %}
      - name: "Treatment - Created At"
        value_template: >
          {% if value_json is defined %}
            {{ value_json[0].created_at }}
          {% else %}
            {{ "None" }}
          {% endif %}

Same here. In rest you can specify unit of measurement.

The first one is the same as mine. I don’t have treatments in nightscout as my pump is not compatible. However could you please post the json output of treatments.json when it does and doesn’t have an output that gives a good result?

I think I might have fixed it with this:

     - name: "Treatment - Notes"
        value_template: >
          {% if value_json is defined %}
            {{ value_json[0].notes| default("unknown")}}
          {% else %}
            {{ "None" }}
          {% endif %}
1 Like

I enter insulin values manually into xDrip+ most of the time, as it has all my absorption rates etc so it shows in the app how long until insulin is gone, also a predicted blood glucose value based on IOB. Noting, I’m not using a pump.

Here is the JSON just in case it helps others.

With the notes field present, noting it only shows up when a note is added or a sensor update is posted to Nightscout:

{
    "_id": "6d77c03932954f0c885c2eb7",
    "timestamp": 1712015885266,
    "eventType": "Sensor Stop",
    "enteredBy": "xdrip",
    "notes": "Stopped by transmitter: Stopped",
    "uuid": "6d77c039-3295-4f0c-885c-2eb7fbc9de4d",
    "created_at": "2024-04-01T23:58:05.000Z",
    "sysTime": "2024-04-02T10:58:05.266+1100",
    "utcOffset": 0,
    "carbs": null,
    "insulin": null
}

Without the notes field e.g. a normal response without a note

[
    {
        "_id": "d44c693eba9447d1b441e1db",
        "timestamp": 1712658423163,
        "eventType": "<none>",
        "enteredBy": "xdrip",
        "uuid": "d44c693e-ba94-47d1-b441-e1dbd8813dc3",
        "insulin": 5,
        "insulinInjections": "[{\"insulin\":\"Afrezza\",\"units\":5.0}]",
        "created_at": "2024-04-09T10:27:03.000Z",
        "sysTime": "2024-04-09T20:27:03.163+1000",
        "utcOffset": 0,
        "carbs": null
    },

The notes and eventType fields look useful for Automations and notifications etc. But I recon it’s a bug or design flaw in the Nightscout API that the notes field is only present when a new note is submitted and not always present with a null value.

1 Like

Might be I am doing a mistake, but I could never get REST to work with the API secret in the URL like in the example. Instead I had to do this:


  - resource: https://<mysite>/api/v1/entries/current.json
    headers:
      API-SECRET: !secret <name of your nightscout api secret in sha1 hash>
      accept: application/json

This post Please add diabetes specific medical units like "International Unit (IU)", mmol/L and mg/dL as a units of measure - #4 by XiTatiON uses the password as part of the url, try that.