Rules and Expressions with Open Weather API

This is what I use.

{{$context.response.data.daily.1.weather.0.description}}
1 Like

I should have been clear that I was referring to the format only. The number after daily should correspond to the day for data is wanted.

This is really cool !.. I got this weather report ready to send it to Alexa or Pushover message…

“At this moment there is 20 C. For today there will be scattered clouds and a mild weather, with a minimum of 14 C and a maximum of 22 C. Enjoy your day.”

I will be to make the same but for a night report for next day weather …

3 Likes

Would you mind sharing your http string for pulling the today, tonight and tomorrow reports? All I seem to get is “scattered clouds”, “sunny” etc.

Hi my friend. The http call is the same, but the expression has to ask for the correct context value. For tomorrow data I am using:

$context.response.data.daily.1.weather.0.main
round($context.response.data.daily.1.temp.min)
round($context.response.data.daily.1.temp.max)

“1” value is the 2nd array of the context response (tomorrow) and “0” value is the 1st array of the weather context response (weather data), also you can use “description” instead “main” for a deeply data of the weather. I hope this helps !

2 Likes

Hi Friends !
Does anybody knows how to get the day of the week (DOW) from forecast daily “dt” (UTC time stamp) value ?

I know for example that 1646326800: is “Thu Mar 03 2022 17:00:00 GMT+0000”, but I want to get “Thu” to translate to the Spanish “Jue”.

THANKS !

Hi @Carlos_Juarez-
This Custom Tiles tile should automatically display the days of week based on the language code you enter. Are you perhaps referring to the work you were doing in the Rules to Query Open Weather where you are querying the weather with rules?

1 Like

Hi Josh… Oh yes !.. Im referring to the rules to query Open Weather, I think in the wrong post. !

Hi @Carlos_Juarez - I merged the posts into your main Open Weather Rule API thread.

You could use an Expression to format the dt value that you mentioned. The Date Functions and Date Formats help documents might be helpful here. The formatDate() method can be used to format your timestamps to a string to get the day of week:

formatDate(now(), 'ddd', 'es')

So if you have the timestamp in seconds from the Open Weather request in a variable, we just need to convert it into milliseconds by multiplying it by 1000 to be used with the formatDate() function. You could also just reference the $context.response.data.your.value if desired.

formatDate($myTimestamp * 1000, 'ddd', 'es')
1 Like

Can someone tell me what I have wrong in these 2 expressions querying the OpenWeather API?
They are string variables and return <>

This is the what I use for wind speed but I use the manual method.


{{round($context.response.data.current.wind_speed)}} mph

It will return a string like this

3 mph

1 Like

That worked for wind speed. Still getting an error for wind gust. I reformatted gust to what you are using.

This should work.

{{round($context.response.data.current.wind_gust)}} mph

I have got this … !! Icons are animated gifs made by myself …

6 Likes

I’m using the API call to pull in this information and it works just fine. To pull it in, mine looks like this:


and the output looks like this:
image

My questions are about the json response:

  • Since the ‘alert’ section won’t always be included in the response, is there an appropriate way to process it only when data exists (maybe an if statement to check to see if it exists before processing it).
  • Since I wouldn’t know how many values for the ‘alert’ exist (today it is both wind and tornado) – how should I handle this to only process the correct number of ‘events’ that exist in the json file?
  • Is there any way to insert a carriage return into the text?

If it’s an array, using the map() concept below accounts for this.

Otherwise, using a conditional ternary expression works:

condition ? trueOperation : falseOperation

Since it’s an array, I would probably use a map() and join() to handle it.

alerts = $context.response.data.alerts
myString = map(alerts, concat(x.event, " until ", formatDate(x.end * 1000, "h:mm a")))
join(myString, "\r\n")

The General Functions in Expressions documentation covers the basics above the above functions or you can search the community as there were a few recent posts about expressions and arrays that may be helpful. This discussion links to a few Array related discussions of interest.

The \r\n in my example above inserts a new line (Carriage Return / Line Feed). It won’t show up in the variable preview or variable editor, but it will in a notification or a dashboard variable tile.

Thanks…

I’m missing something yet… Could you please share your API pull statement?

You can get it from the below, there are several topics on opening an account, getting a key and calling the OpenWeatherMap API.

josh

Aug '22

You can do this with the HTTP Action in SharpTools today. I agree that it makes for a good feature request as a native integration though to simplify this process for people who don’t have weather devices attached to their SmartThings. :smiley:

Let’s take the Open Weather /weather endpoint which provides the current weather. The URL is in the format:

https://api.openweathermap.org/data/2.5/weather?lat=-33&lon=96&appid={{ApiKey}}

And the response is in the format:

{
   "coord":{
      "lon":96,
      "lat":-33
   },
   "weather":[
      {
         "id":500,
         "main":"Rain",
         "description":"light rain",
         "icon":"10n"
      }
   ],
   "base":"stations",
   "main":{
      "temp":286.4,
      "feels_like":286.08,
      "temp_min":286.4,
      "temp_max":286.4,
      "pressure":1032,
      "humidity":88,
      "sea_level":1032,
      "grnd_level":1032
   },
   "visibility":10000,
   "wind":{
      "speed":6.89,
      "deg":143,
      "gust":7.83
   },
   "rain":{
      "1h":0.19
   },
   "clouds":{
      "all":97
   },
   "dt":1661023588,
   "sys":{
      "sunrise":1661040462,
      "sunset":1661080271
   },
   "timezone":21600,
   "id":0,
   "name":"",
   "cod":200
}

So if we wanted to get the main weather report, we would be looking for the weather.0.main element in the response. So we can make the HTTP call and then get the response data using Context Variables (Response > HTTP > Data) and use the object property notation to grab that nested property for use in our notification:

1 Like

ok, I had a OpenWeather 3.0 API and it seems to be able to pull the response. I appear to have an issue with the CONCAT statement: << expression evaluation error >>

Couple of things:

  • The one I’m using is the 3.0 version as well:
  • If you don’t have a weather update today (hopefully you don’t) - you’ll get the ‘<< expression evaluation error >>’ – but you can check by pasting your openweather API link into a browser, copying that response and then pasting into a json formatter. It’ll look something like this when you have an alert (this was mine from yesterday):
    image

What I can’t figure out is how to set a boolean variable to true if the alert exists and then set the alert field, and if its false to set to plain language such as ‘No Weather Alerts Today’

I keep trying something like this, but no luck as the $context.response.data.alerts doesn’t exist in the json

image