Page 2 of 5

Re: buienradar lua script

Posted: Thursday 23 August 2018 10:53
by JuanUil
nice :D :D

Re: buienradar lua script

Posted: Wednesday 29 August 2018 0:00
by Driesser
Hmm,

It rained a couple of time last weekend (also shown on the "BuienRadarMeter") but thes witch didn't flipped.

I added an the following switch, but it doesn't do anything.
Image

My Lua Script is as follow

Code: Select all

-- 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='Buien Radar'
local myBuienRadarMeter='BuienRadarMeter'
local myBuienRadarFlg='flgBuienRadar'

-- longitude latitude
local lat='51.59'
local lon='4.77'

time = os.date("*t")
if time.min % 5 == 0 then 
    
    print('BuienRadar module')

    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("os.capture needed more than 1 call: " .. rep-r)
        end
      return s
    end
 
    --  get data from buienradar
    local command = "curl --max-time 5 -L -s 'http://gpsgadget.buienradar.nl/data/raintext?lat=" .. lat .. "&lon=" .. lon .. "'"
    -- print("command: " .. command)
    local tmp = os.capture(command, 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 = "now; " .. tos(tomm(rainNow)) .. "mm/h"
                if rainMax > rainNow then
                    tmp = tmp .. " upto " .. tos(tomm(rainMax)) .. "mm/h"
                end
            elseif rainSoon>0 then
                tmp = "soon in 15mins; " .. tos(tomm(rainSoon)) .. "mm/h"
                if rainMax > rainSoon then
                    tmp = tmp .. " upto " .. tos(tomm(rainMax)) .. "mm/h"
                end
            elseif rainTime ~= "" then
                tmp = "expected @ " .. rainTime .. "; upto " .. tos(tomm(rainMax)) .. "mm/h"
            else tmp = "No rain"
            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
I thought the switch should be the same als rule 20.
Image

Can you help me?

Re: buienradar lua script

Posted: Wednesday 29 August 2018 0:16
by HansieNL
Is your switch a dummy switch as it is saying buienradar hardware and not dummy?
-- create dummy switch from dummy hardware for triggering screens etc based on rain
Edit: My mistake. I named my dummy hardware Dummy and create all dummies from there :oops:

Re: buienradar lua script

Posted: Monday 28 January 2019 21:34
by sjocha
HansieNL wrote: Thursday 05 April 2018 17:07 @nf999: Got it working :-)
hello i'm having the same error (attempt to perform arithmetic on a nil value)
could you share how you solved this problem ?

Re: buienradar lua script

Posted: Friday 19 April 2019 13:55
by HansieNL
I got several times a day errors: Error: EventSystem: Warning!, lua script /home/pi/domoticz/scripts/lua/script_time_buienradar.lua has been running for more than 10 seconds. Is there something I can do to get rid of these errors?

Re: buienradar lua script

Posted: Friday 19 April 2019 17:12
by jvdz
I am running a buienradar LUA script as well, but am not running it with the Domoticz event systems as these type of script have to go to the internet to grab the information, so will slow down the single threaded process. So I am running it with crontab and use the JSON/API interface to update the Domoticz devices to avoid these issues and potential slowness of the event system. :)

Jos

Re: buienradar lua script

Posted: Sunday 21 April 2019 9:06
by dressie
jvdz wrote:I am running a buienradar LUA script as well, but am not running it with the Domoticz event systems as these type of script have to go to the internet to grab the information, so will slow down the single threaded process. So I am running it with crontab and use the JSON/API interface to update the Domoticz devices to avoid these issues and potential slowness of the event system. :)

Jos
Interesting, could you tell me how to set that up?

Verstuurd vanaf mijn SM-G950F met Tapatalk


Re: buienradar lua script

Posted: Sunday 14 July 2019 13:31
by jvdz
dressie wrote: Sunday 21 April 2019 9:06 Interesting, could you tell me how to set that up?
Sure: this is the script I run via crontab and the info is in it. Let me know when you have questions.
Just file in the information in the Config section. This script requires 2 devices: a Rain and a Percentage device.
Scriptfile: buienradar_rainprediction.lua

Code: Select all

-------------------------------------------------------------------
---  Get rain info from buienradar for the next xx minutes
---  This is the crontab entry I have which creates the log file in /var/tmp. remember to have this cleaned regularly which I do daily.
---           Crontab task:  */5 * * * * sudo lua /home/pi/domoticz/scripts/buienradar_rainprediction.lua >> /var/tmp/BRP.log
-- Start config ---------------------------------------------------------
lat='52.??'
lon='4.??'
-- use information for the next xx minutes to calculate the percentage for prediction
minutesinfuture=15
-- Domoticz server url
domoticzurl="http://192.168.xx.xx:8080"
-- IDX dummy rain device
DEVIDX=111
-- IDX dummy General Percentage device
PDEVIDX=222
debug=true
-- can be anywhere writeable
tempfilename = '/var/tmp/rain.tmp'
-- END config ---------------------------------------------------------
url='http://gadgets.buienradar.nl/data/raintext?lat='..lat..'&lon='..lon
if debug then print(url) end
read = os.execute('curl -Lo '..tempfilename..' "'..url..'"')
file = io.open(tempfilename, "r")
totalrain=0
rainlines=0
-- now analyse the received lines, format is like 000|15:30 per line.
while true do
	line = file:read("*line")
	if not line then break end
	linetime=string.sub(tostring(line), 5, 9)
	-- Linetime2 holds the full date calculated from the time on a line
	linetime2 = os.time{year=os.date('%Y'), month=os.date('%m'), day=os.date('%d'), hour=string.sub(linetime,1,2), min=string.sub(linetime,4,5), sec=os.date('%S')}
	difference = os.difftime (linetime2,os.time())

	-- When a line entry has a time in the future AND is in the given range, then totalize the rainfall
	if ((difference >= 20) and (difference<=minutesinfuture*60)) then
		rain=tonumber(string.sub(tostring(line), 0, 3))
		totalrain = totalrain+rain
		rainlines=rainlines+1
		if debug then print('Rain in timerange: '..rain .. '   Total rain now: '..totalrain .. '   difference:' .. difference .. '    Line:' ..line ) end
	end

end
file:close()

-- Returned value is average rain fall for next time
-- 0 is no rain, 255 is very heavy rain
-- When needed, mm/h is calculated by 10^((value -109)/32) (example: 77 = 0.1 mm/hour)
if rainlines == 0 then
	averagerain=0
else
	averagerain=math.ceil(totalrain/rainlines)
end

function round(num, idp)
  return tonumber(string.format("%." .. (idp or 0) .. "f", num))
end
-- Calculate the mm/h
calcmmh = round(10^((averagerain -109)/32),2)
if debug then print(os.date() .. "  averagerain:" .. averagerain .. "  calcmmh:" .. calcmmh) end

-- Update Domotics Device
url=domoticzurl..'/json.htm?type=command&param=udevice&idx=' .. DEVIDX .. '&nvalue=0&svalue=' .. calcmmh .. ';' .. calcmmh
if debug then print(url) end
read = os.execute('curl -s "'..url..'"')
if debug then print(read) end

-- Update percentage regenkans Domotics Device
result = round(averagerain*0.392156862745098,2)
url=domoticzurl..'/json.htm?type=command&param=udevice&idx=' .. PDEVIDX .. '&nvalue=0&svalue=' .. result
if debug then print(url) end
read = os.execute('curl -s "'..url..'"')
if debug then print(read) end
if debug then print('==> Rain chance: '..result .. '%') end
if debug then print('------------------------------------------------------------------------------------------------------------------------------------------') end
Jos

Re: buienradar lua script

Posted: Monday 15 July 2019 23:09
by goblin
it is partly working.
i get an error in my log file "
2019-07-15 22:55:02.046 Status: Warning: Expecting svalue with at least 2 elements separated by semicolon, 1 elements received ("0"), notification not sent (Hardware: 8 - regen_percentage, ID: 82006, Unit: 1, Type: 55 - Rain, SubType: 3 - TFA)"

any idea to solve this.
i have two dummies (copied from devices)
6 regen_percentage 14056 1 regen percentage Rain TFA 0, 0
3 regen_buienalarm 14053 1 regen_buienalarm Rain TFA 0;0

the only thing i see is the difference at the end (data)
i have checked for typos but all is copy/paste from above.

thanks

Re: buienradar lua script

Posted: Tuesday 16 July 2019 7:23
by Joep123
goblin wrote: Monday 15 July 2019 23:09 any idea to solve this.
i have two dummies (copied from devices)
6 regen_percentage 14056 1 regen percentage Rain TFA 0, 0
3 regen_buienalarm 14053 1 regen_buienalarm Rain TFA 0;0
You created 2 rain devices but you need 1 rain device and 1 percentage device:

338 Dummy Hardware 00082338 1 regen_percentage General Percentage 0%
139 Dummy Hardware 140DB 1 regen_buienalarm Rain TFA 0;0

Re: buienradar lua script

Posted: Tuesday 16 July 2019 21:26
by goblin
thanks, solved (i think)
now waiting for rain :D

Re: buienradar lua script

Posted: Monday 12 August 2019 13:20
by dheuts
Thanks a lot for the nice script!

I want to edit the script so it predicts if there will be rain in the next hours instead of minutes.
How to edit the script?

I changed the minutes to 600, to know if there will be rain in the next 10 hours....
But it's not working, the User Variable stays: "0".

Re: buienradar lua script

Posted: Monday 12 August 2019 14:28
by jvdz
When you open the downloaded file from buienradar you would see that the provided information is limited to something like this: (file date 14:25)

Code: Select all

000|14:30
000|14:35
000|14:40
000|14:45
113|14:50
131|14:55
132|15:00
132|15:05
133|15:10
134|15:15
135|15:20
131|15:25
092|15:30
092|15:35
000|15:40
077|15:45
092|15:50
092|15:55
099|16:00
077|16:05
000|16:10
000|16:15
000|16:20
000|16:25
So that means you can only predict for the next 2 hours.

Jos

Re: buienradar lua script

Posted: Saturday 11 January 2020 15:02
by arnaudarduino
Hello Jos, and all other

The "Afvalkalender"i till working. Great Txs.

Now i like to put buienradar to Domoticz
I'v made two devices from the dummy does nothing switch 1 Rain ( TFA) sensor and 1 Percetage (General) sensor.
Uploaded the "buienradar_rainprediction.lua"script .(latest version with crontab)

But ,the box with percentage shows up (74)
the box with the ammount of rain is not showing up (75).
When i make another box (76) that one shows up too.

Re: buienradar lua script

Posted: Saturday 11 January 2020 15:12
by jvdz
I assume you use the last posted version which is ran by the crontab?
Have you tried running it from the commandline first , with debug=true, to see what it does?

Jos

Re: buienradar lua script

Posted: Saturday 11 January 2020 16:12
by arnaudarduino
When i run this from the command line:
~/domoticz/scripts/lua $ sudo lua buienradar_rainprediction.lua
http://gadgets.buienradar.nl/data/raint ... 2&lon=5.47
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0
100 264 100 264 0 0 2486 0 --:--:-- --:--:-- --:--:-- 2486
Rain in timerange: 0 Total rain now: 0 difference:60 Line:000|16:05
Rain in timerange: 0 Total rain now: 0 difference:360 Line:000|16:10
Rain in timerange: 0 Total rain now: 0 difference:660 Line:000|16:15
Sat Jan 11 16:04:21 2020 averagerain:0 calcmmh:0
http://192.168.2.18:8080/json.htm?type= ... svalue=0;0
{
"status" : "OK",
"title" : "Update Device"
}
0
http://192.168.2.18:8080/json.htm?type= ... 0&svalue=0
{
"status" : "OK",
"title" : "Update Device"
}
0
==> Rain chance: 0%
Look there is no box idx 75
https://ibb.co/G33T3M5

Re: buienradar lua script

Posted: Saturday 11 January 2020 16:29
by jvdz
So that looks good i would say. The amount of rainfall builds up as it is expected, so makes sense it's zero at this moment. Just let it run and see what happens when rain is expected.
Jos

Re: buienradar lua script

Posted: Saturday 11 January 2020 17:41
by marktn
Nice script!

I have only a error, EventSystem: in buienradar: [string "-- BuienRadar Module..."]:121: attempt to perform arithmetic on a nil value

Any idea?

Re: buienradar lua script

Posted: Saturday 11 January 2020 17:44
by jvdz
Same questions .... what happens when rain from the commandline with debug=true?
Jos

Re: buienradar lua script

Posted: Saturday 11 January 2020 22:39
by arnaudarduino
The box didn't show up ,because the MENU weather was off.
Now it works. Of course.

Txs