Is it gonna rain within the next X minutes?

In this subforum you can show projects you have made, or you are busy with. Please create your own topic.
Bishop
Posts: 21
Joined: Sunday 11 June 2017 14:06
Target OS: Raspberry Pi
Domoticz version:
Contact:

Re: Is it gonna rain within the next X minutes?

Post by Bishop » Sunday 03 September 2017 14:39

jvdz wrote:
Saturday 13 May 2017 17:10
HansieNL wrote: Interesting... are you willing to share your script?
Of course: This is the script I have and it feed both a Rain and a percentage device. In case you don't want the percentage chance of rain device you need to remove the code at the bottom of the lua which tries to update that.
The filename and crontab task are listed in the source.
Just update the information in the top part and check if you want both devices to be fed with information.

Jos

Code: Select all

-------------------------------------------------------------------
---  Get rain info from buienradar for the next xx minutes
---
--- Crontab task:  */5 * * * * sudo lua /home/pi/domoticz/scripts/buienradar_rainprediction.lua >> /var/tmp/BRP.log
-- config ---------------------------------------------------------
lat='5?.??'
lon='4.??'
-- use information for the next xx minutes
minutesinfuture=15
-- Domoticz server url
domoticzurl="http://192.168.0.??:8080"
-- rain device
DEVIDX=???
-- General Percentage device
PDEVIDX=???
-- set to true or false
debug=true
-- set to the appropriate tmp path
tempfilename = '/var/tmp/rain.tmp' -- can be anywhere writeable
-- config ---------------------------------------------------------
-- http://gadgets.buienradar.nl/data/raintext?lat=5?.??&lon=4.??
--
-- url='http://gps.buienradar.nl/getrr.php?lat='..lat..'&lon='..lon  -- old link
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 Devices
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
-- end update percentage device -------------------------------------
if debug then print('------------------------------------------------------------------------------------------------------------------------------------------') end
Hey

I've been using your script for a while now. Works perfectly.
It's currently running as a cronjob. I would like to have it added to the Domoticz Event system as a dzVents script. The main reason for this is that in that case the Domoticz automatic backup and my manual backup of the database file will include these scripts. And, as I understand, dzVents is more resource-friendly. Is this possible or..?

User avatar
jvdz
Posts: 950
Joined: Tuesday 30 December 2014 20:25
Target OS: Raspberry Pi
Domoticz version: Stable
Location: Netherlands
Contact:

Re: Is it gonna rain within the next X minutes?

Post by jvdz » Sunday 03 September 2017 20:31

I wouldn't do it as the event system is single threaded, although I am not sure how Dzvents is implemented, but it could make other tasks less responsive when at the same time the Rain information is being picked-up.
Not sure I understand the Domoticz automatic backup argument as I though that only takes care of the domoticz.db, the actual data for Domoticz?

Jos

Bishop
Posts: 21
Joined: Sunday 11 June 2017 14:06
Target OS: Raspberry Pi
Domoticz version:
Contact:

Re: Is it gonna rain within the next X minutes?

Post by Bishop » Sunday 03 September 2017 21:50

jvdz wrote:
Sunday 03 September 2017 20:31
Not sure I understand the Domoticz automatic backup argument as I though that only takes care of the domoticz.db, the actual data for Domoticz?
As far as I can tell it also makes contains your blockly, lua and dzVents you've made inside Domoticz.

User avatar
jvdz
Posts: 950
Joined: Tuesday 30 December 2014 20:25
Target OS: Raspberry Pi
Domoticz version: Stable
Location: Netherlands
Contact:

Re: Is it gonna rain within the next X minutes?

Post by jvdz » Sunday 03 September 2017 22:09

Blockly and LUA I knew when using the internal editor, so guess you also mean the DzVents script done with the internal Editor, as they are saved into the domoticz.db as well. I am using only the separate files in the LUA directory and those aren't backed up. Assume the same counts for when using DzVents scripts in the directory structure.

Jos

curious
Posts: 75
Joined: Saturday 02 April 2016 19:38
Target OS: -
Domoticz version:
Contact:

Re: Is it gonna rain within the next X minutes?

Post by curious » Thursday 12 October 2017 20:37

@jvdz I would like to use your script. However it is not very clear to me where to put the code.
It seems there are two blocks with "config"-code. Should I put all this in the crontab too, or should I put it somewhere else. ?

User avatar
jvdz
Posts: 950
Joined: Tuesday 30 December 2014 20:25
Target OS: Raspberry Pi
Domoticz version: Stable
Location: Netherlands
Contact:

Re: Is it gonna rain within the next X minutes?

Post by jvdz » Thursday 12 October 2017 21:02

@curious, the filename is shown on line 4 of the script which shows the crontab schedule and shells file: /home/pi/domoticz/scripts/buienradar_rainprediction.lua
You basically can store it anywhere you like and make the change in the crontab line.

So all you need to do is :
1. Create a file called buienradar_rainprediction.lua
2. copy & paste the code into this file
3. Edit this section at the top and fill in your information:

Code: Select all

-- config ---------------------------------------------------------
lat='5?.??'
lon='4.??'
-- use information for the next xx minutes
minutesinfuture=15
-- Domoticz server url
domoticzurl="http://192.168.0.??:8080"
-- rain device
DEVIDX=???
-- General Percentage device
PDEVIDX=???
-- set to true or false
debug=true
-- set to the appropriate tmp path
tempfilename = '/var/tmp/rain.tmp' -- can be anywhere writeable
-- config ---------------------------------------------------------
4. Save the file
5. test the file by running it: lua buienradar_rainprediction.lua
6. start crontab and add a line like this:

Code: Select all

*/5 * * * * sudo lua /home/pi/domoticz/scripts/buienradar_rainprediction.lua >> /var/tmp/BRP.log
You should be in business. Just check BRP.log for issues.

Jos

curious
Posts: 75
Joined: Saturday 02 April 2016 19:38
Target OS: -
Domoticz version:
Contact:

Re: Is it gonna rain within the next X minutes?

Post by curious » Thursday 12 October 2017 21:32

@Jos

Thanks for your quick response. Seems to work, but no rain is expected tonight :)

Post Reply

Who is online

Users browsing this forum: No registered users and 2 guests