As more and more open datasets approach the point where they’re receiving “real time” updates, the topic of how to receive push or webhook notifications when a dataset is updated and matches certain conditions. For example, you might want to be notified when there are crimes in your neighborhood, when your local government releases a new dataset, or when the current number of outstanding pot hole requests go over a certain threshold.
Currently (or at least as of when this article is posted), Socrata Publica doesn’t support such notifications, but with a few open source tools, you can create incredibly powerful workflows alert you based on changes in open data (and do the rest of your bidding)!
We’re going to use an open source tool called Huginn to create our custom workflows. If you’re familiar with IFTTT, Huginn will seem conceptually similar - it allows you to set up triggers and actions that occur based on them. However, it is far more powerful - Huginn workflows can branch, have conditionals, make API calls, and even execute arbitrary JavaScript.
This tutorial will walk you through a simple scenario: My commute each morning takes me across the Aurora Bridge in Seattle, a high-level bridge that is prone to icing when it gets cold enough in the winter. The City of Seattle has recently published real-time road sensor readings that include road temperature readings.
This tutorial assumes a couple of things:
Starting from our source dataset, I want to take the average of the last five road temperature readings and determine if they are below freezing. That will smooth out any momentary drops in the road temperature. So, I want to:
https://data.seattle.gov/resource/ivtm-938t.json
$where
filter to only get the readings for the Aurora Bridge: stationname = 'AuroraBridge'
$order
the results from latest to oldest: datetime DESC
$limit
myself to only 5
results$select
to aggregate the results with a AVG(roadsurfacetemperature)
That last bit is a bit tricky, since it needs to be applied after all the other work is done. Don’t fret, because we have a SoQL feature that helps with that. Using the sub-query functionality of $query
, we can chain our aggregation after the rest of our query.
The full query looks like the below, and outputs a single value representing the average of the last 5 sensor readings:
Our first step in Huginn will be to create a “Website Agent” to make our API call and turn the result into an event for our workflow:
Name
: Name your agent. I called mine “Fetch Aurora Bridge rolling average surface temperature”Schedule
: Choose how often you want your agent to check the API. I chose “5 min”Sources
: Leave blankPropagate Immediately
: You can leave this unchecked, but I’m impatient and check itRecievers
: Leave blank for nowOptions
: Copy the details from the screenshot. For url
, use the full URL for your query from above.In this next step, we’ll use a “Trigger Agent” to conditionally pass on the events generated by our Website Agent and turn them into alerts to be messaged about.
Name
: Name your agent. I named mine “Is the bridge freezing?”Sources
: Select the agent you created in Step 1Propagate Immediately
: I’m impatient, so I check this box. If you leave it unchecked, you’ll need to wait for Huginn to pass on your events with each check it does, and it may take several minutes to be notified.Receivers
: Leave this blank for nowOptions
section of your agent configuration, match the details from the screenshot to the right. Most importantly:
type
: The type of check to perform. We want to see if our value is less than or equal to freeing, so we use field<=value
path
: The JSON output by your SoQL query and extracted by your Website Agent, in my case rolling_average
value
: The value to check against, 32
message
: The message we want to format and pass on to the next step. It’s a Liquid-templated string, and we used Watch out! Temperature has reached degrees and the bridge may be icy!
This is where the rubber hits the road! We’ll be setting up a “Twilio Agent” to send us a text message via Twilio when the above criteria is met.
Heads Up! Twilio is a paid service, and if you want to send actual text messages, you’ll need to add a credit card to your account. If you just want to try things out, you can use your test credentials, but the workflow won’t send actual alerts.
Follow the steps below to set up your Twilio Agent:
Name
: Name your agent. I called mine “Text message me an alert”Sources
: Select the agent you created in Step 2Propagate Immediately
: You can leave this unchecked, but I’m impatient and check itOptions
, make yours look similar to the screenshot, filling in the details below based on your credentials in Twilio:
account_sid
and auth_token
: Your account SID and secret auth token from your Twilio account detailssender_cell
: The phone number Twilio is configured to send fromreceiver_cell
: The cell phone number you want the text message to go toreceive_text
: Must be set to true
to have the agent send a text messagereceive_call
: Must be set to false
expected_receive_period_in_days
: Set this to however often you expect this agent to receive a “bridge frozen” event from it source. The agent will wait this long before setting a flag to note that it might be broken. I set mine to 180
, which might not be long enough in Seattle.At this point you have a few options to test things out:
value
in your Trigger Agent to trigger at a much higher temperature.To create an agent that can emit fake events:
Name
: Name your agent. I called mine “Let’s pretend its freezing!”Receivers
: Select the agent you created in Step 2Next, use your Manual Event Agent to fake an event that makes it look like the temperature dropped below freeing:
rolling_average
-150
Your fake event will then propagate through the system, and if everything is configured properly, you’ll get a text message to your device!
By now, hopefully your mind is buzzing with ideas of how you can use Huginn to monitor and alert based on open data! Please let us know what you come up with!
After sleeping on this post, I actually realized we could skip the “Trigger Agent” by modifying our SoQL query slightly. By using the $having
filter on our aggregation, we can make it only return a temperature value when its less than our specified threshold:
Don’t be surprised if the query above doesn’t output any records when you click on it, that’s the point! It’ll only return a rolling_average
when its 32 degrees or less. This would allow us to connect our Website Agent directly to our Twilio Agent, simplifying our workflow! However, its also harder to test, and we’d need to format our message either with aggregation in our SELECT
or with an additional “Liquid Output Agent” before the Twilio Agent. That exercise is left up to the reader!