Can you save a URL to a global variable?


#1

Is it possible to set a global variable with a url? For example. @coomonsite = http://google.com?


#2

Sure, just use the string type for text values.


#3

Ty. Figured that out…smh. Now let’s take it one further?

@key=123456
@city=newyork
@url=‘http://weatherunderground.com/‘{@key}’/q/‘{@city}

Is that possible? To include variables to make up a complete url?

Tried to do that entering as an expression and it won’t take. Keeps coming up not set


#4

Very close, it would either be this as a value:
@url=http://weatherunderground.com/{@key}/q/{@city}

Or this as an expression (a few of many possible ways):
@url=‘http://weatherunderground.com/{@key}/q/{@city}'
@url=‘http://weatherunderground.com/‘ + @key + ’/q/' + @city


#5

It’s possible that it’s showing not set if you have yet to save the overall piston with new variables for the first time.


#6

Yeah sorry… it looks like it’s not possible to define a global variable in terms of another global. When setting a global with an Expression I always get (not set) and when setting with a Value I just get the raw value http://weatherunderground.com/{@key}/q/{@city} without the key or city substituted in.

If you need to do a lot of this I recommend using string formatting. Set the value of @url to http://weatherunderground.com/%s/q/%s then to use it in a piston use the expression format(@url, @key, @city) which will give you the full URL (read more on format). With this approach I recommend naming the global in a way that helps you remember what values it expects and in which order, like @weatherUrlFormat__key_city.

Also, since your example used Weather Underground… if you are trying to get weather for your home (i.e. the location of your hub) there is an excellent $weather system variable with probably all the data you could want.


#7

This is awesome info, did not know about the format stuff and the string pieces. Will definitely be doing some more research. Thank you.

As far as the precanned $weather. I am aware and yes, it does work Home hub location. Although some of the attributes are limited. Also, I am creating a couple of pistons to do the following, push weather alerts, current conditions and forcast conditions via text message to my phone. In addition to this I have 3 virtual switches set up to choose what city I want the info from. That is why I am actually using the weather underground api instead of the $weather expression.