Rules and Expressions with Open Weather API

I would add that you can combine that all together into a single expression to build your string or use a fallback string.

You can even do it with expression-scoped variables so you don’t have to have a bunch of other global variables if you don’t need them.

alerts = $context.response.data.alerts

hasAlerts = count(alerts) > 0

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

hasAlerts ? myString : "No alerts"

So from top to bottom:

  1. Alias the alerts data with a simpler (expression-scoped) variable
  2. Check if we even have alerts
  3. Build the alert string to format the various (potential) events
  4. If we have events, show the string, otherwise show a default message
2 Likes

Wow, this is good!

(The alerts field doesn’t exist in the feed when there are no alerts, for anyone down the road…)

I tried both the Initialize Array and the Conditionally Count options and they both work great!

Thanks so much.

1 Like

I’ve found this site to be very helpful in parsing json to give me the proper sytanx: https://jsonpathfinder.com/ just paste in the reply you get from the API and then click around to build the expression

1 Like