Page 1 of 1
Checking if a variable is equal to either one of two values
Posted: 17 Nov 2014 13:19
by Skoinks
Hey guys!
Well, I'm trying to set up a flow which changes my wallpaper based on the current weather. I'm using the HTTP get function to get my data from an xml file. It all works fine up to the moment where I try to check whether my "forecast" variable is equal to either "sunny" or "clear" (I want to use the same wallpaper for either state). If I check whether
it works perfectly fine. I tried using a logical operator, so it'd go:
but it tells me "Condition not executed successfully. Could not convert value 'sunny' to boolean. I understand what the problem is, but I can't figure out a different way to do it. My other alternative is to do an extra condition check for every state, which would be ok for this example, but a bit down the line, I'd end up with maybe 35 different checks for each possible weather state. Any ideas?
Re: Checking if a variable is equal to either one of two val
Posted: 17 Nov 2014 13:48
by kintrupf
Skoinks wrote:forecast == "sunny" OR "clear"
This should work:
Code: Select all
(forecast == "sunny") OR (forecast == "clear")
Re: Checking if a variable is equal to either one of two val
Posted: 17 Nov 2014 13:57
by Skoinks
It seems so obvious now... Thank you very much!
Re: Checking if a variable is equal to either one of two val
Posted: 17 Nov 2014 17:23
by Bushmills
in case of many items to compare against, this may be an alternative, put into a Condition:
Code: Select all
containsElement(newList("sunny", "clear", "foggy", "hailing"),forecast)
For your wallpaper selector, another route could be taken, avoiding conditionals and comparisons completely:
create a map, with names of weather conditions as keys. Assign the name of the desired wallpaper to each. Then,
simply look up the wallpaper name, using forecast as key.
Re: Checking if a variable is equal to either one of two val
Posted: 17 Nov 2014 19:15
by Skoinks
Well, the reason I'm grouping them isn't because I need a list of all the possible weather conditions, but rather because there are some weather types that I want to use the same wallpaper for. For example rain and shower use one; cloudy and overcast a different one, etc.
Do you think the way you describe would be better in that case?
I really appreciate the help, by the way, I'm glad there's a nice community around Automagic!
Re: Checking if a variable is equal to either one of two val
Posted: 17 Nov 2014 19:32
by Bushmills
I'd probably use the approach with the map, because if I ever wanted to change wallpaper for a specific forecast, all I needed is specifying the name for the corresponding forecast, rather than fiddling with logic and comparisons. This makes it more maintenance friendly. Also, it becomes more evident whether and how all weather conditions have been dealt with, reducing the chance that I may have missed some.
Re: Checking if a variable is equal to either one of two val
Posted: 18 Nov 2014 23:06
by mcyber
Bushmills wrote:in case of many items to compare against, this may be an alternative, put into a Condition:
Code: Select all
containsElement(newList("sunny", "clear", "foggy", "hailing"),forecast)
..............................
Very elegant and inspiring solution.
Re: Checking if a variable is equal to either one of two val
Posted: 19 Nov 2014 12:15
by Bushmills
Working around maps, and using lists, is another possibility:
Code: Select all
conditions = newList("sunny", "clear", "cloudy", "rain", "thunderstorm", "snow");
wallpapers = newList("sun", "sun", "cloud", "cloud", "bolt", "flake");
wallpaper = getElement(wallpapers,indexOfElement(conditions,forecast))+".png"
You may want to test first whether forecast is member of conditions, or catch the case that it isn't with an exception.
Re: Checking if a variable is equal to either one of two val
Posted: 26 Feb 2015 05:08
by crisalarcons
Is there any way to check if a variable value is "different" than a given value? Opposite for "equal". Something like if {variable1} <>(? different) "Ok"
Re: Checking if a variable is equal to either one of two val
Posted: 26 Feb 2015 06:47
by MURTUMA
Operator != means inequal.