Skip to content
Note: Navigation options above may not work on this devices screen size, if requiring link access, please switch to Desktop layout for this session.

Add Current Weather In FreePBX

Asterisk and FreePBX allow for a myriad of customisations, some of these however need hand editing asterisk files as well as some FreePBX configuring (due to how FreePBX uses its database and overwrites some asterisk files), so some things you just can't entirely do from within FreePBX, or in asterisk (when using FreePBX that is), like set up Lenny, or provide a Weather service.

Providing a weather service is what we're concerned with setting up here in this roughly 10 minute read, it'll provide current conditions like temperature, humidity, wind speed, then read out the current 3 day forecast as well as check for any I/B Warnings.

Update August 2024 - Just be aware, that the free data site we are using - wttr.in, have recently become unreliable on occasions with stale data, as an example, its currently 36c here and has been for about one hour according to the official Met office website, they also report it's been over 32c for three hours, yet wttr.in are saying it's only 30c, this points to be out by about four and a half hours, I realise their API feeds to their data sources have limits, but IMHO it is best to report "unavailable" than data that's nearly five hours old.

Now I already hear some of you thinking it's kind of pointless in 2024 to create a dial-in weather service, in most cases, you're right, but don't fall into the false belief that everyone has a smartphone or internet connection, because they don't, if you run a hotel, motel, or retirement village (where it's actually a rather popular request) (and often very expensive to get inhouse WiFi), you'll probably find this of some use, and it's reasonably easy to set up. The first part of this article, getting current conditions, is applicable worldwide, however the second part, for actual local area forecasts, I'm afraid is Australia specific, though I'm sure many of you can figure out how to adapt it to suite your own countries local Met format.

In asterisk dial plans, the application Playback() is a call to asterisks sound files, the custom files we also use in our dial plan that do not exist are the tempfeelslike, betaforecasts, and ts-alert audio files, they along with the two main scripts AWXforecast.sh and AWXcurrent.sh are available to download here, extract the playback files into /var/lib/asterisk/sounds/en/custom and the two AWX files into /usr/local/sbin

The playback file AWXforecast is a file that will be generated on the first run of that script, so don't worry that it doesn't exist at first, this is the file that reads out the area forecast, if you are not in Australia, you may want to skip the entry altogether, as noted below.


The Dial Plan

OK let's get on with it, first up, you need to login to the command line of your FreePBX server, use your favourite editor, mine's pico, but vi and mcedit are just as good, and no, emacs is bad, very, very, bad :-D Now edit /etc/asterisk/extensions_custom.conf, at the top of this file insert the following dial plan - being careful not to wrap lines...

[Weather]
exten => weather,1,NoOp()
same => n,Set(WTEMP=${FILTER(0123456789,${CURL(file:///var/tmp/WTEMP)})})
same => n,Set(WFTEMP=${FILTER(0123456789,${CURL(file:///var/tmp/WFTEMP)})})
same => n,Set(WHUMID=${FILTER(0123456789,${CURL(file:///var/tmp/WHUMID)})})
same => n,Set(WBAROM=${FILTER(0123456789,${CURL(file:///var/tmp/WBAROM)})})
same => n,Set(WWINDS=${FILTER(0123456789,${CURL(file:///var/tmp/WWINDS)})})
same => n,Wait(1)
same => n,Playback(weather)
same => n,Wait(0.3)
same => n,Playback(currently)
same => n,Wait(0.3)
same => n,Playback(temperature)
same => n,Wait(0.1)
same => n,SayNumber(${WTEMP})
same => n,Playback(degrees)
same => n,Wait(0.3)
same => n,Playback(custom/tempfeelslike)
same => n,Wait(0.1)
same => n,SayNumber(${WFTEMP})
same => n,Playback(degrees)
same => n,Wait(0.3)
same => n,Playback(humidity)
same => n,Wait(0.1)
same => n,SayNumber(${WHUMID})
same => n,Playback(percent)
same => n,Wait(0.3)
same => n,Playback(barometric&pressure)
same => n,Wait(0.1)
same => n,SayNumber(${WBAROM})
same => n,Wait(0.3)
same => n,Playback(wind)
same => n,Wait(0.1)
same => n,SayNumber(${WWINDS})
same => n,Playback(kilometers-per-hour)
same => n,Wait(.4)
same => n,Playback(custom/betaforecast)
same => n,Wait(0.5)
same => n,ExecIf($[${STAT(e,/var/lib/asterisk/sounds/WXTS.wav)}]?Playback(WXTS))
same => n,Wait(.6)
same => n,Playback(AWXforecast)
same => n,Wait(.5)
same => n,Playback(goodbye)
same => n,Hangup()


Current Conditions

Next we need to edit the AWXcurrent.sh file we just put into /usr/local/sbin to configure our current location for local weather conditions, scroll down to line 45, where it says LOCAL set your location, it defaults to Brisbane, so change it to where ever you are or the nearest major location, you can also use ICAO airport codes (eg: Brisbane or ybbn).

Right under this, you need to get the area severe weather/thunderstorm warning IDQ page set it in the variable IDQTS, the default IDQ21035.txt. is Brisbane's, if you don't know yours, you may need to do some googling, or wait until the BoM issue a warning for your area and grab it that way.

Now add your first cron entry to roots crontab
*/20 * * * *   /usr/local/sbin/AWXcurrent.sh > /dev/null 2>&1

This gets the weather conditions once every 20 mins and stores them locally, so FreePBX/asterisk only needs to look up on-demand by reading the local files, thus avoiding the delays of remote polling which often results in considerable periods of silence whilst it retrieves the data, and reduces the workload of an under resourced and often congested, remote service. It also polls for any warnings - provided you set that up with the right IDQTS.


Forecasts

If you're not in Australia, you can probably skip this next Forecast section, you might be able to adapt it to your region but I'll be of no help, even if you're in Aus, at present you may wish to omit this as well, text to speech services that can be used in a scripted fully automated fashion are very limited, and the good ones are not generally within most peoples acceptable budget, so we're using linux's included espeak, yes - the quality is very robotic, but it does get the information out and I guess is better than nothing, there are others like vsox and festival, but they too are robotic sounding, and much of a muchness.

As I alluded to above, there does exist commercial programs with somewhat more human sounding voices for a cost, one such program is Cepstral, but I strongly advise downloading the demo and playing with it first, Cepstral still has some imperfections with a few weird pronunciations, but certainly better than espeak, festival and co, but not as good as some of the free online services, but again, the online services don't really work in scripted automation, as for Cepstrals pricing, you'll need to talk to them as they don't advertise prices on their website (you don't need the telephone version because we just need to convert text to speech and nothing on-the-fly, so just get the Linux version.)

Brief example of Weather Audio, MP3 @128 kb/s, 1m16s

Again, if you are skipping the forecasts, see earlier where I suggest taking that entry out of the dial plan - but it wont hurt leaving it in, asterisk will skip any playback files that don't exist, log an error for it, and move on (the errors not harmful so ignore it.)

OK, on to the forecasts, the first thing you need to do is go over to the BoM's web site and find the page for your local weather forecast, then, usually located at the bottom on left hand side of the page just before the footer section, is the Product ID number, take a note of that, you'll need it to enter into the next script. As an example, clicking on the image at left for my area you'll see Product IDQ10090, which when you look at our forecast script - AWXforecast.sh, you'll see reference to IDQ10090 at IDQNUM variable.

As forecasts are only issued a few times a day, we only need to run this script a few times a day, just after the typical forecast release times (see below cron entry), in /usr/local/sbin edit AWXforecast.sh and set the IDQNUM variable to suite your forecast area, you can also edit or remove the lines that cut unrelated locations if you know what you're doing, but it wont harm anything leaving them in... well, not unless you're in one of those areas :-P Remember to chmod +x both your AWX* scripts as well.

Next add a cron entry into roots crontab to update the forecast
30 1,5,11,17,21 * * *   /usr/local/sbin/AWXforecast.sh > /dev/null 2>&1

That's it for the console and scripts at the moment, don't close the console just yet though, we'll need to come back shortly to reload FreePBX to get all this into action.


Over in FreePBX GUI

Next jump on over to your FreePBX interface and go to Admin and Custom Destinations, click on Add Destination and set the following
Target:            Weather,weather,1
Description:        Weather

Then click Submit, and Apply, once it refreshes we'll tell FreePBX how you can access it, I'm using extension 161 as feature code since that wont affect anything else, now go to Applications and Misc Application, click Add Misc Applications and set the following
Enable:     Yes
Description:    Weather
Feature Code:    161
Destination:     Custom Destinations  (from the drop down)
                   Weather  (from the next drop down)

Click on Submit, and Apply, now go back to your console window and run
asterisk -rvx 'core restart when convenient'

This waits until no channels are in use then restarts asterisk.

Congrats, you're done, login to asterisk " asterisk -rvvvv " pick up a handset and dial 161... You should see the progress and hear your current actual and apparent temps, humidity, barometric pressure and wind speed, and lastly if you used it, the area forecast.


Trackbacks

No Trackbacks

Comments

Display comments as Linear | Threaded

Skylar on :

Followed this instructional, easy to set up, took me about 20 minutes. Linux really does need a better TTS, even Windows' is much better, but I guess you're paying for that so it would want to be. I checked out and opted for Cepstral, it's 99% perfect I think, cost wasn't too much, way cheaper than some other options, not sure I should be saying what it cost me, and maybe why you didn't say either, I kind of get the impression we each get custom pricing, might be wrong though.

Our family runs this 24 unit motel in Coastal SE NSW, it'll be interesting to see how much use this weather service gets, and if it doesn't do much, the cost of Cepstral wont be wasted, we can at create our own professional sounding IVR messages, which includes an ever changing what's on around town blurb for our guests, so thanks for this.

Skylar on :

Just updating my original comment, it's been 10 days now, and since we added this, and added it to our rooms hotlist cards, it's been used 29 times, sometimes a couple times a day by some guests, in particular when we had that heavy periods of wet weather, well worth the time and small investment of cepstral.

GavGav on :

Hey. I am going to confess that I read this when I saw it on facebook nearly 2 years ago, I scoffed a tit, dude it's 2020's nobody uses phones to ring for weather because of smart phones everywhere, including me, always got the latest iphone since it's inception, I kind of thought yeah okay when it comes to old timers in retirement homes and stuff, but the wife, kids and I have just got back home from a road trip to Perth for the Easter break that's nearly over and thank heavens for us they go back to school next week, anywho, along this trip, we had 2 overnight stays in motels in the middle of nowhere, zero bars, no reception anywhere outside of the bigger towns on my Telstra or wifies Vodafone, nudda. The motels both had foxtel satellite, even the land line phones was on a satellite dish,at one motel at least there was internet from NBN skymuster for the office and manager but nothing for the units.

This would have been a perfect fit for a dial weather and I had wished they had it, so man, even dudes like me who once thought you're wasting your time and this must have been some sort of joke, are humbled and can see there is a place for this type of thing, even though I have no idea what pbx they were using, I did make a suggestion to one of them, the other was a grumpy old man who didnt know what a pbx was :-)

Add Comment

E-Mail addresses will not be displayed and will only be used for E-Mail notifications.
Standard emoticons like :-) and ;-) are converted to images.
Enclosing asterisks marks text as bold (*word*), underscore are made via _word_.
Form options

Multiple DNSBL checks will be performed on submission of your comments.
Accepted comments will then be subject to moderation approval before displaying.