# Telia data plan

The following homeyscript will login and parse the total GB and remaining GB of your Telia mobile subscription. Name it `telia.js`, or similar:

```javascript
const between = function(str, strf, strt) {
    return str.split(strf).pop().split(strt)[0].trim();
}

if (args.length === 0) {
    throw new Error("Cannot run without arguments. Format is:  mobilenumber;password")
}

let a = args[0].split(';')
let mobile = a[0].trim()
let password = a[1].trim()

let postbody = {
    "Username": mobile,
    "Password": password
}
let cookie = "cookieConsentLevel=3; ROUTEID_CSS=.NCNO-1; ROUTEID_RECSS=.NCNO-1; _gcl_au=1.1.1071439771.1588404619; _gid=GA1.2.693156927.1588404620; _fbp=fb.1.1588404619754.1570612504; _dc_gtm_UA-3214365-1=1; _hjid=8ca343c8-c972-4794-8ff6-7156d0adb5d5; nmstat=1588404710200; _scid=bb633357-5e50-42b9-b694-ba82d0a707ac; _ga=GA1.1.1888154566.1588404619; _ga_75H3PZJWHY=GS1.1.1588404619.1.0.1588404621.58"
const response = await fetch("https://min-side.telia.no/re/api/mssa-proxy/no/rs/auth/basic?goToMinbedrift=false", {
  "headers": {
    "accept": "application/json",
    "accept-language": "nb-NO,nb;q=0.9,no;q=0.8,nn;q=0.7,en-US;q=0.6,en;q=0.5",
    "authorization": "Basic bXNzYS1wcm94eS11c2VyOnJmVGV0VzVQblA=",
    "cache-control": "no-cache",
    "client-id": "css",
    "content-type": "application/json",
    "sec-fetch-dest": "empty",
    "sec-fetch-mode": "cors",
    "sec-fetch-site": "same-origin",
    "cookie": cookie
  },
  "referrer": "https://min-side.telia.no/re/login?CT_ORIG_URL=https%3A%2F%2Fwww.telia.no%2Fmin-side",
  "referrerPolicy": "no-referrer-when-downgrade",
  "body": JSON.stringify(postbody),
  "method": "POST",
  "mode": "cors"
});

//console.log("Response", response)

let raw = response.headers.raw()['set-cookie']
let rawd = raw[0].split(';')
let session = null
rawd.forEach(el => {
    let dc = el.split('=')
    if (dc[0] === 'CTSESSION') {
        session = dc[1]
    }
})

cookie += '; CTSESSION=' + session


const response2 = await fetch("https://www.telia.no/forbruk/?ts=1588404892199", {
  "headers": {
    "accept": "*/*",
    "accept-language": "nb-NO,nb;q=0.9,no;q=0.8,nn;q=0.7,en-US;q=0.6,en;q=0.5",
    "sec-fetch-dest": "empty",
    "sec-fetch-mode": "cors",
    "sec-fetch-site": "same-origin",
    "x-requested-with": "XMLHttpRequest",
    "cookie": cookie
  },
  "referrer": "https://www.telia.no/forbruk/",
  "referrerPolicy": "no-referrer-when-downgrade",
  "body": null,
  "method": "GET",
  "mode": "cors"
});
const body = await response2.text()

let total = between(body, '<p class="donut-chart__value">', '</p><p class="donut-chart__value-caption">igjen av totalt</p>')
let remaining = between(body, '<p class="donut-chart__value-caption">', '</p></div></div></div></div></div><div class="chart-legend__container">')
// console.log(body)

let tot = parseFloat(total.split(' ')[0] )
let rem = parseFloat(remaining.split(' ')[0])

console.log("total "+ tot)
console.log("remaining " + rem)

await setTagValue("telia_" + mobile + "_tot", {type: 'number', title:'Telia [' + mobile + '] total'}, tot)
await setTagValue("telia_" + mobile + "_rem", {type: 'number', title:'Telia [' + mobile + '] remaining'}, rem)

return true
```

{% hint style="warning" %}
The script ignores unit, only reads the value. If your plan is 200MB, the unit that will be presented in Telia minside is in MB and not GB.

The script uses the minside, which is not a good way to fetch the data. The reason is that I did not find any proper API by Telia.
{% endhint %}

In order to update data, create a flow like this:

![Example flow fetching subscription data for three mobile subscriptions](/files/-M6LMoILV9Mia2dFdPwk)

The homeyscript above takes one paramter in this format:   `12345678;password`

The number is your mobile number, and the password is your password in Telia minside. If you enter wrong password a few subsequent attempts, Telia will block your password, and you will need to reset the password.

The homeyscript will create a tag for the total available data for this month, and another tag for the remaining data for this month.

The flow above add a Logic **Set numeric variable** to store the results.

![Logic view showing total and remaining data plans in GB.](/files/-M6LPg76W--63Z5nmAu1)


---

# Agent Instructions: 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/telia-data-plan.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.
