Page 11 of 16

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

Posted: Tuesday 09 May 2017 20:22
by josimd
How do I know if the lua script is running? Script is located in domoticz\scripts\lua

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

Posted: Tuesday 09 May 2017 20:42
by BarryT
josimd wrote:How do I know if the lua script is running? Script is located in domoticz\scripts\lua
In the log of domoticz?

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

Posted: Tuesday 09 May 2017 20:45
by BarryT
mlamie wrote: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
Good job, but you can also do it this all in 1 script..
Please look a couple messages back, its written as 1 script with 3 options:
Device on/off
Text device
Send notification

If you need help let me know :)

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

Posted: Tuesday 09 May 2017 20:46
by mlamie
josimd wrote:How do I know if the lua script is running? Script is located in domoticz\scripts\lua
If all ok, you should see these lines in the log under status tab.
Image

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

Posted: Tuesday 09 May 2017 20:50
by mlamie
BarryT wrote:
mlamie wrote: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
Good job, but you can also do it this all in 1 script..
Please look a couple messages back, its written as 1 script with 3 options:
Device on/off
Text device
Send notification

If you need help let me know :)
Hi BarryT, if you can help that is appreciated. Are you able to combine the suggested improvements in a single script then I will update the wiki. P.s. I also see that the rain.tmp file is updated each minute...

In this post 'zicht' suggest to not use a tmp file...
http://www.domoticz.com/forum/viewtopic ... 40#p128588
In this post it is mentioned not all 3 devices are working as expected.
http://www.domoticz.com/forum/viewtopic ... 60#p130239

So I'm a bit dazzled.

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

Posted: Tuesday 09 May 2017 21:06
by josimd
BarryT wrote:
josimd wrote:How do I know if the lua script is running? Script is located in domoticz\scripts\lua
In the log of domoticz?
No info found in "Instellingen / Log" also no errors....

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

Posted: Tuesday 09 May 2017 21:21
by mlamie
josimd wrote:
BarryT wrote:
josimd wrote:How do I know if the lua script is running? Script is located in domoticz\scripts\lua
In the log of domoticz?
No info found in "Instellingen / Log" also no errors....
Can you put the script in the events under Domoticz and see if that will result a status log update?

Image

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

Posted: Tuesday 09 May 2017 22:27
by krizzz
ok...@BarryT, thanks for your help. Highly apreciated. The basic script is up and running! Now I am trying to add notification in the script and that doesnt work,........YET! Again. Thanks.

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

Posted: Wednesday 10 May 2017 19:44
by josimd
mlamie wrote:
josimd wrote:
BarryT wrote: In the log of domoticz?
No info found in "Instellingen / Log" also no errors....
Can you put the script in the events under Domoticz and see if that will result a status log update?

Image
Txs...it's working now..

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

Posted: Friday 12 May 2017 11:52
by gielie
I use the following code in my rain code

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
Tise part

Code: Select all

--commandArray['SendNotification']='Regenscript#Geen regen meer verwacht'
is disabled otherwise i get an message every minute when its not raining, but, when it IS raining i do get a message every minute, is it possible to change this is only once when it start to rain or when something changes?

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

Posted: Friday 12 May 2017 21:16
by mlamie
Weird, I used both scripts as mentioned on the wiki page and don't have any issue.
http://www.domoticz.com/wiki/Is_it_gonna_rain

Did you make is a time driven script or is it a device change driven script?

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

Posted: Saturday 13 May 2017 11:22
by poudenes
Does anybody got suddenly the follow error as well:

string "local RainTextIDX = 232 -- your domoticz te..."]:26: attempt to perform arithmetic on global 'rain' (a nil value)

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

Posted: Saturday 13 May 2017 16:36
by HansieNL
poudenes wrote:Does anybody got suddenly the follow error as well:

string "local RainTextIDX = 232 -- your domoticz te..."]:26: attempt to perform arithmetic on global 'rain' (a nil value)
I got this one again "Error: EventSystem: Warning!, lua script /home/pi/domoticz/scripts/lua/script_time_rain.lua has been running for more than 10 seconds"

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

Posted: Saturday 13 May 2017 16:46
by jvdz
As stated before: you really shouldn't be running this with the event system as it is single threaded making the even system unresponsive during the run of this script.
I have a variation of the script running in the background as a CRON scheduled task and is running fine for quite a while now.

Jos

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

Posted: Saturday 13 May 2017 16:49
by HansieNL
jvdz wrote:As stated before: you really shouldn't be running this with the event system as it is single threaded making the even system unresponsive during the run of this script.
I have a variation of the script running in the background as a CRON scheduled task and is running fine for quite a while now.

Jos
Interesting... are you willing to share your script?

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

Posted: Saturday 13 May 2017 17:10
by jvdz
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

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

Posted: Saturday 13 May 2017 21:16
by poudenes
jvdz wrote:As stated before: you really shouldn't be running this with the event system as it is single threaded making the even system unresponsive during the run of this script.
I have a variation of the script running in the background as a CRON scheduled task and is running fine for quite a while now.

Jos
So its better to have a script running outside Domoticz? I have also some scripts running inside Domoticz...

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

Posted: Wednesday 17 May 2017 8:26
by poudenes
Hi All,

Moves the LUA script from inside Domoticz into the /scripts/lua/ location to see if some errors will stop. But there still there:

Code: Select all

2017-05-17 06:42:10.435 Error: EventSystem: Warning!, lua script /home/osmc/domoticz/scripts/lua/script_time_regen.lua has been running for more than 10 seconds
2017-05-17 06:45:10.346 Error: EventSystem: Warning!, lua script /home/osmc/domoticz/scripts/lua/script_time_regen.lua has been running for more than 10 seconds
2017-05-17 06:49:10.404 Error: EventSystem: Warning!, lua script /home/osmc/domoticz/scripts/lua/script_time_regen.lua has been running for more than 10 seconds
2017-05-17 06:52:10.189 Error: EventSystem: Warning!, lua script /home/osmc/domoticz/scripts/lua/script_time_regen.lua has been running for more than 10 seconds
2017-05-17 06:59:10.377 Error: EventSystem: Warning!, lua script /home/osmc/domoticz/scripts/lua/script_time_regen.lua has been running for more than 10 seconds
2017-05-17 07:05:10.435 Error: EventSystem: Warning!, lua script /home/osmc/domoticz/scripts/lua/script_time_regen.lua has been running for more than 10 seconds
2017-05-17 07:07:10.172 Error: EventSystem: Warning!, lua script /home/osmc/domoticz/scripts/lua/script_time_regen.lua has been running for more than 10 seconds
2017-05-17 07:11:10.352 Error: EventSystem: Warning!, lua script /home/osmc/domoticz/scripts/lua/script_time_regen.lua has been running for more than 10 seconds
2017-05-17 07:16:10.178 Error: EventSystem: Warning!, lua script /home/osmc/domoticz/scripts/lua/script_time_regen.lua has been running for more than 10 seconds
2017-05-17 07:31:10.258 Error: EventSystem: Warning!, lua script /home/osmc/domoticz/scripts/lua/script_time_regen.lua has been running for more than 10 seconds
2017-05-17 07:33:10.479 Error: EventSystem: Warning!, lua script /home/osmc/domoticz/scripts/lua/script_time_regen.lua has been running for more than 10 seconds
2017-05-17 07:36:10.460 Error: EventSystem: Warning!, lua script /home/osmc/domoticz/scripts/lua/script_time_regen.lua has been running for more than 10 seconds
2017-05-17 07:44:10.252 Error: EventSystem: Warning!, lua script /home/osmc/domoticz/scripts/lua/script_time_regen.lua has been running for more than 10 seconds
2017-05-17 07:46:10.482 Error: EventSystem: Warning!, lua script /home/osmc/domoticz/scripts/lua/script_time_regen.lua has been running for more than 10 seconds
2017-05-17 07:54:10.126 Error: EventSystem: Warning!, lua script /home/osmc/domoticz/scripts/lua/script_time_regen.lua has been running for more than 10 seconds
2017-05-17 08:03:10.369 Error: EventSystem: Warning!, lua script /home/osmc/domoticz/scripts/lua/script_time_regen.lua has been running for more than 10 seconds
2017-05-17 08:12:10.421 Error: EventSystem: Warning!, lua script /home/osmc/domoticz/scripts/lua/script_time_regen.lua has been running for more than 10 seconds
This is the script im using:

Code: Select all

local RainTextIDX = 233   -- your domoticz text device
local checktime = 300
debug = false -- enable or disable debug output
tempfilename = '/home/osmc/domoticz/temp/Rain120.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 3 -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=120
   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=('Het blijft droog!')
   end

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

return commandArray

My Internet connection is stable (VPN on mac is always on without disconnections) and speed is around 90-100Mbit/sec

Can someone help me or tell me why this error is still there?

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

Posted: Sunday 21 May 2017 12:27
by bertbigb
I have the feeling that you didn't understood the instructions quite well.
This script shouldn't run like a so-called time or device script.
It is a script you fire from crontab at regular times. So to provide more help I would like to know the full path and name of the script. Make sure the script is disabled in the domoticz event screen. Ad the script to your crontab and fire it. Let's see what happens.


Verzonden vanaf mijn iPhone met Tapatalk

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

Posted: Sunday 21 May 2017 16:07
by poudenes
bertbigb wrote:I have the feeling that you didn't understood the instructions quite well.
This script shouldn't run like a so-called time or device script.
It is a script you fire from crontab at regular times. So to provide more help I would like to know the full path and name of the script. Make sure the script is disabled in the domoticz event screen. Ad the script to your crontab and fire it. Let's see what happens.


Verzonden vanaf mijn iPhone met Tapatalk
Clear answer. I will add it to crontab and see what happens