Page 5 of 5

Re: buienradar lua script

Posted: Monday 29 June 2020 11:36
by jvdz
I have no idea what those 2 values exactly do as I am using my own LUA scripts for both the Rain prediction (posted somewhere in this thread) and a separate script for the weather predictions, where I filter out the information I need and put that in Domoticz for processing.
This is running for a couple of years already, when this integrated hardware wasn't there yet.
I have separate scripts as they point to different Buienradar websites and the rain prediction runs every 5 minutes and the weather forecast runs every 10 minutes.

Jos

Re: buienradar lua script

Posted: Monday 29 June 2020 11:44
by Joske
okay, i will take a look.
I thought it should be simple, but the wind-sensor gives an output like: 225.00;SW;79;114;15.2;15.2
So i guess the windage (in this example it was 11.4 m/s) is given as : 114.
So its not very simple i guess : )

But always nice to learn, the rain LUA word perfectly!

What i do see:
You can go to 'notifications' on the sensor. there its possible to get an notification (for example en e-mail) when the windage is above X m/s.
Thats a start :)

What i shall try:
Als 'notification' type i choose: HTTP
And the als HTTP i give the command for the screen; hopefully it works :)
Will test it now

EDIT:
Via http it doesn't work within the notification setting.
I see i am not the only one : (https://www.domoticz.com/forum/viewtopic.php?t=6828 )

On a rainy day with lots of time i will try to test other options, if anyone has an working 'windage' script thats always welcome :roll:

Re: buienradar lua script

Posted: Monday 29 June 2020 14:29
by jvdz
Joske wrote: Monday 29 June 2020 11:44 okay, i will take a look.
I thought it should be simple, but the wind-sensor gives an output like: 225.00;SW;79;114;15.2;15.2
So i guess the windage (in this example it was 11.4 m/s) is given as : 114.
So its not very simple i guess : )
I gave the formula already for that a few posts ago! ;)
jvdz wrote: Monday 29 June 2020 10:03 As you can see, there is also a Wind sensor there and you can use that data to retrieve the windspeed and windgustspeed :

Code: Select all

sDirectionDegrees, sDirection, sSpeed, sGust, sTemperature, sFeel = otherdevices_svalues['Wind']:match("([^;]+);([^;]+);([^;]+);([^;]+);([^;]+);([^;]+)")


So you can very simply test on either the variable sSpeed or sGust depending which value you find important.
Something like this to get you started?:

Code: Select all

commandArray = {}
-- only check windspeed when Wind info is updated
if devicechanged['Wind'] then
	sDirectionDegrees, sDirection, sSpeed, sGust, sTemperature, sFeel = otherdevices_svalues['Wind']:match("([^;]+);([^;]+);([^;]+);([^;]+);([^;]+);([^;]+)")
	-- check whether the sunscreen is "out"
	if (otherdevices['Sunscreen'] == 'Open') then
		-- send warning when speed is over 60km
		if sSpeed < 60 then
			print('Windsnelheid hoger dan 60 km en Sunscreen is open.')
		end
	end
end
return commandArray
Jos

Re: buienradar lua script

Posted: Monday 29 June 2020 14:33
by Joske
:roll: :oops:

Oepsie...thanks!
I will try to make it work !

Re: buienradar lua script

Posted: Monday 29 June 2020 18:29
by Joske
Managed by doing this: https://www.domoticz.com/forum/viewtopic.php?t=28163

A blockly with:

IF > WIND > 10 AND Sunscreen = Open
Do: Close Sunscreen

Now looking for the best setting; i don't know if it takes the peak ('windvlaag') or the everage windspeed (snelheid in m/s)


Edit:
It looks like its taking the peaks, thats fine.
There are peaks with 15m/sec here wright now, and when i play with the parameters i can see the screens doesn't even goes out because of the wind.

Perfect :D

Re: buienradar lua script

Posted: Friday 02 October 2020 17:16
by Jimster
Anybody else having issues with the LUA script recently?
Seems like the output from buienradar has changed.

Code: Select all

077|17:15
077|17:20
86,6329598612474|17:25
86,6329598612474|17:30
077|17:35
077|17:40
86,6329598612474|17:45
86,6329598612474|17:50
92,2678801510292|17:55
99,3670401387526|18:00
112,64618727381878|18:05
112,64618727381878|18:10
105,8988795837422|18:15
111,53379987352399|18:20
112,64618727381878|18:25
105,8988795837422|18:30
118,6329598612474|18:35
123,30905700295102|18:40
129,590485647558|18:45
131,3670401387526|18:50
131,3670401387526|18:55
118,6329598612474|19:00
112,64618727381878|19:05
105,8988795837422|19:10

Re: buienradar lua script

Posted: Friday 02 October 2020 18:05
by jvdz
yep... I changed my version to this to t=get the rain and time variables filled now :

Code: Select all

while true do
   line = file:read("*line")
   if not line then break end
   rain,linetime = line:match("(%d+).-|(.+)%s?")
   rain = tonumber(rain) or 0
   linetime = linetime or "00:00"
   linetime = linetime:gsub("[\r\n]","")
   if debug then print('Rain in timerange: '..rain .. '   linetime:'..linetime..'    Line:' ..line ) end

Jos

Re: buienradar lua script

Posted: Friday 02 October 2020 20:14
by Jimster
jvdz wrote: Friday 02 October 2020 18:05 yep... I changed my version to this to t=get the rain and time variables filled now :

Code: Select all

while true do
   line = file:read("*line")
   if not line then break end
   rain,linetime = line:match("(%d+).-|(.+)%s?")
   rain = tonumber(rain) or 0
   linetime = linetime or "00:00"
   linetime = linetime:gsub("[\r\n]","")
   if debug then print('Rain in timerange: '..rain .. '   linetime:'..linetime..'    Line:' ..line ) end

Jos
Hi Jos,

Thanks for your help. I'm not experienced in scripting, so I have no clue where to change this. Can you post your full script and if possible in Dutch?

Re: buienradar lua script

Posted: Friday 02 October 2020 20:33
by jvdz
Just Email me your script and I will have a look as it could very well be you are using a totally different version.
My email address can be found in my github account details.

Jos

ps: .. and in Dutch is fine too. ;)

Re: buienradar lua script

Posted: Saturday 03 October 2020 13:15
by Jimster
Thanks to @jvdz it's working again.

Change this line in the script from

Code: Select all

for k,v in tmp:gmatch('(.-)|(.-)\r?\n') do
to

Code: Select all

for k,v in tmp:gmatch('(%d+).-|(.-)\r?\n') do
Thanks Jos!

Re: buienradar lua script

Posted: Saturday 03 October 2020 17:08
by jvdz
You're welcome. It actually simply takes the numeric part up to the comma and skips the rest as they change the values for rain|time from:

Code: Select all

123|10:00
to

Code: Select all

123,456789|10:00
.. but we really do not need the part behind the comma (which is use in the Netherlands as Decimal sign).

The other option would to replace the comma for a decimal point in the string value before converting to a number.

Jos

Re: buienradar lua script

Posted: Tuesday 26 January 2021 18:47
by Joske
nf999 wrote: Saturday 31 March 2018 21:26
So incidentally this script will go over 10secs. If you don't want this adjust the nr of repetitions to 2, or reduce the timeout to 3 secs in os.capture. This setup was fine for me.
This worked fine for the error, thanks!!

Re: buienradar lua script

Posted: Thursday 06 January 2022 22:54
by HansieNL
jvdz wrote: Saturday 03 October 2020 17:08 The other option would to replace the comma for a decimal point in the string value before converting to a number.
Can you help me to change my lua script so it will work with the decimal point? Thanks in advance.

Code: Select all

-- script_time_buienradar.lua
-- 
-- BuienRadar Module
--
-- curl in os required!!
-- create dummy text device from dummy hardware 'Buien Radar'
-- create dummy rain sensor from dummy hardware 'BuienRadarMeter'
-- create dummy switch from dummy hardware for triggering screens etc based on rain
-- add as time based lua script
-- set your longitude & latitude below!
-- 
-- 2017-12-27 working version
-- 2017-12-28 trying to get the rain device working .. missing documentation ..
-- 2017-12-30 isolated the bug, i was overwriting the updatedevice .. should be ok now
-- 2018-03-28 updated buienradar url
-- 2018-04-02 added follow redirect for curl & fixed typo as tipped by gizmocuz 
-- 

commandArray = {}

local myBuienRadarDevice = 'Buienradar'
local myBuienRadarMeter = 'RegenData'
local myBuienRadarFlg = 'Paraplu'

-- longitude latitude
local lat='51.00'
local lon='3.00'

time = os.date("*t")
if time.min % 5 == 0 then 
    
    print('(Buienradar) Get data from Buienradar')

    function os.capture(cmd, rep)   -- execute command to get site
        -- rep is nr of repeats if result is empty
        r = rep or 1
        local s = ""
        while ( s == "" and r > 0) do
            r = r-1
            local f = assert(io.popen(cmd, 'r'))
            s = assert(f:read('*a'))
            f:close()
        end
        if ( rep - r > 1 ) then
            print("(Buienradar) os.capture needed more than 1 call: " .. rep-r)
        end
      return s
    end
 
    --  get data from buienradar
    -- local command = "curl --max-time 3 -L -s 'https://gpsgadget.buienradar.nl/data/raintext?lat=" .. lat .. "&lon=" .. lon .. "'"  -- timeout 3 secs
    local command = "curl --max-time 3 -L -s 'https://gps.buienradar.nl/getrr.php?lat=" .. lat .. "&lon=" .. lon .. "'"  -- timeout 3 secs
    -- print("command: " .. command)
    local tmp = os.capture(command, 3)  -- nr of repetitions 3
 
    -- print('buienRadar data:\n' .. tmp)
    
    if ( tmp == "" ) then
        print("(BuienRadar) Empty result from curl command")
    else
        -- analyse data 

        -- to mm/h 10^((waarde-109)/32)        
        function tomm(r)
            return 10^((r-109)/32)
        end
        
        -- to string formatted
        function tos(r, c)
            c = c or 1
            return string.format("%." .. c .. "f", r)
        end

        local c=0
        local rainNow=0
        local rainNowAvg = 0
        local rainSoon = 0
        local rainTime = ""
        local rainMax = 0
        for k,v in tmp:gmatch('(.-)|(.-)\r?\n') do  -- k is rain value, v is time
            kn = tonumber(k)
            if c<=1 then
                if rainNow < kn then
                    rainNow = kn
                end
                if kn > 0 then
                    rainNowAvg = rainNowAvg + tomm(kn)/2
                end
            end
            if c<=3 and rainSoon < kn then 
                rainSoon = kn
            end
            if rainTime == "" and kn > 0 then
                rainTime = v
            end
            if kn > rainMax then
                rainMax = kn
            end
            c = c+1
        end
        
        -- if c = 0 no data found!
        if ( c == 0 ) then
            print("(BuienRadar) Unparsable result from curl command")
        else
        
            if rainNow>0 then
                tmp = "Nu " .. tos(tomm(rainNow)) .. " mm/uur"
                if rainMax > rainNow then
                    tmp = tmp .. " tot " .. tos(tomm(rainMax)) .. " mm/uur"
                end
            elseif rainSoon>0 then
                tmp = "Verwacht " .. tos(tomm(rainSoon)) .. " mm/uur"
                if rainMax > rainSoon then
                    tmp = tmp .. " tot " .. tos(tomm(rainMax)) .. " mm/uur"
               end
            elseif rainTime ~= "" then
                tmp = "Verwacht om " .. rainTime .. " tot " .. tos(tomm(rainMax)) .. " mm/uur"
            else tmp = "Voorlopig droog"
            end
    
            -- calculate totalrainfall using rainNowAvg as average of 2 next reports
            -- print("buienRadarMeterOld: " .. otherdevices_svalues[myBuienRadarMeter])
            local rainTot = tonumber(otherdevices_svalues[myBuienRadarMeter]:match("[^;]+;([^;]+)")) + rainNowAvg/12 -- /12 to acount for 5min measurements ?? ..
            -- print("buienRadarDebug: rainNow=" .. tos(tomm(rainNow)) .. " rainNowAvg=" .. tos(rainNowAvg) .. " rainSoon=" .. 
            --      tos(tomm(rainSoon)) .. " rainTot=" .. tos(rainTot,2) .. " rainTime=" .. rainTime .. " rainMax=" .. tos(tomm(rainMax)))
            local cmd = otherdevices_idx[myBuienRadarMeter] .. "|0|" .. tos(tomm(rainNow)*100,0) .. ";" .. tos(rainTot,2)
            -- print("buienRadar: " .. cmd)
            table.insert(commandArray, { ['UpdateDevice'] = cmd } ) -- table.insert needed to avoid overwriting with next updatedevice
            
            -- write to text device
            if otherdevices[myBuienRadarDevice] ~= tmp then
                table.insert(commandArray, { ['UpdateDevice'] = otherdevices_idx[myBuienRadarDevice] .. '|0|' .. tmp })
                
                -- trigger based when rainNow or rainSoon
                if ( rainNow>0 or rainSoon>0 ) then
                    if otherdevices[myBuienRadarFlg] == "Off" then
                        commandArray[myBuienRadarFlg] = "On"
                    end
                elseif otherdevices[myBuienRadarFlg] == "On" then
                    commandArray[myBuienRadarFlg] = "Off"
                end
            end
        end -- unparsable
    end -- empty result
end

return commandArray

Re: buienradar lua script

Posted: Friday 07 January 2022 9:49
by jvdz
I see you didn't change line 80 yet as suggested as fix a couple of posts ago or you do you like to see the other solution?

Maybe this script helps to explain the issue .... just save this is a test.lua file and run this with lua from the commandline:

Code: Select all

tmp = "123,456789|10:00\r\n"

print("-- the original issue after buienradar changed the value to include decimals using a ,")
for k,v in tmp:gmatch('(.-)|(.-)\r?\n') do
	print("Data:",k,v)
	kn = tonumber(k)
	print("Result:",kn,v)
end
print("")
print("-- proposed fix in regex")
for k,v in tmp:gmatch('(%d+).-|(.-)\r?\n') do
	print("Data:",k,v)
	kn = tonumber(k)
	print("Result:",kn,v)
end
print("")
print("-- the second option replace , with .")
for k,v in tmp:gmatch('(.-)|(.-)\r?\n') do
	print("Data:",k,v)
	k = k:gsub(",",".")
	kn = tonumber(k)
	print("Result:",kn,v)
end
Jos

Re: buienradar lua script

Posted: Sunday 09 January 2022 18:11
by HansieNL
@jvdz Runned your LUA script and it makes clear what's happening with the comma.
I would like to see the other solution (replace the comma for a decimal point).
If you have time can you show that solution?
Thanks in advance.

Re: buienradar lua script

Posted: Sunday 09 January 2022 18:31
by jvdz
Mmm ... that one is in that script too ... in the last part ;)

Re: buienradar lua script

Posted: Sunday 09 January 2022 18:47
by HansieNL
jvdz wrote: Sunday 09 January 2022 18:31 Mmm ... that one is in that script too ... in the last part ;)
Ok. I hope that I understand it well :?

Code: Select all

...
for k,v in tmp:gmatch('(.-)|(.-)\r?\n') do  -- k is rain value, v is time
    k = k:gsub(",",".")  -- replace the comma for a decimal point
    kn = tonumber(k)
...

Re: buienradar lua script

Posted: Sunday 09 January 2022 18:58
by jvdz
Correct..