Skip to content

Add timestamp of the glucose value to the sensor #39

@dreimer1986

Description

@dreimer1986

I think adding one line to sensor.py would be a good idea. To be more precise:

    @property
    def extra_state_attributes(self) -> Dict[str, int | float]:
        return {
            "value_mmol_l": self.gcm.value,
            "value_mg_dl": self.gcm.value_in_mg_per_dl,
            "target_high_mmol_l": round(self.connection.target_high / 18, 1),
            "target_low_mmol_l": round(self.connection.target_low / 18, 1),
            "target_high_mg_dl": self.connection.target_high,
            "target_low_mg_dl": self.connection.target_low,
            "value_timestamp": self.gcm._timestamp,
        }

I added "value_timestamp" to the sensor to be able to really know how old the mentioned value is. I need this to show if the value I show on a e-Ink price tag in my living room is really up to date. I fell for the idea that the sensor is recent when it got an update from LibreView, but this is not always the case. Thus I looked into the API can found this quite useful addition.

Now my openEPaperLink Automation looks like this:

alias: Zucker anzeigen
description: ""
triggers:
  - trigger: time_pattern
    minutes: /30
conditions:
  - condition: time
    after: "06:00:00"
    before: "02:00:00"
actions:
  - action: open_epaper_link.drawcustom
    target:
      device_id:
        - d289b497b44e2d5a8458e1f8bb8bf698
    data:
      rotate: 0
      ttl: 60
      dry-run: false
      background: white
      payload:
        - type: rectangle
          x_start: 1
          x_end: 294
          y_start: 40
          y_end: 110
          width: 1
          fill: white
          outline: black
        - type: text
          value: >-
            {% if state_attr('sensor.daniel_reimer_glucose_level','icon') ==
            "mdi:arrow-down-thick" %}
              Zucker: {{ states('sensor.daniel_reimer_glucose_level') }} mg/dl 🢃
            {% elif state_attr('sensor.daniel_reimer_glucose_level','icon') ==
            "mdi:arrow-bottom-right-thick" %}
              Zucker: {{ states('sensor.daniel_reimer_glucose_level') }} mg/dl 🢆
            {% elif state_attr('sensor.daniel_reimer_glucose_level','icon') ==
            "mdi:arrow-right-thick" %}
              Zucker: {{ states('sensor.daniel_reimer_glucose_level') }} mg/dl 🢂
            {% elif state_attr('sensor.daniel_reimer_glucose_level','icon') ==
            "mdi:arrow-top-right-thick" %}
              Zucker: {{ states('sensor.daniel_reimer_glucose_level') }} mg/dl 🢅
            {% elif state_attr('sensor.daniel_reimer_glucose_level','icon') ==
            "mdi:arrow-up-thick" %}
              Zucker: {{ states('sensor.daniel_reimer_glucose_level') }} mg/dl 🢁
            {% else %}
              Zucker: {{ states('sensor.daniel_reimer_glucose_level') }} mg/dl ⮽
            {% endif %}
          x: 10
          "y": 50
          font: ppb.ttf
          size: 28
          color: >-
            {% if states('sensor.daniel_reimer_glucose_level')|int <=
            state_attr('sensor.daniel_reimer_glucose_level','target_low_mg_dl')
            %}
              accent
            {% elif states('sensor.daniel_reimer_glucose_level')|int >=
            state_attr('sensor.daniel_reimer_glucose_level','target_high_mg_dl')
            %}
              accent
            {% else %}
              black
            {% endif %}
        - type: text
          value: >-
            Ablauf: {{ (states('sensor.daniel_reimer_sensor_expiry')|as_datetime
            - now()).days }} {{ ("Tage" if
            (states('sensor.daniel_reimer_sensor_expiry')|as_datetime -
            now()).days > 1 else "Tag") }}
          x: 10
          "y": 80
          font: ppb.ttf
          size: 28
          color: >-
            {% if (states('sensor.daniel_reimer_sensor_expiry')|as_datetime -
            now()).days <= 3 %}
              accent
            {% else %}
              black
            {% endif %}
        - type: text
          value: "Sensorwert Alter: {{as_datetime(as_timestamp(now()) - as_timestamp(strptime(state_attr('sensor.daniel_reimer_glucose_level', 'value_timestamp'), '%d/%m/%Y %I:%M:%S %p'))).minute}} Minuten"
          font: ppb.ttf
          x: 10
          "y": 152
          size: 10
          color: black
          anchor: lb
        - type: text
          value: "Status von: {{ states('sensor.time') | string }} Uhr"
          font: ppb.ttf
          x: 296
          "y": 152
          size: 10
          color: black
          anchor: rb
mode: single

And this is the current result:
https://photos.app.goo.gl/xEEdYKmmJk5zpSV37

Metadata

Metadata

Assignees

Labels

No labels
No labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions