(HomeyScript) HELP with grabbing info from website

Hi,

I’m no JavaScript developer, so I humbly ask for help here.

I want to grab some information on a website, and then put it into a variable I can use with an AVD from Device Capabilities.

The website I would like to grab information from, is this:
https://www.lillesand.kommune.no/badevannstemperaturer.534462.no.html

The section I want to put in a variable in Homey is the temperature in the box named “Langedalsvatnet”. Is this possible?

I asked ChatGPT for help, and it tried to solve it using the “fetch” function. The code didn’t work properly. And since I’m no JavaScript developer, I didn’t manage to fix it either. I do, however, have some basic Python knowledge, so I have a basic understanding of how coding processes usually work.

When searching on Google, someone earlier referred to an example script doing more or less the same thing as I would like to achieve. Sadly, it seems like this script is unavailable at the moment.

Does anyone have the knowledge to help me solve this?

Thanks in advance!

Code:

// Get water temperature of Langedalsvatnet
const URL = 'https://www.lillesand.kommune.no/badevannstemperaturer.534462.no.html';

const response = await fetch(URL);
if (response.status !== 200) throw Error('Invalid response from website');

const html  = await response.text();
const match = html.match(/Langedalsvatnet.*?([\d\.]+)\s°C/s);

if (! match) throw Error('Cannot find water temperature');

return Number(match[1]);

Example flow:

2 Likes

Wow, that was fast! Thank you so much! This works great :smiley:

Are you able to do it from this link as well (the temperature underneath “Bystranda”)? I tried to just replace the link and change the header it looks for, but the code returned with an error :frowning:

That page works in a completely different way, here’s a script that you can use with the same card:

const URL = 'https://api-badetassen.eu.ngrok.io/test/get/iot/wt/data?when=last';

const response = await fetch(URL);
if (response.status !== 200) throw Error('Invalid response from website');

const data   = await response.json();
const record = data.find(d => d.site_name === 'bystranda');

if (! record) throw Error('Cannot find record');

return record.records.measValue;
1 Like

Ah, then I understand.

Thank you so much for your help! I’ll have a look at your codes and try to understand what happens.

Have a nice week! :grinning: