It is currently April 19th, 2024, 3:13 pm

Trouble with IfCondition and IfTrueAction

Get help with creating, editing & fixing problems with skins
BunnyHelp
Posts: 3
Joined: May 6th, 2018, 10:50 pm

Trouble with IfCondition and IfTrueAction

Post by BunnyHelp »

Hello!
Again, I'm attempting to create my first skin where the desktop background changes based on time of day, and weather.

I have 3 "weather conditions" being found - raining, snowing, or hot outside.
And 4 "time conditions" - dawn, day, night, and dusk.
The weather conditions should prioritize over time-related ones.

All of this information is gathered through a WebParser which looks at a http://wxdata.weather.com XML file.

For the weather conditions, I'm hijacking the <icon> bit, which gives a number 1-47, previously explained here https://forum.rainmeter.net/viewtopic.php?t=27709

If, say, the number of <icon> matches something that resembles snow (13), I want the desktop background to change to any random picture that lies within a specific snow-background-folder.

For this matching of <icon> to the desired condition, I'm using the IfCondition option, with a series of OR functions (paired with an ending "IfTrueAction").

My trouble is - I can't get this to work. For some reason if I "number" the first IfCondition option (Rain) and it's attaching IfTrueAction (like make it IfCondition10/IfTrueAction10), it won't do anything when it's given a proper number to make the OR statement true.
But if I leave it "blank" (Just having the option of the measure IfCondition/IfTrueAction) it won't listen to the <icon> number whatsoever - it's ALWAYS true (or at least seems to be?)

An explanation / fix would be GREATLY appreciated.



If you prefer a .rmskin file (I would), here you go:
https://mega.nz/#!yaIEnJga!-3NweN8ntMykRYvcUmfYwe78exm_e8GGsWfa9gcefw4
Note: The file is fairly hefty (217 MB) because of the 1440p / 4K background images.



If not, my code / wall of text:

Code: Select all

[Rainmeter]
Update=1000
DynamicWindowSize=1
AccurateText=1

[Metadata]
Name=AdventureTimeChangeBackground
Author=BunnyHelp
Information=
Version=In Development 1.0
License=Creative Commons Attribution - Non - Commercial - Share Alike 3.0

[Variables]
WeatherCodeLocation=USOH1000
WeatherUnit=m

[Lua]
Measure=Script
ScriptFile=#@#SunRiseSet.lua



;==============================================
;-------------------MEASURES-------------------
;==============================================

;-------------------WEATHER--------------------
;==============================================


[MeasureWeatherXML]
Measure=WebParser
UpdateRate=1000
Url=http://xml.weather.com/weather/local/#WeatherCodeLocation#?cc=*&unit=#WeatherUnit#&dayf=6
RegExp=(?siU)<tmp>(.*)</tmp>.*<t>(.*)</t>.*<icon>(.*)</icon>.*<sunr>(.*)</sunr>.*<suns>(.*)</suns>.*


[MeasureWeatherTemp]
Measure=WebParser
Url=[MeasureWeatherXML]
StringIndex=1

[MeasureWeatherDescriptionToday]
Measure=WebParser
Url=[MeasureWeatherXML]
StringIndex=2

[MeasureWeatherIcon]
Measure=WebParser
Url=[MeasureWeatherXML]
StringIndex=3

[MeasureSunrise]
Measure=WebParser
Url=[MeasureWeatherXML]
StringIndex=4

[MeasureSunset]
Measure=WebParser
Url=[MeasureWeatherXML]
StringIndex=5


;-------------------TIME--------------------------
;=================================================

[MeasureTime]
Measure=Time
Format=%H:%M

;-------------------DATE--------------------------
;=================================================


[MeasureDate]
Measure=Time
Format=%d

[MeasureMonth]
Measure=Time
Format=of %B

[MeasureYear]
Measure=Time
Format=%Y


;-----------------WALLPAPER-----------------------
;=================================================

[MeasureWallpaperWeather]
Measure=String
IfConditionMode=1
DynamicVariables=1
UpdateDivider=1800

;Rain
IfCondition=(MeasureWeatherIcon = 0) || (MeasureWeatherIcon = 1) || (MeasureWeatherIcon = 2) || (MeasureWeatherIcon = 3) || (MeasureWeatherIcon = 4) || (MeasureWeatherIcon = 9) || (MeasureWeatherIcon = 10) || (MeasureWeatherIcon = 11) || (MeasureWeatherIcon = 12) || (MeasureWeatherIcon = 17) || (MeasureWeatherIcon = 18) || (MeasureWeatherIcon = 35) || (MeasureWeatherIcon = 37) || (MeasureWeatherIcon = 38) || (MeasureWeatherIcon = 39) || (MeasureWeatherIcon = 40) || (MeasureWeatherIcon = 45) || (MeasureWeatherIcon = 47)
IfTrueAction=[!EnableMeasure MeasureRainWallpaper][!SetWallpaper "[MeasureRainWallpaper]" Fill]


;Snow
IfCondition2=(MeasureWeatherIcon = 5) || (MeasureWeatherIcon = 6) || (MeasureWeatherIcon = 7) || (MeasureWeatherIcon = 8) || (MeasureWeatherIcon = 13) || (MeasureWeatherIcon = 14) || (MeasureWeatherIcon = 15) || (MeasureWeatherIcon = 16) || (MeasureWeatherIcon = 41) || (MeasureWeatherIcon = 42) || (MeasureWeatherIcon = 43) ||(MeasureWeatherIcon = 46)  
IfTrueAction2=[!EnableMeasure MeasureSnowWallpaper][!SetWallpaper "[MeasureSnowWallpaper]" Fill]


;Hot
IfCondition3=(MeasureWeatherTemp >= 27) || (MeasureWeatherIcon = 36)
IfTrueAction3=[!SetWallpaper "[MeasureHotWallpaper]" Fill]
IfFalseAction3=[!EnableMeasure MeasureWallpaperTime][!UpdateMeasure MeasureWallpaperTime]


[MeasureWallpaperTime]
Measure=String
DynamicVariables=1
UpdateDivider=1800
;Dawn
IfCondition4=([Lua:sunStateString] = Dawn)
IfTrueAction4=[!EnableMeasure MeasureDawnWallpaper][!SetWallpaper "[MeasureDawnWallpaper]" Fill]


;Day
IfCondition5=([Lua:sunStateString] = Day)
IfTrueAction5=[!EnableMeasure MeasureDayWallpaper][!SetWallpaper "[MeasureDayWallpaper]" Fill]



;Night
IfCondition6=([Lua:sunStateString] = Night)
IfTrueAction6=[!EnableMeasure MeasureNightWallpaper][!SetWallpaper "[MeasureNightWallpaper]" Fill]



;Dusk
IfCondition7=([Lua:sunStateString] = Dusk)
IfTrueAction7=[!EnableMeasure MeasureDuskWallpaper][!SetWallpaper "[MeasureDuskWallpaper]" Fill]

Disabled=1


[MeasureDayWallpaper]
UpdateDivider=1800
Measure=Plugin
Plugin=QuotePlugin
PathName=#@#Backgrounds\Day
FileFilter=*.png
Disabled=1


[MeasureRainWallpaper]
UpdateDivider=1800
Measure=Plugin
Plugin=QuotePlugin
PathName=#@#Backgrounds\Rain
FileFilter=*.png
Disabled=1


[MeasureSnowWallpaper]
UpdateDivider=1800
Measure=Plugin
Plugin=QuotePlugin
PathName=#@#Backgrounds\Snow
FileFilter=*.png
Disabled=1


[MeasureHotWallpaper]
UpdateDivider=1800
Measure=Plugin
Plugin=QuotePlugin
PathName=#@#Backgrounds\Hot
FileFilter=*.png
Disabled=1


[MeasureNightWallpaper]
UpdateDivider=1800
Measure=Plugin
Plugin=QuotePlugin
PathName=#@#Backgrounds\Night
FileFilter=*.png
Disabled=1

[MeasureDawnWallpaper]
UpdateDivider=1800
Measure=Plugin
Plugin=QuotePlugin
PathName=#@#Backgrounds\Dawn
FileFilter=*.png
Disabled=1

[MeasureDuskWallpaper]
UpdateDivider=1800
Measure=Plugin
Plugin=QuotePlugin
PathName=#@#Backgrounds\Dusk
FileFilter=*.png
Disabled=1

;============================================
;-------------------STYLES-------------------
;============================================
[StyleText]
FontColor=255,255,255,255
Padding=5,5,5,5
FontSize=20
StringAlign=Left
FontFace=Thunderman
Antialias=1


[StyleTextBigger]
FontColor=255,255,255,255
Padding=5,5,5,5
FontSize=32
StringAlign=Left
FontFace=Thunderman
Antialias=1

[StyleSuperscript]
FontColor=255,255,255,255
Padding=5,5,5,5
FontSize=12
StringAlign=Left
FontFace=Thunderman
Antialias=1




;============================================
;-------------------METERS-------------------
;============================================


[MeterBackground]
Meter=Image
W=520
H=700
SolidColor=20,20,20,100

;-------------------TIME---------------------------
;==================================================

[MeterTime]
Meter=String
MeasureName=MeasureTime
MeterStyle=StyleTextBigger
X=0
Y=5


;-------------------DATE---------------------------
;==================================================

[MeterDate]
Meter=String
MeasureName=MeasureDate
MeterStyle=StyleText
X=-r
Y=50r

[MeterTH]
Meter=String
Text=th
MeterStyle=StyleSuperscript
X=40r
Y=r

[MeterMonth]
Meter=String
MeasureName=MeasureMonth
MeterStyle=StyleText
X=29r
Y=r

[MeterComma]
Meter=String
Text=,
MeterStyle=StyleText
X=102r
Y=2r

[MeterYear]
Meter=String
MeasureName=MeasureYear
MeterStyle=StyleText
X=15r
Y=3r

;-------------------WEATHER------------------
;============================================

[MeterDay]
Meter=String
MeterStyle=StyleText
X=0
Y=155
Prefix="Today"

[MeterDesc]
Meter=String
MeasureName=MeasureWeatherDescriptionToday
MeterStyle=StyleText
X=r
Y=200

[MeterTemp]
Meter=String
MeasureName=MeasureWeatherTemp
MeterStyle=StyleText
X=70r
Y=1r
Postfix="°C"

;These are just for testing that information is being pulled correctly


[MeterIcon]
Meter=String
MeasureName=MeasureWeatherIcon
MeterStyle=StyleText
X=0
Y=50r

[MeterSunset]
Meter=String
MeasureName=MeasureSunset
MeterStyle=StyleText
X=r
Y=50r

[MeterSunrise]
Meter=String
MeasureName=MeasureSunrise
MeterStyle=StyleText
X=r
Y=50r

[MeterMinutesSunrise]
Meter=String
MeterStyle=StyleText
X=r
Y=50r
Text=[Lua:sunStateString]
DynamicVariables=1


[MeterPathTest]
Meter=String
MeterStyle=StyleText
X=r
Y=50r
Text=[MeasureRainWallpaper][MeasureDayWallpaper][MeasureNightWallpaper][MeasureDuskWallpaper][MeasureDawnWallpaper][MeasureHotWallpaper][MeasureSnowWallpaper]
DynamicVariables=1
The Lua file (made with the help and love of jsmorley):

Code: Select all

function Initialize()

   measureSunrise = SKIN:GetMeasure('measureSunrise')
   measureSunset = SKIN:GetMeasure('measureSunset')
   
end

function Update()

   inSunrise = measureSunrise:GetStringValue()
   inSunset = measureSunset:GetStringValue()
   
   NextChange(inSunrise, inSunset)
   
   return formatString
   
end

function NextChange(inSunrise, inSunset)

   currentTime = os.date('*t')
   secondsSinceMidnight = os.time() - os.time({year=currentTime.year, month=currentTime.month, day=currentTime.day, hour=0, min=0, sec=0})
   secondsTilMidnight = os.time({year=currentTime.year, month=currentTime.month, day=currentTime.day, hour=23, min=59, sec=59}) - os.time()
   
   riseHour = string.match(inSunrise, '(.-):')
   riseMinute = string.match(inSunrise, '.-:(%d%d)')
   setHour = string.match(inSunset, '(.-):') + 12
   setMinute = string.match(inSunset, '.-:(%d%d)')
   
   if currentTime.hour >= 0 and currentTime.hour < 12 then
      dayStateString = 'Morning'
   elseif currentTime.hour >= 12 and currentTime.hour < setHour then
      dayStateString = 'Afternoon'
   else
      dayStateString = 'Evening'
   end
   
   if os.time() >= os.time({year=currentTime.year, month=currentTime.month, day=currentTime.day, hour=setHour, min=setMinute})  then
      dayState = 'SunRiseTomorrow'
   elseif os.time() <= os.time({year=currentTime.year, month=currentTime.month, day=currentTime.day, hour=riseHour, min=riseMinute}) then
      dayState = 'SunRiseToday'
   else
      dayState = 'SunSetToday'
   end
   
   if dayState == 'SunRiseTomorrow' then
      secondsTilChange = secondsTilMidnight + (riseHour * 3600) + (riseMinute * 60)
   end
   
   if dayState == 'SunRiseToday' then
      secondsTilChange = ((riseHour * 3600) + (riseMinute * 60)) - secondsSinceMidnight
   end
   
   if dayState == 'SunSetToday' then
      secondsTilChange = os.time({year=currentTime.year, month=currentTime.month, day=currentTime.day, hour=setHour, min=setMinute}) - os.time()
   end
   
   formatTable = ConvertSeconds(secondsTilChange)
   formatString = formatTable.hours..' hours '..formatTable.mins..' minutes '..formatTable.secs..' seconds'
   
   if dayState == 'SunSetToday' then
      sunStateString = 'Day'
      changeString = 'Sunset is in '
   else 
      sunStateString = 'Night'
      changeString = 'Sunrise is in '
   end

   if os.time() >= os.time({year=currentTime.year, month=currentTime.month, day=currentTime.day, hour=setHour, min=setMinute}) - 1800 and os.time() <= os.time({year=currentTime.year, month=currentTime.month, day=currentTime.day, hour=setHour, min=setMinute}) + 1800 then
      sunStateString = 'Dusk'
   end

   if os.time() >= os.time({year=currentTime.year, month=currentTime.month, day=currentTime.day, hour=riseHour, min=riseMinute}) - 1800 and os.time() <= os.time({year=currentTime.year, month=currentTime.month, day=currentTime.day, hour=riseHour, min=riseMinute}) + 1800 then
      sunStateString = 'Dawn'
   end
   
   return formatString
   
end


function ConvertSeconds(secondsArg)
   
   local daysDiff = math.floor(secondsArg / 86400)
   local remainder = (secondsArg % 86400)
   local hoursDiff = math.floor(remainder / 3600)
   local remainder = (secondsArg % 3600)
   local minsDiff = math.floor(remainder / 60)
   local secsDiff = (remainder % 60)
   
   local elapsedTable = {days=daysDiff, hours=hoursDiff, mins=minsDiff, secs=secsDiff}   
   
   return elapsedTable
   
end
User avatar
ikarus1969
Posts: 571
Joined: February 28th, 2011, 3:20 pm
Location: Vienna, Austria

Re: Trouble with IfCondition and IfTrueAction

Post by ikarus1969 »

Hi!

The "IfCondition" only can operate with numerical values. The measure "[MeasureWeatherIcon]" retrieves a string.
So, without some adaptions, this can't work.
As always there are several ways to deal with that.

One try would be to change all the "MeasureWeatherIcon = ..." in the "IfCondition"s to "[MeasureWeatherIcon: = ...]" which should use the numerical value of the measure.

If that does not help you can define a measure "[MeasureWeatherIcon_Num]" which gives you the numerical value of the icon-number:

Code: Select all

[MeasureWeatherIcon_Num]
Measure=CALC
Formula=[MeasureWeatherIcon]
DynamicVariables=1
and use that if the "IfCondition"s.
User avatar
balala
Rainmeter Sage
Posts: 16144
Joined: October 11th, 2010, 6:27 pm
Location: Gheorgheni, Romania

Re: Trouble with IfCondition and IfTrueAction

Post by balala »

Besides ikarus1969!s reply, I'd add the followings:
  • As he said, the IfConditions can be applied only to numeric values. You can't check if a string is or isn't equal with another one. For such task you have to use the IfMatch options. So the first thing you should have to do would be to rewrite the [MeasureWallpaperTime] measure:

    Code: Select all

    [MeasureWallpaperTime]
    Measure=String
    String=[Lua:sunStateString]
    DynamicVariables=1
    ;UpdateDivider=1800
    ;Dawn
    IfMatch=Dawn
    IfMatchAction=[!EnableMeasure MeasureDawnWallpaper][!SetWallpaper "[MeasureDawnWallpaper]" Fill]
    ;Day
    IfMatch2=Day
    IfMatchAction2=[!EnableMeasure MeasureDayWallpaper][!SetWallpaper "[MeasureDayWallpaper]" Fill]
    ;Night
    IfMatch3=Night
    IfMatchAction3=[!EnableMeasure MeasureNightWallpaper][!SetWallpaper "[MeasureNightWallpaper]" Fill]
    ;Dusk
    IfMatch4=Dusk
    IfMatchAction4=[!EnableMeasure MeasureDuskWallpaper][!SetWallpaper "[MeasureDuskWallpaper]" Fill]
    Disabled=1
    As you can see, I added the String=[Lua:sunStateString] option to this measure. This way the measure gets its value through the sunStateString function of the SunRiseSet.lua script code. The IfMatch options check what this value is equal with, executing the bangs posted into the appropriate IfTrueAction option. Also take care that I commented out the UpdateDivider=1800 option, which I think isn't needed.
  • I'd also remove / comment out the UpdateDivider=1800 option of the [MeasureWallpaperWeather] measure. Again I think it isn't needed, because if it is there, when you load / refresh the skin, the value of the [MeasureWeatherIcon] measure is 0 (in the very first moment the WebParser measures - and not just - always return 0). The condition of the first IfCondition option is met this way, the bangs of IfTrueAction are executed, but due to the UpdateDivider set to 1800, when the [MeasureWeatherIcon] gets its non zero value, the IfConditions of the [MeasureWallpaperWeather] measure aren't reevaluated. Up to 1800 seconds (half hour) the appropriate IfTrueAction won't be executed and appropriate wallpaper won't be set. Removing the UpdateDivider=1800 option fixes this issue.
  • I'm not sure at all when the bangs of the IfTrueAction options of the [MeasureWallpaperWeather] measure should set the wallpaper and when those of the IfTrueAction options of the [MeasureWallpaperTime] measure. Both measures are disabled, but only the [MeasureWallpaperTime] measure is enabled through the IfFalseAction3 option of the [MeasureWallpaperWeather] measure. A disabled measure doesn1t set wallpaper (its bangs are never executed).
  • The IfConditions must be numbered from beginning in each distinct measure. You have IfCondition - IfCondition3 in the [MeasureWallpaperWeather] measure, then have continued with IfCondition4 - IfCondition7 in the [MeasureWallpaperTime] measure. You should had to restart the numbering, numbering them from 1 in the second measure (using IfCondition - IfCondition4 in the [MeasureWallpaperTime] measure). As you have them, the IfTrueAction options of the [MeasureWallpaperTime] measure are never executed (even if the measure is enabled).