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: 1068
Joined: Tuesday 30 December 2014 20:25
Target OS: Raspberry Pi
Domoticz version: Stable
Location: Westland/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: 1068
Joined: Tuesday 30 December 2014 20:25
Target OS: Raspberry Pi
Domoticz version: Stable
Location: Westland/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: 109
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: 1068
Joined: Tuesday 30 December 2014 20:25
Target OS: Raspberry Pi
Domoticz version: Stable
Location: Westland/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: 109
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 :)

jberinga
Posts: 13
Joined: Tuesday 11 August 2015 14:20
Target OS: NAS (Synology & others)
Domoticz version: 2.2903
Location: The Netherlands
Contact:

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

Post by jberinga » Monday 23 October 2017 22:41

Quick question: Why do I get this error in the log?

Error: EventSystem: in script_time_rain: [string "---------------------------------------------..."]:89: expected near 'local'

Thanks!

JB

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

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

Post by jvdz » Tuesday 24 October 2017 9:43

You will have to show your complete script as it seems to have an issue with line 89.

Jos

jberinga
Posts: 13
Joined: Tuesday 11 August 2015 14:20
Target OS: NAS (Synology & others)
Domoticz version: 2.2903
Location: The Netherlands
Contact:

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

Post by jberinga » Tuesday 24 October 2017 17:01

Sorry Jos

Here it is:
Spoiler: show
----------------------------------------------------------------------------------------------------------------
-- IsItGonnaRain( int minutesinfuture)
-- returns: int avarage rainfall for the next minutesinfuture
--
-- Function to get rain prediction from Buienradar.nl (dutch)
--
-- http://gratisweerdata.buienradar.nl/#Buienradar You may use their free service for non-commercial purposes.
--
-- Written in LUA by Hans van der Heijden (h4nsie @ gmail.com)
-- Spring 2015
-- 14-11-2016 v0.4 changed lat/lon to max 2 decmals after comma
-- 28-03-2015 v0.3 bug: quotes around url added.
-- 27-03-2015 v0.2 return value is now average for next time
-- 26-03-2015 v0.1 Initial release
-- todo: some error checking on http and file handling (tmp file)
----------------------------------------------------------------------------------------------------------------
function IsItGonnaRain( minutesinfuture )
-- config ---------------------------------------------------------
lat='53.21' -- example lat/lon for
lon='5.27' -- NOTE: lat and lon should have max 2 decimals after comma to function (requirement Buienrader)
debug=false
tempfilename = '/var/tmp/rain.tmp' -- can be anywhere writeable
-- config ---------------------------------------------------------

-- url='http://gps.buienradar.nl/getrr.php?lat= ... lon='..lon
-- isn't working sice 2016/08/29: url is changed in:
url='http://gadgets.buienradar.nl/data/raint ... lon='..lon
if debug then print(url) end
read = os.execute('curl -s -o '..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
if debug then print('Line:'..line) end
linetime=string.sub(tostring(line), 5, 9)
if debug then print('Linetime: '..linetime) end

-- 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 > 0) and (difference<=minutesinfuture*60)) then
if debug then print('Line in time range found') end
rain=tonumber(string.sub(tostring(line), 0, 3))
totalrain = totalrain+rain
rainlines=rainlines+1
if debug then print('Rain in timerange: '..rain) end
if debug then print('Total rain now: '..totalrain) 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)
averagerain=totalrain/rainlines

return(averagerain)
end

commandArray = {}

time = os.date("*t")
if ((time.min % 15)==0) then

----- REGEN ------
minuten=20
regen = IsItGonnaRain(minuten)
print('Regen verwacht: '..regen..' mm binnen '..minuten..' minuten.')

if regen > 70 and otherdevices['Rain Expected']=='Off' then
commandArray['SendNotification']='Regenscript#Regen verwacht'
commandArray['Rain Expected']='On'
end
if regen == 0 and otherdevices['Rain Expected']=='On' then
commandArray['SendNotification']='Regenscript#Geen regen meer verwacht'
commandArray['Rain Expected']='Off'
end

end

return commandArray

local RainTextIDX = 121 -- your domoticz virtual TEXT device (add virtual text device first, if you didnt do this allready and use its idx)
debug = false -- enable or disable debug output
tempfilename = '/var/tmp/rain.tmp' -- be sure this is in the correct map, and give it chmod 0666
totalrain = 0
rainlines = 0

function IsItGonnaRain( minutesinfuture )
url='http://gadgets.buienradar.nl/data/raint ... 1&lon=5.27' -- NOTE: lat and lon should have max 2 decimals after comma to function (requirement Buienrader)
if debug then print(url) end
read = os.execute('curl -s -o '..tempfilename..' "'..url..'"')
file = io.open(tempfilename, "r")

while true do
line = file:read("*line")
if not line then break end
if debug then print('Line:'..line) end
linetime=string.sub(tostring(line), 5, 9)
if debug then print('Linetime: '..linetime) end

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())

if ((difference > 0) and (difference<=minutesinfuture*60)) then
if debug then print('Line in time range found') end
rain=tonumber(string.sub(tostring(line), 0, 3))
totalrain = totalrain+rain
rainlines=rainlines+1
if debug then print('Rain in timerange: '..rain) end
if debug then print('Total rain now: '..totalrain) end
end

end
file:close()

averagerain=totalrain/rainlines
return(averagerain)
end

function round(num, idp)
local mult = 10^(idp or 0)
return math.floor(num * mult + 0.5) / mult
end

commandArray = {}
minuten=30
RainPrediction = IsItGonnaRain(minuten)
RainmmHour=10^((RainPrediction-109)/32)

if (RainPrediction > 20 ) then
verw = 3
RainPredictionText=('('..round(RainmmHour, 1)..' mm) regen verwacht!')
else
verw = 2
RainPredictionText=('Voorlopig blijft het droog!')
end

commandArray[2] = {['UpdateDevice'] = RainTextIDX .. '|0|' .. tostring(RainPredictionText)}

return commandArray
I saved the script in the Domoticz Event system and set it as a LUA time script.

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

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

Post by jvdz » Tuesday 24 October 2017 17:36

Looks like you have copied multiple scripts together and have an extra "return commandArray" on line 87. It also contains multiple commandArray = {} statements. Just group your Functions together at the top and put your logic together within the If as assume you want to do this only one time each 15 minutes?
EDIT: The script also contains 2 times the same function IsItGonnaRain(minutesinfuture) with different content!

Jos

jberinga
Posts: 13
Joined: Tuesday 11 August 2015 14:20
Target OS: NAS (Synology & others)
Domoticz version: 2.2903
Location: The Netherlands
Contact:

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

Post by jberinga » Tuesday 24 October 2017 17:48

After reading the wiki (http://www.domoticz.com/wiki/Is_it_gonna_rain) I thought that both scripts should be in one LUA script....

Now I have two scripts and it seems to work!

Thanks Jos!

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

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

Post by jvdz » Tuesday 24 October 2017 17:51

You can merge them together when you want, but that means you need to modify the code so it will work as one. :-)

Jos

jberinga
Posts: 13
Joined: Tuesday 11 August 2015 14:20
Target OS: NAS (Synology & others)
Domoticz version: 2.2903
Location: The Netherlands
Contact:

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

Post by jberinga » Tuesday 24 October 2017 17:54

Well I am not a star in modifying code :? , so lets leave it like this :D

User avatar
EdwinK
Posts: 920
Joined: Sunday 22 January 2017 22:46
Target OS: Raspberry Pi
Domoticz version: BETA
Location: Rhoon
Contact:

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

Post by EdwinK » Sunday 05 November 2017 19:33

Now I have been working again on this, and was going to use your script, Jos, but
6. start crontab and add a line like this:

*/5 * * * * sudo lua /home/pi/domoticz/scripts/buienradar_rainprediction.lua >> /var/tmp/BRP.log
Doesn't seem to work on Synology, And somehow the other scripts, won't work either :(

Any clue's?
Running latest BETA on a Pi-3 | Toon Thermostat | RFxcom | Dashticz V2 |

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

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

Post by jvdz » Sunday 05 November 2017 20:02

What does the BRP.LOG tell you?
Does the same command work from the command prompt?

Jos

User avatar
EdwinK
Posts: 920
Joined: Sunday 22 January 2017 22:46
Target OS: Raspberry Pi
Domoticz version: BETA
Location: Rhoon
Contact:

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

Post by EdwinK » Sunday 05 November 2017 20:14

Doesn't tell anything. Doesn't run at all.
@DiskStation:/var/tmp# sudo lua /var/scripts/buienradar_rainprediction.lua >> /var/tmp/BRP.log
sudo: lua: command not found
It's clear, can't run lua from Synology terminal. Time to get another Pi..
Running latest BETA on a Pi-3 | Toon Thermostat | RFxcom | Dashticz V2 |

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

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

Post by jvdz » Sunday 05 November 2017 20:35

or maybe just install LUA ? :-)

Jos

User avatar
EdwinK
Posts: 920
Joined: Sunday 22 January 2017 22:46
Target OS: Raspberry Pi
Domoticz version: BETA
Location: Rhoon
Contact:

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

Post by EdwinK » Sunday 05 November 2017 20:37

Not sure if that is possible ;)

Edit: Guess it can ;) Just finished the downloading/installing of it. Now back to the movie

Btw. This is in the log file
http://gadgets.buienradar.nl/data/raint ... 6&lon=4.41
Rain in timerange: 0 Total rain now: 0 difference:180 Line:000|20:50
Rain in timerange: 0 Total rain now: 0 difference:480 Line:000|20:55
Rain in timerange: 57 Total rain now: 57 difference:780 Line:057|21:00
Sun Nov 5 20:47:20 2017 averagerain:19 calcmmh:0
http://192.168.0.10:8084/json.htm?type= ... svalue=0;0
1792
http://192.168.0.10:8084/json.htm?type= ... value=7.45
1792
==> Rain chance: 7.45%
Still not running from crontab
*/5 * * * * root sudo lua /var/scripts/buienradar_rainprediction.lua >> /var/tmp//BRP.log
Running latest BETA on a Pi-3 | Toon Thermostat | RFxcom | Dashticz V2 |

quilae
Posts: 6
Joined: Tuesday 07 November 2017 22:57
Target OS: Raspberry Pi
Domoticz version:
Contact:

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

Post by quilae » Thursday 16 November 2017 22:29

Using the script as (for example) shown in this post: viewtopic.php?f=38&t=5903&p=155037&hili ... in#p148937 the 'DEVIDX' sensor is always filled with two numbers separated by a ';' and the two numbers are always equal. What's the purpose of that?

Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest