| 
		
	
	
	
		
	Posts: 171 
	Threads: 31 
	Joined: May 2017
	
 Reputation: 
2 
	
	
		 (21.01.2019, 07:56)admin Wrote:  Use this for sunrise/sunset. Object data type must be set to text/string.
 Code: astro = data.forecast.forecastday[ 1 ].astro
 grp.write('32/4/1', astro.sunrise)
 grp.write('32/4/2', astro.sunset)
 grp.write('32/4/3', astro.moonrise)
 grp.write('32/4/4', astro.moonset)
You can also use built-in rscalc function for this: https://forum.logicmachine.net/showthread.php?tid=1010
 
thanks admin...
	 
		
	 
	
	
	
		
	Posts: 303 
	Threads: 87 
	Joined: May 2017
	
 Reputation: 
2 
	
	
		 (18.01.2019, 10:09)Daniel. Wrote:  Hi I'm doing it like that
 
 
 Code: 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/United_Kingdom/England/Farnborough/varsel.xml'
 data, err = socket.http.request(url)
 
 if data then
 require('lxp').new({
 StartElement = parsetag
 }):parse(data)
 
 
 grp.update('Weather precipitation', tonumber(forecast[1].precipitation))
 grp.update('Weather symbol', forecast[1].symbol)
 grp.update('Weather windDirection', forecast[1].wind_direction)
 grp.update('Weather windSpeed', tonumber(forecast[1].wind_speed))
 grp.update('Weather temperature', tonumber(forecast[1].temperature))
 grp.update('Weather pressure', tonumber(forecast[1].pressure))
 
 --log(forecast)
 
 else
 alert('Fetch failed: ' .. tostring(err))
 end
 Looks great, but i still dont understand where to put the GA in. Sorry for the low lewel of knowlegde.    
		
	 
	
	
	
		
	Posts: 1807 
	Threads: 7 
	Joined: Jul 2015
	
 Reputation: 
121 
	
	
		Hi,
 Is already in the script but this sample uses the object names instead of GA’s.
 
 Change in all lines grp.update('Weather precipitation', ... to grp.update('1/1/1', ... etcetera
 
 BR,
 
 Erwin
 
		
	 
	
	
	
		
	Posts: 303 
	Threads: 87 
	Joined: May 2017
	
 Reputation: 
2 
	
	
		 (30.01.2019, 21:19)Erwin van der Zwart Wrote:  Hi,
 Is already in the script but this sample uses the object names instead of GA’s.
 
 Change in all lines grp.update('Weather precipitation', ... to grp.update('1/1/1', ... etcetera
 
 BR,
 
 Erwin
 
require('socket.http')
 
forecast = {}
 
function parsetag(parser, tag, attributes) 
  if tag == '27/3/0' then 
    table.insert(forecast, {}) 
  elseif tag == '27/3/1' then 
    forecast[ #forecast ].symbol = attributes.name 
  elseif tag == '27/3/2' then 
    forecast[ #forecast ].precipitation = attributes.value 
  elseif tag == '27/3/3' then 
    forecast[ #forecast ].wind_direction = attributes.code 
  elseif tag == '27/3/4' then 
    forecast[ #forecast ].wind_speed = attributes.mps 
  elseif tag == '27/3/6' then 
    forecast[ #forecast ].temperature = attributes.value 
  elseif tag == '27/3/5' then 
    forecast[ #forecast ].pressure = attributes.value 
  end 
end
 
url = 'http://www.yr.no/place/United_Kingdom/England/London/varsel.xml' 
data, err = socket.http.request(url)
 
if data then 
  require('lxp').new({ 
    StartElement = parsetag 
  }):parse(data)
  
  grp.update('27/3/7', tonumber(forecast[1].precipitation)) 
  grp.update('27/3/8', forecast[1].symbol) 
  grp.update('27/3/9', forecast[1].wind_direction) 
  grp.update('27/3/10', tonumber(forecast[1].wind_speed)) 
  grp.update('27/3/11', tonumber(forecast[1].temperature)) 
  grp.update('27/3/12', tonumber(forecast[1].pressure))
 
  --log(forecast)
   
else 
  alert('Fetch failed: ' .. tostring(err)) 
end
 
Still not getting any value, what am i doing wrong. :    ?
	 
		
	 
	
	
	
		
	Posts: 1807 
	Threads: 7 
	Joined: Jul 2015
	
 Reputation: 
121 
	
		
		
		24.02.2019, 06:52 
(This post was last modified: 24.02.2019, 06:53 by Erwin van der Zwart.)
		
	 
		Hi,
 I never said to change the TAG’s (:
 
 if tag == 'youchangedthistogrpaddressesandthatiswrong'
 
 BR,
 
 Erwin
 
		
	 
	
	
	
		
	Posts: 303 
	Threads: 87 
	Joined: May 2017
	
 Reputation: 
2 
	
	
		[attachment=1217 Wrote:Erwin van der Zwart pid='11967' dateline='1550991145']
 Hi, 
 I never said to change the TAG’s (:
 
 if tag == 'youchangedthistogrpaddressesandthatiswrong'
 
 BR,
 
 Erwin
 
Of course you didnt. I may have to blame some saturday night fever or something. Changed it back to how it was supposed to be and then it is working..
 
But if i use any city in the world script is working. But using any Norwegian city i get that the service is stopped. And script not working. Kinda strange since it is an norwegian site.
 
Anyone got this working in Norway.
	 
		
	 
	
	
	
		
	Posts: 8410 
	Threads: 45 
	Joined: Jun 2015
	
 Reputation: 
481 
	
	
		Looks like they don't provide XML in this format anymore. I suggest using Apixu instead, you just have to register for a free account: https://forum.logicmachine.net/showthrea...8#pid11518
		
	 
	
	
	
		
	Posts: 5284 
	Threads: 29 
	Joined: Aug 2017
	
 Reputation: 
237 
	
	
		Hi 
It works fine only you need to use correct location for example for Oslo it is https://www.yr.no/place/Norway/Oslo/Oslo...varsel.xml 
Just go here https://www.yr.no/  search for your City and copy location from this url and paste in to XML url.  
BR
	
------------------------------Ctrl+F5
 
		
	 
	
	
	
		
	Posts: 236 
	Threads: 48 
	Joined: Nov 2015
	
 Reputation: 
2 
	
		
		
		26.02.2019, 10:25 
(This post was last modified: 26.02.2019, 10:26 by FatMax.)
		
	 
		I made (partially found) a quick and dirty integration with met.no for a weather service a while back. Met.no is the same instance as YR.no. 
Maybe someone can use (and improve, its messy) it. This version only collects the temperature and sends it to 1/1/0. Temperature is correct for the locations I have tried it. Loads of other information available in the output file. Also, you need to specify the correct longitude and latitude on the LM/SL for it to work.
 Code: require('uci')
 forecast = {}
 
 
 function parsetag(parser, tag, attributes)
 if tag == 'time' then
 table.insert(forecast, {})
 elseif tag == 'precipitation' then
 forecast[ #forecast ].precipitation = attributes.value
 elseif tag == 'windSpeed' then
 forecast[ #forecast ].wind_speed = attributes.mps
 elseif tag == 'temperature' then
 forecast[ #forecast ].temperature = attributes.value
 end
 end
 
 latitude = uci.get('genohm-scada.core.latitude')
 latitude = tonumber(latitude)
 
 longitude = uci.get('genohm-scada.core.longitude')
 longitude = tonumber(longitude)
 
 url = 'https://api.met.no/weatherapi/locationforecast/1.9/?lat='..latitude..'&lon='..longitude..''
 
 
 
 data, err = require('ssl.https').request(url)
 
 if data then
 require('lxp').new({
 StartElement = parsetag
 }):parse(data)
 grp.write('1/1/0/', forecast[1].temperature)
 else
 log('Weather fetch failed: ' .. tostring(err))
 end
		
	 
	
	
	
		
	Posts: 30 
	Threads: 18 
	Joined: Jul 2016
	
 Reputation: 
0 
	
	
		Hello. I use weather script for https://www.yr.no/  . And next required for visualization- how much and what values can take variable  forecast[1].symbol ( cloudy, rain...) to assign icons for each state?
	
		
	 
	
	
	
		
	Posts: 8410 
	Threads: 45 
	Joined: Jun 2015
	
 Reputation: 
481 
	
	
		All symbols are listed here: https://hjelp.yr.no/hc/en-us/articles/20...bols-on-Yr 
Full icon name is also supplied in XML:
 Code: forecast[ #forecast ].icon_name = attributes.var
		
	 
	
	
	
		
	Posts: 120 
	Threads: 24 
	Joined: Jul 2015
	
 Reputation: 
0 
	
		
		
		22.03.2019, 22:20 
(This post was last modified: 22.03.2019, 22:21 by Evens.)
		
	 
		Hi,
 How to get moon data from apixu script?
 
 moon_phase
 moon_illumination
 
 BR Even Sundgot.
 
		
	 
	
	
	
		
	Posts: 1807 
	Threads: 7 
	Joined: Jul 2015
	
 Reputation: 
121 
	
	
		Hi,
 Just log the table ‘current’ to see what index fields are available and if the data exists use it like the rest, could be something like current.moon_phase but you will know when looking at the data table.
 
 BR,
 
 Erwin
 
		
	 
	
	
	
		
	Posts: 120 
	Threads: 24 
	Joined: Jul 2015
	
 Reputation: 
0 
	
	
		 (23.03.2019, 08:00)Erwin van der Zwart Wrote:  Hi,
 Just log the table ‘current’ to see what index fields are available and if the data exists use it like the rest, could be something like current.moon_phase but you will know when looking at the data table.
 
 BR,
 
 Erwin
 
Hi Erwin,
 
Thanks for the reply, when i log astro i get this reply:
* table: [moonset]
 * string: 07:42 AM
 [sunrise]
 * string: 06:28 AM
 [moonrise]
 * string: 10:39 PM
 [sunset]
 * string: 06:58 PM
So it seems like there is no data for moon, but the strange thing is that in the Api manual the moon data is described.
BR Even Sundgot.
 
		
	 
	
	
	
		
	Posts: 8410 
	Threads: 45 
	Joined: Jun 2015
	
 Reputation: 
481 
	
	
		Apixu free tier does not provide astronomy data. Moon phase calculation is relatively simple. Here's a formula that gives number of days since new moon cycle: Code: local date = os.date('*t')local y, m, d = date.year, date.month, date.day
 
 if m < 3 then
 y = y - 1
 m = m + 12
 end
 
 local a = math.floor(y / 100)
 local b = math.floor(a / 4)
 local c = 2 - a + b
 local e = math.floor(365.25 * (y + 4716))
 local f = math.floor(30.6001 * (m + 1))
 local jd = c + d + e + f - 1524.5
 
 local dn = jd - 2451549.5
 local nm = dn / 29.53
 local dc = (nm % 1) * 29.53
 
 log(dc)
		
	 
	
	
	
		
	Posts: 120 
	Threads: 24 
	Joined: Jul 2015
	
 Reputation: 
0 
	
	
		 (25.03.2019, 07:34)admin Wrote:  Apixu free tier does not provide astronomy data. Moon phase calculation is relatively simple. Here's a formula that gives number of days since new moon cycle:
 Code: local date = os.date('*t')local y, m, d = date.year, date.month, date.day
 
 if m < 3 then
 y = y - 1
 m = m + 12
 end
 
 local a = math.floor(y / 100)
 local b = math.floor(a / 4)
 local c = 2 - a + b
 local e = math.floor(365.25 * (y + 4716))
 local f = math.floor(30.6001 * (m + 1))
 local jd = c + d + e + f - 1524.5
 
 local dn = jd - 2451549.5
 local nm = dn / 29.53
 local dc = (nm % 1) * 29.53
 
 log(dc)
 
Thanks Admin,
 
Is there a way to get what phase the moon is in right now?
 
BR Even Sundgot.
	 
		
	 
	
	
	
		
	Posts: 8410 
	Threads: 45 
	Joined: Jun 2015
	
 Reputation: 
481 
	
	
		Use this: Code: local date = os.date('*t')local y, m, d = date.year, date.month, date.day
 
 if m < 3 then
 y = y - 1
 m = m + 12
 end
 
 local a = math.floor(y / 100)
 local b = math.floor(a / 4)
 local c = 2 - a + b
 local e = math.floor(365.25 * (y + 4716))
 local f = math.floor(30.6001 * (m + 1))
 local jd = c + d + e + f - 1524.5
 
 local dn = jd - 2451550.1
 local nm = dn / 29.530588853
 local dc = (nm % 1) * 29.530588853
 
 local ph
 
 if dc < 1.84566 then
 ph = 0
 elseif dc < 5.53699 then
 ph = 1
 elseif dc < 9.22831 then
 ph = 2
 elseif dc < 12.91963 then
 ph = 3
 elseif dc < 16.61096 then
 ph = 4
 elseif dc < 20.30228 then
 ph = 5
 elseif dc < 23.99361 then
 ph = 6
 elseif dc < 27.68493 then
 ph = 7
 else
 ph = 0
 end
 
 local phases = {
 [0] = 'new moon',
 [1] = 'evening crescent',
 [2] = 'first quarter',
 [3] = 'waxing gibbous',
 [4] = 'full moon',
 [5] = 'waning gibbous',
 [6] = 'last quarter',
 [7] = 'morning crescent',
 }
 
 text = phases[ ph ]
 log(ph, text)
You can use ph  number as object value and set custom values according to phases  table (0..7).
	
		
	 
	
	
	
		
	Posts: 20 
	Threads: 4 
	Joined: Mar 2019
	
 Reputation: 
2 
	
	
		Code: require('socket.http')
 ------------------------------------------------------------------------------------------------------------------------------
 --
 -- Weather forecast from Yr, delivered by the Norwegian Meteorological Institute and the NRK
 --
 -- This is a combo of the codes contributions from the Admin, Thomas, and me MichelDeLigne
 --
 -- Usage:
 -- * Define your place in the variable url in the function yrno_fetch()
 --
 -- * Ending the url with "forecast_hour_by_hour.xml" will give access to the predictions for a city for the next 48 hours, per hour
 -- * Ending the url with "forecast.xml" will give predictions for 9 days, per 6 hours slices.
 -- Search for your location in https://www.yr.no/
 -- When found, add forecast.xml or forecast_hour_by_hour.xml to the url
 --
 -- Don't run the script more that once per hour, or 4 times a day for the 6h forecast. Yr.no web admins will certainly thank you for that.
 -- (no they won't, but they won't curse you for polling their servers every minutes either, which is a total waste of time/bandwidth).
 
 -- Previsions for the next hour (or 6h if the url ends with forecast.xml) would be contained in:
 -- forecast[1].symbol             "Partly cloudy"
 -- forecast[1].icon                "03n" (available at https://hjelp.yr.no/hc/en-us/articles/203786121-Weather-symbols-on-Yr)
 -- forecast[1].precipitation        [mm]
 -- forecast[1].wind_direction        "SSW", "E", ...
 -- forecast[1].wind_speed        [m/s]
 -- forecast[1].temperature        [°C]
 -- forecast[1].pressure            [hPa]
 -- ld_minTemp                [°C] Min temperature for the next day
 -- ld_maxTemp                [°C] Max temperature for the next day
 -- lt_tomorrow                What the next day is
 
 -- Next hour forecast would be in forecast[2].symbol
 -- and so on
 
 -------------------------------------------------------------------------------------------------------------------------------
 
 forecast = {}
 
 ---------------------------------------------------------------
 -- Functions
 ---------------------------------------------------------------
 function parsetag(parser, tag, attributes)
 if tag == 'time' then
 table.insert(forecast, {})
 forecast[ #forecast ].from = attributes.from
 elseif tag == 'symbol' then
 forecast[ #forecast ].symbol = attributes.name
 forecast[ #forecast ].icon = attributes.var
 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
 
 -------------------------------
 function yrno_fetch()
 ld_minTemp=0
 ld_maxTemp=0
 next_day={}
 
 -- local url = 'https://www.yr.no/place/Belgium/Wallonia/Marche-lez-%C3%89caussinnes/forecast.xml'
 local url = 'http://www.yr.no/place/Belgium/Wallonia/Marche-lez-%C3%89caussinnes/forecast_hour_by_hour.xml'
 
 local data, err = socket.http.request(url)
 
 if data then
 require('lxp').new({
 StartElement = parsetag
 }):parse(data)
 
 -- log(forecast)
 else
 alert('Fetch failed: ' .. tostring(err))
 return 1
 end
 
 ll_tomorrow = os.time()+24*3600 -- tomorrow as long
 lt_tomorrow = os.date("*t",ll_tomorrow) -- tomorrow as table for use if forecast_write
 
 --gets the date of tomorrow
 local ls_match = os.date("%Y--%m--%d",ll_tomorrow)
 
 for index, value in ipairs(forecast) do
 if (value.from:match("^"..ls_match)) then
 local ld_temp=tonumber(value.temperature)
 if (ld_minTemp == nil) then
 ld_minTemp=ld_temp
 end
 if (ld_maxTemp == nil) then
 ld_maxTemp=ld_temp
 end
 if (ld_temp<ld_minTemp or index==1) then
 ld_minTemp=ld_temp
 end
 if (ld_temp>ld_maxTemp or index==1) then
 ld_maxTemp=ld_temp
 end
 end
 end
 
 return 0
 end
 
 ------------------------------------------
 -- Main code
 ------------------------------------------
 
 if yrno_fetch() then
 -- write the data to the bus, or do something else with it
 -- If the fetch fails, we do nothing.
 
 --log(ld_minTemp, ld_maxTemp, lt_tomorrow, forecast[1].symbol, forecast[1].icon, forecast[1].precipitation, forecast[1].wind_direction,
 --forecast[1].wind_speed, forecast[1].temperature, forecast[1].pressure)
 
 --grp.write('1/1/12', ld_minTemp, dt.float16)
 --grp.write('1/1/13', ld_maxTemp, dt.float16)
 --grp.write('1/1/14', lt_tomorrow, dt.date)
 
 --grp.write('1/1/15', forecast[1].symbol)
 --grp.write('1/1/16', forecast[1].icon)
 
 --grp.write('1/1/17', forecast[1].precipitation)
 --grp.write('1/1/18', forecast[1].wind_direction)
 --grp.write('1/1/19', forecast[1].wind_speed)
 --grp.write('1/1/20', forecast[1].temperature)
 --grp.write('1/1/21', forecast[1].pressure)
 end
Hi,
 
Here is a small contribution from me and others (mostly others in fact).  
The script fetches the weather forecast from Yr.no
 
I've put together some of the previous scripts, and removedsome functions and correct the error handling.  
I am not a big fan of using functions only once. I find it makes the code less readable because you have to jump all over the place when you read it. 
 
There is also a description at the beginning of the code explaining how to use it.
 
Cheers. 
Michel
	 
		
	 
	
	
	
		
	Posts: 86 
	Threads: 10 
	Joined: Jul 2015
	
 Reputation: 
1 
	
	
		Hi to all,
 I tried Michell's code (with either his or my city code) and I am always getting an alert:
 
 Fetch failed: error:1409442E:lib(20):func(148):reason(1070)
 
 and the script stops. I tried to find any error description in yr.no but no luck.
 
 Any ideas?
 Thank you in advance.
 
		
	 
	
	
	
		
	Posts: 5284 
	Threads: 29 
	Joined: Aug 2017
	
 Reputation: 
237 
	
	
	
	
------------------------------Ctrl+F5
 
		
	 |