Page 1 of 1

External Temp/Baro with METAR script

Posted: Sunday 31 July 2016 22:21
by ld57
Hi!

I've just finished my 1st Domoticz script: it will give you external temperature and barometer by querying METAR (https://en.wikipedia.org/wiki/METAR) stations.

1- Make sure you have the "curl" package on your PI
2- Simply find your nearest METAR stations and edit the station variable below.
3- Idx is of course the index of a virtual sensor "Temp + Humidity + Baro".

script_time_metar.lua:

Code: Select all

-- METAR time script

local idx = 10
local station = "LFJL"

command = "curl http://tgftp.nws.noaa.gov/data/observations/metar/stations/" .. station .. ".TXT"
commandArray = {}

local m = os.date('%M')
if (m % 30 == 5) then
        -- update at 5 and 35 minutes of every hour
        local handle = io.popen(command)
        local data = handle:read("*a")
        handle:close()

        if (data == '') then
                 print("Error metar data not fetched")
                 return
        end
        
        data = string.gsub(data, "M", "-")
        local pres = tonumber(string.match(data, ' Q([0-9]+)'))
        local temp = tonumber(string.match(data, ' ([-0-9]+)/[-0-9]+ '))
        local dew = tonumber(string.match(data, ' [-0-9]+/([-0-9]+) '))
        local fore = 5

        -- very basic prediction
        if (pres < 1000) then
                fore = 4
        elseif (pres < 1020) then
                fore = 3
        elseif (pres < 1030) then
                fore = 2
        else    
                fore = 1
        end
        -- print (data, pres, temp)
        local hum = 100*(math.exp((17.625*dew)/(243.04+dew))/math.exp((17.625*temp)/(243.04+temp)))   
        
        
        commandArray[1] = {['UpdateDevice'] = idx .. '|0|' .. temp .. ';' .. hum .. ';0;' .. pres .. ';' .. fore}
end

return commandArray
It will run 2 times per hour since METAR data is not updated often.
I may improve that script with weather prediction soon.

Enjoy!

Re: External Temp/Baro with METAR script

Posted: Saturday 06 August 2016 18:24
by ld57
Hi!
I've updated the script so that the 'metar' package is no longer needed, only curl. I also added basic weather prediction and humidity calculation.
Enjoy!

Re: External Temp/Baro with METAR script

Posted: Tuesday 03 January 2017 13:37
by pavelbor
Hello,

looks like script does not handle minus temperatures.
M04/M07 indicates the temperature is −4 °C (25 °F) and the dew point is −7 °C (19 °F). An M in front of the number indicates that the temperature/dew point is below zero Celsius.

Code: Select all

local temp = tonumber(string.match(data, ' ([0-9]+)/[0-9]+ '))
Spoiler: show
METAR:
2017/01/03 12:20
EETN 031220Z 23005KT 180V260 9999 3600E -SHSN BKN009 FEW014CB BKN032 M04/M07 Q0999 RESHSN R08/490395 NOSIG

Re: External Temp/Baro with METAR script

Posted: Tuesday 03 January 2017 20:05
by ld57
Thanks! I've just updated the script above to fix this bug :)

Re: External Temp/Baro with METAR script

Posted: Tuesday 03 January 2017 21:00
by pavelbor
Thanks for quick reply! Good job!

Would be nice to add wind information from METAR data into Domoticz WIND type dummy sensor.

12012MPS indicates the wind direction is from 120° (east-southeast) at a speed of 12 m/s (23 knots; 27 mph; 44 km/h). Speed measurements can be in knots (abbreviated KT) or meters per second (abbreviated MPS).
090V150 indicates the wind direction is varying from 90° true (east) to 150° true (south-southeast).

Re: External Temp/Baro with METAR script

Posted: Saturday 07 January 2017 17:23
by lost
pavelbor wrote:Thanks for quick reply! Good job!

Would be nice to add wind information from METAR data into Domoticz WIND type dummy sensor.

12012MPS indicates the wind direction is from 120° (east-southeast) at a speed of 12 m/s (23 knots; 27 mph; 44 km/h). Speed measurements can be in knots (abbreviated KT) or meters per second (abbreviated MPS).
090V150 indicates the wind direction is varying from 90° true (east) to 150° true (south-southeast).
Some airports already reports their data through open weather... and wind data is taken into account.