Skip to content

Instantly share code, notes, and snippets.

@tjhorner
Last active May 21, 2025 03:03
Show Gist options
  • Save tjhorner/51fb1d9549e7b12c5b1eddcd23f89604 to your computer and use it in GitHub Desktop.
Save tjhorner/51fb1d9549e7b12c5b1eddcd23f89604 to your computer and use it in GitHub Desktop.
blueprint:
name: Glucose Lights
description: Change light color on a gradient of green to red based on glucose value
author: TJ Horner (@tjhorner)
domain: automation
input:
glucose_sensor:
name: Glucose Value Sensor
selector:
entity:
filter:
domain: sensor
device_class: [ blood_glucose_concentration ]
target_lights:
name: Target Lights
description: The lights to update when glucose value changes. (Must support RGB color.)
selector:
entity:
multiple: true
filter:
domain: light
low_value:
name: Low Threshold
description: |
Readings above but close to this value will make the lights greener.
Readings below this value will make the lights pure red.
default: 70
selector:
number:
min: 0
max: 300
high_value:
name: High Threshold
description: |
Readings below but close to this value will make the lights redder.
Readings above this value will make the lights red.
default: 180
selector:
number:
min: 0
max: 300
mode: single
triggers:
- trigger: state
entity_id: !input glucose_sensor
- trigger: state
entity_id: !input target_lights
from: "off"
to: "on"
conditions:
- condition: state
entity_id: !input target_lights
state: "on"
actions:
- variables:
glucose_sensor_id: !input glucose_sensor
high_thresh: !input high_value
low_thresh: !input low_value
- variables:
color: |
{% set glucose = states(glucose_sensor_id) | int %}
{% set pct = (1 - ((glucose - low_thresh) / (high_thresh - low_thresh))) * 100 %}
{% set r, g, b = 0, 0, 0 %}
{% if (pct > 100) or (pct < 0) %}
{% set r = 255 %}
{% elif (pct <= 51) %}
{% set r = 255 %}
{% set g = (5.0 * pct) | round | int %}
{% else %}
{% set g = 255 %}
{% set r = (505 - 4.89 * pct) | round | int %}
{% endif %}
{{ [ r, g, b ] }}
- action: light.turn_on
data:
rgb_color: "{{ color }}"
target:
entity_id: !input target_lights
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment