buienradar lua script

Moderator: leecollings

JuanUil
Posts: 497
Joined: Friday 22 May 2015 12:21
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.11083
Location: Asten NB Nederland
Contact:

Re: buienradar lua script

Post by JuanUil »

nice :D :D
Your mind is like a parachute,
It only works when it is opened!

RPI4 several Fibaro, KaKu, Neocoolcam switches, Z-Wave, Zigbee2Mqtt, Ikea bulbs and remote, Zigbee temp nodes
Driesser
Posts: 2
Joined: Wednesday 22 August 2018 23:05
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: buienradar lua script

Post 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?
User avatar
HansieNL
Posts: 957
Joined: Monday 28 September 2015 15:13
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: buienradar lua script

Post 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:
Blah blah blah
sjocha
Posts: 4
Joined: Monday 28 January 2019 21:31
Target OS: Linux
Domoticz version:
Contact:

Re: buienradar lua script

Post 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 ?
User avatar
HansieNL
Posts: 957
Joined: Monday 28 September 2015 15:13
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: buienradar lua script

Post 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?
Blah blah blah
User avatar
jvdz
Posts: 2189
Joined: Tuesday 30 December 2014 19:25
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.107
Location: Netherlands
Contact:

Re: buienradar lua script

Post 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
New Garbage collection scripts: https://github.com/jvanderzande/GarbageCalendar
dressie
Posts: 156
Joined: Monday 25 May 2015 22:44
Target OS: Raspberry Pi / ODroid
Domoticz version: V2020.2
Location: Netherlands
Contact:

Re: buienradar lua script

Post 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

User avatar
jvdz
Posts: 2189
Joined: Tuesday 30 December 2014 19:25
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.107
Location: Netherlands
Contact:

Re: buienradar lua script

Post 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
New Garbage collection scripts: https://github.com/jvanderzande/GarbageCalendar
goblin
Posts: 31
Joined: Sunday 23 July 2017 14:55
Target OS: Raspberry Pi / ODroid
Domoticz version: 2023.1
Location: Haarlemmermeer
Contact:

Re: buienradar lua script

Post 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
Joep123
Posts: 56
Joined: Monday 26 March 2018 18:44
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Contact:

Re: buienradar lua script

Post 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
goblin
Posts: 31
Joined: Sunday 23 July 2017 14:55
Target OS: Raspberry Pi / ODroid
Domoticz version: 2023.1
Location: Haarlemmermeer
Contact:

Re: buienradar lua script

Post by goblin »

thanks, solved (i think)
now waiting for rain :D
dheuts
Posts: 65
Joined: Monday 25 March 2019 15:14
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Brunssum, Netherlands
Contact:

Re: buienradar lua script

Post 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".
Raspberry Pi 4 With Domoticz - RFXCom - Tuya Wifi LED lights - Chuango Alarm - Zwave+ - Zigbee2MQTT - Anna Thermostat - Broadlink IR, P1 - Eufy Robo Vacuum - Worx Robo Mower
User avatar
jvdz
Posts: 2189
Joined: Tuesday 30 December 2014 19:25
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.107
Location: Netherlands
Contact:

Re: buienradar lua script

Post 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
New Garbage collection scripts: https://github.com/jvanderzande/GarbageCalendar
arnaudarduino
Posts: 21
Joined: Wednesday 06 July 2016 22:31
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: buienradar lua script

Post 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.
Last edited by arnaudarduino on Saturday 11 January 2020 16:24, edited 2 times in total.
User avatar
jvdz
Posts: 2189
Joined: Tuesday 30 December 2014 19:25
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.107
Location: Netherlands
Contact:

Re: buienradar lua script

Post 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
New Garbage collection scripts: https://github.com/jvanderzande/GarbageCalendar
arnaudarduino
Posts: 21
Joined: Wednesday 06 July 2016 22:31
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: buienradar lua script

Post 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
Last edited by arnaudarduino on Saturday 11 January 2020 16:49, edited 4 times in total.
User avatar
jvdz
Posts: 2189
Joined: Tuesday 30 December 2014 19:25
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.107
Location: Netherlands
Contact:

Re: buienradar lua script

Post 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
New Garbage collection scripts: https://github.com/jvanderzande/GarbageCalendar
marktn
Posts: 233
Joined: Tuesday 21 April 2015 21:39
Target OS: Windows
Domoticz version:
Contact:

Re: buienradar lua script

Post 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?
Domoticz latest stable, Intel NUC, Proxmox, Zigate, RFLink, Kaku switches, Ikea lights, Xiaomi sensors, Honeywell smoke detectors, Yeelight, Shelly, Slave Domoticz PI 3B+, Node-Red, Espeasy.
User avatar
jvdz
Posts: 2189
Joined: Tuesday 30 December 2014 19:25
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.107
Location: Netherlands
Contact:

Re: buienradar lua script

Post by jvdz »

Same questions .... what happens when rain from the commandline with debug=true?
Jos
New Garbage collection scripts: https://github.com/jvanderzande/GarbageCalendar
arnaudarduino
Posts: 21
Joined: Wednesday 06 July 2016 22:31
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: buienradar lua script

Post by arnaudarduino »

The box didn't show up ,because the MENU weather was off.
Now it works. Of course.

Txs
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest