Posts: 177
Threads: 48
Joined: Mar 2021
Reputation:
2
Hello,
Please, I hope you could help me in my issue
However, I was trying to get the weather infomation from apixu.com and write them on the KNX bus.
I just followed the exemple on the openrb.com this one (
https://openrb.com/featch-weather-forecast-from-apixu ), I only changed the key variable as it's mentioned in the exemple.
This is the scheduled script that I used.
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
require (
'json' )
https =
require (
'ssl.https' )
escape =
require (
'socket.url' ).
escape
key =
'737ad147e5be532e3d78995a68f4a160'
location =
'Norway'
url =
'https://api.apixu.com/v1/forecast.json?key=%s&q=%s&days=2'
url =
string.format (
url ,
key ,
escape (
location ))
res =
https.request (
url )
data =
json.pdecode (
res )
if type (
data ) ~=
'table' then
alert (
'failed to load weather data' )
return
end
if data.error then
log (
'error' ,
data.error )
return
end
current =
data.current
today =
data.forecast.forecastday [
1 ].
day
tomorrow =
data.forecast.forecastday [
2 ].
day
grp.write (
'32/1/1' ,
current.temp_c )
grp.write (
'32/1/2' ,
current.feelslike_c )
grp.write (
'32/1/3' ,
current.humidity )
grp.write (
'32/1/4' ,
current.wind_kph )
grp.write (
'32/1/5' ,
current.uv )
grp.write (
'32/1/6' ,
current.condition.text )
grp.write (
'32/1/7' ,
current.pressure_mb )
grp.write (
'32/1/8' ,
current.precip_mm )
grp.write (
'32/2/1' ,
today.mintemp_c )
grp.write (
'32/2/2' ,
today.maxtemp_c )
grp.write (
'32/2/3' ,
today.avgtemp_c )
grp.write (
'32/2/4' ,
today.avghumidity )
grp.write (
'32/2/5' ,
today.maxwind_kph )
grp.write (
'32/2/6' ,
today.uv )
grp.write (
'32/2/7' ,
today.condition.text )
grp.write (
'32/2/8' ,
today.totalprecip_mm )
grp.write (
'32/3/1' ,
tomorrow.mintemp_c )
grp.write (
'32/3/2' ,
tomorrow.maxtemp_c )
grp.write (
'32/3/3' ,
tomorrow.avgtemp_c )
grp.write (
'32/3/4' ,
tomorrow.avghumidity )
grp.write (
'32/3/5' ,
tomorrow.maxwind_kph )
grp.write (
'32/3/6' ,
tomorrow.uv )
grp.write (
'32/3/7' ,
tomorrow.condition.text )
grp.write (
'32/3/8' ,
tomorrow.totalprecip_mm )
No data was found on those groupe addresses,
Or if you could give me any other solution, I only wants to write 1 on the KNX bus if there's a snow tomorrow, the place is Bergen in Norway
Very grateful in advance
Posts: 8174
Threads: 43
Joined: Jun 2015
Reputation:
473
yr.no provides free detailed forecast API.
This example will check if there will be snow in the next day. You only need to adjust your location (latitude/longitude) and the status object address. There might be some limits on how often the API can be accessed so create a scheduled script that runs every 3 hours or so.
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
latitude =
60.3894
longitude =
5.33
status =
'1/1/1'
json =
require (
'json' )
http =
require (
'socket.http' )
args =
'lat=' ..
latitude ..
'&lon=' ..
longitude
url =
'https://api.met.no/weatherapi/locationforecast/2.0/compact?' ..
args
mac =
0
io.readfile (
'/sys/class/net/eth0/address' ):
gsub (
'%x%x' ,
function (
v )
mac =
mac *
256 +
tonumber (
v ,
16 )
end )
http.USERAGENT =
'LM ' ..
mac
res ,
err =
http.request (
url )
data =
json.pdecode (
res )
if type (
data ) ==
'table' then
date =
os.date (
'*t' )
date.day =
date.day +
1
time =
os.time (
date )
timestamp =
os.date (
'%Y-%m-%d' ,
time )
snow =
false
for i ,
entry in ipairs (
data.properties.timeseries )
do
if entry.time :
sub (
1 , #
timestamp ) ==
timestamp then
next1h =
entry.data.next_1_hours
code =
''
if type (
next1h ) ==
'table' and type (
next1h.summary ) ==
'table' then
code =
next1h.summary.symbol_code or ''
end
if code :
find (
'snow' )
then
snow =
true
break
end
end
end
grp.checkwrite (
status ,
snow )
end
Posts: 31
Threads: 5
Joined: Oct 2017
Reputation:
0
Hi Admin,
May I ask for a little bit of help also?
Do you experience problems with the setup explained here:
https://openrb.com/featch-weather-forecast-from-apixu
Have and account and key but I get the following message:
"#################################################################################################################################"
1 "# #"
2 "# IMPORTANT - PLEASE UPDATE YOUR API ENDPOINT #"
3 "# #"
4 "# This API endpoint is deprecated and has now been shut down. To keep using the apixu API, please update your integration #"
5 "# to use the new weatherstack API endpoint, designed as a simple drop-in replacement. #"
6 "# You will be required to create an account at
https://weatherstack.com and obtain an API access key. #"
7 "# #"
8 "# For more information on how to upgrade please visit our Github Tutorial at:
https://github.com/apilayer/weatherstack#readme #"
9 "# #"
a "#################################################################################################################################"
Thank you in advance!
Posts: 8174
Threads: 43
Joined: Jun 2015
Reputation:
473
The free tier in weatherstack does not provide forecast data, only current conditions. Consider using yr.no instead. We will publish an updated integration script for yr.no next week.
Posts: 31
Threads: 5
Joined: Oct 2017
Reputation:
0
27.05.2022, 13:23
(This post was last modified: 27.05.2022, 13:24 by Mrinj .)
Thank you for your replay!
I've been using YR until recently but it stopped. Looking forward to your YR script update
Posts: 369
Threads: 83
Joined: Jun 2017
Reputation:
8
Hi,
may be some news about new yr.no weather script? Looks like one of the best free weather informers now..
Alex
Posts: 58
Threads: 2
Joined: Dec 2020
Reputation:
25
Hi, here is new yr weather script.
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366
latitude =
56.9496
longitude =
24.1052
status_temp =
grp.create ({
datatype =
dt.float16 ,
address =
'33/1/1' ,
name =
'current_temp' ,})
status_pressure =
grp.create ({
datatype =
dt.float16 ,
address =
'33/1/2' ,
name =
'current_press' ,})
status_humidity =
grp.create ({
datatype =
dt.float16 ,
address =
'33/1/3' ,
name =
'current_humid' ,})
status_wind_direction =
grp.create ({
datatype =
dt.float16 ,
address =
'33/1/4' ,
name =
'current_wind_dir' ,})
status_wind_speed =
grp.create ({
datatype =
dt.float16 ,
address =
'33/1/5' ,
name =
'current_wind_sp' ,})
status_uv_index =
grp.create ({
datatype =
dt.float16 ,
address =
'33/1/6' ,
name =
'current_uv_index' ,})
status_feelslike_temp =
grp.create ({
datatype =
dt.float16 ,
address =
'33/1/7' ,
name =
'current_feels_temp' ,})
status_rain_today =
grp.create ({
datatype =
dt.bool ,
address =
'33/1/8' ,
name =
'rain_today' ,})
status_snow_today =
grp.create ({
datatype =
dt.bool ,
address =
'33/1/9' ,
name =
'snow_today' ,})
status_min_temp_today =
grp.create ({
datatype =
dt.float16 ,
address =
'33/1/10' ,
name =
'today_temp_min' ,})
status_max_temp_today =
grp.create ({
datatype =
dt.float16 ,
address =
'33/1/11' ,
name =
'today_temp_max' ,})
status_min_pressure_today =
grp.create ({
datatype =
dt.float16 ,
address =
'33/1/12' ,
name =
'today_press_min' ,})
status_max_pressure_today =
grp.create ({
datatype =
dt.float16 ,
address =
'33/1/13' ,
name =
'today_press_max' ,})
status_min_wind_speed_today =
grp.create ({
datatype =
dt.float16 ,
address =
'33/1/14' ,
name =
'today_wind_min' ,})
status_max_wind_speed_today =
grp.create ({
datatype =
dt.float16 ,
address =
'33/1/15' ,
name =
'today_press_max' ,})
status_min_uv_today =
grp.create ({
datatype =
dt.float16 ,
address =
'33/1/16' ,
name =
'today_uv_min' ,})
status_max_uv_today =
grp.create ({
datatype =
dt.float16 ,
address =
'33/1/17' ,
name =
'today_uv_max' ,})
status_rain_tomor =
grp.create ({
datatype =
dt.bool ,
address =
'33/1/18' ,
name =
'rain_tomor' ,})
status_snow_tomor =
grp.create ({
datatype =
dt.bool ,
address =
'33/1/19' ,
name =
'snow_tomor' ,})
status_min_temp_tomor =
grp.create ({
datatype =
dt.float16 ,
address =
'33/1/20' ,
name =
'tomor_temp_min' ,})
status_max_temp_tomor =
grp.create ({
datatype =
dt.float16 ,
address =
'33/1/21' ,
name =
'tomor_temp_max' ,})
status_min_pressure_tomor =
grp.create ({
datatype =
dt.float16 ,
address =
'33/1/22' ,
name =
'tomor_press_min' ,})
status_max_pressure_tomor =
grp.create ({
datatype =
dt.float16 ,
address =
'33/1/23' ,
name =
'tomor_press_max' ,})
status_min_wind_speed_tomor =
grp.create ({
datatype =
dt.float16 ,
address =
'33/1/24' ,
name =
'tomor_wind_min' ,})
status_max_wind_speed_tomor =
grp.create ({
datatype =
dt.float16 ,
address =
'33/1/25' ,
name =
'tomor_wind_max' ,})
status_min_uv_tomor =
grp.create ({
datatype =
dt.float16 ,
address =
'33/1/26' ,
name =
'tomor_uv_min' ,})
status_max_uv_tomor =
grp.create ({
datatype =
dt.float16 ,
address =
'33/1/27' ,
name =
'tomor_uv_max' ,})
rain_tod_YR =
false
snow_tod_YR =
false
rain_tom_YR =
false
snow_tom_YR =
false
temperature_YR = -
273
air_pressure_YR = -
1
humidity_YR = -
1
wind_direction_YR = -
1
wind_speed_YR = -
1
uv_index_YR = -
1
feelslike_temp_YR = -
273
function init_min_max_table ()
local min_max = {}
min_max.min_t =
8000
min_max.max_t = -
273
min_max.min_p =
8000
min_max.max_p = -
1
min_max.min_w =
8000
min_max.max_w = -
1
min_max.min_uv =
8000
min_max.max_uv = -
1
return min_max
end
function set_timestamp (
day )
local date =
os.date (
'*t' )
date.day =
date.day +
day
local time =
os.time (
date )
return os.date (
'%Y-%m-%d' ,
time ),
date.hour
end
function min_max_temp_press_wind (
dat_tab ,
min_max_tab )
min_max_tab.min_t =
math.min (
min_max_tab.min_t ,
dat_tab.air_temperature or 8000 )
min_max_tab.max_t =
math.max (
min_max_tab.max_t ,
dat_tab.air_temperature or -
273 )
min_max_tab.min_p =
math.min (
min_max_tab.min_p ,
dat_tab.air_pressure_at_sea_level or 8000 )
min_max_tab.max_p =
math.max (
min_max_tab.max_p ,
dat_tab.air_pressure_at_sea_level or -
1 )
min_max_tab.min_w =
math.min (
min_max_tab.min_w ,
dat_tab.wind_speed or 8000 )
min_max_tab.max_w =
math.max (
min_max_tab.max_w ,
dat_tab.wind_speed or -
1 )
min_max_tab.min_uv =
math.min (
min_max_tab.min_uv ,
dat_tab.ultraviolet_index_clear_sky or 8000 )
min_max_tab.max_uv =
math.max (
min_max_tab.max_uv ,
dat_tab.ultraviolet_index_clear_sky or -
1 )
return min_max_tab
end
function check_for_rain (
next1h )
local is_rainy =
false
local code =
''
if type (
next1h ) ==
'table' and type (
next1h.summary ) ==
'table' then
code =
next1h.summary.symbol_code or ''
end
if code :
find (
'rain' )
then
is_rainy =
true
end
return is_rainy
end
function check_for_snow (
next1h )
local is_snowy =
false
local code =
''
if type (
next1h ) ==
'table' and type (
next1h.summary ) ==
'table' then
code =
next1h.summary.symbol_code or ''
end
if code :
find (
'snow' )
then
is_snowy =
true
end
return is_snowy
end
function calc_heat_index (
temperature ,
humidity )
if temperature <
26.7 then
log (
'for heat_index calculation temperature should be higher than +26.7 Celsius degree' )
return -
273
end
if humidity <
0 or humidity >
100 then
log (
'humidity should be in a range from 0 ... 100%' )
return -
273
end
local T =
temperature
local H =
humidity
local c_1 = -
8.785
local c_2 =
1.611
local c_3 =
2.339
local c_4 = -
0.146
local c_5 = -
0.0123
local c_6 = -
0.0164
local c_7 =
0.00221
local c_8 =
0.000725
local c_9 = -
0.00000358
local heat_index =
c_1 +
c_2 *
T +
c_3 *
H +
c_4 *
T *
H +
c_5 *
T *
T +
c_6 *
H *
H +
c_7 *
T *
T *
H +
c_8 *
T *
H *
H +
c_9 *
T *
T *
H *
H
return heat_index
end
function calc_wind_chill (
temperature ,
wind_speed )
if temperature >
10 then
log (
'temperature should be less than +10.0 Celsius degree' )
return -
273
end
if wind_speed <
0 or wind_speed >
49.2 then
log (
'wind speed should be in a range 0 ... 49.2 m/s' )
return -
273
end
local T_air =
temperature
local a =
0.62
local b =
0.51
local T_1 =
13.1
local T_2 =
14.6
local M_0 =
1.3333
local M =
wind_speed
local T_wind_chill
if M >
M_0 then
T_wind_chill = (
a *
T_air +
T_1 ) + (
b *
T_air -
T_2 )*(
M /
M_0 )^
0.16
else
T_wind_chill =
T_air
end
return T_wind_chill
end
function calc_feelslike (
temperature ,
wind_speed ,
humidity )
local T_feelslike
if temperature <=
10.0 then
T_feelslike =
calc_wind_chill (
temperature ,
wind_speed )
elseif temperature >=
26.7 then
T_feelslike =
calc_heat_index (
temperature ,
humidity )
else
T_feelslike =
temperature
end
return T_feelslike
end
min_max_today =
init_min_max_table ()
min_max_tomor =
init_min_max_table ()
json =
require (
'json' )
http =
require (
'socket.http' )
args =
'lat=' ..
latitude ..
'&lon=' ..
longitude
url =
'https://api.met.no/weatherapi/locationforecast/2.0/complete?' ..
args
mac =
0
io.readfile (
'/sys/class/net/eth0/address' ):
gsub (
'%x%x' ,
function (
v )
mac =
mac *
256 +
tonumber (
v ,
16 )
end )
http.USERAGENT =
'LM ' ..
mac
res ,
err =
http.request (
url )
data =
json.pdecode (
res )
if type (
data ) ==
'table' then
timestamp ,
hour =
set_timestamp (
0 )
if hour <
10 then
timestamp_current_hour =
timestamp ..
'T0' ..
hour ..
':00:00Z'
else
timestamp_current_hour =
timestamp ..
'T' ..
hour ..
':00:00Z'
end
timestamp_next =
set_timestamp (
1 )
for i ,
entry in ipairs (
data.properties.timeseries )
do
if entry.time ==
timestamp_current_hour then
current_data =
entry.data.instant.details
if type (
current_data ) ==
'table' then
temperature_YR =
current_data.air_temperature or -
273
air_pressure_YR =
current_data.air_pressure_at_sea_level or -
1
humidity_YR =
current_data.relative_humidity or -
1
wind_direction_YR =
current_data.wind_from_direction or -
1
wind_speed_YR =
current_data.wind_speed or -
1
uv_index_YR =
current_data.ultraviolet_index_clear_sky or -
1
feelslike_temp_YR =
calc_feelslike (
temperature_YR ,
wind_speed_YR ,
humidity_YR )
end
end
if entry.time :
sub (
1 , #
timestamp ) ==
timestamp then
current_data =
entry.data.instant.details
if type (
current_data ) ==
'table' then
min_max_today =
min_max_temp_press_wind (
current_data ,
min_max_today )
end
if rain_tod_YR ==
false then
rain_tod_YR =
check_for_rain (
entry.data.next_1_hours )
end
if snow_tod_YR ==
false then
snow_tod_YR =
check_for_snow (
entry.data.next_1_hours )
end
end
if entry.time :
sub (
1 , #
timestamp_next ) ==
timestamp_next then
tomor_data =
entry.data.instant.details
if type (
tomor_data ) ==
'table' then
min_max_tomor =
min_max_temp_press_wind (
tomor_data ,
min_max_tomor )
end
if rain_tom_YR ==
false then
rain_tom_YR =
check_for_rain (
entry.data.next_1_hours )
end
if snow_tom_YR ==
false then
snow_tom_YR =
check_for_snow (
entry.data.next_1_hours )
end
end
end
grp.write (
status_temp ,
temperature_YR )
grp.write (
status_pressure ,
air_pressure_YR )
grp.write (
status_humidity ,
humidity_YR )
grp.write (
status_wind_direction ,
wind_direction_YR )
grp.write (
status_wind_speed ,
wind_speed_YR )
grp.write (
status_uv_index ,
uv_index_YR )
grp.write (
status_feelslike_temp ,
feelslike_temp_YR )
grp.write (
status_rain_today ,
rain_tod_YR )
grp.write (
status_snow_today ,
snow_tod_YR )
grp.write (
status_min_temp_today ,
min_max_today.min_t )
grp.write (
status_max_temp_today ,
min_max_today.max_t )
grp.write (
status_min_pressure_today ,
min_max_today.min_p )
grp.write (
status_max_pressure_today ,
min_max_today.max_p )
grp.write (
status_min_wind_speed_today ,
min_max_today.min_w )
grp.write (
status_max_wind_speed_today ,
min_max_today.max_w )
grp.write (
status_min_uv_today ,
min_max_today.min_uv )
grp.write (
status_max_uv_today ,
min_max_today.max_uv )
grp.write (
status_rain_tomor ,
rain_tom_YR )
grp.write (
status_snow_tomor ,
snow_tom_YR )
grp.write (
status_min_temp_tomor ,
min_max_tomor.min_t )
grp.write (
status_max_temp_tomor ,
min_max_tomor.max_t )
grp.write (
status_min_pressure_tomor ,
min_max_tomor.min_p )
grp.write (
status_max_pressure_tomor ,
min_max_tomor.max_p )
grp.write (
status_min_wind_speed_tomor ,
min_max_tomor.min_w )
grp.write (
status_max_wind_speed_tomor ,
min_max_tomor.max_w )
grp.write (
status_min_uv_tomor ,
min_max_tomor.min_uv )
grp.write (
status_max_uv_tomor ,
min_max_tomor.max_uv )
end
Attached Files
Posts: 369
Threads: 83
Joined: Jun 2017
Reputation:
8
09.06.2022, 08:21
(This post was last modified: 09.06.2022, 08:27 by AlexLV .)
Hi, super job is done!. Will test it in nearest time.
Some ideas for improvements/additional possibilities:
1. If weather info is needed for place where device is located - possible not to show coordinates directly in script but take them from system:
require('uci')
latitude = uci.get('genohm-scada.core.latitude')
latitude = tonumber(latitude) or 0
longitude = uci.get('genohm-scada.core.longitude')
longitude = tonumber(longitude) or 0
If 0 - no coordinates - should be entered than..;
2. Would be great if we can show group addresses for weather conditions in the beginning of script, because these groups will be created later in this script (addresses showed natively, not by names). I see it was planned to do so, but was commented.. For me groups 33/1/XX are used, For test I will manually change them in script;
3. In my existing weather script I use weather condition icons. So condition is taken from informer, than condition's icon is showed at visu...
May be ''symbol code'' info from yr.no we can add to script and use with icons?
4. how often we can start this script? Once a day? Some informers not allowed very often requests and can block them, or allows for additional payment..
BR,
Alex
Posts: 58
Threads: 2
Joined: Dec 2020
Reputation:
25
I have found out that, there was wrong name for object of maximum wind speed
was
Code:
1
status_max_wind_speed_today =
grp.create ({
datatype =
dt.float16 ,
address =
'33/1/15' ,
name =
'today_press_max' ,})
should be
Code:
1
status_max_wind_speed_today =
grp.create ({
datatype =
dt.float16 ,
address =
'33/1/15' ,
name =
'today_wind_max' ,})
Posts: 58
Threads: 2
Joined: Dec 2020
Reputation:
25
10.06.2022, 07:53
(This post was last modified: 10.06.2022, 08:11 by RomansP .)
The updated version of weather script.
1.) Added possibility to read coordinates (latitude and longitude) from system.
2.) Changed data types of objects:
a.) for humidity dt.scale
b.) for uv_index dt.uint8
c.) for pressure dt.uint16
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389
latitude =
56.9496
longitude =
24.1052
status_temp =
grp.create ({
datatype =
dt.float16 ,
address =
'33/1/1' ,
name =
'current_temp' ,})
status_pressure =
grp.create ({
datatype =
dt.uint16 ,
address =
'33/1/2' ,
name =
'current_press' ,})
status_humidity =
grp.create ({
datatype =
dt.scale ,
address =
'33/1/3' ,
name =
'current_humid' ,})
status_wind_direction =
grp.create ({
datatype =
dt.float16 ,
address =
'33/1/4' ,
name =
'current_wind_dir' ,})
status_wind_speed =
grp.create ({
datatype =
dt.float16 ,
address =
'33/1/5' ,
name =
'current_wind_sp' ,})
status_uv_index =
grp.create ({
datatype =
dt.uint8 ,
address =
'33/1/6' ,
name =
'current_uv_index' ,})
status_feelslike_temp =
grp.create ({
datatype =
dt.float16 ,
address =
'33/1/7' ,
name =
'current_feels_temp' ,})
status_rain_today =
grp.create ({
datatype =
dt.bool ,
address =
'33/1/8' ,
name =
'rain_today' ,})
status_snow_today =
grp.create ({
datatype =
dt.bool ,
address =
'33/1/9' ,
name =
'snow_today' ,})
status_min_temp_today =
grp.create ({
datatype =
dt.float16 ,
address =
'33/1/10' ,
name =
'today_temp_min' ,})
status_max_temp_today =
grp.create ({
datatype =
dt.float16 ,
address =
'33/1/11' ,
name =
'today_temp_max' ,})
status_min_pressure_today =
grp.create ({
datatype =
dt.uint16 ,
address =
'33/1/12' ,
name =
'today_press_min' ,})
status_max_pressure_today =
grp.create ({
datatype =
dt.uint16 ,
address =
'33/1/13' ,
name =
'today_press_max' ,})
status_min_wind_speed_today =
grp.create ({
datatype =
dt.float16 ,
address =
'33/1/14' ,
name =
'today_wind_min' ,})
status_max_wind_speed_today =
grp.create ({
datatype =
dt.float16 ,
address =
'33/1/15' ,
name =
'today_wind_max' ,})
status_min_uv_today =
grp.create ({
datatype =
dt.uint8 ,
address =
'33/1/16' ,
name =
'today_uv_min' ,})
status_max_uv_today =
grp.create ({
datatype =
dt.uint8 ,
address =
'33/1/17' ,
name =
'today_uv_max' ,})
status_rain_tomor =
grp.create ({
datatype =
dt.bool ,
address =
'33/1/18' ,
name =
'rain_tomor' ,})
status_snow_tomor =
grp.create ({
datatype =
dt.bool ,
address =
'33/1/19' ,
name =
'snow_tomor' ,})
status_min_temp_tomor =
grp.create ({
datatype =
dt.float16 ,
address =
'33/1/20' ,
name =
'tomor_temp_min' ,})
status_max_temp_tomor =
grp.create ({
datatype =
dt.float16 ,
address =
'33/1/21' ,
name =
'tomor_temp_max' ,})
status_min_pressure_tomor =
grp.create ({
datatype =
dt.uint16 ,
address =
'33/1/22' ,
name =
'tomor_press_min' ,})
status_max_pressure_tomor =
grp.create ({
datatype =
dt.uint16 ,
address =
'33/1/23' ,
name =
'tomor_press_max' ,})
status_min_wind_speed_tomor =
grp.create ({
datatype =
dt.float16 ,
address =
'33/1/24' ,
name =
'tomor_wind_min' ,})
status_max_wind_speed_tomor =
grp.create ({
datatype =
dt.float16 ,
address =
'33/1/25' ,
name =
'tomor_wind_max' ,})
status_min_uv_tomor =
grp.create ({
datatype =
dt.uint8 ,
address =
'33/1/26' ,
name =
'tomor_uv_min' ,})
status_max_uv_tomor =
grp.create ({
datatype =
dt.uint8 ,
address =
'33/1/27' ,
name =
'tomor_uv_max' ,})
rain_tod_YR =
false
snow_tod_YR =
false
rain_tom_YR =
false
snow_tom_YR =
false
temperature_YR = -
273
air_pressure_YR = -
1
humidity_YR = -
1
wind_direction_YR = -
1
wind_speed_YR = -
1
uv_index_YR = -
1
feelslike_temp_YR = -
273
function init_min_max_table ()
local min_max = {}
min_max.min_t =
8000
min_max.max_t = -
273
min_max.min_p =
8000
min_max.max_p = -
1
min_max.min_w =
8000
min_max.max_w = -
1
min_max.min_uv =
8000
min_max.max_uv = -
1
return min_max
end
function set_timestamp (
day )
local date =
os.date (
'*t' )
date.day =
date.day +
day
local time =
os.time (
date )
return os.date (
'%Y-%m-%d' ,
time ),
date.hour
end
function min_max_temp_press_wind (
dat_tab ,
min_max_tab )
min_max_tab.min_t =
math.min (
min_max_tab.min_t ,
dat_tab.air_temperature or 8000 )
min_max_tab.max_t =
math.max (
min_max_tab.max_t ,
dat_tab.air_temperature or -
273 )
min_max_tab.min_p =
math.min (
min_max_tab.min_p ,
dat_tab.air_pressure_at_sea_level or 8000 )
min_max_tab.max_p =
math.max (
min_max_tab.max_p ,
dat_tab.air_pressure_at_sea_level or -
1 )
min_max_tab.min_w =
math.min (
min_max_tab.min_w ,
dat_tab.wind_speed or 8000 )
min_max_tab.max_w =
math.max (
min_max_tab.max_w ,
dat_tab.wind_speed or -
1 )
min_max_tab.min_uv =
math.min (
min_max_tab.min_uv ,
dat_tab.ultraviolet_index_clear_sky or 8000 )
min_max_tab.max_uv =
math.max (
min_max_tab.max_uv ,
dat_tab.ultraviolet_index_clear_sky or -
1 )
return min_max_tab
end
function check_for_rain (
next1h )
local is_rainy =
false
local code =
''
if type (
next1h ) ==
'table' and type (
next1h.summary ) ==
'table' then
code =
next1h.summary.symbol_code or ''
end
if code :
find (
'rain' )
then
is_rainy =
true
end
return is_rainy
end
function check_for_snow (
next1h )
local is_snowy =
false
local code =
''
if type (
next1h ) ==
'table' and type (
next1h.summary ) ==
'table' then
code =
next1h.summary.symbol_code or ''
end
if code :
find (
'snow' )
then
is_snowy =
true
end
return is_snowy
end
function calc_heat_index (
temperature ,
humidity )
if temperature <
26.7 then
log (
'for heat_index calculation temperature should be higher than +26.7 Celsius degree' )
return -
273
end
if humidity <
0 or humidity >
100 then
log (
'humidity should be in a range from 0 ... 100%' )
return -
273
end
local T =
temperature
local H =
humidity
local c_1 = -
8.785
local c_2 =
1.611
local c_3 =
2.339
local c_4 = -
0.146
local c_5 = -
0.0123
local c_6 = -
0.0164
local c_7 =
0.00221
local c_8 =
0.000725
local c_9 = -
0.00000358
local heat_index =
c_1 +
c_2 *
T +
c_3 *
H +
c_4 *
T *
H +
c_5 *
T *
T +
c_6 *
H *
H +
c_7 *
T *
T *
H +
c_8 *
T *
H *
H +
c_9 *
T *
T *
H *
H
return heat_index
end
function calc_wind_chill (
temperature ,
wind_speed )
if temperature >
10 then
log (
'temperature should be less than +10.0 Celsius degree' )
return -
273
end
if wind_speed <
0 or wind_speed >
49.2 then
log (
'wind speed should be in a range 0 ... 49.2 m/s' )
return -
273
end
local T_air =
temperature
local a =
0.62
local b =
0.51
local T_1 =
13.1
local T_2 =
14.6
local M_0 =
1.3333
local M =
wind_speed
local T_wind_chill
if M >
M_0 then
T_wind_chill = (
a *
T_air +
T_1 ) + (
b *
T_air -
T_2 )*(
M /
M_0 )^
0.16
else
T_wind_chill =
T_air
end
return T_wind_chill
end
function calc_feelslike (
temperature ,
wind_speed ,
humidity )
local T_feelslike
if temperature <=
10.0 then
T_feelslike =
calc_wind_chill (
temperature ,
wind_speed )
elseif temperature >=
26.7 then
T_feelslike =
calc_heat_index (
temperature ,
humidity )
else
T_feelslike =
temperature
end
return T_feelslike
end
min_max_today =
init_min_max_table ()
min_max_tomor =
init_min_max_table ()
json =
require (
'json' )
http =
require (
'socket.http' )
args =
'lat=' ..
latitude ..
'&lon=' ..
longitude
url =
'https://api.met.no/weatherapi/locationforecast/2.0/complete?' ..
args
mac =
0
io.readfile (
'/sys/class/net/eth0/address' ):
gsub (
'%x%x' ,
function (
v )
mac =
mac *
256 +
tonumber (
v ,
16 )
end )
http.USERAGENT =
'LM ' ..
mac
res ,
err =
http.request (
url )
data =
json.pdecode (
res )
if type (
data ) ==
'table' then
timestamp ,
hour =
set_timestamp (
0 )
if hour <
10 then
timestamp_current_hour =
timestamp ..
'T0' ..
hour ..
':00:00Z'
else
timestamp_current_hour =
timestamp ..
'T' ..
hour ..
':00:00Z'
end
timestamp_next =
set_timestamp (
1 )
for i ,
entry in ipairs (
data.properties.timeseries )
do
if entry.time ==
timestamp_current_hour then
current_data =
entry.data.instant.details
if type (
current_data ) ==
'table' then
temperature_YR =
current_data.air_temperature or -
273
air_pressure_YR =
current_data.air_pressure_at_sea_level or -
1
humidity_YR =
current_data.relative_humidity or -
1
wind_direction_YR =
current_data.wind_from_direction or -
1
wind_speed_YR =
current_data.wind_speed or -
1
uv_index_YR =
current_data.ultraviolet_index_clear_sky or -
1
feelslike_temp_YR =
calc_feelslike (
temperature_YR ,
wind_speed_YR ,
humidity_YR )
end
end
if entry.time :
sub (
1 , #
timestamp ) ==
timestamp then
current_data =
entry.data.instant.details
if type (
current_data ) ==
'table' then
min_max_today =
min_max_temp_press_wind (
current_data ,
min_max_today )
end
if rain_tod_YR ==
false then
rain_tod_YR =
check_for_rain (
entry.data.next_1_hours )
end
if snow_tod_YR ==
false then
snow_tod_YR =
check_for_snow (
entry.data.next_1_hours )
end
end
if entry.time :
sub (
1 , #
timestamp_next ) ==
timestamp_next then
tomor_data =
entry.data.instant.details
if type (
tomor_data ) ==
'table' then
min_max_tomor =
min_max_temp_press_wind (
tomor_data ,
min_max_tomor )
end
if rain_tom_YR ==
false then
rain_tom_YR =
check_for_rain (
entry.data.next_1_hours )
end
if snow_tom_YR ==
false then
snow_tom_YR =
check_for_snow (
entry.data.next_1_hours )
end
end
end
grp.write (
status_temp ,
temperature_YR )
grp.write (
status_pressure ,
air_pressure_YR )
grp.write (
status_humidity ,
humidity_YR )
grp.write (
status_wind_direction ,
wind_direction_YR )
grp.write (
status_wind_speed ,
wind_speed_YR )
grp.write (
status_uv_index ,
uv_index_YR )
grp.write (
status_feelslike_temp ,
feelslike_temp_YR )
grp.write (
status_rain_today ,
rain_tod_YR )
grp.write (
status_snow_today ,
snow_tod_YR )
grp.write (
status_min_temp_today ,
min_max_today.min_t )
grp.write (
status_max_temp_today ,
min_max_today.max_t )
grp.write (
status_min_pressure_today ,
min_max_today.min_p )
grp.write (
status_max_pressure_today ,
min_max_today.max_p )
grp.write (
status_min_wind_speed_today ,
min_max_today.min_w )
grp.write (
status_max_wind_speed_today ,
min_max_today.max_w )
grp.write (
status_min_uv_today ,
min_max_today.min_uv )
grp.write (
status_max_uv_today ,
min_max_today.max_uv )
grp.write (
status_rain_tomor ,
rain_tom_YR )
grp.write (
status_snow_tomor ,
snow_tom_YR )
grp.write (
status_min_temp_tomor ,
min_max_tomor.min_t )
grp.write (
status_max_temp_tomor ,
min_max_tomor.max_t )
grp.write (
status_min_pressure_tomor ,
min_max_tomor.min_p )
grp.write (
status_max_pressure_tomor ,
min_max_tomor.max_p )
grp.write (
status_min_wind_speed_tomor ,
min_max_tomor.min_w )
grp.write (
status_max_wind_speed_tomor ,
min_max_tomor.max_w )
grp.write (
status_min_uv_tomor ,
min_max_tomor.min_uv )
grp.write (
status_max_uv_tomor ,
min_max_tomor.max_uv )
end
4. How often we can start this script?
You can run the script every four hours, should work fine.
Attached Files
Posts: 31
Threads: 5
Joined: Oct 2017
Reputation:
0
Thank you for the script RomansP!
Have tested, but I get no data coming to addresses 33/1/*. All stay at 0.
Although I receive a link with data when I log (url) at line 272.
Is it still working with you?
Posts: 58
Threads: 2
Joined: Dec 2020
Reputation:
25
22.06.2022, 08:17
(This post was last modified: 22.06.2022, 08:57 by RomansP .)
Hi, Mrinj!
Yes it is still working for me. Can you attach your script, so I will try to figure out what could gone wrong.
The updated version for script.
Added check for http.request , so there will be a log if something gone wrong.
Code:
1 2 3 4 5 6 7
res ,
code =
http.request (
url )
if res and code ==
200 then
data =
json.pdecode (
res )
else
log (
'request error' ,
res ,
code )
end
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395
latitude =
56.9496
longitude =
24.1052
status_temp =
grp.create ({
datatype =
dt.float16 ,
address =
'33/1/1' ,
name =
'current_temp' ,})
status_pressure =
grp.create ({
datatype =
dt.uint16 ,
address =
'33/1/2' ,
name =
'current_press' ,})
status_humidity =
grp.create ({
datatype =
dt.scale ,
address =
'33/1/3' ,
name =
'current_humid' ,})
status_wind_direction =
grp.create ({
datatype =
dt.float16 ,
address =
'33/1/4' ,
name =
'current_wind_dir' ,})
status_wind_speed =
grp.create ({
datatype =
dt.float16 ,
address =
'33/1/5' ,
name =
'current_wind_sp' ,})
status_uv_index =
grp.create ({
datatype =
dt.uint8 ,
address =
'33/1/6' ,
name =
'current_uv_index' ,})
status_feelslike_temp =
grp.create ({
datatype =
dt.float16 ,
address =
'33/1/7' ,
name =
'current_feels_temp' ,})
status_rain_today =
grp.create ({
datatype =
dt.bool ,
address =
'33/1/8' ,
name =
'rain_today' ,})
status_snow_today =
grp.create ({
datatype =
dt.bool ,
address =
'33/1/9' ,
name =
'snow_today' ,})
status_min_temp_today =
grp.create ({
datatype =
dt.float16 ,
address =
'33/1/10' ,
name =
'today_temp_min' ,})
status_max_temp_today =
grp.create ({
datatype =
dt.float16 ,
address =
'33/1/11' ,
name =
'today_temp_max' ,})
status_min_pressure_today =
grp.create ({
datatype =
dt.uint16 ,
address =
'33/1/12' ,
name =
'today_press_min' ,})
status_max_pressure_today =
grp.create ({
datatype =
dt.uint16 ,
address =
'33/1/13' ,
name =
'today_press_max' ,})
status_min_wind_speed_today =
grp.create ({
datatype =
dt.float16 ,
address =
'33/1/14' ,
name =
'today_wind_min' ,})
status_max_wind_speed_today =
grp.create ({
datatype =
dt.float16 ,
address =
'33/1/15' ,
name =
'today_wind_max' ,})
status_min_uv_today =
grp.create ({
datatype =
dt.uint8 ,
address =
'33/1/16' ,
name =
'today_uv_min' ,})
status_max_uv_today =
grp.create ({
datatype =
dt.uint8 ,
address =
'33/1/17' ,
name =
'today_uv_max' ,})
status_rain_tomor =
grp.create ({
datatype =
dt.bool ,
address =
'33/1/18' ,
name =
'rain_tomor' ,})
status_snow_tomor =
grp.create ({
datatype =
dt.bool ,
address =
'33/1/19' ,
name =
'snow_tomor' ,})
status_min_temp_tomor =
grp.create ({
datatype =
dt.float16 ,
address =
'33/1/20' ,
name =
'tomor_temp_min' ,})
status_max_temp_tomor =
grp.create ({
datatype =
dt.float16 ,
address =
'33/1/21' ,
name =
'tomor_temp_max' ,})
status_min_pressure_tomor =
grp.create ({
datatype =
dt.uint16 ,
address =
'33/1/22' ,
name =
'tomor_press_min' ,})
status_max_pressure_tomor =
grp.create ({
datatype =
dt.uint16 ,
address =
'33/1/23' ,
name =
'tomor_press_max' ,})
status_min_wind_speed_tomor =
grp.create ({
datatype =
dt.float16 ,
address =
'33/1/24' ,
name =
'tomor_wind_min' ,})
status_max_wind_speed_tomor =
grp.create ({
datatype =
dt.float16 ,
address =
'33/1/25' ,
name =
'tomor_wind_max' ,})
status_min_uv_tomor =
grp.create ({
datatype =
dt.uint8 ,
address =
'33/1/26' ,
name =
'tomor_uv_min' ,})
status_max_uv_tomor =
grp.create ({
datatype =
dt.uint8 ,
address =
'33/1/27' ,
name =
'tomor_uv_max' ,})
rain_tod_YR =
false
snow_tod_YR =
false
rain_tom_YR =
false
snow_tom_YR =
false
temperature_YR = -
273
air_pressure_YR = -
1
humidity_YR = -
1
wind_direction_YR = -
1
wind_speed_YR = -
1
uv_index_YR = -
1
feelslike_temp_YR = -
273
function init_min_max_table ()
local min_max = {}
min_max.min_t =
8000
min_max.max_t = -
273
min_max.min_p =
8000
min_max.max_p = -
1
min_max.min_w =
8000
min_max.max_w = -
1
min_max.min_uv =
8000
min_max.max_uv = -
1
return min_max
end
function set_timestamp (
day )
local date =
os.date (
'*t' )
date.day =
date.day +
day
local time =
os.time (
date )
return os.date (
'%Y-%m-%d' ,
time ),
date.hour
end
function min_max_temp_press_wind (
dat_tab ,
min_max_tab )
min_max_tab.min_t =
math.min (
min_max_tab.min_t ,
dat_tab.air_temperature or 8000 )
min_max_tab.max_t =
math.max (
min_max_tab.max_t ,
dat_tab.air_temperature or -
273 )
min_max_tab.min_p =
math.min (
min_max_tab.min_p ,
dat_tab.air_pressure_at_sea_level or 8000 )
min_max_tab.max_p =
math.max (
min_max_tab.max_p ,
dat_tab.air_pressure_at_sea_level or -
1 )
min_max_tab.min_w =
math.min (
min_max_tab.min_w ,
dat_tab.wind_speed or 8000 )
min_max_tab.max_w =
math.max (
min_max_tab.max_w ,
dat_tab.wind_speed or -
1 )
min_max_tab.min_uv =
math.min (
min_max_tab.min_uv ,
dat_tab.ultraviolet_index_clear_sky or 8000 )
min_max_tab.max_uv =
math.max (
min_max_tab.max_uv ,
dat_tab.ultraviolet_index_clear_sky or -
1 )
return min_max_tab
end
function check_for_rain (
next1h )
local is_rainy =
false
local code =
''
if type (
next1h ) ==
'table' and type (
next1h.summary ) ==
'table' then
code =
next1h.summary.symbol_code or ''
end
if code :
find (
'rain' )
then
is_rainy =
true
end
return is_rainy
end
function check_for_snow (
next1h )
local is_snowy =
false
local code =
''
if type (
next1h ) ==
'table' and type (
next1h.summary ) ==
'table' then
code =
next1h.summary.symbol_code or ''
end
if code :
find (
'snow' )
then
is_snowy =
true
end
return is_snowy
end
function calc_heat_index (
temperature ,
humidity )
if temperature <
26.7 then
log (
'for heat_index calculation temperature should be higher than +26.7 Celsius degree' )
return -
273
end
if humidity <
0 or humidity >
100 then
log (
'humidity should be in a range from 0 ... 100%' )
return -
273
end
local T =
temperature
local H =
humidity
local c_1 = -
8.785
local c_2 =
1.611
local c_3 =
2.339
local c_4 = -
0.146
local c_5 = -
0.0123
local c_6 = -
0.0164
local c_7 =
0.00221
local c_8 =
0.000725
local c_9 = -
0.00000358
local heat_index =
c_1 +
c_2 *
T +
c_3 *
H +
c_4 *
T *
H +
c_5 *
T *
T +
c_6 *
H *
H +
c_7 *
T *
T *
H +
c_8 *
T *
H *
H +
c_9 *
T *
T *
H *
H
return heat_index
end
function calc_wind_chill (
temperature ,
wind_speed )
if temperature >
10 then
log (
'temperature should be less than +10.0 Celsius degree' )
return -
273
end
if wind_speed <
0 or wind_speed >
49.2 then
log (
'wind speed should be in a range 0 ... 49.2 m/s' )
return -
273
end
local T_air =
temperature
local a =
0.62
local b =
0.51
local T_1 =
13.1
local T_2 =
14.6
local M_0 =
1.3333
local M =
wind_speed
local T_wind_chill
if M >
M_0 then
T_wind_chill = (
a *
T_air +
T_1 ) + (
b *
T_air -
T_2 )*(
M /
M_0 )^
0.16
else
T_wind_chill =
T_air
end
return T_wind_chill
end
function calc_feelslike (
temperature ,
wind_speed ,
humidity )
local T_feelslike
if temperature <=
10.0 then
T_feelslike =
calc_wind_chill (
temperature ,
wind_speed )
elseif temperature >=
26.7 then
T_feelslike =
calc_heat_index (
temperature ,
humidity )
else
T_feelslike =
temperature
end
return T_feelslike
end
min_max_today =
init_min_max_table ()
min_max_tomor =
init_min_max_table ()
json =
require (
'json' )
http =
require (
'socket.http' )
args =
'lat=' ..
latitude ..
'&lon=' ..
longitude
url =
'https://api.met.no/weatherapi/locationforecast/2.0/complete?' ..
args
mac =
0
io.readfile (
'/sys/class/net/eth0/address' ):
gsub (
'%x%x' ,
function (
v )
mac =
mac *
256 +
tonumber (
v ,
16 )
end )
http.USERAGENT =
'LM ' ..
mac
res ,
code =
http.request (
url )
if res and code ==
200 then
data =
json.pdecode (
res )
else
log (
'request error' ,
res ,
code )
end
if type (
data ) ==
'table' then
timestamp ,
hour =
set_timestamp (
0 )
if hour <
10 then
timestamp_current_hour =
timestamp ..
'T0' ..
hour ..
':00:00Z'
else
timestamp_current_hour =
timestamp ..
'T' ..
hour ..
':00:00Z'
end
timestamp_next =
set_timestamp (
1 )
for i ,
entry in ipairs (
data.properties.timeseries )
do
if entry.time ==
timestamp_current_hour then
current_data =
entry.data.instant.details
if type (
current_data ) ==
'table' then
temperature_YR =
current_data.air_temperature or -
273
air_pressure_YR =
current_data.air_pressure_at_sea_level or -
1
humidity_YR =
current_data.relative_humidity or -
1
wind_direction_YR =
current_data.wind_from_direction or -
1
wind_speed_YR =
current_data.wind_speed or -
1
uv_index_YR =
current_data.ultraviolet_index_clear_sky or -
1
feelslike_temp_YR =
calc_feelslike (
temperature_YR ,
wind_speed_YR ,
humidity_YR )
end
end
if entry.time :
sub (
1 , #
timestamp ) ==
timestamp then
current_data =
entry.data.instant.details
if type (
current_data ) ==
'table' then
min_max_today =
min_max_temp_press_wind (
current_data ,
min_max_today )
end
if rain_tod_YR ==
false then
rain_tod_YR =
check_for_rain (
entry.data.next_1_hours )
end
if snow_tod_YR ==
false then
snow_tod_YR =
check_for_snow (
entry.data.next_1_hours )
end
end
if entry.time :
sub (
1 , #
timestamp_next ) ==
timestamp_next then
tomor_data =
entry.data.instant.details
if type (
tomor_data ) ==
'table' then
min_max_tomor =
min_max_temp_press_wind (
tomor_data ,
min_max_tomor )
end
if rain_tom_YR ==
false then
rain_tom_YR =
check_for_rain (
entry.data.next_1_hours )
end
if snow_tom_YR ==
false then
snow_tom_YR =
check_for_snow (
entry.data.next_1_hours )
end
end
end
grp.write (
status_temp ,
temperature_YR )
grp.write (
status_pressure ,
air_pressure_YR )
grp.write (
status_humidity ,
humidity_YR )
grp.write (
status_wind_direction ,
wind_direction_YR )
grp.write (
status_wind_speed ,
wind_speed_YR )
grp.write (
status_uv_index ,
uv_index_YR )
grp.write (
status_feelslike_temp ,
feelslike_temp_YR )
grp.write (
status_rain_today ,
rain_tod_YR )
grp.write (
status_snow_today ,
snow_tod_YR )
grp.write (
status_min_temp_today ,
min_max_today.min_t )
grp.write (
status_max_temp_today ,
min_max_today.max_t )
grp.write (
status_min_pressure_today ,
min_max_today.min_p )
grp.write (
status_max_pressure_today ,
min_max_today.max_p )
grp.write (
status_min_wind_speed_today ,
min_max_today.min_w )
grp.write (
status_max_wind_speed_today ,
min_max_today.max_w )
grp.write (
status_min_uv_today ,
min_max_today.min_uv )
grp.write (
status_max_uv_today ,
min_max_today.max_uv )
grp.write (
status_rain_tomor ,
rain_tom_YR )
grp.write (
status_snow_tomor ,
snow_tom_YR )
grp.write (
status_min_temp_tomor ,
min_max_tomor.min_t )
grp.write (
status_max_temp_tomor ,
min_max_tomor.max_t )
grp.write (
status_min_pressure_tomor ,
min_max_tomor.min_p )
grp.write (
status_max_pressure_tomor ,
min_max_tomor.max_p )
grp.write (
status_min_wind_speed_tomor ,
min_max_tomor.min_w )
grp.write (
status_max_wind_speed_tomor ,
min_max_tomor.max_w )
grp.write (
status_min_uv_tomor ,
min_max_tomor.min_uv )
grp.write (
status_max_uv_tomor ,
min_max_tomor.max_uv )
end
Attached Files
Posts: 177
Threads: 48
Joined: Mar 2021
Reputation:
2
(22.06.2022, 08:00) Mrinj Wrote: Thank you for the script RomansP!
Have tested, but I get no data coming to addresses 33/1/*. All stay at 0.
Although I receive a link with data when I log (url) at line 272.
Is it still working with you?
Great script,
Thank you for sharing it.
Posts: 31
Threads: 5
Joined: Oct 2017
Reputation:
0
(22.06.2022, 08:17) Thank you RomansP! Very kind of you! Wrote: Testing with your V14 script I get the following error in the log:
-------------
* arg: 1
* string: request error
* arg: 2
* nil
* arg: 3
* string: error:1409442E:lib(20):func(148):reason(1070)
-------------
As sad earlier, from your script I only change the addresses to 53/1/* from your script cause my 33/*/* once are busy
Script is attached.
Thanks!
BR,
Mrinj
RomansPHi, Mrinj!
Yes it is still working for me. Can you attach your script, so I will try to figure out what could gone wrong.
The updated version for script.
Added check for http.request , so there will be a log if something gone wrong.
Code:
1 2 3 4 5 6 7
res ,
code =
http.request (
url )
if res and code ==
200 then
data =
json.pdecode (
res )
else
log (
'request error' ,
res ,
code )
end
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395
latitude =
56.9496
longitude =
24.1052
status_temp =
grp.create ({
datatype =
dt.float16 ,
address =
'33/1/1' ,
name =
'current_temp' ,})
status_pressure =
grp.create ({
datatype =
dt.uint16 ,
address =
'33/1/2' ,
name =
'current_press' ,})
status_humidity =
grp.create ({
datatype =
dt.scale ,
address =
'33/1/3' ,
name =
'current_humid' ,})
status_wind_direction =
grp.create ({
datatype =
dt.float16 ,
address =
'33/1/4' ,
name =
'current_wind_dir' ,})
status_wind_speed =
grp.create ({
datatype =
dt.float16 ,
address =
'33/1/5' ,
name =
'current_wind_sp' ,})
status_uv_index =
grp.create ({
datatype =
dt.uint8 ,
address =
'33/1/6' ,
name =
'current_uv_index' ,})
status_feelslike_temp =
grp.create ({
datatype =
dt.float16 ,
address =
'33/1/7' ,
name =
'current_feels_temp' ,})
status_rain_today =
grp.create ({
datatype =
dt.bool ,
address =
'33/1/8' ,
name =
'rain_today' ,})
status_snow_today =
grp.create ({
datatype =
dt.bool ,
address =
'33/1/9' ,
name =
'snow_today' ,})
status_min_temp_today =
grp.create ({
datatype =
dt.float16 ,
address =
'33/1/10' ,
name =
'today_temp_min' ,})
status_max_temp_today =
grp.create ({
datatype =
dt.float16 ,
address =
'33/1/11' ,
name =
'today_temp_max' ,})
status_min_pressure_today =
grp.create ({
datatype =
dt.uint16 ,
address =
'33/1/12' ,
name =
'today_press_min' ,})
status_max_pressure_today =
grp.create ({
datatype =
dt.uint16 ,
address =
'33/1/13' ,
name =
'today_press_max' ,})
status_min_wind_speed_today =
grp.create ({
datatype =
dt.float16 ,
address =
'33/1/14' ,
name =
'today_wind_min' ,})
status_max_wind_speed_today =
grp.create ({
datatype =
dt.float16 ,
address =
'33/1/15' ,
name =
'today_wind_max' ,})
status_min_uv_today =
grp.create ({
datatype =
dt.uint8 ,
address =
'33/1/16' ,
name =
'today_uv_min' ,})
status_max_uv_today =
grp.create ({
datatype =
dt.uint8 ,
address =
'33/1/17' ,
name =
'today_uv_max' ,})
status_rain_tomor =
grp.create ({
datatype =
dt.bool ,
address =
'33/1/18' ,
name =
'rain_tomor' ,})
status_snow_tomor =
grp.create ({
datatype =
dt.bool ,
address =
'33/1/19' ,
name =
'snow_tomor' ,})
status_min_temp_tomor =
grp.create ({
datatype =
dt.float16 ,
address =
'33/1/20' ,
name =
'tomor_temp_min' ,})
status_max_temp_tomor =
grp.create ({
datatype =
dt.float16 ,
address =
'33/1/21' ,
name =
'tomor_temp_max' ,})
status_min_pressure_tomor =
grp.create ({
datatype =
dt.uint16 ,
address =
'33/1/22' ,
name =
'tomor_press_min' ,})
status_max_pressure_tomor =
grp.create ({
datatype =
dt.uint16 ,
address =
'33/1/23' ,
name =
'tomor_press_max' ,})
status_min_wind_speed_tomor =
grp.create ({
datatype =
dt.float16 ,
address =
'33/1/24' ,
name =
'tomor_wind_min' ,})
status_max_wind_speed_tomor =
grp.create ({
datatype =
dt.float16 ,
address =
'33/1/25' ,
name =
'tomor_wind_max' ,})
status_min_uv_tomor =
grp.create ({
datatype =
dt.uint8 ,
address =
'33/1/26' ,
name =
'tomor_uv_min' ,})
status_max_uv_tomor =
grp.create ({
datatype =
dt.uint8 ,
address =
'33/1/27' ,
name =
'tomor_uv_max' ,})
rain_tod_YR =
false
snow_tod_YR =
false
rain_tom_YR =
false
snow_tom_YR =
false
temperature_YR = -
273
air_pressure_YR = -
1
humidity_YR = -
1
wind_direction_YR = -
1
wind_speed_YR = -
1
uv_index_YR = -
1
feelslike_temp_YR = -
273
function init_min_max_table ()
local min_max = {}
min_max.min_t =
8000
min_max.max_t = -
273
min_max.min_p =
8000
min_max.max_p = -
1
min_max.min_w =
8000
min_max.max_w = -
1
min_max.min_uv =
8000
min_max.max_uv = -
1
return min_max
end
function set_timestamp (
day )
local date =
os.date (
'*t' )
date.day =
date.day +
day
local time =
os.time (
date )
return os.date (
'%Y-%m-%d' ,
time ),
date.hour
end
function min_max_temp_press_wind (
dat_tab ,
min_max_tab )
min_max_tab.min_t =
math.min (
min_max_tab.min_t ,
dat_tab.air_temperature or 8000 )
min_max_tab.max_t =
math.max (
min_max_tab.max_t ,
dat_tab.air_temperature or -
273 )
min_max_tab.min_p =
math.min (
min_max_tab.min_p ,
dat_tab.air_pressure_at_sea_level or 8000 )
min_max_tab.max_p =
math.max (
min_max_tab.max_p ,
dat_tab.air_pressure_at_sea_level or -
1 )
min_max_tab.min_w =
math.min (
min_max_tab.min_w ,
dat_tab.wind_speed or 8000 )
min_max_tab.max_w =
math.max (
min_max_tab.max_w ,
dat_tab.wind_speed or -
1 )
min_max_tab.min_uv =
math.min (
min_max_tab.min_uv ,
dat_tab.ultraviolet_index_clear_sky or 8000 )
min_max_tab.max_uv =
math.max (
min_max_tab.max_uv ,
dat_tab.ultraviolet_index_clear_sky or -
1 )
return min_max_tab
end
function check_for_rain (
next1h )
local is_rainy =
false
local code =
''
if type (
next1h ) ==
'table' and type (
next1h.summary ) ==
'table' then
code =
next1h.summary.symbol_code or ''
end
if code :
find (
'rain' )
then
is_rainy =
true
end
return is_rainy
end
function check_for_snow (
next1h )
local is_snowy =
false
local code =
''
if type (
next1h ) ==
'table' and type (
next1h.summary ) ==
'table' then
code =
next1h.summary.symbol_code or ''
end
if code :
find (
'snow' )
then
is_snowy =
true
end
return is_snowy
end
function calc_heat_index (
temperature ,
humidity )
if temperature <
26.7 then
log (
'for heat_index calculation temperature should be higher than +26.7 Celsius degree' )
return -
273
end
if humidity <
0 or humidity >
100 then
log (
'humidity should be in a range from 0 ... 100%' )
return -
273
end
local T =
temperature
local H =
humidity
local c_1 = -
8.785
local c_2 =
1.611
local c_3 =
2.339
local c_4 = -
0.146
local c_5 = -
0.0123
local c_6 = -
0.0164
local c_7 =
0.00221
local c_8 =
0.000725
local c_9 = -
0.00000358
local heat_index =
c_1 +
c_2 *
T +
c_3 *
H +
c_4 *
T *
H +
c_5 *
T *
T +
c_6 *
H *
H +
c_7 *
T *
T *
H +
c_8 *
T *
H *
H +
c_9 *
T *
T *
H *
H
return heat_index
end
function calc_wind_chill (
temperature ,
wind_speed )
if temperature >
10 then
log (
'temperature should be less than +10.0 Celsius degree' )
return -
273
end
if wind_speed <
0 or wind_speed >
49.2 then
log (
'wind speed should be in a range 0 ... 49.2 m/s' )
return -
273
end
local T_air =
temperature
local a =
0.62
local b =
0.51
local T_1 =
13.1
local T_2 =
14.6
local M_0 =
1.3333
local M =
wind_speed
local T_wind_chill
if M >
M_0 then
T_wind_chill = (
a *
T_air +
T_1 ) + (
b *
T_air -
T_2 )*(
M /
M_0 )^
0.16
else
T_wind_chill =
T_air
end
return T_wind_chill
end
function calc_feelslike (
temperature ,
wind_speed ,
humidity )
local T_feelslike
if temperature <=
10.0 then
T_feelslike =
calc_wind_chill (
temperature ,
wind_speed )
elseif temperature >=
26.7 then
T_feelslike =
calc_heat_index (
temperature ,
humidity )
else
T_feelslike =
temperature
end
return T_feelslike
end
min_max_today =
init_min_max_table ()
min_max_tomor =
init_min_max_table ()
json =
require (
'json' )
http =
require (
'socket.http' )
args =
'lat=' ..
latitude ..
'&lon=' ..
longitude
url =
'https://api.met.no/weatherapi/locationforecast/2.0/complete?' ..
args
mac =
0
io.readfile (
'/sys/class/net/eth0/address' ):
gsub (
'%x%x' ,
function (
v )
mac =
mac *
256 +
tonumber (
v ,
16 )
end )
http.USERAGENT =
'LM ' ..
mac
res ,
code =
http.request (
url )
if res and code ==
200 then
data =
json.pdecode (
res )
else
log (
'request error' ,
res ,
code )
end
if type (
data ) ==
'table' then
timestamp ,
hour =
set_timestamp (
0 )
if hour <
10 then
timestamp_current_hour =
timestamp ..
'T0' ..
hour ..
':00:00Z'
else
timestamp_current_hour =
timestamp ..
'T' ..
hour ..
':00:00Z'
end
timestamp_next =
set_timestamp (
1 )
for i ,
entry in ipairs (
data.properties.timeseries )
do
if entry.time ==
timestamp_current_hour then
current_data =
entry.data.instant.details
if type (
current_data ) ==
'table' then
temperature_YR =
current_data.air_temperature or -
273
air_pressure_YR =
current_data.air_pressure_at_sea_level or -
1
humidity_YR =
current_data.relative_humidity or -
1
wind_direction_YR =
current_data.wind_from_direction or -
1
wind_speed_YR =
current_data.wind_speed or -
1
uv_index_YR =
current_data.ultraviolet_index_clear_sky or -
1
feelslike_temp_YR =
calc_feelslike (
temperature_YR ,
wind_speed_YR ,
humidity_YR )
end
end
if entry.time :
sub (
1 , #
timestamp ) ==
timestamp then
current_data =
entry.data.instant.details
if type (
current_data ) ==
'table' then
min_max_today =
min_max_temp_press_wind (
current_data ,
min_max_today )
end
if rain_tod_YR ==
false then
rain_tod_YR =
check_for_rain (
entry.data.next_1_hours )
end
if snow_tod_YR ==
false then
snow_tod_YR =
check_for_snow (
entry.data.next_1_hours )
end
end
if entry.time :
sub (
1 , #
timestamp_next ) ==
timestamp_next then
tomor_data =
entry.data.instant.details
if type (
tomor_data ) ==
'table' then
min_max_tomor =
min_max_temp_press_wind (
tomor_data ,
min_max_tomor )
end
if rain_tom_YR ==
false then
rain_tom_YR =
check_for_rain (
entry.data.next_1_hours )
end
if snow_tom_YR ==
false then
snow_tom_YR =
check_for_snow (
entry.data.next_1_hours )
end
end
end
grp.write (
status_temp ,
temperature_YR )
grp.write (
status_pressure ,
air_pressure_YR )
grp.write (
status_humidity ,
humidity_YR )
grp.write (
status_wind_direction ,
wind_direction_YR )
grp.write (
status_wind_speed ,
wind_speed_YR )
grp.write (
status_uv_index ,
uv_index_YR )
grp.write (
status_feelslike_temp ,
feelslike_temp_YR )
grp.write (
status_rain_today ,
rain_tod_YR )
grp.write (
status_snow_today ,
snow_tod_YR )
grp.write (
status_min_temp_today ,
min_max_today.min_t )
grp.write (
status_max_temp_today ,
min_max_today.max_t )
grp.write (
status_min_pressure_today ,
min_max_today.min_p )
grp.write (
status_max_pressure_today ,
min_max_today.max_p )
grp.write (
status_min_wind_speed_today ,
min_max_today.min_w )
grp.write (
status_max_wind_speed_today ,
min_max_today.max_w )
grp.write (
status_min_uv_today ,
min_max_today.min_uv )
grp.write (
status_max_uv_today ,
min_max_today.max_uv )
grp.write (
status_rain_tomor ,
rain_tom_YR )
grp.write (
status_snow_tomor ,
snow_tom_YR )
grp.write (
status_min_temp_tomor ,
min_max_tomor.min_t )
grp.write (
status_max_temp_tomor ,
min_max_tomor.max_t )
grp.write (
status_min_pressure_tomor ,
min_max_tomor.min_p )
grp.write (
status_max_pressure_tomor ,
min_max_tomor.max_p )
grp.write (
status_min_wind_speed_tomor ,
min_max_tomor.min_w )
grp.write (
status_max_wind_speed_tomor ,
min_max_tomor.max_w )
grp.write (
status_min_uv_tomor ,
min_max_tomor.min_uv )
grp.write (
status_max_uv_tomor ,
min_max_tomor.max_uv )
end
Attached Files
Posts: 8174
Threads: 43
Joined: Jun 2015
Reputation:
473
This lib/func/reason error is due to an outdated firmware.
Posts: 31
Threads: 5
Joined: Oct 2017
Reputation:
0
(23.06.2022, 06:24) admin Wrote: This lib/func/reason error is due to an outdated firmware.
Thank you admin!
I will update and test
Posts: 16
Threads: 1
Joined: Aug 2015
Reputation:
0
Hello,
Please, I hope you could help me in my issue
However, Until now i had script work fine , but now i cant obtain weather dates,
This is the scheduled script that I used.
I can see on log :
* arg: 1
* string: <html>
<head><title>400 The plain HTTP request was sent to HTTPS port</title></head>
<body>
<center><h1>400 Bad Request</h1></center>
<center>The plain HTTP request was sent to HTTPS port</center>
<hr><center>nginx/1.18.0 (Ubuntu)</center>
</body>
</html>
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
latitude =
40.4538899
longitude = -
3.6211718
json =
require (
'json' )
http2 =
require (
'socket.http' )
log (
http2 )
args =
'lat=' ..
latitude ..
'&lon=' ..
longitude
url =
'https://api.met.no/weatherapi/locationforecast/2.0/compact?' ..
args
mac =
0
io.readfile (
'/sys/class/net/eth0/address' ):
gsub (
'%x%x' ,
function (
v )
mac =
mac *
256 +
tonumber (
v ,
16 )
end )
http2.USERAGENT =
'LM ' ..
mac
res ,
err =
http2.request (
url )
log (
res ,
err )
data =
json.pdecode (
res )
log (
data )
if type (
data ) ==
'table' then
date =
os.date (
'*t' )
date.day =
date.day +
1
time =
os.time (
date )
timestamp =
os.date (
'%Y-%m-%d' ,
time )
log (
data )
Posts: 8174
Threads: 43
Joined: Jun 2015
Reputation:
473
You have an old firmware which does not support HTTPS directly in socket.http. ssl.https must be used instead.
The request part can be rewritten like this to work with older firmware:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
latitude =
40.4538899
longitude = -
3.6211718
https =
require (
'ssl.https' )
json =
require (
'json' )
ltn12 =
require (
'ltn12' )
args =
'lat=' ..
latitude ..
'&lon=' ..
longitude
url =
'https://api.met.no/weatherapi/locationforecast/2.0/compact?' ..
args
mac =
0
io.readfile (
'/sys/class/net/eth0/address' ):
gsub (
'%x%x' ,
function (
v )
mac =
mac *
256 +
tonumber (
v ,
16 )
end )
response = {}
res ,
code =
https.request ({
url =
url ,
protocol =
'tlsv12' ,
headers = {
[
'user-agent' ] =
'LM ' ..
mac
},
sink =
ltn12.sink.table (
response )
})
if res and code ==
200 then
data =
json.pdecode (
table.concat (
response ))
end
if type (
data ) ==
'table' then
log (
data )
else
log (
'request failed' ,
res ,
code )
end
Posts: 16
Threads: 1
Joined: Aug 2015
Reputation:
0
Thaks a lot!!
If I use the script above , I have no data , I receive :
table:
[data]
* table:
[next_6_hours]
* table:
[summary]
* table:
nesting too deep
[details]
* table:
nesting too deep
[next_12_hours]
* table:
[summary]
* table:
nesting too deep
[next_1_hours]
* table:
[summary]
* table:
nesting too deep
Posts: 58
Threads: 2
Joined: Dec 2020
Reputation:
25
Hi, Acla, it is correct, you need to parse all the data. You can use this script.
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338
status_temp =
grp.create ({
datatype =
dt.float16 ,
address =
'33/1/1' ,
name =
'current_temp' ,})
status_pressure =
grp.create ({
datatype =
dt.uint16 ,
address =
'33/1/2' ,
name =
'current_press' ,})
status_humidity =
grp.create ({
datatype =
dt.scale ,
address =
'33/1/3' ,
name =
'current_humid' ,})
status_wind_direction =
grp.create ({
datatype =
dt.float16 ,
address =
'33/1/4' ,
name =
'current_wind_dir' ,})
status_wind_speed =
grp.create ({
datatype =
dt.float16 ,
address =
'33/1/5' ,
name =
'current_wind_sp' ,})
status_uv_index =
grp.create ({
datatype =
dt.uint8 ,
address =
'33/1/6' ,
name =
'current_uv_index' ,})
status_feelslike_temp =
grp.create ({
datatype =
dt.float16 ,
address =
'33/1/7' ,
name =
'current_feels_temp' ,})
status_rain_today =
grp.create ({
datatype =
dt.bool ,
address =
'33/1/8' ,
name =
'rain_today' ,})
status_snow_today =
grp.create ({
datatype =
dt.bool ,
address =
'33/1/9' ,
name =
'snow_today' ,})
status_min_temp_today =
grp.create ({
datatype =
dt.float16 ,
address =
'33/1/10' ,
name =
'today_temp_min' ,})
status_max_temp_today =
grp.create ({
datatype =
dt.float16 ,
address =
'33/1/11' ,
name =
'today_temp_max' ,})
status_min_pressure_today =
grp.create ({
datatype =
dt.uint16 ,
address =
'33/1/12' ,
name =
'today_press_min' ,})
status_max_pressure_today =
grp.create ({
datatype =
dt.uint16 ,
address =
'33/1/13' ,
name =
'today_press_max' ,})
status_min_wind_speed_today =
grp.create ({
datatype =
dt.float16 ,
address =
'33/1/14' ,
name =
'today_wind_min' ,})
status_max_wind_speed_today =
grp.create ({
datatype =
dt.float16 ,
address =
'33/1/15' ,
name =
'today_wind_max' ,})
status_min_uv_today =
grp.create ({
datatype =
dt.uint8 ,
address =
'33/1/16' ,
name =
'today_uv_min' ,})
status_max_uv_today =
grp.create ({
datatype =
dt.uint8 ,
address =
'33/1/17' ,
name =
'today_uv_max' ,})
status_rain_tomor =
grp.create ({
datatype =
dt.bool ,
address =
'33/1/18' ,
name =
'rain_tomor' ,})
status_snow_tomor =
grp.create ({
datatype =
dt.bool ,
address =
'33/1/19' ,
name =
'snow_tomor' ,})
status_min_temp_tomor =
grp.create ({
datatype =
dt.float16 ,
address =
'33/1/20' ,
name =
'tomor_temp_min' ,})
status_max_temp_tomor =
grp.create ({
datatype =
dt.float16 ,
address =
'33/1/21' ,
name =
'tomor_temp_max' ,})
status_min_pressure_tomor =
grp.create ({
datatype =
dt.uint16 ,
address =
'33/1/22' ,
name =
'tomor_press_min' ,})
status_max_pressure_tomor =
grp.create ({
datatype =
dt.uint16 ,
address =
'33/1/23' ,
name =
'tomor_press_max' ,})
status_min_wind_speed_tomor =
grp.create ({
datatype =
dt.float16 ,
address =
'33/1/24' ,
name =
'tomor_wind_min' ,})
status_max_wind_speed_tomor =
grp.create ({
datatype =
dt.float16 ,
address =
'33/1/25' ,
name =
'tomor_wind_max' ,})
status_min_uv_tomor =
grp.create ({
datatype =
dt.uint8 ,
address =
'33/1/26' ,
name =
'tomor_uv_min' ,})
status_max_uv_tomor =
grp.create ({
datatype =
dt.uint8 ,
address =
'33/1/27' ,
name =
'tomor_uv_max' ,})
rain_tod_YR =
false
snow_tod_YR =
false
rain_tom_YR =
false
snow_tom_YR =
false
temperature_YR = -
273
air_pressure_YR = -
1
humidity_YR = -
1
wind_direction_YR = -
1
wind_speed_YR = -
1
uv_index_YR = -
1
feelslike_temp_YR = -
273
function init_min_max_table ()
local min_max = {}
min_max.min_t =
8000
min_max.max_t = -
273
min_max.min_p =
8000
min_max.max_p = -
1
min_max.min_w =
8000
min_max.max_w = -
1
min_max.min_uv =
8000
min_max.max_uv = -
1
return min_max
end
function set_timestamp (
day )
local date =
os.date (
'*t' )
date.day =
date.day +
day
local time =
os.time (
date )
return os.date (
'%Y-%m-%d' ,
time ),
date.hour
end
function min_max_temp_press_wind (
dat_tab ,
min_max_tab )
min_max_tab.min_t =
math.min (
min_max_tab.min_t ,
dat_tab.air_temperature or 8000 )
min_max_tab.max_t =
math.max (
min_max_tab.max_t ,
dat_tab.air_temperature or -
273 )
min_max_tab.min_p =
math.min (
min_max_tab.min_p ,
dat_tab.air_pressure_at_sea_level or 8000 )
min_max_tab.max_p =
math.max (
min_max_tab.max_p ,
dat_tab.air_pressure_at_sea_level or -
1 )
min_max_tab.min_w =
math.min (
min_max_tab.min_w ,
dat_tab.wind_speed or 8000 )
min_max_tab.max_w =
math.max (
min_max_tab.max_w ,
dat_tab.wind_speed or -
1 )
min_max_tab.min_uv =
math.min (
min_max_tab.min_uv ,
dat_tab.ultraviolet_index_clear_sky or 8000 )
min_max_tab.max_uv =
math.max (
min_max_tab.max_uv ,
dat_tab.ultraviolet_index_clear_sky or -
1 )
return min_max_tab
end
function check_for_rain (
next1h )
local is_rainy =
false
local code =
''
if type (
next1h ) ==
'table' and type (
next1h.summary ) ==
'table' then
code =
next1h.summary.symbol_code or ''
end
if code :
find (
'rain' )
then
is_rainy =
true
end
return is_rainy
end
function check_for_snow (
next1h )
local is_snowy =
false
local code =
''
if type (
next1h ) ==
'table' and type (
next1h.summary ) ==
'table' then
code =
next1h.summary.symbol_code or ''
end
if code :
find (
'snow' )
then
is_snowy =
true
end
return is_snowy
end
function calc_heat_index (
temperature ,
humidity )
if temperature <
26.7 then
log (
'for heat_index calculation temperature should be higher than +26.7 Celsius degree' )
return -
273
end
if humidity <
0 or humidity >
100 then
log (
'humidity should be in a range from 0 ... 100%' )
return -
273
end
local T =
temperature
local H =
humidity
local c_1 = -
8.785
local c_2 =
1.611
local c_3 =
2.339
local c_4 = -
0.146
local c_5 = -
0.0123
local c_6 = -
0.0164
local c_7 =
0.00221
local c_8 =
0.000725
local c_9 = -
0.00000358
local heat_index =
c_1 +
c_2 *
T +
c_3 *
H +
c_4 *
T *
H +
c_5 *
T *
T +
c_6 *
H *
H +
c_7 *
T *
T *
H +
c_8 *
T *
H *
H +
c_9 *
T *
T *
H *
H
return heat_index
end
function calc_wind_chill (
temperature ,
wind_speed )
if temperature >
10 then
log (
'temperature should be less than +10.0 Celsius degree' )
return -
273
end
if wind_speed <
0 or wind_speed >
49.2 then
log (
'wind speed should be in a range 0 ... 49.2 m/s' )
return -
273
end
local T_air =
temperature
local a =
0.62
local b =
0.51
local T_1 =
13.1
local T_2 =
14.6
local M_0 =
1.3333
local M =
wind_speed
local T_wind_chill
if M >
M_0 then
T_wind_chill = (
a *
T_air +
T_1 ) + (
b *
T_air -
T_2 )*(
M /
M_0 )^
0.16
else
T_wind_chill =
T_air
end
return T_wind_chill
end
function calc_feelslike (
temperature ,
wind_speed ,
humidity )
local T_feelslike
if temperature <=
10.0 then
T_feelslike =
calc_wind_chill (
temperature ,
wind_speed )
elseif temperature >=
26.7 then
T_feelslike =
calc_heat_index (
temperature ,
humidity )
else
T_feelslike =
temperature
end
return T_feelslike
end
min_max_today =
init_min_max_table ()
min_max_tomor =
init_min_max_table ()
latitude =
40.4538899
longitude = -
3.6211718
https =
require (
'ssl.https' )
json =
require (
'json' )
ltn12 =
require (
'ltn12' )
args =
'lat=' ..
latitude ..
'&lon=' ..
longitude
url =
'https://api.met.no/weatherapi/locationforecast/2.0/complete?' ..
args
mac =
0
io.readfile (
'/sys/class/net/eth0/address' ):
gsub (
'%x%x' ,
function (
v )
mac =
mac *
256 +
tonumber (
v ,
16 )
end )
response = {}
res ,
code =
https.request ({
url =
url ,
protocol =
'tlsv12' ,
headers = {
[
'user-agent' ] =
'LM ' ..
mac
},
sink =
ltn12.sink.table (
response )
})
if res and code ==
200 then
data =
json.pdecode (
table.concat (
response ))
else
log (
'request error' ,
res ,
code )
end
if type (
data ) ==
'table' then
timestamp ,
hour =
set_timestamp (
0 )
if hour <
10 then
timestamp_current_hour =
timestamp ..
'T0' ..
hour ..
':00:00Z'
else
timestamp_current_hour =
timestamp ..
'T' ..
hour ..
':00:00Z'
end
timestamp_next =
set_timestamp (
1 )
for i ,
entry in ipairs (
data.properties.timeseries )
do
if entry.time ==
timestamp_current_hour then
current_data =
entry.data.instant.details
if type (
current_data ) ==
'table' then
temperature_YR =
current_data.air_temperature or -
273
air_pressure_YR =
current_data.air_pressure_at_sea_level or -
1
humidity_YR =
current_data.relative_humidity or -
1
wind_direction_YR =
current_data.wind_from_direction or -
1
wind_speed_YR =
current_data.wind_speed or -
1
uv_index_YR =
current_data.ultraviolet_index_clear_sky or -
1
feelslike_temp_YR =
calc_feelslike (
temperature_YR ,
wind_speed_YR ,
humidity_YR )
end
end
if entry.time :
sub (
1 , #
timestamp ) ==
timestamp then
current_data =
entry.data.instant.details
if type (
current_data ) ==
'table' then
min_max_today =
min_max_temp_press_wind (
current_data ,
min_max_today )
end
if rain_tod_YR ==
false then
rain_tod_YR =
check_for_rain (
entry.data.next_1_hours )
end
if snow_tod_YR ==
false then
snow_tod_YR =
check_for_snow (
entry.data.next_1_hours )
end
end
if entry.time :
sub (
1 , #
timestamp_next ) ==
timestamp_next then
tomor_data =
entry.data.instant.details
if type (
tomor_data ) ==
'table' then
min_max_tomor =
min_max_temp_press_wind (
tomor_data ,
min_max_tomor )
end
if rain_tom_YR ==
false then
rain_tom_YR =
check_for_rain (
entry.data.next_1_hours )
end
if snow_tom_YR ==
false then
snow_tom_YR =
check_for_snow (
entry.data.next_1_hours )
end
end
end
grp.write (
status_temp ,
temperature_YR )
grp.write (
status_pressure ,
air_pressure_YR )
grp.write (
status_humidity ,
humidity_YR )
grp.write (
status_wind_direction ,
wind_direction_YR )
grp.write (
status_wind_speed ,
wind_speed_YR )
grp.write (
status_uv_index ,
uv_index_YR )
grp.write (
status_feelslike_temp ,
feelslike_temp_YR )
grp.write (
status_rain_today ,
rain_tod_YR )
grp.write (
status_snow_today ,
snow_tod_YR )
grp.write (
status_min_temp_today ,
min_max_today.min_t )
grp.write (
status_max_temp_today ,
min_max_today.max_t )
grp.write (
status_min_pressure_today ,
min_max_today.min_p )
grp.write (
status_max_pressure_today ,
min_max_today.max_p )
grp.write (
status_min_wind_speed_today ,
min_max_today.min_w )
grp.write (
status_max_wind_speed_today ,
min_max_today.max_w )
grp.write (
status_min_uv_today ,
min_max_today.min_uv )
grp.write (
status_max_uv_today ,
min_max_today.max_uv )
grp.write (
status_rain_tomor ,
rain_tom_YR )
grp.write (
status_snow_tomor ,
snow_tom_YR )
grp.write (
status_min_temp_tomor ,
min_max_tomor.min_t )
grp.write (
status_max_temp_tomor ,
min_max_tomor.max_t )
grp.write (
status_min_pressure_tomor ,
min_max_tomor.min_p )
grp.write (
status_max_pressure_tomor ,
min_max_tomor.max_p )
grp.write (
status_min_wind_speed_tomor ,
min_max_tomor.min_w )
grp.write (
status_max_wind_speed_tomor ,
min_max_tomor.max_w )
grp.write (
status_min_uv_tomor ,
min_max_tomor.min_uv )
grp.write (
status_max_uv_tomor ,
min_max_tomor.max_uv )
end