It is currently March 28th, 2024, 11:42 pm

How to check if it is day or night

Get help with creating, editing & fixing problems with skins
pupihed
Posts: 2
Joined: June 2nd, 2021, 5:05 am

How to check if it is day or night

Post by pupihed »

Hey everyone,
So I recently found out about rainmeter and I really like it but I want to add a thing where all of my skins, as well as my background change based on the time of day (day or night). For the background change, I use a program called WinDynamicDesktop which works great, but I also want to change my other skins (specifically cleartext and visbubble) based on if it is day or night. I tried writing some code and the "Night" variable works when changing it manually, but I cannot get the code to change the skins automatically as it is supposed to do. I found a site that allows me to get the data for when the sun rises and sets (a photo of what the site looks like is below), but either I cannot get the code to read the data properly, or I forgot to add something in, and I was wondering if anyone could help if possible.
I will attach the photo of the site and my code below.
Thanks in advance!

The site:
Image
The code:

Code: Select all

[Rainmeter]
Update=1000

[Metadata]
Name=Theme Switcher
Author=Pupihed
Information=Switches theme based on Sunset / Sunrise

[Variables]
Night=1

[MeasureTime]
Measure=Time
Format=%#I:%M %p

[MeasureCurrentHour]
Measure=Time
Format=%#H

[MeasureCurrentMinute]
Measure=Time
Format=%#M

[MeasureCurrentSeconds]
Measure=Calc
Formula=(MeasureCurrentHour * 3600) + (MeasureCurrentMinute * 60)

[MeasureRiseSet]
Measure=WebParser
UpdateRate=600
URL=http://api.weatherapi.com/v1/astronomy.xml?key=[API KEY GOES HERE]&q=Montreal
RegExp=(?siU)<sunrise>(.*):(.*) AM</sunrise><sunset>(.*):(.*) PM</sunset>

[MeasureRiseHour]
Measure=WebParser
URL=[MeasureRiseSet]
StringIndex=1

[MeasureRiseMinute]
Measure=WebParser
URL=[MeasureRiseSet]
StringIndex=2

[MeasureSetHour]
Measure=WebParser
URL=[MeasureRiseSet]
StringIndex=3

[MeasureSetMinute]
Measure=WebParser
URL=[MeasureRiseSet]
StringIndex=4

[MeasureSunriseSeconds]
Measure=Calc
Formula=(MeasureRiseHour * 3600) + (MeasureRiseMinute * 60)

[MeasureSunSetSeconds]
Measure=Calc
Formula=((MeasureSetHour + 12) * 3600) + (MeasureSetMinute * 60)

[MeasureTestTime]
Measure=Calc
Formula=1
IfCondition=(MeasureCurrentSeconds >= MeasureSunriseSeconds) && (MeasureCurrentSeconds < MeasureSunsetSeconds)
IfTrueAction=[!SetVariable MeasureNight "0"][!UpdateMeasure "MeasureNight"]
IfFalseAction=[!SetVariable MeasureNight "1"][!UpdateMeasure "MeasureNight"]

[MeterTime]
Meter=String
MeasureName="#Night#"
DynamicVariables=1

[MeasureNight]
Measure=Calc
Formula=#Night#
IfCondition=(MeasureNight=1)
IfTrueAction=[!ActivateConfig "Cleartext" "Cleartext Pure.ini"][!ActivateConfig "Visbubble" "Wave.ini"][!DeactivateConfig "CleartextLight" "Cleartext Pure.ini"][!DeactivateConfig "VisbubbleLight" "Wave.ini"]
IfFalseAction=[!ActivateConfig "CleartextLight" "Cleartext Pure.ini"][!ActivateConfig "VisbubbleLight" "Wave.ini"][!DeactivateConfig "Cleartext" "Cleartext Pure.ini"][!DeactivateConfig "Visbubble" "Wave.ini"]
DynamicVariables=1
User avatar
death.crafter
Rainmeter Sage
Posts: 1399
Joined: April 24th, 2021, 8:13 pm

Re: How to check if it is day or night

Post by death.crafter »

pupihed wrote: June 2nd, 2021, 5:14 am
The code:

Code: Select all


[MeasureTestTime]
Measure=Calc
Formula=1
IfCondition=(MeasureCurrentSeconds >= MeasureSunriseSeconds) && (MeasureCurrentSeconds < MeasureSunsetSeconds)
IfTrueAction=[!SetVariable MeasureNight "0"][!UpdateMeasure "MeasureNight"]
IfFalseAction=[!SetVariable MeasureNight "1"][!UpdateMeasure "MeasureNight"]

1. You are using [!SetVariable MeasureNight "0"] while it should be [!SetVariable Night "0"]

2. Check your log to see if the data you got is correct.
pupihed wrote: June 2nd, 2021, 5:14 am

Code: Select all

[MeterTime]
Meter=String
MeasureName="#Night#"
DynamicVariables=1
3. It's Text=#Night# and not MeasureName=#Night#.
from the Realm of Death
User avatar
balala
Rainmeter Sage
Posts: 16110
Joined: October 11th, 2010, 6:27 pm
Location: Gheorgheni, Romania

Re: How to check if it is day or night

Post by balala »

pupihed wrote: June 2nd, 2021, 5:14 am I found a site that allows me to get the data for when the sun rises and sets (a photo of what the site looks like is below), but either I cannot get the code to read the data properly, or I forgot to add something in, and I was wondering if anyone could help if possible.
Yes, you did. The Sunrise and Sunset times are not one after the other in the html code, there is a new line between them. So you have to modify a little bit the RegExp option of the [MeasureRiseSet] measure in the following form: RegExp=(?siU)<sunrise>(.*):(.*) AM</sunrise>.*<sunset>(.*):(.*) PM</sunset> (see with red what have I added).
Besides this I also would remove the AM / PM elements of the same RegExp option. Probably in most cases and for most places around the world this is true, however I'd tend to renounce to them: RegExp=(?siU)<sunrise>(.*):(.*) .M</sunrise>.*<sunset>(.*):(.*) .M</sunset>. Probably a much better solution would be to acquire the AM / PM strings and modify the formulas of the [MeasureSunriseSeconds] and [MeasureSunSetSeconds] measure accordingly to get the appropriate case into account as well. This is possible and it's not even to hard to implement. If you are interested please let me know to assist you to modify the code accordingly.
Also take into account what death.crafter have wrote, he's right. Is extremely important his first observation (related to the name of variable, which has to be Night, not MeasureNight, in the IfTrueAction and IfFalseAction options of the [MeasureTestTime] measure).

And a comment: posting screenshots with either code of skins, or html code (or any other type of code) is completely useless. No one will type all that code to can check it. So, please instead of such screenshots, post better a short editable code. Thanks.
pupihed
Posts: 2
Joined: June 2nd, 2021, 5:05 am

Re: How to check if it is day or night

Post by pupihed »

Thank you guys!
It works perfectly now. And sorry about the screenshot, balala, I'll keep that in mind for next time. And about modifying the code to acquire the am/pm strings and modify the formulas of the measure seconds, I am interested to implement that in if that makes my code more efficient, if it is not too much work for you, balala.
Thanks again and stay safe!
User avatar
balala
Rainmeter Sage
Posts: 16110
Joined: October 11th, 2010, 6:27 pm
Location: Gheorgheni, Romania

Re: How to check if it is day or night

Post by balala »

pupihed wrote: June 3rd, 2021, 6:24 pm Thank you guys!
It works perfectly now. And sorry about the screenshot, balala, I'll keep that in mind for next time. And about modifying the code to acquire the am/pm strings and modify the formulas of the measure seconds, I am interested to implement that in if that makes my code more efficient, if it is not too much work for you, balala.
Thanks again and stay safe!
Add the following [MeasureRiseAMPM] and [MeasureSetAMPM] measures and modify the [MeasureRiseSet], [MeasureRiseHour], [MeasureRiseMinute], [MeasureSetHour], [MeasureSetMinute], [MeasureSunriseSeconds] and [MeasureSunSetSeconds] measures as it follows:

Code: Select all

[MeasureRiseSet]
Measure=WebParser
UpdateRate=600
URL=http://api.weatherapi.com/v1/astronomy.xml?key=[API KEY GOES HERE]&q=Montreal
RegExp=(?siU)<sunrise>(.*):(.*) (.{2})</sunrise>.*<sunset>(.*):(.*) (.{2})</sunset>

[MeasureRiseHour]
Measure=WebParser
URL=[MeasureRiseSet]
StringIndex=1

[MeasureRiseMinute]
Measure=WebParser
URL=[MeasureRiseSet]
StringIndex=2

[MeasureRiseAMPM]
Measure=WebParser
URL=[MeasureRiseSet]
StringIndex=3
Substitute="AM":"0","PM":"1"

[MeasureSetHour]
Measure=WebParser
URL=[MeasureRiseSet]
StringIndex=4

[MeasureSetMinute]
Measure=WebParser
URL=[MeasureRiseSet]
StringIndex=5

[MeasureSetAMPM]
Measure=WebParser
URL=[MeasureRiseSet]
StringIndex=6
Substitute="AM":"0","PM":"1"

[MeasureSunriseSeconds]
Measure=Calc
Formula=((( MeasureRiseHour * ( 1 - (( MeasureRiseHour = 12 ) && ( [MeasureRiseAMPM] = 0 ))) + 12 * [MeasureRiseAMPM] ) * 3600 ) + ( MeasureRiseMinute * 60 ))
DynamicVariables=1

[MeasureSunSetSeconds]
Measure=Calc
Formula=((( MeasureSetHour * ( 1 - (( MeasureSetHour = 12 ) && ( [MeasureSetAMPM] = 0 ))) + 12 * [MeasureSetAMPM] ) * 3600 ) + ( MeasureSetMinute * 60 ))
DynamicVariables=1
(take care not to forget to add the DynamicVariables=1 options to the [MeasureSunriseSeconds] and [MeasureSunSetSeconds] measures, because without them the measures doesn't properly work, but they didn't have them).
Does it work?