This forum uses cookies
This forum makes use of cookies to store your login information if you are registered, and your last visit if you are not. Cookies are small text documents stored on your computer; the cookies set by this forum can only be used on this website and pose no security risk. Cookies on this forum also track the specific topics you have read and when you last read them. Please confirm that you accept these cookies being set.

Get temperature data from web page
#1
Hi,

I want to get a temperature from a web page (Sea and water temperature) and send it to a group address. The temperature i want is "Vatten" for "Yngsjö havsbad".

The closest i could find in the forum was this script, post #5, Forum post. But i get stuck when it comes to "find matching string and fetch the data you want with".

Does anyone have any ideas how this could be done? Any tips or pointing me in a better direction?

Best regards //Simon
Reply
#2
require('ssl.https')

data = ssl.https.request('https://www.havochvatten.se/badplatser-och-badvatten/vattenprov-badtemperatur/vattentemperatur-och-kvalitet-pa-badvatten-pa-sydkusten.html')
log(data)
Reply
#3
Try this:
Code:
url = 'https://www.havochvatten.se/badplatser-och-badvatten/vattenprov-badtemperatur/vattentemperatur-och-kvalitet-pa-badvatten-pa-sydkusten.html'
data = require('socket.http').request(url)

lines = data:split('\n')

for _, line in ipairs(lines) do
  if line:find('<tr><td headers="head11">') then
    location = line:match('<tr><td headers="head11">([^,]+)')
    watertemp = tonumber(line:match('<td headers="head13">([^ ]+).+</td>'))
    airtemp = tonumber(line:match('<td headers="head14">([^ ]+).+</td>'))
    location = location:gsub('&#xAD;', '')

    log(location, watertemp, airtemp)
  end
end
Reply
#4
Great, but how do i split up the log to send the value i want?

I tried this code, it sends a value but not corectly. It first sends the value 5,6 310 times, then 13 other values once. At least all the values are from the correct table.

Code:
url = 'https://www.havochvatten.se/badplatser-och-badvatten/vattenprov-badtemperatur/vattentemperatur-och-kvalitet-pa-badvatten-pa-sydkusten.html'
data = require('socket.http').request(url)

lines = data:split('\n')

for _, line in ipairs(lines) do
  if line:find('<tr><td headers="head11">') then
    location = line:match('<tr><td headers="head11">([^,]+)')
    watertemp = tonumber(line:match('<td headers="head13">([^ ]+).+</td>'))
    airtemp = tonumber(line:match('<td headers="head14">([^ ]+).+</td>'))
    location = location:gsub('&#xAD;', '')

    log(location, watertemp, airtemp)
  end
badtemp = watertemp
grp.write('10/5/101', badtemp)
end


   

   
Reply
#5
Which values exactly do you want and in which group addresses?
Reply
#6
(11.04.2023, 14:02)admin Wrote: Which values exactly do you want and in which group addresses?

I want the first value (5 ℃) from this row: Yngsjö havsbad, Kristian­stad 5 ℃ 8.1 ℃ (see attached pictures)

Group adress: 10/5/101

   

   
4.9 ℃
6.5 ℃
Reply
#7
Try this:
Code:
url = 'https://www.havochvatten.se/badplatser-och-badvatten/vattenprov-badtemperatur/vattentemperatur-och-kvalitet-pa-badvatten-pa-sydkusten.html'
data = require('socket.http').request(url)

pos = data:find('Yngsjö havsbad')
watertemp = tonumber(data:match('<td headers="head13">([^ ]+).+</td>', pos))

grp.checkwrite('10/5/101', watertemp)
Reply
#8
Thanks, works perfectly!

//Simon
Reply
#9
Hi,

Since you kindly helped me last time, they've change the website and now i have to update the script..

Is anyone willing to help me, again?

Source URL
Value marked with blue.
   

Best regards //Simon
Reply
#10
(23.08.2023, 13:49)Ohmarn Wrote: Hi,

Since you kindly helped me last time, they've change the website and now i have to update the script..

Is anyone willing to help me, again?

Source URL
Value marked with blue.


Best regards //Simon

Used ChatGTP for this:

Code:
url = 'https://www.havochvatten.se/badplatser-och-badvatten/vattenprov-badtemperatur/vattentemperatur-och-kvalitet-pa-badvatten-pa-sydkusten.html'
data = require('socket.http').request(url)

-- Function to extract the temperature for a specific location
local function extractTemperatureForLocation(html, locationName)
    local locationStartIdx, locationEndIdx = html:find(locationName)
    if not locationStartIdx then
        return nil
    end

    local temperatureStartIdx, temperatureEndIdx, temperature = html:find("(%d+%.%d+)°C", locationEndIdx)
    if not temperatureStartIdx then
        return nil
    end

    return temperature
end

-- Location name to search for
local locationName = "Yngsjö havsbad"

-- Extract temperature for the specific location
local temperature = extractTemperatureForLocation(data, locationName)

if temperature then
    -- Print the extracted temperature
    --log(locationName .. " temperature: " .. temperature .. "°C")
      grp.checkwrite('10/5/101', temperature)
else
    log(locationName .. " temperature not found.")
end
Reply


Forum Jump: