> For the complete documentation index, see [llms.txt](https://homey.solweb.no/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://homey.solweb.no/custom-integrations/is-it-rainy-yr.no.md).

# 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.

```javascript
// 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.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://homey.solweb.no/custom-integrations/is-it-rainy-yr.no.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
