Checking if a variable is equal to either one of two values

Post your questions and help other users.

Moderator: Martin

Post Reply
User avatar
Skoinks
Posts: 6
Joined: 17 Nov 2014 13:12

Checking if a variable is equal to either one of two values

Post by Skoinks » 17 Nov 2014 13:19

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

Code: Select all

forecast == "sunny"
it works perfectly fine. I tried using a logical operator, so it'd go:

Code: Select all

forecast == "sunny" OR "clear"
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?

User avatar
kintrupf
Posts: 257
Joined: 10 Sep 2013 08:59

Re: Checking if a variable is equal to either one of two val

Post by kintrupf » 17 Nov 2014 13:48

Skoinks wrote:forecast == "sunny" OR "clear"
This should work:

Code: Select all

(forecast == "sunny") OR (forecast == "clear")

User avatar
Skoinks
Posts: 6
Joined: 17 Nov 2014 13:12

Re: Checking if a variable is equal to either one of two val

Post by Skoinks » 17 Nov 2014 13:57

It seems so obvious now... Thank you very much!

User avatar
Bushmills
Posts: 286
Joined: 23 Sep 2014 21:56

Re: Checking if a variable is equal to either one of two val

Post by Bushmills » 17 Nov 2014 17:23

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.

User avatar
Skoinks
Posts: 6
Joined: 17 Nov 2014 13:12

Re: Checking if a variable is equal to either one of two val

Post by Skoinks » 17 Nov 2014 19:15

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!

User avatar
Bushmills
Posts: 286
Joined: 23 Sep 2014 21:56

Re: Checking if a variable is equal to either one of two val

Post by Bushmills » 17 Nov 2014 19:32

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.

mcyber
Posts: 37
Joined: 08 Nov 2013 14:21

Re: Checking if a variable is equal to either one of two val

Post by mcyber » 18 Nov 2014 23:06

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.

User avatar
Bushmills
Posts: 286
Joined: 23 Sep 2014 21:56

Re: Checking if a variable is equal to either one of two val

Post by Bushmills » 19 Nov 2014 12:15

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.

crisalarcons
Posts: 6
Joined: 29 May 2014 01:16

Re: Checking if a variable is equal to either one of two val

Post by crisalarcons » 26 Feb 2015 05:08

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"

User avatar
MURTUMA
Posts: 697
Joined: 05 Mar 2013 22:43

Re: Checking if a variable is equal to either one of two val

Post by MURTUMA » 26 Feb 2015 06:47

Operator != means inequal.

Post Reply