Rules and Expressions with Open Weather API

I don’t know if this helps, but this is what I use.

weather = $context.response.data
alerts = isEmpty(weather.alerts) ? [] : weather.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 today!"
1 Like

Thank you, that worked!! Looks pretty similar, I guess the problem was with initializing the arrays in the first two lines.

Hi Vinod_K, when you trigger your rule, do you get [" before and "] after the alert text? I am, and not sure what is causing the extraneous characters.

image

Hmm, I’ll take a look as I haven’t seen an alert for a while.

@josh any ideas how to fix the formatting that @T_Shoaf pointed out? I tried a couple different ways with the replace function but most I could do is get rid of the square brackets.

The join() in your example isn’t doing anything.

Originally, join() was being called as the last line in the expression, so the result of the join() method was being output as the result of the expression. In your current example, you’re calling join(), but not assigning the output of it anywhere, so it’s effectively a meaningless method call. You would want to assign the result of the join() method call to a variable and then reference that variable.

weather = $context.response.data
alerts = isEmpty(weather.alerts) ? [] : weather.alerts
hasAlerts = count(alerts) > 0


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

hasAlerts ? myString : "No alerts today!"

The only change I’ve made to your expression is in the second to last line to assign the output of the join() method call to the variable that you use later.

1 Like

Thank you both! I made the change, but no alerts in my area right now so I’ll update after I get to see the alert formatting. Thanks again!

Unreal! Thanks Josh. That worked…here I am reinventing the wheel lol

1 Like

Well, I think it is set up, but I can’t get it to evalute the variables. Here is my rule flow

I added all the excludes because the reponse included everything. Even with the excludes, the excluded stuff is still being received.

I am setting two variables. After the script runs, they evaluate to NaN, which I assume is Not a Number.

Any thoughts on what I am doing wrong?

It looks like your expressions are formatted incorrectly. Each step of the expression has to be on its own line.

:no_entry_sign: WRONG
So instead of:

positions = $context.response.data.current.temp toFixed(positions,1)

 

:white_check_mark: RIGHT
You would need each step on its own line for the syntax to be valid:

positions = $context.response.data.current.temp 
toFixed(positions,1)

The exclude would need to be a single parameter with a comma separated list:

&exclude=hourly,minutely,daily,alert

All is good now. Thanks!

I thought all was well until Openweather told me I used 2000 API calls yesterday. I run a rule that should only execute every 10 minutes to update the weather info

Looking at the log, it seems to be triggering every minute

Am I doing something wrong?

It looks like you may have manually triggered your true/false timer twice. It appears to be running in duplicate.

1 Like

Is there a way to prevent more than one instance of a rule running?

I’ll preface this with a reminder that looping like this isn’t something we officially support, but there are some best-practices you can use to help with this sort of thing:

:test_tube: Unofficial Variable Loop Rule Best Practices

That being said, we are considering introducing a first class loop feature and are looking for feedback from the community on what use-cases they are using loops for (in general, event driven rules are preferred, but there are some use-cases where a loop makes sense)

Thanks @josh. I think I have it to only a single instance now with the Stays for xxx clause. This was a big deal because I was getting multiple thousand hits on OpenWeather API and it was starting to cost real money. This should take me from 2000+ pings daily to 72.

Hey Ken,
The multiple running is most likely caused by manually flipping your true/false AFTER it has already been started once. The second time triggers your rule while the first is still running. That will add another instance every time the variable is flipped.

1 Like

With the updates trigger, that’s absolutely right. The state stays trigger helps deduplicate the events which is why it’s the first thing listed in the unofficial recommendations linked above. :slight_smile:

Here’s my response from Ken’s other thread:

Yup…it looked like Ken was using DELAY, not STAYS. I’ve had relatively few issues since changing mine to STAYS, but… it’s not perfect. About 4 or 5 times a week I’ll have to restart my 10 minute weather timer. No errors or clues as to why it stops. No real complaints, just waiting for the “First Class” timer!!! LOL

Is there a way to tell it has stopped? This is one of the reasons I had parallel rules running. I have a once a day rule that resets my variables. If it hasn’t failed, I will now have two running.