Wolfram Language

Open live version

Email Freeze Alerts

Create a task in the Wolfram Cloud that checks the forecast every day and sends you an alert if freezing weather is forecast.


code

CheckForFreeze[] := Module[{temperatureData, minimumTemperature}, temperatureData = WolframAlpha[ "temperature Urbana IL", {{"TemperatureChart:WeatherData", 1}, "ComputableData"}]; minimumTemperature = Min[Cases[ temperatureData, {Append[Take[DateList[], 3] + {0, 0, 1}, ___], t_} :> t, \[Infinity]]]; If[QuantityMagnitude[ UnitConvert[minimumTemperature, "DegreesCelsius"]] < 0, SendFreezeAlertMail[]] ]
SendFreezeAlertMail[] := SendMail[ "To" -> "who@mailprovider.com", "From" -> "who@mailprovider.com", "Subject" -> "FREEZE ALERT", "Body" -> { "Freezing weather expected tomorrow in Urbana, IL ...", "\n\n", WolframAlpha["temperature Urbana, IL", IncludePods -> "TemperatureChart:WeatherData", AppearanceElements -> {"Pods"}, TimeConstraint -> {30, Automatic, Automatic, Automatic}], "\n\n", "http://www.wolframalpha.com/input/?i=temperature+Urbana+IL" }, "Server" -> "mail.mailprovider.com", "UserName" -> "who", "Password" -> "xxxxxxx", "EncryptionProtocol" -> "SSL" ]
obj = CloudSave[CheckForFreeze]
With[{file = obj}, CloudDeploy[ ScheduledTask[CloudGet[file]; CheckForFreeze[], DateObject[{_, _, _, 18, _, _}]]]]

how it works

To send yourself an email message when a freeze is forecast, (1) write a function that sends an alert email with temperature data, (2) write a function that checks the forecast for freezing weather the next day and invokes the alert mail function if it is expected, (3) save those functions in the Wolfram Cloud, and (4) schedule a task in the Wolfram Cloud that wakes up every day at 18:00 and loads and executes those functions.

The function to send an alert email is a straightforward invocation of SendMail:

SendFreezeAlertMail[] := SendMail[ "To" -> "who@mailprovider.com", "From" -> "who@mailprovider.com", "Subject" -> "FREEZE ALERT", "Body" -> { "Freezing weather expected tomorrow in Urbana, IL ...", "\n\n", WolframAlpha["temperature Urbana IL", IncludePods -> "TemperatureChart:WeatherData", AppearanceElements -> {"Pods"}, TimeConstraint -> {30, Automatic, Automatic, Automatic}], "\n\n", "http://www.wolframalpha.com/input/?i=temperature+Urbana+IL" }, "Server" -> "mail.mailprovider.com", "UserName" -> "who", "Password" -> "xxxxxxx", "EncryptionProtocol" -> "SSL" ]

The body of the message gets a plot of temperatures from Wolfram|Alpha. To obtain the expression that returns that data, evaluate == temperature Urbana IL (without quotes), and choose Formatted pod from the + menu to the right of the temperature plot in the output:

The CheckForFreeze function gets forecasted temperatures from Wolfram|Alpha and sends an alert message if any of tomorrows temperatures are below freezing:

CheckForFreeze[] := Module[{temperatureData, minimumTemperature}, temperatureData = WolframAlpha["temperature Urbana IL", {{"TemperatureChart:WeatherData", 1}, "ComputableData"}]; minimumTemperature = Min[Cases[ temperatureData, {Append[Take[DateList[], 3] + {0, 0, 1}, ___], t_} :> t, \[Infinity]]]; If[QuantityMagnitude[minimumTemperature] < 32, SendFreezeAlertMail[]] ]

To obtain the expression that returns the forecast, evaluate == temperature Urbana IL (without quotes), and choose Computable data from the + menu to the right of the temperature plot in the output:

Temperature data in the forecast are of the form {{y, m, d, h, m, s}, t}, for example,

temperatureData = WolframAlpha["temperature Urbana IL", {{"TemperatureChart:WeatherData", 1}, "ComputableData"}]; temperatureData[[1, 1, 1, 1]]

Extract the temperatures from the forecast that are tagged with tomorrow's date by forming a pattern that matches just those elements whose {y, m, d} match tomorrow's date, as follows.

DateList[] gives the current date:

DateList[]

Taking the first three elements of that list and adding 1 to the day gives tomorrows date:

Take[DateList[], 3] + {0, 0, 1}

Make a pattern that matches just those temperature forecast elements whose {y, m, d} match tomorrows date:

{Append[Take[DateList[], 3] + {0, 0, 1}, ___], t_}

Extract those temperatures with Cases and take the minimum:

minimumTemperature = Min[Cases[ temperatureData, {Append[Take[DateList[], 3] + {0, 0, 1}, ___], t_} :> t, \[Infinity]]]

That code is all that is necessary to check the forecast and send an alert message. To deploy it in the cloud, first save the CheckForFreeze function in the Wolfram Cloud:

obj = CloudSave[CheckForFreeze]

Then schedule a task to run at 18:00 every day that loads and executes CheckForFreeze:

With[{file = obj}, CloudDeploy[ ScheduledTask[CloudGet[file]; CheckForFreeze[], DateObject[{_, _, _, 18, _, _}]]]]