A simple homeyscript fetching the yr.no APIs hourly and keeping a few betterlogic varaibles updated with information about the rain condition the last four hours and the next hour.
// Please update lat and lot to match your location!constlat='63.75'constlon='10.2638'// ---- o ---- o ---- o ---- o ---- o ----constapifurl='https://api.met.no/weatherapi/locationforecast/2.0/compact?lat='+ lat +'&lon='+ lonconstapinurl='https://api.met.no/weatherapi/nowcast/2.0/complete?lat='+ lat +'&lon='+ lonconstbetterlogic=awaitHomey.apps.getApp({id:"net.i-dev.betterlogic"})constgetVar=async (name) => {let x =awaitbetterlogic.apiGet(name)if (!x) thrownewError("Could not find variable ["+ name +"]")returnx.value}asyncfunctiongetForecast() {console.log("Fetching "+ apifurl)let resp =awaitfetch(apifurl, {"method":"GET","headers": {"Accept":"application/json","User-Agent":"AthomHomey Smart home github.com/andreassolberg" } });//console.log("Status: " + resp.status + " " + resp.statusText)if (!resp.ok) {returnnull } let data =awaitresp.json()return data}asyncfunctiongetNow() {console.log("Fetching "+ apinurl)let resp =awaitfetch(apinurl, {"method":"GET","headers": {"Accept":"application/json","User-Agent":"AthomHomey Smart home github.com/andreassolberg" } });//console.log("Status: " + resp.status + " " + resp.statusText)if (!resp.ok) {returnnull } let data =awaitresp.json()let precip_hour =Number.parseFloat(data.properties.timeseries[0].data.next_1_hours.details.precipitation_amount)let precip_now =Number.parseFloat(data.properties.timeseries[0].data.instant.details.precipitation_rate)return [precip_hour, precip_now]}let nowdata =awaitgetNow()if (nowdata ===null) returnfalse;//let forecast = await getForecast()console.log("Data: ")//console.log(nowdata)//console.log(forecast)let history =JSON.parse(awaitgetVar('yr_history'))if (!_.isArray(history)) { history = []}if (history.length>=5) {history.shift()}history.push(nowdata[1])let rainsum =_.sum(history)let rainy = (rainsum >=1.5)console.log(history)console.log("Sum is "+ rainsum)awaitbetterlogic.apiPut("yr_precip_hour/"+encodeURIComponent(nowdata[0]))//betterlogic.apiPut("yr_precip_now/" + encodeURIComponent(nowdata[1]))awaitbetterlogic.apiPut("yr_precip/"+encodeURIComponent(rainsum))awaitbetterlogic.apiPut("yr_rainy/"+encodeURIComponent(JSON.stringify(rainy)))awaitbetterlogic.apiPut("yr_history/"+encodeURIComponent(JSON.stringify(history)))returntrue
You need to prepare and create three variables in better logic. Install better logic app, if you do not have it already. These are the variables:
Variable name
type
Initial value
yr_precip_hour
number
0
yr_rainy
boolean
false
yr_history
string
null
yr_precip
number
0
Next, you need to setup a flow that runs once ever hour that executes the homeyscript above.
Now, you can create flows that triggers when the variables yr_precip_hour or yr_rainy changes.
yr_rainy is true if the precipation sum of the last four hours and the next hour is more than 1.5 mm.