Inspired by Python Plugin: Most recent Dutch earthquake I wanted to make something simular but with dzVents, and for any location worldwide.
This is what I came up with so far.
It requires a text sensor to write the output to.
Code: Select all
--this scripts gets earthquake info from https://earthquake.usgs.gov for a defined area and then updates a text sensor with Magnitude and location
--it takes the first entry (= the most recent one) and therefore runs every ten minutes
--alternatively you could run it less frequently and search through the results and look for a specific location string
--the search area can be either be a rectangular (defined by the coordinates of the 4 corners) or circular zone (defined by the coordinates of the center
--and set the radius in km
--if you are curious, or to test it, you could remove the coordinates from the url and get worldwide info
return {
on = {
timer = { 'every 10 minutes'},
httpResponses = { 'kwakeV2' } -- matches callback string below
},
execute = function(domoticz, triggerItem)
-----use these variables if you want to define a square on the map for the detection area
local minlatitude = 35
local maxlatitude = 68
local minlongitude = -12
local maxlongitude = 35
-----use these variables if you want to define a radius
local latitude = 47
local longitude = 3
local maxradiuskm = 2000
--change the url in the openURL part below to this one if you want to define a radius around a central point
--https://earthquake.usgs.gov/fdsnws/event/1/query?format=geojson&limit=10&latitude='..latitude..'&longitude='..longitude..'&maxradiuskm='..maxradiuskm
--
function titleCase( first, rest )
return first:upper()..rest:lower()
end
local sensor = domoticz.devices('Quakes')
local currInfo = tostring(sensor.text)
if (triggerItem.isTimer) then
domoticz.openURL({
url = 'https://earthquake.usgs.gov/fdsnws/event/1/query?format=geojson&limit=10&minlatitude='..minlatitude..'&maxlatitude='..maxlatitude..'&minlongitude='..minlongitude..'&maxlongitude='..maxlongitude,
method = 'GET',
callback = 'kwakeV2'
})
elseif (triggerItem.isHTTPResponse) then
local response = triggerItem
if (response.ok and response.isJSON) then
local mgt = tonumber(response.json.features[1].properties.mag)
local lugar = tostring(response.json.features[1].properties.place)
local cuando = tostring(response.json.features[1].properties.time)
local t = string.sub(cuando, 1,10)
local reString = os.date('%H:%M %a %d %B %Y', t)
lugar = string.gsub(lugar, "(%a)([%w_']*)", titleCase)
local qInfo = tostring('Magnitude of '.. mgt.. ' in '..lugar.. ' at '..reString)
if qInfo ~= currInfo then
sensor.updateText(qInfo)
end
end
else
print('**kwake failed to fetch info')
end
end
}
In this script I get the latest event (and therefore it runs every 10 minutes)