Re: Is it gonna rain within the next X minutes?
Posted: Tuesday 09 May 2017 20:22
How do I know if the lua script is running? Script is located in domoticz\scripts\lua
Open source Home Automation System
https://forum.domoticz.com/
In the log of domoticz?josimd wrote:How do I know if the lua script is running? Script is located in domoticz\scripts\lua
Good job, but you can also do it this all in 1 script..mlamie wrote:I checked the scripts used by me and they both do not have the 5 in the line as you mentioned...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.Code: Select all
read = os.execute('curl -s -o '..tempfilename..' "'..url..'"')
http://www.domoticz.com/wiki/Is_it_gonna_rain
If all ok, you should see these lines in the log under status tab.josimd wrote:How do I know if the lua script is running? Script is located in domoticz\scripts\lua
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...BarryT wrote:Good job, but you can also do it this all in 1 script..mlamie wrote:I checked the scripts used by me and they both do not have the 5 in the line as you mentioned...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.Code: Select all
read = os.execute('curl -s -o '..tempfilename..' "'..url..'"')
http://www.domoticz.com/wiki/Is_it_gonna_rain
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
No info found in "Instellingen / Log" also no errors....BarryT wrote:In the log of domoticz?josimd wrote:How do I know if the lua script is running? Script is located in domoticz\scripts\lua
Can you put the script in the events under Domoticz and see if that will result a status log update?josimd wrote:No info found in "Instellingen / Log" also no errors....BarryT wrote:In the log of domoticz?josimd wrote:How do I know if the lua script is running? Script is located in domoticz\scripts\lua
Txs...it's working now..mlamie wrote:Can you put the script in the events under Domoticz and see if that will result a status log update?josimd wrote:No info found in "Instellingen / Log" also no errors....BarryT wrote: In the log of domoticz?
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
Code: Select all
--commandArray['SendNotification']='Regenscript#Geen regen meer verwacht'
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"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)
Interesting... are you willing to share your script?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
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.HansieNL wrote: Interesting... are you willing to share your script?
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¶m=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¶m=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
So its better to have a script running outside Domoticz? I have also some scripts running inside Domoticz...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
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
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
Clear answer. I will add it to crontab and see what happensbertbigb 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