Premise Integrating weather alerts into your system

Motorola Premise

etc6849

Senior Member
Here's a few examples of how to use weather alerts from the Weather Underground module user 123 created.  
 
The idea is your system can text your phone, make announcements, pause your movie, change to the weather channel, flash lights, etc when an alert of predefined severity is received.
 
Under Home.Weather.Alerts add this "OnChangeAlertsTotal" script.

if sysevent.newVal > 0 then
' iff an alert was added
if sysevent.oldVal < sysevent.newVal then
system.addTimer 5,"gWeatherAlert",1,"AlertNotification_" & this.ObjectID
end if
end if

The script calls a global script gWeatherAlert.
 
gWeatherAlert will text my phone, and even announce a warning (if it is severe enough)
 
gWeatherAlert looks like this:

function gWeatherAlert()
' email alert
dim oAlert
for each oAlert in Home.Weather.Alerts.GetObjectsByType(Schema.Modules.WeatherUnderground.Classes.Alert.Path)
tSubject = oAlert.AlertPhenomena & "--" & oAlert.AlertDescription
iSeverity = gGetWeatherSeverity(oAlert)
tBody = "SEVERITY LEVEL: " & iSeverity & "...EXPIRES: " & oAlert.AlertExpiryDateTime & "..." & oAlert.AlertMessage
if iSeverity >= 0 then
gTextPhones tBody, tSubject
end if

if iSeverity > 3 then
' announce warning
gMuteAndPause ' mute and pause all media zones
Devices.CustomDevices.M1_Panel.Voice.WeatherWarning.Play = true
for each oMediaCenter in devices.CustomDevices.GetObjectsByType(Modules.Microsoft.Classes.MediaCenter.Path,false)
oMediaCenter.Send_Message "Possible Tornado or Hurricane Warning",Now,300
next
end if
next
set oAlert = nothing
end function

You'll have to modify the code under the two "if iSeverity >...then" statements.  What is there now will not be compatible with your system.
 
gGetWeatherSeverity is the global script that correlates a user defined severity to the Weather Underground U.S. alert type.  The idea is you tailor the iSeverity variable to meet your needs.  For my area, I am most concerned with Tornado and Hurricane warnings, so I wrote code to give these a higher iSeverity than advisories and watches.
 
gGetWeatherSeverity looks like this:

function gGetWeatherSeverity(oAlert)
iSeverity = 0
bValid = true
sAlertType = trim(lcase(oAlert.AlertType))
sAlertMessage = oAlert.AlertMessage
bWarning = false
bWatch = false

select case sAlertType
case "hur" ' hurricane advisory
iSeverity = 4
case "tor" ' tornado warning
iSeverity = 5
case "tow" ' tornado watch
iSeverity = 4
case "wrn" ' thunderstorm warning
iSeverity = 3
case "sew" ' thunderstorm watch
iSeverity = 1
case "win" ' winter adv
iSeverity = 1
case "flo" ' flood warning
iSeverity = 2
case "wat" ' flood watch
iSeverity = 0
case "wnd" ' high wind advisory
iSeverity = 1
case "svr" ' severe weather statement
iSeverity = 0
case "hea" ' heat adv
iSeverity = 0
case "fog" ' fog
iSeverity = 0
case "spe" ' special weather statement
iSeverity = 0
case "fir" ' fire adv
iSeverity = 4
case "vol" ' volcanic activity
iSeverity = 5
case "hww" ' hurricane wind warning
iSeverity = 5
case "rec" ' record set
iSeverity = 0
case "rep" ' public reports
iSeverity = 0
case "pub" ' public information statement
iSeverity = 0
case else
bValid = false
end select

' if no valid identifier, then use keywords
if bValid = false then
' is there a potential warning
if instr(1,sAlertMessage,"warning",1) then
bWarning = true
elseif instr(1,sAlertMessage,"watch",1) then
bWatch = true
end if

if bWatch then
if instr(1,sAlertMessage,"tornado",1) then
iSeverity = 4
elseif instr(1,sAlertMessage,"hurricane",1) then
iSeverity = 4
elseif instr(1,sAlertMessage,"thunderstorm",1) then
iSeverity = 2
end if
elseif bWarning then
if instr(1,sAlertMessage,"tornado",1) then
iSeverity = 5
elseif instr(1,sAlertMessage,"hurricane",1) then
iSeverity = 5
elseif instr(1,sAlertMessage,"thunderstorm",1) then
iSeverity = 3
end if
end if
end if

gGetWeatherSeverity = iSeverity
end function

A few keywords are searched for if there is an invalid AlertType.  This is probably not necessary, but I have seen a WU alert that was missing its AlertType once, so I put the keywords in for the more severe weather warnings just to be safe.
 
As a disclaimer do not use this as a weather alert radio.  Premise will only grab alert updates every 15 minutes and 30 minutes is the default.  This may not be enough of a warning for tornados!!!
 
You can modify sys://Schema/Modules/WeatherUnderground/Classes/Weather/OnChangeUpdateInterval and sys://Schema/Modules/WeatherUnderground/Classes/Weather/StartUpdateTimer to allow more frequent updates.  I changed my system to get rid of the 15 minute limit and I am using 5 minutes.
 
StartUpdateTimer

'
if this.UpdateInterval > 1 then
system.addTimer this.UpdateInterval * 60, "this.Update = true", 1, "Weather_" & this.ObjectID
end if

OnChangeUpdateInterval
Code:
'
' Validate UpdateInterval
if sysevent.newVal > 0 and sysevent.newVal < 1 then
	this.UpdateInterval = 30
else
	if sysevent.newVal > 1 then
		this.StartUpdateTimer()
	else
		this.RemoveUpdateTimer()
	end if
end if
 
Very nice! I added a visual alert and popup to the Mb, as well. After I get my latest effort completed, I'll give this a try...

btw, you should be able tu use the ssmtp module and an old SMS via email post I had to send these out...although maybe you have it here and I missed it
 
The global script is using the ssmtp module; I just didn't post everything.  However, I realized that the weather module purges the alerts and then re-adds them every time it updates.  This resulted in too many emails, so I modified the code which now has a gEmailUserOnce global script.  It works great!  Of course, any small change to the alert description results in another email, but I'm ok with that.  I've included all of the global scripts below, but there maybe some needed customization to match your system as I have alerts show up in media center and announce them over my Elk M1G too.
 
Code:
function gWeatherAlert()
	' email alert
	dim oAlert
	for each oAlert in Home.Weather.Alerts.GetObjectsByType(Schema.Modules.WeatherUnderground.Classes.Alert.Path)
		tSubject = oAlert.AlertPhenomena & "--" & oAlert.AlertDescription
		iSeverity = gGetWeatherSeverity(oAlert)
		tBody = "SEVERITY LEVEL: " & iSeverity & "...EXPIRES: " & oAlert.AlertExpiryDateTime & "..." & oAlert.AlertMessage
		if iSeverity >= 0 then
			gEmailAllUsersOnce tBody, tSubject
		end if

		if iSeverity > 3 then
			' announce warning
			gMuteAndPause	' mute and pause all media zones
			Devices.CustomDevices.M1_Panel.Voice.WeatherWarning.Play = true
			for each oMediaCenter in devices.CustomDevices.GetObjectsByType(Modules.Microsoft.Classes.MediaCenter.Path,false)
				oMediaCenter.Send_Message "Possible Tornado or Hurricane Warning",Now,300
			next
		end if
	next
	set oAlert = nothing
end function

function gGetWeatherSeverity(oAlert)
	iSeverity = 0
	bValid = true
	sAlertType = trim(lcase(oAlert.AlertType))
	sAlertMessage = oAlert.AlertMessage
	bWarning = false
	bWatch = false
	
	select case sAlertType
		case "hur"	' hurricane advisory
			iSeverity = 4
		case "tor"	' tornado warning
			iSeverity = 5
		case "tow"	' tornado watch
			iSeverity = 4
		case "wrn"	' thunderstorm warning
			iSeverity = 3
		case "sew"	' thunderstorm watch
			iSeverity = 1
		case "win"	' winter adv
			iSeverity = 1
		case "flo"	' flood warning
			iSeverity = 2
		case "wat"	' flood watch
			iSeverity = 0
		case "wnd"	' high wind advisory
			iSeverity = 1
		case "svr"	' severe weather statement
			iSeverity = 0
		case "hea" 	' heat adv
			iSeverity = 0
		case "fog"	' fog
			iSeverity = 0
		case "spe"	' special weather statement
			iSeverity = 0
		case "fir"	' fire adv
			iSeverity = 4
		case "vol"	' volcanic activity
			iSeverity = 5
		case "hww"	' hurricane wind warnintg
			iSeverity = 5
		case "rec"	' record set
			iSeverity = 0
		case "rep"	' public reports
			iSeverity = 0
		case "pub"	' public information statement
			iSeverity = 0
		case else
			bValid = false
	end select

	' if no valid identifier, then use keywords
	if bValid = false then
		' is there a potential warning 
		if instr(1,sAlertMessage,"warning",1) then
			bWarning = true
		elseif instr(1,sAlertMessage,"watch",1) then
			bWatch = true
		end if
		
		if bWatch then
			if instr(1,sAlertMessage,"tornado",1) then
				iSeverity = 4
			elseif instr(1,sAlertMessage,"hurricane",1) then
				iSeverity = 4
			elseif instr(1,sAlertMessage,"thunderstorm",1) then
				iSeverity = 2
			end if
		elseif bWarning then
			if instr(1,sAlertMessage,"tornado",1) then
				iSeverity = 5
			elseif instr(1,sAlertMessage,"hurricane",1) then
				iSeverity = 5
			elseif instr(1,sAlertMessage,"thunderstorm",1) then
				iSeverity = 3
			end if
		end if
	end if
		
	gGetWeatherSeverity = iSeverity
end function



function gEmailUserOnce(sTextBody, sSubject, sEmailAddress)
	bMatch = false
	for each oMailMessage in devices.CustomDevices.SSMTP.MailLog.GetObjectsByType(modules.SSMTP.Classes.MailMessage)
		if oMailMessage.MailSubject = sSubject then
			if oMailMessage.MailTextBody = sTextBody then
				if oMailMessage.MailTo = sEmailAddress then
					bMatch = true
				end if
			end if
		end if
	next
	
	if not bMatch then
		' Emails iff duplicate message is NOT already in the sent mail log
		devices.CustomDevices.SSMTP.MailTo = sEmailAddress
		devices.CustomDevices.SSMTP.MailSubject = sSubject
		devices.CustomDevices.SSMTP.MailTextBody = sTextBody
		devices.CustomDevices.SSMTP.MailSend = true
	end if
end function

function gEmailAllUsersOnce(sStatus, sSubject)
	gEmailUserOnce sStatus, sSubject, "[email protected]"
end function

function gMuteAndPause()
	' mute and pause all currently selected media devices
	' called by gDoorbell and gIncomingCall
	for each oAVInput in Devices.GetObjectsByType(Schema.Device.AudioVideoInput.Path)
		if not oAVInput.Parent.CurrentSource is nothing then
			' if the current source is the AV input being checked...
			if oAVInput.Parent.CurrentSource.Name = oAVInput.Name then
				' if the AV switch has audio control, mute the audio
				if oAVInput.Parent.IsOfExplicitType(Schema.Device.Audio.Path) then
					oAVInput.Parent.Mute = true
				end if
				' pause the selected source
				if not oAVInput.Source is nothing then
					if oAVInput.Source.IsOfExplicitType(Schema.Device.Transport.Path) then
						oAVInput.Source.SetValueForced "Pause",true
					end if
				end if
			end if
		end if
	next
end function
 
The biggest challenge I had was testing alerts...how did you generate test alerts? (I don't get many alerts where I live)
 
Back
Top