It is currently April 26th, 2024, 11:24 pm

Display image based on sunrise / sunset measures

Get help with creating, editing & fixing problems with skins
User avatar
xenium
Posts: 867
Joined: January 4th, 2018, 9:52 pm

Display image based on sunrise / sunset measures

Post by xenium »

I would like an image to be displayed when the time is in the range between sunrise and sunset.
What's wrong with the code below?

Code: Select all

[Rainmeter]
Update=1000


[Variables]
UpdateRateSeconds=600

URL=https://www.foreca.com/Bulgaria/Sofia

[MeasureSunrise]
Measure=WebParser
Url=#URL#
RegExp=(?siU)Sun rise: <strong>(.*)</strong>
UpdateRate=#UpdateRateSeconds#
StringIndex=1

[MeasureSunset]
Measure=WebParser
Url=#URL#
RegExp=(?siU)Sun set:  <strong>(.*)</strong>
UpdateRate=#UpdateRateSeconds#
StringIndex=1

[MeasureTime]
Measure=Time
Format=%H:%M
IfCondition=(MeasureTime >= MeasureSunrise) && (MeasureTime <= MeasureSunset)
IfTrueAction=[!ShowMeter "SUN"]
IfFalseAction==[!HideMeter "SUN"]

[MeterSunrise]
Meter=String
MeasureName=MeasureSunrise
X=0
Y=0
W=120
H=60
FontSize=8
FontColor=250,250,250,250
StringStyle=Normal
StringEffect=SHADOW
FontFace=arial
StringAlign=Left
FontEffectColor=0,0,0,80
AntiAlias=1
Prefix="Sunrise: "

[MeterSunset]
Meter=String
MeasureName=MeasureSunset
X=0
Y=15r
W=120
H=60
FontSize=8
FontColor=250,250,250,250
StringStyle=Normal
StringEffect=SHADOW
FontFace=arial
StringAlign=Left
FontEffectColor=0,0,0,80
AntiAlias=1
Prefix="Sunset: "

[SUN]
Meter=String
FontSize=20
FontWeight=400
x=0
y=50r
FontColor=255,255,255,255
SolidColor=47,47,47,255
Padding=5,5,5,5
AntiAlias=1
Text=SUN
Hidden=1
Thank you
User avatar
SilverAzide
Rainmeter Sage
Posts: 2611
Joined: March 23rd, 2015, 5:26 pm

Re: Display image based on sunrise / sunset measures

Post by SilverAzide »

Just a guess, but assuming your WebParser measures are returning values, then the problem is that your condition in MeasureTime is trying to compare a number to a string. MeasureSunrise/MeasureSunset need to be converted with Time measures to be actual timestamp values, then the MeasureTime condition (once adjusted to reference the time measures) will work as you expect.
Gadgets Wiki GitHub More Gadgets...
User avatar
balala
Rainmeter Sage
Posts: 16174
Joined: October 11th, 2010, 6:27 pm
Location: Gheorgheni, Romania

Re: Display image based on sunrise / sunset measures

Post by balala »

SilverAzide wrote:Just a guess, but assuming your WebParser measures are returning values, then the problem is that your condition in MeasureTime is trying to compare a number to a string. MeasureSunrise/MeasureSunset need to be converted with Time measures to be actual timestamp values, then the MeasureTime condition (once adjusted to reference the time measures) will work as you expect.
SilverAzide is right, the IfCondition can't be applied to Strings, just to numbers.
To fix the issue, I'd add a few measures, to convert the Sunrise and Sunset to numerical values. I'd multiply the number of hours with 24 (the number of hours within a day) and would add the number of minutes (as you probably figured out, this has to be done twice: once for the Sunrise and secondly for the Sunset). This way you get a number between 0 (midnight) and 1440 (the number of minutes within a day). But to can do all this, you have to add four String meters, which extract the hour and minute of Sunrise and Sunset ([MeasureSunriseHour], [MeasureSunriseMinute], [MeasureSunsetHour] respectively [MeasureSunsetMinute]), then have to add Calc measures to calculate the numerical values associated with the Sunrise and Sunset ([MeasureSunriseMoment] and [MeasureSunsetMoment]).
The current time also has to be converted in a similar way, to number. So, you need a Time measure to get the hour ([MeasureHour]) and another to get the minute ([MeasureMinute]), then one to make the needed conversion ([MeasureTime]). This last measure will also have the appropriately modified IfCondition options.
All this being said, here is the modified code:

Code: Select all

[Rainmeter]
Update=1000

[Variables]
UpdateRateSeconds=600
URL=https://www.foreca.com/Bulgaria/Sofia

[MeasureSunrise]
Measure=WebParser
Url=#URL#
RegExp=(?siU)Sun rise: <strong>(.*)</strong>
UpdateRate=#UpdateRateSeconds#
StringIndex=1

[MeasureSunriseHour]
Measure=String
String=[MeasureSunrise]
RegExpSubstitute=1
Substitute="^(\d{1,2}):\d{1,2}$":"\1"
DynamicVariables=1

[MeasureSunriseMinute]
Measure=String
String=[MeasureSunrise]
RegExpSubstitute=1
Substitute="^\d{1,2}:(\d{1,2})$":"\1"
DynamicVariables=1

[MeasureSunriseMoment]
Measure=Calc
Formula=( 60 * [MeasureSunriseHour] + [MeasureSunriseMinute] )
DynamicVariables=1

[MeasureSunset]
Measure=WebParser
Url=#URL#
RegExp=(?siU)Sun set:  <strong>(.*)</strong>
UpdateRate=#UpdateRateSeconds#
StringIndex=1

[MeasureSunsetHour]
Measure=String
String=[MeasureSunset]
RegExpSubstitute=1
Substitute="^(\d{1,2}):\d{1,2}$":"\1"
DynamicVariables=1

[MeasureSunsetMinute]
Measure=String
String=[MeasureSunset]
RegExpSubstitute=1
Substitute="^\d{1,2}:(\d{1,2})$":"\1"
DynamicVariables=1

[MeasureSunsetMoment]
Measure=Calc
Formula=( 60 * [MeasureSunsetHour] + [MeasureSunsetMinute] )
DynamicVariables=1

[MeasureHour]
Measure=Time
Format=%#H

[MeasureMinute]
Measure=Time
Format=%#M

[MeasureTime]
Measure=Calc
Formula=( 60 * MeasureHour + MeasureMinute )
IfCondition=((#CURRENTSECTION#>=MeasureSunriseMoment)&&(#CURRENTSECTION#<=MeasureSunsetMoment))
IfTrueAction=[!ShowMeter "SUN"]
IfFalseAction=[!HideMeter "SUN"]

[MeterSunrise]
Meter=String
MeasureName=MeasureSunrise
X=0
Y=0
W=120
H=60
FontSize=8
FontColor=250,250,250,250
StringStyle=Normal
StringEffect=SHADOW
FontFace=arial
StringAlign=Left
FontEffectColor=0,0,0,80
AntiAlias=1
Prefix="Sunrise: "

[MeterSunset]
Meter=String
MeasureName=MeasureSunset
X=0
Y=15r
W=120
H=60
FontSize=8
FontColor=250,250,250,250
StringStyle=Normal
StringEffect=SHADOW
FontFace=arial
StringAlign=Left
FontEffectColor=0,0,0,80
AntiAlias=1
Prefix="Sunset: "

[SUN]
Meter=String
FontSize=20
FontWeight=400
x=0
y=50r
FontColor=255,255,255,255
SolidColor=47,47,47,255
Padding=5,5,5,5
AntiAlias=1
Text=SUN
Hidden=1
Note that in the posted code the equality sign is doubled into the IfFalseAction option of the [MeasureTime] measure. I also fixed this.
User avatar
xenium
Posts: 867
Joined: January 4th, 2018, 9:52 pm

Re: Display image based on sunrise / sunset measures

Post by xenium »

balala wrote:SilverAzide is right, the IfCondition can't be applied to Strings, just to numbers.
To fix the issue, I'd add a few measures, to convert the Sunrise and Sunset to numerical values. I'd multiply the number of hours with 24 (the number of hours within a day) and would add the number of minutes (as you probably figured out, this has to be done twice: once for the Sunrise and secondly for the Sunset). This way you get a number between 0 (midnight) and 1440 (the number of minutes within a day). But to can do all this, you have to add four String meters, which extract the hour and minute of Sunrise and Sunset ([MeasureSunriseHour], [MeasureSunriseMinute], [MeasureSunsetHour] respectively [MeasureSunsetMinute]), then have to add Calc measures to calculate the numerical values associated with the Sunrise and Sunset ([MeasureSunriseMoment] and [MeasureSunsetMoment]).
The current time also has to be converted in a similar way, to number. So, you need a Time measure to get the hour ([MeasureHour]) and another to get the minute ([MeasureMinute]), then one to make the needed conversion ([MeasureTime]). This last measure will also have the appropriately modified IfCondition options.
All this being said, here is the modified code:

Code: Select all

[Rainmeter]
Update=1000

[Variables]
UpdateRateSeconds=600
URL=https://www.foreca.com/Bulgaria/Sofia

[MeasureSunrise]
Measure=WebParser
Url=#URL#
RegExp=(?siU)Sun rise: <strong>(.*)</strong>
UpdateRate=#UpdateRateSeconds#
StringIndex=1

[MeasureSunriseHour]
Measure=String
String=[MeasureSunrise]
RegExpSubstitute=1
Substitute="^(\d{1,2}):\d{1,2}$":"\1"
DynamicVariables=1

[MeasureSunriseMinute]
Measure=String
String=[MeasureSunrise]
RegExpSubstitute=1
Substitute="^\d{1,2}:(\d{1,2})$":"\1"
DynamicVariables=1

[MeasureSunriseMoment]
Measure=Calc
Formula=( 60 * [MeasureSunriseHour] + [MeasureSunriseMinute] )
DynamicVariables=1

[MeasureSunset]
Measure=WebParser
Url=#URL#
RegExp=(?siU)Sun set:  <strong>(.*)</strong>
UpdateRate=#UpdateRateSeconds#
StringIndex=1

[MeasureSunsetHour]
Measure=String
String=[MeasureSunset]
RegExpSubstitute=1
Substitute="^(\d{1,2}):\d{1,2}$":"\1"
DynamicVariables=1

[MeasureSunsetMinute]
Measure=String
String=[MeasureSunset]
RegExpSubstitute=1
Substitute="^\d{1,2}:(\d{1,2})$":"\1"
DynamicVariables=1

[MeasureSunsetMoment]
Measure=Calc
Formula=( 60 * [MeasureSunsetHour] + [MeasureSunsetMinute] )
DynamicVariables=1

[MeasureHour]
Measure=Time
Format=%#H

[MeasureMinute]
Measure=Time
Format=%#M

[MeasureTime]
Measure=Calc
Formula=( 60 * MeasureHour + MeasureMinute )
IfCondition=((#CURRENTSECTION#>=MeasureSunriseMoment)&&(#CURRENTSECTION#<=MeasureSunsetMoment))
IfTrueAction=[!ShowMeter "SUN"]
IfFalseAction=[!HideMeter "SUN"]

[MeterSunrise]
Meter=String
MeasureName=MeasureSunrise
X=0
Y=0
W=120
H=60
FontSize=8
FontColor=250,250,250,250
StringStyle=Normal
StringEffect=SHADOW
FontFace=arial
StringAlign=Left
FontEffectColor=0,0,0,80
AntiAlias=1
Prefix="Sunrise: "

[MeterSunset]
Meter=String
MeasureName=MeasureSunset
X=0
Y=15r
W=120
H=60
FontSize=8
FontColor=250,250,250,250
StringStyle=Normal
StringEffect=SHADOW
FontFace=arial
StringAlign=Left
FontEffectColor=0,0,0,80
AntiAlias=1
Prefix="Sunset: "

[SUN]
Meter=String
FontSize=20
FontWeight=400
x=0
y=50r
FontColor=255,255,255,255
SolidColor=47,47,47,255
Padding=5,5,5,5
AntiAlias=1
Text=SUN
Hidden=1
Note that in the posted code the equality sign is doubled into the IfFalseAction option of the [MeasureTime] measure. I also fixed this.
...and I thought it was something simple ... :D
Thank you very much ! :bow:
User avatar
balala
Rainmeter Sage
Posts: 16174
Joined: October 11th, 2010, 6:27 pm
Location: Gheorgheni, Romania

Re: Display image based on sunrise / sunset measures

Post by balala »

Glad to help.
Please test the code for a day and let me know if it does work as intended.
User avatar
balala
Rainmeter Sage
Posts: 16174
Joined: October 11th, 2010, 6:27 pm
Location: Gheorgheni, Romania

Re: Display image based on sunrise / sunset measures

Post by balala »

And in the meantime just one more thing you should have to do: there is no reason to have two different parent WebParser measures. As known, the WebParser measures should have to have a parent-child construction.
So, replace the [MeasureSunrise] and [MeasureSunset] measures (as I said both parent WebParser measures), with the following three measures (in this approach [MeasureSunriseSet] is the parent measure, while [MeasureSunrise] and [MeasureSunset] are the child measures):

Code: Select all

[MeasureSunriseSet]
Measure=WebParser
Url=#URL#
RegExp=(?siU)Sun rise: <strong>(.*)</strong>.*Sun set:  <strong>(.*)</strong>
UpdateRate=#UpdateRateSeconds#

[MeasureSunrise]
Measure=WebParser
Url=[MeasureSunriseSet]
StringIndex=1

[MeasureSunset]
Measure=WebParser
Url=[MeasureSunriseSet]
StringIndex=2
User avatar
xenium
Posts: 867
Joined: January 4th, 2018, 9:52 pm

Re: Display image based on sunrise / sunset measures

Post by xenium »

balala wrote:Glad to help.
Please test the code for a day and let me know if it does work as intended.
It works and for the day.
We used locations from Greenland (where the sun sets after 23:00, 23:30) :D
Thanks again for your help!
User avatar
balala
Rainmeter Sage
Posts: 16174
Joined: October 11th, 2010, 6:27 pm
Location: Gheorgheni, Romania

Re: Display image based on sunrise / sunset measures

Post by balala »

You're welcome.
User avatar
xenium
Posts: 867
Joined: January 4th, 2018, 9:52 pm

Re: Display image based on sunrise / sunset measures

Post by xenium »

A question:
If, remove the "#" sign from [MeasureHour] and [MeasureMinute], affect [MeasureTime] in the above code?
I want to add a clock in the skin and use the two measures without "#" sign.
User avatar
balala
Rainmeter Sage
Posts: 16174
Joined: October 11th, 2010, 6:27 pm
Location: Gheorgheni, Romania

Re: Display image based on sunrise / sunset measures

Post by balala »

xenium wrote:If, remove the "#" sign from [MeasureHour] and [MeasureMinute], affect [MeasureTime] in the above code?
I want to add a clock in the skin and use the two measures without "#" sign.
Try it out, but probably won't be a problem. The Formula and IfCondition options are using the numeric values of the measures, so having the leading zeros won't affect how the code is working.