I’ll just throw this out here and you decide if it’s worth your while.
If you don’t mind having the state of sensor.bryan_cgm
in a terse, space-delimited format like this:
164 4 Flat 6
instead of a descriptive format like this:
sgv:164, trend:4, direction:Flat, time:6
then you can use the following code which streamlines the Template Sensors.
- platform: rest
name: bryan_cgm
resource: !secret nightscout_url
value_template: >
{{value_json[0].sgv}} {{value_json[0].trend}} {{value_json[0].direction}} {{ ( (as_timestamp(now()) - as_timestamp(value_json[0].dateString) ) / 60) | round(0) }}
- platform: template
sensors:
bryan_cgm_sgv:
value_template: >
{{ states('sensor.bryan_cgm').split()[0] }}
unit_of_measurement: "mg/dl"
bryan_cgm_trend:
value_template: >
{{ states('sensor.bryan_cgm').split()[1] }}
bryan_cgm_direction:
value_template: >
{{ states('sensor.bryan_cgm').split()[2] }}
bryan_cgm_time:
value_template: >
{{ states('sensor.bryan_cgm').split()[3] }}
NOTE:
Because it is using the space character as a delimiter, none of the data values are allowed to contain a space. For example, should the direction
parameter potentially have a value consisting of two words then you cannot, obviously, use the space character as a delimiter. In that case, the template can be easily modified to use a comma as a delimiter.