Page 13 of 16

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

Posted: Sunday 03 September 2017 21:50
by Bishop
jvdz wrote: Sunday 03 September 2017 20:31Not 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.

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

Posted: Sunday 03 September 2017 22:09
by jvdz
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

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

Posted: Thursday 12 October 2017 20:37
by curious
@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. ?

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

Posted: Thursday 12 October 2017 21:02
by jvdz
@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

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

Posted: Thursday 12 October 2017 21:32
by curious
@Jos

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

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

Posted: Monday 23 October 2017 22:41
by jberinga
Quick question: Why do I get this error in the log?

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

Thanks!

JB

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

Posted: Tuesday 24 October 2017 9:43
by jvdz
You will have to show your complete script as it seems to have an issue with line 89.

Jos

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

Posted: Tuesday 24 October 2017 17:01
by jberinga
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.

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

Posted: Tuesday 24 October 2017 17:36
by jvdz
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

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

Posted: Tuesday 24 October 2017 17:48
by jberinga
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!

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

Posted: Tuesday 24 October 2017 17:51
by jvdz
You can merge them together when you want, but that means you need to modify the code so it will work as one. :-)

Jos

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

Posted: Tuesday 24 October 2017 17:54
by jberinga
Well I am not a star in modifying code :? , so lets leave it like this :D

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

Posted: Sunday 05 November 2017 18:33
by EdwinK
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?

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

Posted: Sunday 05 November 2017 19:02
by jvdz
What does the BRP.LOG tell you?
Does the same command work from the command prompt?

Jos

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

Posted: Sunday 05 November 2017 19:14
by EdwinK
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..

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

Posted: Sunday 05 November 2017 19:35
by jvdz
or maybe just install LUA ? :-)

Jos

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

Posted: Sunday 05 November 2017 19:37
by EdwinK
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

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

Posted: Thursday 16 November 2017 21:29
by quilae
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?

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

Posted: Thursday 16 November 2017 21:40
by jvdz
The content is described in the WiKi: https://www.domoticz.com/wiki/Domoticz_ ... L%27s#Rain

Jos

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

Posted: Thursday 16 November 2017 21:50
by quilae
Thanks for your quick reply. In the meantime I figured out myself it has to do with this line:

Code: Select all

url=domoticzurl..'/json.htm?type=command&param=udevice&idx=' .. DEVIDX .. '&nvalue=0&svalue=' .. calcmmh .. ';' .. calcmmh
I did take a look at your wiki reference but I don't understand what the RAINCOUNTER value as shown on the wiki is supposed to mean.