Logic Machine Forum
Wind speed close shutter - Printable Version

+- Logic Machine Forum (https://forum.logicmachine.net)
+-- Forum: LogicMachine eco-system (https://forum.logicmachine.net/forumdisplay.php?fid=1)
+--- Forum: Scripting (https://forum.logicmachine.net/forumdisplay.php?fid=8)
+--- Thread: Wind speed close shutter (/showthread.php?tid=2458)



Wind speed close shutter - MantasJ - 08.02.2020

Hello,

I have a task where I need to get current wind speed and if it is above certain number (in this case 12 meters/second), the shutter in outside would close. I wanted to do this from KNX weather station, but it seems it doesn't provide actual wind speed accurately, so my guess is I should propably use some API service. 

I have tried this script:


Quote:require('socket.http')

forecast = {}


function parsetag(parser, tag, attributes)
  if tag == 'time' then
    table.insert(forecast, {})
  elseif tag == 'symbol' then
    forecast[ #forecast ].symbol = attributes.name
  elseif tag == 'precipitation' then
    forecast[ #forecast ].precipitation = attributes.value
  elseif tag == 'windDirection' then
    forecast[ #forecast ].wind_direction = attributes.code
  elseif tag == 'windSpeed' then
    forecast[ #forecast ].wind_speed = attributes.mps
  elseif tag == 'temperature' then
    forecast[ #forecast ].temperature = attributes.value
  elseif tag == 'pressure' then
    forecast[ #forecast ].pressure = attributes.value
  end
end

url = 'http://www.yr.no/place/Lithuania/Klaipeda/Klaipėda/varsel.xml'
data, err = socket.http.request(url)

if data then
  require('lxp').new({
    StartElement = parsetag
  }):parse(data)

  log(forecast)
else
  alert('Fetch failed: ' .. tostring(err))
end

But it returns some odd numbers which doesn't really match the forecast from elsewhere, but then I thought maybe I just don't understand what it returns. So how data is returned? The table has 36 entries and I though maybe it is 35 from days starting from first entry (today) forecast, or no? Maybe it is some king of interval based?

Thank You


RE: Wind speed close shutter - admin - 08.02.2020

Try using hourly forecast xml: https://www.yr.no/place/Lithuania/Klaipeda/Klaip%C4%97da/forecast_hour_by_hour.xml
Wind speed is specified in miles per hour for some reason so a bit of extra conversion is required.


RE: Wind speed close shutter - Joep - 08.02.2020

There are better api's available using JSON. But first of all i would like to say that for this purpose it's better to meassure at the side where your shutter is installed. Specially if you use it for a professional installation side. Some weather API's can be found here. https://superdevresources.com/weather-forecast-api-for-developing-apps/
Below an example we use with a Dutch weather API in JSON.

require('ssl.https')
require('json')

city = 'Amsterdam'

data = ssl.https.request('http://weerlive.nl/api/json-10min.php?locatie=' ..city)
datatable = json.pdecode(data)
datatable = datatable['liveweer'][1]
--log(datatable)

temp = datatable.temp --actuele temperatuur
--log('Actuele temperatuur: ' ..temp.. '°C')

windms = datatable.windms --windsnelheid in meter per seconde
--log('Windsnelheid: ' ..windms.. ' m/s')


RE: Wind speed close shutter - Erwin van der Zwart - 08.02.2020

Hi,

I would add a check to be sure that the values from the online service are received as expected, we do the same with a signal from a local weather station (alive signal).

Make a resident / scheduled script at like this:
Code:
lastupdate = grp.find('1/1/1').updatetime
difference = os.time() - lastupdate
if difference > then 3600 then -- 1 hour not changed
   alert('No new value received from online weather service')
   -- or mail / push / sms etcetera
end
BR,

Erwin