Is it rainy? (yr.no)

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!
const lat = '63.75'
const lon = '10.2638'
// ---- o ---- o ---- o ---- o ---- o ----

const apifurl = 'https://api.met.no/weatherapi/locationforecast/2.0/compact?lat=' + lat + '&lon=' + lon
const apinurl = 'https://api.met.no/weatherapi/nowcast/2.0/complete?lat=' + lat + '&lon=' + lon

const betterlogic = await Homey.apps.getApp({id: "net.i-dev.betterlogic"})
const getVar = async (name) => {
    let x = await betterlogic.apiGet(name)
    if (!x)  throw new Error("Could not find variable [" + name + "]")
    return x.value
}

async function getForecast() {
  console.log("Fetching " + apifurl)
    let resp = await fetch(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) {
      return null
    } 
    let data = await resp.json()
    return data
}

async function getNow() {
  console.log("Fetching " + apinurl)
  let resp = await fetch(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) {
    return null
  } 
  let data = await resp.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 = await getNow()
if (nowdata === null) return false;
//let forecast = await getForecast()

console.log("Data: ")
//console.log(nowdata)
//console.log(forecast)

let history = JSON.parse(await getVar('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)


await betterlogic.apiPut("yr_precip_hour/" + encodeURIComponent(nowdata[0]))
//betterlogic.apiPut("yr_precip_now/" + encodeURIComponent(nowdata[1]))
await betterlogic.apiPut("yr_precip/" + encodeURIComponent(rainsum))
await betterlogic.apiPut("yr_rainy/" + encodeURIComponent(JSON.stringify(rainy)))
await betterlogic.apiPut("yr_history/" + encodeURIComponent(JSON.stringify(history)))


return true

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.

Last updated