It is currently April 26th, 2024, 4:03 am

How to subtract epoch times from WebParser and if X minutes old it will change font type

Get help with creating, editing & fixing problems with skins
364def49db
Posts: 9
Joined: December 19th, 2021, 3:12 am

How to subtract epoch times from WebParser and if X minutes old it will change font type

Post by 364def49db »

I'm working on a Skin that will show my Glucose readings from Nightscout. However, I'm getting hung up on the time... I was wondering if someone could direct me in how I can accomplish subtracting the current time from the timestamp of the glucose and if it says more than 10 minutes old it can change the font to show the value is old.

Do I have to convert it out of epoch time, or can I simply do some math with it to determine if it's X minutes old?
User avatar
balala
Rainmeter Sage
Posts: 16172
Joined: October 11th, 2010, 6:27 pm
Location: Gheorgheni, Romania

Re: How to subtract epoch times from WebParser and if X minutes old it will change font type

Post by balala »

364def49db wrote: December 19th, 2021, 6:43 pm I'm working on a Skin that will show my Glucose readings from Nightscout. However, I'm getting hung up on the time... I was wondering if someone could direct me in how I can accomplish subtracting the current time from the timestamp of the glucose and if it says more than 10 minutes old it can change the font to show the value is old.

Do I have to convert it out of epoch time, or can I simply do some math with it to determine if it's X minutes old?
You can get the timestamp of a value returned by a Time measure simply by the [TimeMeasure:TimeStamp] section variable (requires to add the DynamicVariables=1 option to the measure or meter where you want to use this - or in fact any - section variable).
However if a time is obtained through a WebParser measure, you have to use the value of that WebParser measure into a TimeStamp option of a Time measure and to use the timestamp of that Time measure. Not too complicated, but requires a little bit of experience in working with WebParser and Time measures.
So would be nice to post a sample code, in order to can get more specific help.
364def49db
Posts: 9
Joined: December 19th, 2021, 3:12 am

Re: How to subtract epoch times from WebParser and if X minutes old it will change font type

Post by 364def49db »

Here is the code I have thus far, and by no means am I an expert - novice as best.

Here is the WebParse: {"status":[{"now":1639959264062}],"bgs":[{"sgv":"108","trend":6,"direction":"SingleDown","datetime":1639959023000,"bgdelta":-17,"iob":"0","cob":0}],"cals":[]}

Clearly I"m not grabbing all aspects of this, just the ones that are important to me. So I would be doing the math from NOW and datetime and if past say X minutes to strike out the MeterGlucose text or to display a warning saying its out of date.

Code: Select all

[Rainmeter]
Update=1000

[cUpdate]
Measure=Calc
Formula= (cUpdate+1) % 61
IfEqualValue=60
IfEqualAction=!Refresh


[Variables]
fontName=Trebuchet MS
textSize=8
colorBar=235,170,0,255
colorText=255,255,255,205
NightScoutURL=
; Not used yet.
;LowGlucoseAlert = 69
;HighGlucoseAlert = 181

; ----------------------------------
; MEASURES return some kind of value
; ----------------------------------

[MeasureSite]
Measure=WebParser
UpdateRate=600
URL=#NightScoutURL#pebble
RegExp=(?siU)now":(.*)}],.*sgv":"(.*)",.*trend":(.*),.*direction":"(.*)",.*datetime":(.*),.*bgdelta":(.*),
; Output StringIndex
;	1 - Current Time
;	2 - Glucose
;	3 - Delta [Not used]
;	4 - Direction (Text)
;	5 - Glucose Timestamp
;	6 - BG Delta

[MeasureCurrentTime]
Measure=WebParser
URL=[MeasureSite]
StringIndex=1

[MeasureGlucose]
Measure=WebParser
URL=[MeasureSite]
StringIndex=2

[MeasureDeltaDirection]
Measure=WebParser
URL=[MeasureSite]
StringIndex=4
IfMatch=Flat
IfMatchAction=[!SetOption MeterDeltaDirection Text "🡢"]
IfMatch2=FortyFiveUp
IfMatchAction2=[!SetOption MeterDeltaDirection Text "🡥"]
IfMatch3=FortyFiveDown
IfMatchAction3=[!SetOption MeterDeltaDirection Text "🡦"]
IfMatch4=SingleUp
IfMatchAction4=[!SetOption MeterDeltaDirection Text "🡡"]
IfMatch5=DoubleUp
IfMatchAction5=[!SetOption MeterDeltaDirection Text "🡡🡡"]
IfMatch6=SingleDown
IfMatchAction6=[!SetOption MeterDeltaDirection Text "🡣"]
IfMatch7=DoubleDown
IfMatchAction7=[!SetOption MeterDeltaDirection Text "🡣🡣"]

[MeasureGlucoseDelta]
Measure=WebParser
URL=[MeasureSite]
StringIndex=6
IfCondition=MeasureGlucoseDelta < 0
IfTrueAction=[!SetOption MeterGlucoseDelta FontColor 220,20,60]
IfCondition2=MeasureGlucoseDelta > 0
IfTrueAction2=[!SetOption MeterGlucoseDelta FontColor 125,252,0]
DynamicVariables=1

; ----------------------------------
; STYLES are used to "centralize" options
; ----------------------------------

[styleGlucose]
StringAlign=Left
StringCase=Upper
StringStyle=Bold
StringEffect=Shadow
FontEffectColor=0,0,0,50
FontColor=#colorText#
FontFace=#fontName#
FontSize=30
AntiAlias=1
ClipString=1

[styleLeftText]
StringAlign=Left
; Meters using styleLeftText will be left-aligned.
StringCase=None
StringStyle=Bold
StringEffect=Shadow
FontEffectColor=0,0,0,20
FontColor=#colorText#
FontFace=#fontName#
FontSize=#textSize#
AntiAlias=1
ClipString=1

[styleRightText]
StringAlign=Right
StringCase=None
StringStyle=Bold
StringEffect=Shadow
FontEffectColor=0,0,0,20
FontColor=#colorText#
FontFace=#fontName#
FontSize=#textSize#
AntiAlias=1
ClipString=1

[styleSeperator]
SolidColor=255,255,255,15

; ----------------------------------
; METERS display images, text, bars, etc.
; ----------------------------------

[meterGlucose]
Meter=String
MeterStyle=styleGlucose
MeasureName=MeasureBG
X=10
Y=1
W=100
H=40
Text=[&MeasureGlucose] 
DynamicVariables=1

[meterGlucoseDelta]
Meter=String
MeterStyle=styleLeftText
MeasureName=MeasureBG
X=1
Y=25
W=100
H=20
Text=[&MeasureGlucoseDelta] 
DynamicVariables=1

[meterPositiveDelta]
Meter=String
MeasureName=MeasurePositiveDelta
DynamicVariables=1

[MeterDeltaDirection]
Meter=String
MeterStyle=styleLeftText
MeasureName=MeasureBG
X=1
Y=7
W=100
H=20
Text=[&MeasureDeltaDirection]
DynamicVariables=1

;[meterSeperator]
;Meter=Image
;MeterStyle=styleSeperator
;X=10
;Y=52
;W=100
;H=1
As you can tell, I haven't started anything with the time settings but was trying to figure out how to implement it.
User avatar
eclectic-tech
Rainmeter Sage
Posts: 5406
Joined: April 12th, 2012, 9:40 pm
Location: Cedar Point, Ohio, USA

Re: How to subtract epoch times from WebParser and if X minutes old it will change font type

Post by eclectic-tech »

Since both the 'now' time and the 'datetime' are both in epoch seconds, you can subtract those values and divide the result by 3600 (the number of seconds in 1 hour) to get the difference in minutes, then take actions based on that value.

This example code does that in [MeasureTimeDifference] calc measure and changes the glucose text color to red if over 60 minutes since the last datetime.

Code: Select all

[MeasureGlucoseTime]
Measure=WebParser
URL=[MeasureSite]
StringIndex=5

[MeasureTimeDifference]
Measure=Calc
Formula=(MeasureCurrentTime-MeasureGlucoseTime)
IfCondition=MeasureTimeDifference>3600
IfTrueAction=[!SetOption meterGlucose FontColor "255,0,0,205"][!UpdateMeter meterGlucose][!Redraw]
IfFalseAction=[!SetOption meterGlucose FontColor "#*colorText*#"][!UpdateMeter meterGlucose][!Redraw]

[MeasureTimeDifferenceFormatted]
Measure=Time
TimeStamp=[MeasureTimeDifference]
DynamicVariables=1

To test, I created a text file containing the webparser code you posted.
I also added a line of text with the time difference displayed below the measurement.
Of course, the trigger time and action(s) can be anything you want.

Your code with these additions:

Code: Select all

[Rainmeter]
Update=1000

[cUpdate]
Measure=Calc
Formula= (cUpdate+1) % 61
IfEqualValue=60
IfEqualAction=!Refresh


[Variables]
fontName=Trebuchet MS
textSize=8
colorBar=235,170,0,255
colorText=255,255,255,205
NightScoutURL=
; Not used yet.
;LowGlucoseAlert = 69
;HighGlucoseAlert = 181

; ----------------------------------
; MEASURES return some kind of value
; ----------------------------------

[MeasureSite]
Measure=WebParser
UpdateRate=600
URL=#NightScoutURL#pebble
RegExp=(?siU)now":(.*)}],.*sgv":"(.*)",.*trend":(.*),.*direction":"(.*)",.*datetime":(.*),.*bgdelta":(.*),
; Output StringIndex
;	1 - Current Time
;	2 - Glucose
;	3 - Delta [Not used]
;	4 - Direction (Text)
;	5 - Glucose Timestamp
;	6 - BG Delta

[MeasureCurrentTime]
Measure=WebParser
URL=[MeasureSite]
StringIndex=1

[MeasureGlucose]
Measure=WebParser
URL=[MeasureSite]
StringIndex=2

[MeasureDeltaDirection]
Measure=WebParser
URL=[MeasureSite]
StringIndex=4
IfMatch=Flat
IfMatchAction=[!SetOption MeterDeltaDirection Text "🡢"]
IfMatch2=FortyFiveUp
IfMatchAction2=[!SetOption MeterDeltaDirection Text "🡥"]
IfMatch3=FortyFiveDown
IfMatchAction3=[!SetOption MeterDeltaDirection Text "🡦"]
IfMatch4=SingleUp
IfMatchAction4=[!SetOption MeterDeltaDirection Text "🡡"]
IfMatch5=DoubleUp
IfMatchAction5=[!SetOption MeterDeltaDirection Text "🡡🡡"]
IfMatch6=SingleDown
IfMatchAction6=[!SetOption MeterDeltaDirection Text "🡣"]
IfMatch7=DoubleDown
IfMatchAction7=[!SetOption MeterDeltaDirection Text "🡣🡣"]

[MeasureGlucoseDelta]
Measure=WebParser
URL=[MeasureSite]
StringIndex=6
IfCondition=MeasureGlucoseDelta < 0
IfTrueAction=[!SetOption MeterGlucoseDelta FontColor 220,20,60]
IfCondition2=MeasureGlucoseDelta > 0
IfTrueAction2=[!SetOption MeterGlucoseDelta FontColor 125,252,0]
DynamicVariables=1

[MeasureGlucoseTime]
Measure=WebParser
URL=[MeasureSite]
StringIndex=5

[MeasureTimeDifference]
Measure=Calc
Formula=(MeasureCurrentTime-MeasureGlucoseTime)
IfCondition=MeasureTimeDifference>3600
IfTrueAction=[!SetOption meterGlucose FontColor "255,0,0,205"][!UpdateMeter meterGlucose][!Redraw]
IfFalseAction=[!SetOption meterGlucose FontColor "#*colorText*#"][!UpdateMeter meterGlucose][!Redraw]

[MeasureTimeDifferenceFormatted]
Measure=Time
TimeStamp=[MeasureTimeDifference]
DynamicVariables=1

; ----------------------------------
; STYLES are used to "centralize" options
; ----------------------------------

[styleGlucose]
StringAlign=Left
StringCase=Upper
StringStyle=Bold
StringEffect=Shadow
FontEffectColor=0,0,0,50
FontColor=#colorText#
FontFace=#fontName#
FontSize=30
AntiAlias=1
ClipString=1

[styleLeftText]
StringAlign=Left
; Meters using styleLeftText will be left-aligned.
StringCase=None
StringStyle=Bold
StringEffect=Shadow
FontEffectColor=0,0,0,20
FontColor=#colorText#
FontFace=#fontName#
FontSize=#textSize#
AntiAlias=1
ClipString=1

[styleRightText]
StringAlign=Right
StringCase=None
StringStyle=Bold
StringEffect=Shadow
FontEffectColor=0,0,0,20
FontColor=#colorText#
FontFace=#fontName#
FontSize=#textSize#
AntiAlias=1
ClipString=1

[styleSeperator]
SolidColor=255,255,255,15

; ----------------------------------
; METERS display images, text, bars, etc.
; ----------------------------------

[meterGlucose]
Meter=String
MeterStyle=styleGlucose
MeasureName=MeasureBG
X=10
Y=1
W=100
H=40
Text=[&MeasureGlucose] 
DynamicVariables=1

[meterGlucoseDelta]
Meter=String
MeterStyle=styleLeftText
MeasureName=MeasureBG
X=1
Y=25
W=100
H=20
Text=[&MeasureGlucoseDelta] 
DynamicVariables=1

[meterPositiveDelta]
Meter=String
MeasureName=MeasurePositiveDelta
DynamicVariables=1

[MeterDeltaDirection]
Meter=String
MeterStyle=styleLeftText
MeasureName=MeasureBG
X=1
Y=7
W=100
H=20
Text=[&MeasureDeltaDirection]
DynamicVariables=1

[MeterLastMeasureMinutes]
Meter=String
MeterStyle=styleLeftText
MeasureName=MeasureTimeDifferenceFormatted
X=1
Y=40
Text=Measured %1 ago

;[meterSeperator]
;Meter=Image
;MeterStyle=styleSeperator
;X=10
;Y=52
;W=100
;H=1
This should give you some ideas to make the skin change as you want.

EDIT: Use the difference in a time measure as the TimeStamp value to return a formatted view.
364def49db
Posts: 9
Joined: December 19th, 2021, 3:12 am

Re: How to subtract epoch times from WebParser and if X minutes old it will change font type

Post by 364def49db »

So I'm getting different results, when I use the code you give me and the meter it shows different "minutes" at almost every refresh. I'm talking 2 to 7, then 6, and 3 etc....

You mention dividing by 3600, but I do not see it anywhere in the code.

If I do the math manually say on this timestamp:

{"status":[{"now":1640141503742}],"bgs":[{"sgv":"106","trend":4,"direction":"Flat","datetime":1640141425000,"bgdelta":2,"iob":0,"cob":0}],"cals":[]}

1640141503742-1640141425000 = 78742
User avatar
SilverAzide
Rainmeter Sage
Posts: 2610
Joined: March 23rd, 2015, 5:26 pm

Re: How to subtract epoch times from WebParser and if X minutes old it will change font type

Post by SilverAzide »

364def49db wrote: December 22nd, 2021, 1:38 am So I'm getting different results, when I use the code you give me and the meter it shows different "minutes" at almost every refresh. I'm talking 2 to 7, then 6, and 3 etc....

You mention dividing by 3600, but I do not see it anywhere in the code.

If I do the math manually say on this timestamp:

{"status":[{"now":1640141503742}],"bgs":[{"sgv":"106","trend":4,"direction":"Flat","datetime":1640141425000,"bgdelta":2,"iob":0,"cob":0}],"cals":[]}

1640141503742-1640141425000 = 78742
Those timestamp values appear to be in milliseconds, so: 78742 / 1000 = 78.742 seconds. If so, you'd need to do this:

Code: Select all

[MeasureTimeDifference]
Measure=Calc
Formula=((MeasureCurrentTime-MeasureGlucoseTime) / 1000)
IfCondition=MeasureTimeDifference>3600
...
Gadgets Wiki GitHub More Gadgets...
364def49db
Posts: 9
Joined: December 19th, 2021, 3:12 am

Re: How to subtract epoch times from WebParser and if X minutes old it will change font type

Post by 364def49db »

I have JUST figured this out this morning, I have it working now. For simplicity for now I'm going to keep it in miliseconds.
User avatar
eclectic-tech
Rainmeter Sage
Posts: 5406
Joined: April 12th, 2012, 9:40 pm
Location: Cedar Point, Ohio, USA

Re: How to subtract epoch times from WebParser and if X minutes old it will change font type

Post by eclectic-tech »

SilverAzide wrote: December 22nd, 2021, 12:50 pm Those timestamp values appear to be in milliseconds, so: 78742 / 1000 = 78.742 seconds. If so, you'd need to do this:

Code: Select all

[MeasureTimeDifference]
Measure=Calc
Formula=((MeasureCurrentTime-MeasureGlucoseTime) / 1000)
IfCondition=MeasureTimeDifference>3600
...
Doh! :? ... Thanks for catching my mistake. I usually just use the difference value as TimeStamp in a time measure and completely overlooked converting milliseconds in the calculation.