Page 3 of 6

Re: buienradar lua script

Posted: Saturday 01 February 2020 9:16
by bvansteyn
No I have the afvalwijzer working..time for something new!

@Jos; I your script you tell to regularly clean the tmp file. Is this because it's growing al the time? Is is possible to clean this file automatically and if so..how can this be done?

Re: buienradar lua script

Posted: Saturday 01 February 2020 13:54
by jvdz
bvansteyn wrote: Saturday 01 February 2020 9:16 @Jos; I your script you tell to regularly clean the tmp file. Is this because it's growing al the time? Is is possible to clean this file automatically and if so..how can this be done?
I write the information to a file in ram disk, which I cleanup in a daily script I run for things like Backup and cleanup logs.
These are the crontab entry:

Code: Select all

# Run buienradar script
*/5 * * * * sudo lua /home/pi/domoticz/scripts/buienradar_rainprediction.lua >> /var/tmp/BRP.log

# Backup the Domoticz database and Scripts daily at 6PM
0 18 * * * sudo /home/pi/domoticz/scripts/domoticz_backup.sh
... and these lines are in my daily scheduled script domoticz_backup.sh:

Code: Select all

#Clean up some logs
rm /var/tmp/BRP-prev.log
mv /var/tmp/BRP.log  /var/tmp/BRP-prev.log

which keep the previous day log and today's log.
Jos

Re: buienradar lua script

Posted: Saturday 01 February 2020 14:41
by bvansteyn
jvdz wrote: Saturday 01 February 2020 13:54
bvansteyn wrote: Saturday 01 February 2020 9:16 @Jos; I your script you tell to regularly clean the tmp file. Is this because it's growing al the time? Is is possible to clean this file automatically and if so..how can this be done?
I write the information to a file in ram disk, which I cleanup in a daily script I run for things like Backup and cleanup logs.
These are the crontab entry:

Code: Select all

# Run buienradar script
*/5 * * * * sudo lua /home/pi/domoticz/scripts/buienradar_rainprediction.lua >> /var/tmp/BRP.log

# Backup the Domoticz database and Scripts daily at 6PM
0 18 * * * sudo /home/pi/domoticz/scripts/domoticz_backup.sh
... and these lines are in my daily scheduled script domoticz_backup.sh:

Code: Select all

#Clean up some logs
rm /var/tmp/BRP-prev.log
mv /var/tmp/BRP.log  /var/tmp/BRP-prev.log

which keep the previous day log and today's log.
Jos
Great! Thnx, Jos!

Re: buienradar lua script

Posted: Tuesday 28 April 2020 11:16
by Jimster
Anybody else having issues with the buienradar data?
When I open the example link (https://gpsgadget.buienradar.nl/data/ra ... t=51&lon=3) in my browser, I can see the data. When I change the lat&lon I don't get data https://gpsgadget.buienradar.nl/data/ra ... 9&lon=5.11)

I can see this in the Domoticz logs:
2020-04-28 10:50:00.287 Status: LUA: (Buienradar) Get data from Buienradar
2020-04-28 10:50:00.558 Status: LUA: (Buienradar) os.capture needed more than 1 call: 3
2020-04-28 10:50:00.558 Status: LUA: (BuienRadar) Empty result from curl command

Edit: seems to work again.

Re: buienradar lua script

Posted: Friday 19 June 2020 20:16
by Joske
Hello, a question about this LUA;

I did all the requested things, but dont understand one thing:

"...-- create dummy switch from dummy hardware for triggering screens etc based on rain...."

I made an swich, i can see it at the dashboard (its called: 'Regenverwachting'). As an action trigger it gives an ON command to the IDX of the sunscreen(z-wave) switch.
(manually it works).

But what then?
Where do i have to mention this switch?
Is that the ''flgBuienRadar' thats mentioned?
The other (virtual) switch AND text file are present.

Thanks

Re: buienradar lua script

Posted: Friday 19 June 2020 21:12
by HansieNL
Joske wrote: Friday 19 June 2020 20:16 Hello, a question about this LUA;

I did all the requested things, but dont understand one thing:

"...-- create dummy switch from dummy hardware for triggering screens etc based on rain...."

I made an swich, i can see it at the dashboard (its called: 'Regenverwachting'). As an action trigger it gives an ON command to the IDX of the sunscreen(z-wave) switch.
(manually it works).

But what then?
Where do i have to mention this switch?
Is that the ''flgBuienRadar' thats mentioned?
The other (virtual) switch AND text file are present.

Thanks
The flgBuienradar switch will switch to on if rain is expected. You then can use the on status in a script.

Re: buienradar lua script

Posted: Friday 19 June 2020 21:46
by Joske
Thanks for the quik response.

But do i have to change a value in the script?
So the name of the switch 'Regenverwachting' or do i have to rename this switch to 'flgBuienRadar' ?

Under devices i now have:

BuienRadarMeter Rain TFA
Buien Radar General Text

AND a switch with the name 'Regenverwachting' with the action ON:
http://192.168.1.154:8080/json.htm?type ... itchcmd=On

idx127 = the switch that turns on my suncreen

Re: buienradar lua script

Posted: Saturday 20 June 2020 7:56
by Joske
EDIT:

I changed one thing after reading this topic for the 5th time :)

I think i had to make an switch called ''flgBuienRadar' , as an ON action i set tthe earlier mentioned command to set the sunscreen ON.
(so removed my own switch 'Regenverwachting".
Does it make an difference what kind of switch 'flgBuienRadar' is?
Now its an regular ON/OFF switch, when ON : turns ON suncreen (sunscreen goes in).

So the situation now:

-I dit the next things:

-- curl in os required!! CHECK
-- create dummy text device from dummy hardware 'Buien Radar' CHECK
-- create dummy rain sensor from dummy hardware 'BuienRadarMeter' CHECK
-- create dummy switch from dummy hardware for triggering screens etc based on rain ( NAMED: 'flgBuienRadar' = CHECK
-- add as time based lua script CHECK
-- set your longitude & lattitude below! CHECK

Thats correct?

When i go to the 'Buien radar.txt' is only see 2 logs (from yesterday):

2020-06-19 19:55:32 No rain Admin
2020-06-19 19:54:48 Hello World

Under DEVICES i see the 'last seen' was also yesterday, the moment i created the switches:

144 Virtueel 140E0 1 BuienRadarMeter Rain TFA 0;0.00 - - 2020-06-20 08:20:04
147 Virtueel 000140E3 1 flgBuienRadar Light/Switch Switch Off - - 2020-06-20 07:42:05


In the log of Domoticz is see every 5 min activity with this content:

"]2020-06-20 16:20:00.429 Status: LUA: BuienRadar module"...

Is this also correct? Or do i have to see much more 'NO RAIN' because its dry?

Thanks for helping!


-------

Re: buienradar lua script

Posted: Monday 22 June 2020 18:47
by Joske
Can anyone advise me?

Re: buienradar lua script

Posted: Tuesday 23 June 2020 14:20
by jvdz
How is this script ran? Did you put in a File or in the internal Editor?

Jos

Re: buienradar lua script

Posted: Tuesday 23 June 2020 14:25
by Joske
Hello.
Made an new Blockly and copy - pasted the script.

Re: buienradar lua script

Posted: Tuesday 23 June 2020 14:55
by jvdz
Blockly ? I though you were using a LUA script posted earlier...no?

Re: buienradar lua script

Posted: Tuesday 23 June 2020 19:28
by Joske
Sorry!!!!! I meant LUA, exactly like it was told.

I only didn.t understand that one thing;

" i created a dummy switch from dummy hardware for triggering screens etc based on rain ( NAMED: 'flgBuienRadar')...

Re: buienradar lua script

Posted: Friday 26 June 2020 20:57
by Joske
To be more specific; this is the script:

------
-- BuienRadar Module
--
-- curl in os required!!
-- create dummy text device from dummy hardware 'Buien Radar'
-- create dummy rain sensor from dummy hardware 'BuienRadarMeter'
-- create dummy switch from dummy hardware for triggering screens etc based on rain
-- add as time based lua script
-- set your longitude & latitude below!

-- 2017-12-27 working version
-- 2017-12-28 trying to get the rain device working .. missing documentation ..
-- 2017-12-30 isolated the bug, i was overwriting the updatedevice .. should be ok now
-- 2018-03-28 updated buienradar url
-- 2018-04-02 added follow redirect for curl & fixed typo as tipped by gizmocuz

commandArray = {}

local myBuienRadarDevice='Buien Radar'
local myBuienRadarMeter='BuienRadarMeter'
local myBuienRadarFlg='flgBuienRadar'

-- longitude latitude
local lat='52.01'
local lon='5.04'

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

print('BuienRadar module')

function os.capture(cmd, rep) -- execute command to get site
-- rep is nr of repeats if result is empty
r = rep or 1
local s = ""
while ( s == "" and r > 0) do
r = r-1
local f = assert(io.popen(cmd, 'r'))
s = assert(f:read('*a'))
f:close()
end
if ( rep - r > 1 ) then
print("os.capture needed more than 1 call: " .. rep-r)
end
return s
end

-- get data from buienradar
local command = "curl --max-time 5 -L -s 'http://gpsgadget.buienradar.nl/data/raintext?lat=" .. lat .. "&lon=" .. lon .. "'"
-- print("command: " .. command)
local tmp = os.capture(command, 3)

-- print('buienRadar data:\n' .. tmp)

if ( tmp == "" ) then
print("buienRadar: Empty result from curl command")
else
-- analyse data

-- to mm/h 10^((waarde-109)/32)
function tomm(r)
return 10^((r-109)/32)
end

-- to string formatted
function tos(r, c)
c = c or 1
return string.format("%." .. c .. "f", r)
end

local c=0
local rainNow=0
local rainNowAvg = 0
local rainSoon = 0
local rainTime = ""
local rainMax = 0
for k,v in tmp:gmatch('(.-)|(.-)\r?\n') do
-- k is rain value, v is time
kn = tonumber(k)
if c<=1 then
if rainNow < kn then
rainNow = kn
end
if kn > 0 then
rainNowAvg = rainNowAvg + tomm(kn)/2
end
end
if c<=3 and rainSoon < kn then
rainSoon = kn
end
if rainTime == "" and kn > 0 then
rainTime = v
end
if kn > rainMax then
rainMax = kn
end
c = c+1
end

-- if c = 0 no data found!
if ( c == 0 ) then
print("buienRadar: Unparsable result from curl command")
else

if rainNow>0 then
tmp = "now; " .. tos(tomm(rainNow)) .. "mm/h"
if rainMax > rainNow then
tmp = tmp .. " upto " .. tos(tomm(rainMax)) .. "mm/h"
end
elseif rainSoon>0 then
tmp = "soon in 15mins; " .. tos(tomm(rainSoon)) .. "mm/h"
if rainMax > rainSoon then
tmp = tmp .. " upto " .. tos(tomm(rainMax)) .. "mm/h"
end
elseif rainTime ~= "" then
tmp = "expected @ " .. rainTime .. "; upto " .. tos(tomm(rainMax)) .. "mm/h"
else tmp = "No rain"
end

-- calculate totalrainfall using rainNowAvg as average of 2 next reports
-- print("buienRadarMeterOld: " .. otherdevices_svalues[myBuienRadarMeter])
local rainTot = tonumber(otherdevices_svalues[myBuienRadarMeter]:match("[^;]+;([^;]+)")) + rainNowAvg/12 -- /12 to acount for 5min measurements ?? ..
-- print("buienRadarDebug: rainNow=" .. tos(tomm(rainNow)) .. " rainNowAvg=" .. tos(rainNowAvg) .. " rainSoon=" ..
-- tos(tomm(rainSoon)) .. " rainTot=" .. tos(rainTot,2) .. " rainTime=" .. rainTime .. " rainMax=" .. tos(tomm(rainMax)))
local cmd = otherdevices_idx[myBuienRadarMeter] .. "|0|" .. tos(tomm(rainNow)*100,0) .. ";" .. tos(rainTot,2)
-- print("buienRadar: " .. cmd)
table.insert(commandArray, { ['UpdateDevice'] = cmd } ) -- table.insert needed to avoid overwriting with next updatedevice

-- write to text device
if otherdevices[myBuienRadarDevice] ~= tmp then
table.insert(commandArray, { ['UpdateDevice'] = otherdevices_idx[myBuienRadarDevice] .. '|0|' .. tmp })

-- trigger based when rainNow or rainSoon
if ( rainNow>0 or rainSoon>0 ) then
if otherdevices[myBuienRadarFlg] == "Off" then
commandArray[myBuienRadarFlg] = "On"
end
elseif otherdevices[myBuienRadarFlg] == "On" then
commandArray[myBuienRadarFlg] = "Off"
end
end
end -- unparsable
end -- empty result
end

return commandArray

------


The problem is that when i open the LOG from 'Buien Radar' i see that the last time that there has been an check was almost an week ago.

The last lines are:

2020-06-22 03:45:00 No rain timer
2020-06-22 03:35:01 expected @ 04:50; upto 0.2mm/h timer
2020-06-21 18:00:00 No rain EventSystem/Buienradar
2020-06-21 17:55:01 expected @ 18:55; upto 0.1mm/h EventSystem/Buienradar

Re: buienradar lua script

Posted: Friday 26 June 2020 21:11
by jvdz
I still don't understand how the script is ran. Is this done with the internal editor and if so which type of event?
Jos

Re: buienradar lua script

Posted: Friday 26 June 2020 21:16
by Joske
Hi Jos, Jos here ;-)
Within Domoticz i made an Lua script and copy-pasted the script (made the changes like was told, like the right latitude/longtitude).

When you save the script you can turn it on.

So i thought that that would do the trick

Re: buienradar lua script

Posted: Friday 26 June 2020 22:02
by jvdz
... but how did you turn it on, because else it would do something every 5 minutes? :)
What does the page say when you display it?

Re: buienradar lua script

Posted: Friday 26 June 2020 22:16
by Joske
After making the script you can choose for ON.

I don.t understand what you mean with 'wat does the page say?'.

When i go to sensors is see the Buien Radar, thats an .txt file.
There i can click on 'log' and see old requests like i posted

Re: buienradar lua script

Posted: Friday 26 June 2020 22:46
by jvdz
Isn't there also a choice for type of event?

Re: buienradar lua script

Posted: Friday 26 June 2020 23:00
by Joske
I see i choose ( 'trigger' type : ) ' TIME ' , just like i was told in this topic:

"...-- add as time based lua script..."

Can also choose for:

All
Device
Security
User variable

Under 'hardware' (after filtering on 'buien') i see the next 3 devices, and there you see Buien Radar was last seen a week ago:

147 Virtueel 000140E3 1 flgBuienRadar Light/Switch Switch Off - - 2020-06-26 18:48:37
144 Virtueel 140E0 1 BuienRadarMeter Rain TFA 0;0.18 - - 2020-06-26 16:00:00
143 Virtueel 00082143 1 Buien Radar General Text No rain - - 2020-06-22 03:45:00