Page 10 of 16

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

Posted: Sunday 30 April 2017 22:27
by jvdz
Ok, so did you check what it is set to? Probably All which makes it fire on each event, change that to Time to run it only one time per minute.
I've stated this before, but I wouldn't want to have these type of script in the event system, since it is single threaded and no other events will be processed during the running of this script.

Jos

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

Posted: Sunday 30 April 2017 22:30
by poudenes
Maybe i don't understand what you mean.

Here is the lua script im using:

Code: Select all

local RainTextIDX = 229   -- your domoticz text device
debug = false -- enable or disable debug output
tempfilename = '/home/osmc/domoticz/temp/Rain60.tmp' -- be sure this is in the correct map, and give it chmod 0666
totalrain = 0
rainlines = 0

function IsItGonnaRain( minutesinfuture )
   url='http://gpsgadget.buienradar.nl/data/raintext/?lat=52.33&lon=4.87' -- this is important!! change the lat and lon to your needs!!
   if debug then print(url) end
read = os.execute('curl --connect-timeout 5 -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=60
   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
Or do i rename the give event name? like regen-10 (every ten minutes ) ?

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

Posted: Sunday 30 April 2017 22:32
by jvdz
In the event editor on the right hand site of the window it shows LUA under the scriptname and under that probably ALL..... change that to Time.

Jos

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

Posted: Sunday 30 April 2017 22:36
by poudenes
jvdz wrote:In the event editor on the right hand site of the window it shows LUA under the scriptname and under that probably ALL..... change that to Time.

Jos
Thanks. Now i see it every minute. And gonna read also what the difference will be to the time/all/device/security etc... :)

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

Posted: Sunday 30 April 2017 22:40
by jvdz
That selection box just defines on which event the script is supposed to trigger. You had it so it triggers on All events, which is normally a LOT. ;)

Jos

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

Posted: Tuesday 02 May 2017 11:41
by EdwinK
Great. Now I've got it working again.

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

Posted: Tuesday 02 May 2017 18:50
by BarryT
poudenes wrote:2017-04-29 20:22:00.297 EventSystem: Script event triggered: REGEN 17.04.29
2017-04-29 20:22:00.410 EventSystem: Script event triggered: REGEN 17.04.29
2017-04-29 20:22:00.520 EventSystem: Script event triggered: REGEN 17.04.29
2017-04-29 20:22:00.674 EventSystem: Script event triggered: REGEN 17.04.29
2017-04-29 20:22:00.793 EventSystem: Script event triggered: REGEN 17.04.29
2017-04-29 20:22:00.906 EventSystem: Script event triggered: REGEN 17.04.29
2017-04-29 20:22:01.017 EventSystem: Script event triggered: REGEN 17.04.29
2017-04-29 20:22:12.113 EventSystem: Script event triggered: REGEN 17.04.29

Can someone help me with this... the script repeats many times... lots of lines like this for triggering Buitenrader to check if its gonna rain.
Did u use lua device or time?
*edit: its fixed allready :)

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

Posted: Friday 05 May 2017 8:55
by krizzz
I only now noticed that you cannot put a notification fixed to the text device. DO you guys have any ideas how to get a notification so i can run outside and take me pillows from the garden when the rain comes in :)

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

Posted: Friday 05 May 2017 9:56
by BarryT
krizzz wrote:I only now noticed that you cannot put a notification fixed to the text device. DO you guys have any ideas how to get a notification so i can run outside and take me pillows from the garden when the rain comes in :)
Then you need to edit the script.
This will do: switch a virtual device to 'on', send out a notification AND update the text idx when it will give some rain...

Change this

Code: Select all

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

Code: Select all

   if (RainPrediction > 20  ) then
      verw = 3
      RainPredictionText=('('..round(RainmmHour, 1)..' mm) regen verwacht!')
      commandArray['SendNotification']='Regenscript#Regen verwacht'
      commandArray['Regenverwacht']='On'
   else 
      verw = 2
      RainPredictionText=('Voorlopig blijft het droog!')
    --commandArray['SendNotification']='Regenscript#Geen regen meer verwacht'
      commandArray['Regenverwacht']='Off'
   end
Don't forget to add a virtual switch named: Regenverwacht

I added a sendnotification for the no rain state as optional, but look out for this.
It is sending a notification every time the switch is updating (every minute).
If you want to use it, u need to remove the -- before the commandarray

*Edit:
Ofc you could also remove the notification part in the script, and do the notification from the device itself if you wish to use that :)

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

Posted: Friday 05 May 2017 21:51
by krizzz
BarryT wrote:
krizzz wrote:I only now noticed that you cannot put a notification fixed to the text device. DO you guys have any ideas how to get a notification so i can run outside and take me pillows from the garden when the rain comes in :)
Then you need to edit the script.
This will do: switch a virtual device to 'on', send out a notification AND update the text idx when it will give some rain...

Change this

Code: Select all

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

Code: Select all

   if (RainPrediction > 20  ) then
      verw = 3
      RainPredictionText=('('..round(RainmmHour, 1)..' mm) regen verwacht!')
      commandArray['SendNotification']='Regenscript#Regen verwacht'
      commandArray['Regenverwacht']='On'
   else 
      verw = 2
      RainPredictionText=('Voorlopig blijft het droog!')
    --commandArray['SendNotification']='Regenscript#Geen regen meer verwacht'
      commandArray['Regenverwacht']='Off'
   end
Don't forget to add a virtual switch named: Regenverwacht

I added a sendnotification for the no rain state as optional, but look out for this.
It is sending a notification every time the switch is updating (every minute).
If you want to use it, u need to remove the -- before the commandarray

*Edit:
Ofc you could also remove the notification part in the script, and do the notification from the device itself if you wish to use that :)
Super clear and thanks!!!!!

Only problem is now, when adding your code. I seem to have an error now in the script. The log shows

Code: Select all

017-05-05 21:48:00.246 Error: EventSystem: in regen_verwacht: [string "local RainTextIDX = 95 -- your domoticz tex..."]:58: unexpected symbol near '='
And my script now looks like this. I actually copy pasted your example into my script.

Code: Select all

local RainTextIDX = 95   -- your domoticz text device
debug = false -- enable or disable debug output
tempfilename = '/home/pi/domoticz/temp/Rain60.tmp' -- be sure this is in the correct map, and give it chmod 0666
totalrain = 0
rainlines = 0

function IsItGonnaRain( minutesinfuture )
   url='http://gpsgadget.buienradar.nl/data/raintext/?lat=52.50&lon=4.80' -- this is important!! change the lat and lon to your needs!!
   if debug then print(url) end
read = os.execute('curl --connect-timeout 5 -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=60
   RainPrediction = IsItGonnaRain(minuten)
   RainmmHour=10^((RainPrediction-109)/32)

   if (RainPrediction > 20  ) then
      verw = 3
      RainPredictionText=('('..round(RainmmHour, 1)..' mm) regen verwacht!')
      commandArray['SendNotification']='Regenscript#Regen verwacht'
      commandArray['regenverwacht']='On'
   else 
      verw = 2
      RainPredictionText=
      --commandArray['SendNotification']='Regenscript#Geen regen meer verwacht'
      commandArray['regenverwacht']='Off'
   end
   
   commandArray[2] = {['UpdateDevice'] = RainTextIDX .. '|0|' .. tostring(RainPredictionText)}

return commandArray

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

Posted: Sunday 07 May 2017 21:22
by mlamie
I'm reading though this topic and I would like to ask if someone can help me and maybe others by updating the wiki page.
http://www.domoticz.com/wiki/Is_it_gonna_rain

I followed that page, and some things are missing like raintext device. I now only have a rain on/of switch. Help is much appreciated.

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

Posted: Monday 08 May 2017 10:39
by BarryT
krizzz wrote: Super clear and thanks!!!!!

Only problem is now, when adding your code. I seem to have an error now in the script. The log shows

Code: Select all

017-05-05 21:48:00.246 Error: EventSystem: in regen_verwacht: [string "local RainTextIDX = 95 -- your domoticz tex..."]:58: unexpected symbol near '='
And my script now looks like this. I actually copy pasted your example into my script.
Are you sure you did everything correct?
Please check UPPER and/or lowercase letters?
Please check the IDX of the text device for the rain?
I have tested the script and its working like it should do...
Did u use the script_time_regen_verwacht, and not used as a device?

Otherwise delete everything that has to do with the rain script, and start over again.
It's only 2 minutes work :)

**Edit: Please, look better next time.. You forget the line after "RainPredictionText=" :lol:

change

Code: Select all

verw = 2
      RainPredictionText=
      --commandArray['SendNotification']='Regenscript#Geen regen meer verwacht'
      commandArray['regenverwacht']='Off'
   end


into

Code: Select all

verw = 2
      RainPredictionText=('Voorlopig blijft het droog!')
      --commandArray['SendNotification']='Regenscript#Geen regen meer verwacht'
      commandArray['regenverwacht']='Off'
   end

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

Posted: Monday 08 May 2017 10:57
by BarryT
mlamie wrote:I'm reading though this topic and I would like to ask if someone can help me and maybe others by updating the wiki page.
http://www.domoticz.com/wiki/Is_it_gonna_rain

I followed that page, and some things are missing like raintext device. I now only have a rain on/of switch. Help is much appreciated.
Its not that hard.. Please check some pages back :)

Code: Select all

local RainTextIDX = 19 -- 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 = '/home/pi/domoticz/scripts/barry/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/raintext/?lat=11.11&lon=11.11' -- this is important!! change the lat and lon to your needs! dont forget to add the 2 numbers behind the point!
   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

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

Posted: Monday 08 May 2017 14:53
by mlamie
BarryT wrote:
mlamie wrote:I'm reading though this topic and I would like to ask if someone can help me and maybe others by updating the wiki page.
http://www.domoticz.com/wiki/Is_it_gonna_rain

I followed that page, and some things are missing like raintext device. I now only have a rain on/of switch. Help is much appreciated.
Its not that hard.. Please check some pages back :)
If I understand it correctly this is a additional lua script or can it be combined with the one on the wiki? As soon I'm at home I will try and update the wiki page accordingly.

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

Posted: Monday 08 May 2017 16:05
by BarryT
mlamie wrote: If I understand it correctly this is a additional lua script or can it be combined with the one on the wiki? As soon I'm at home I will try and update the wiki page accordingly.
This is an stand-alone lua script.
You can make as many scripts as you attent to use but do not try to use the same devices in 2 or more scripts.

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

Posted: Monday 08 May 2017 16:11
by BarryT
mlamie wrote:I followed that page, and some things are missing like raintext device. I now only have a rain on/of switch. Help is much appreciated.
It does not added automatically, so you need to add it manually as a "virtual" device.
First go to "hardware" and create a "dummy" group.
Now, in that dummy group you can add "virtual" devices.
Create a "text" device and give it a name.
Go to the devices page, and write down its "idx".
Use that IDX in the script as given before.

Dont forget to "accept new sensors/hardware" in settings page before making a virtual device.
Sometimes it doesnt work if you have turned it off..

G'Luck

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

Posted: Monday 08 May 2017 19:10
by mlamie
Ok, I have both scripts, the one from the wiki and the second one in the post above as advised by BarryT. When I compare both are almost identical, but some differences are there. I'm not a programmer, is someone willing to combine them optimize and advise the final version?

For now it looks like both virtual devices work, p.s. both scripts do use the same rain.tmp file.
virtual device 1 = dummy switch 'Rain expected' - I also have a notification trigger
Image
virtual device 2 = dummy text 'regen verwacht'
Image

In the domoticz log:

every 15 min (wiki script - dummy switch 'Rain expected') which is saved in /home/pi/domoticz/scripts/lua :
2017-05-08 19:00:02.588 LUA: Regen verwacht: 0 mm binnen 20 minuten.

every min (forum script - dummy text 'regen verwacht') which I saved in the EventSystem:
2017-05-08 19:00:02.861 EventSystem: Script event triggered: regenverwacht

I sometimes see the error:
2017-05-08 19:15:10.406 Error: EventSystem: Warning!, lua script /home/pi/domoticz/scripts/lua/script_time_rain.lua has been running for more than 10 seconds

Can that aso be solved somehow?

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

Posted: Monday 08 May 2017 19:59
by BarryT
mlamie wrote:I sometimes see the error:
2017-05-08 19:15:10.406 Error: EventSystem: Warning!, lua script /home/pi/domoticz/scripts/lua/script_time_rain.lua has been running for more than 10 seconds

Can that aso be solved somehow?
Good to hear that its working now mlamie :)
If you get a "has been running for more than 10 seconds" please check your network/internet..
It tells you that it hasnt recieved any data after 10 seconds.

Occasionally i get the same warning, and its not that special..
Sometimes buienradar itself has difficulties on its server-side.
The same warning you can get from Weather Underground, Open Weather Map, or any other internet services when it doesnt recieve any or wrong data from the service within 10 seconds.

The reason of the 10 seconds is simple:
If a script is executed and it doesnt get any or corrupted data, it has to break the execute after 10 sec otherwise the system will hang as it cant do a new (script) job anymore.
After that it will re-start the hole scripting process again.

You can do a ping to a host or ping your raspberry to be sure your network is okay:
go to your windows, then cmd, and then the command: "ping -t 192.x.x.x"
You can use the ipaddress of your raspberry for internal network check or buienradar.nl for internet check, and wait some minutes.
If you recieved a time-out, then your network to the raspberry isnt that good or, when checked, even your internet has difficulties.

Log of today gives me 2 errors:

Code: Select all

2017-05-08 13:18:21.564 Error: Wunderground: Error getting http data!
(Explanation: I did a firmware upgrade on the router, so internet switched off for a couple seconds)

Code: Select all

2017-05-08 17:15:12.278 Error: EventSystem: Warning!, lua script regenverwacht has been running for more than 10 seconds
(Explanation: I did a reset on my switch after i added some new devices in my network)

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

Posted: Tuesday 09 May 2017 8:40
by poudenes
Thanks for the explanation ... Had same lines as well. And we all wanna try to remove them ... Don't like red lines haha...
Funny in the script you have a line

Code: Select all

read = os.execute('curl --connect-timeout 5 -s -o '..tempfilename..' "'..url..'"')  
I think it will disconnect after 5 sec if there is a timeout. And the script tells us its running for 10sec. Logical you will think after 5 sec it will break the connection and try it again later.
BarryT wrote:
mlamie wrote:I sometimes see the error:
2017-05-08 19:15:10.406 Error: EventSystem: Warning!, lua script /home/pi/domoticz/scripts/lua/script_time_rain.lua has been running for more than 10 seconds

Can that aso be solved somehow?
Good to hear that its working now mlamie :)
If you get a "has been running for more than 10 seconds" please check your network/internet..
It tells you that it hasnt recieved any data after 10 seconds.

Occasionally i get the same warning, and its not that special..
Sometimes buienradar itself has difficulties on its server-side.
The same warning you can get from Weather Underground, Open Weather Map, or any other internet services when it doesnt recieve any or wrong data from the service within 10 seconds.

The reason of the 10 seconds is simple:
If a script is executed and it doesnt get any or corrupted data, it has to break the execute after 10 sec otherwise the system will hang as it cant do a new (script) job anymore.
After that it will re-start the hole scripting process again.

You can do a ping to a host or ping your raspberry to be sure your network is okay:
go to your windows, then cmd, and then the command: "ping -t 192.x.x.x"
You can use the ipaddress of your raspberry for internal network check or buienradar.nl for internet check, and wait some minutes.
If you recieved a time-out, then your network to the raspberry isnt that good or, when checked, even your internet has difficulties.

Log of today gives me 2 errors:

Code: Select all

2017-05-08 13:18:21.564 Error: Wunderground: Error getting http data!
(Explanation: I did a firmware upgrade on the router, so internet switched off for a couple seconds)

Code: Select all

2017-05-08 17:15:12.278 Error: EventSystem: Warning!, lua script regenverwacht has been running for more than 10 seconds
(Explanation: I did a reset on my switch after i added some new devices in my network)

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

Posted: Tuesday 09 May 2017 14:12
by mlamie
I checked the scripts used by me and they both do not have the 5 in the line as you mentioned...

Code: Select all

read = os.execute('curl -s -o '..tempfilename..' "'..url..'"')
Since I'm not a programmer is there someone willing to check, validate both scripts (wiki = switch device and this forum = text device). For now I have update the wiki page, but duplicates and inconsistencies in both scripts can be deleted I expect.

http://www.domoticz.com/wiki/Is_it_gonna_rain