Page 1 of 3
Rain counter
Posted: Tuesday 15 May 2018 14:20
by tage
Hi. Is it possible to script like this :
viewtopic.php?t=15616
But update a rain sensor instead of a general counter ?
I have connect a tipping bucket rain gauge to an esp and set it up as a switch (on/off) if set it up like this it dosen't matter if the esp lose power or reboot the counting is still works..
/ tage
Re: Rain counter
Posted: Tuesday 15 May 2018 15:15
by elmortero
Re: Rain counter
Posted: Tuesday 15 May 2018 17:10
by tage
Thx. But I don't think I'm able to make a script from scratch. I was hoping I could modify one..
Re: Rain counter
Posted: Tuesday 15 May 2018 19:44
by waaren
tage wrote: ↑Tuesday 15 May 2018 17:10
Thx. But I don't think I'm able to make a script from scratch. I was hoping I could modify one..
Happy to get you started with a dzVents script but need some more info to work with;
Is my assumption correct that every time the bucket tips you send an On command to a domoticz switch ?
Do you do that via a json command or in another way ?
Re: Rain counter
Posted: Tuesday 15 May 2018 20:43
by tage
exacly every time the bucket tips it pass the magnet and make it "on" for a short time in the tipping moment or "off" it is possible to change.
on the esp (wemos d1) im using esp easy (letscontrolit) and using "Domoticz HTTP" im assume it is json.
Re: Rain counter
Posted: Tuesday 15 May 2018 22:33
by waaren
tage wrote: ↑Tuesday 15 May 2018 20:43
exacly every time the bucket tips it pass the magnet and make it "on" for a short time in the tipping moment or "off" it is possible to change.
on the esp (wemos d1) im using esp easy (letscontrolit) and using "Domoticz HTTP" im assume it is json.
Hope this dzVents script will get you going.
Code: Select all
return {
on = { timer = { "at 00:01" },
devices = { "ESPTestSwitch" } }, -- Your switch device triggered by ESP bucket full
logging = { level = domoticz.LOG_DEBUG, -- INFO, ERROR or DEBUG
marker = "ESP test" }, --
execute = function(dz, trigger)
local rainDevice = dz.devices("ESPTestRain") -- Your (virtual) rain device
local rainSwitch = dz.devices("ESPTestSwitch") -- Your (triggered by bucket full) switch
local rainmm = 2 -- find out what 1 bucket full means in terms of mm
local rainTotal = 0
local timeSlice = math.min( (rainDevice.lastUpdate.secondsAgo / 3600), 10) -- at least 1 bucket in 10 hours
local rainAmountHour = dz.utils.round((rainmm / timeSlice),1)
-- Calc raintotal and reset raintotal at midnight
if trigger.isTimer then
rainDevice.updateRain(dz.utils.round(rainDevice.rainRate,0),0) -- reset rainTotal
rainSwitch.switchOff().silent()
dz.log("Reset raintotal to 0",dz.log_DEBUG)
else
rainTotal = dz.utils.round((rainmm + rainDevice.rain),1)
rainDevice.updateRain(rainAmountHour,rainTotal)
dz.log("One bucket full ==>> updating raindevice. rainrate: " .. rainAmountHour .. " mm/hr, " .. rainTotal .." mm in total today " ,dz.log_DEBUG)
rainSwitch.switchOff().silent()
end
end
}
Re: Rain counter
Posted: Wednesday 16 May 2018 6:06
by tage
thx !
got some trouble and this is what the log says :
Code: Select all
2018-05-16 05:53:27.027 Error: dzVents: Error: ESP test: An error occured when calling event handler rain_bucket
2018-05-16 05:53:27.027 Error: dzVents: Error: ESP test: ...moticz/scripts/dzVents/generated_scripts/rain_bucket.lua:16: attempt to index field 'utils' (a nil value)
i think i have edit and change that is needed...
/ tage
Re: Rain counter
Posted: Wednesday 16 May 2018 7:27
by waaren
tage wrote: ↑Wednesday 16 May 2018 6:06
thx !
got some trouble and this is what the log says : attempt to index field 'utils' (a nil value)
/ tage
What is your domoticz / dzVents version ?
the isTrigger method was introduced in dzVents 2.4.0
the utils round function was introduced in dzVents 2.4.0
the silent() option to switchOff() function was introduced in dzVents 2.3.0
if you really cannot update to a domoticz Beta version with dzVents 2.4.0 or higher you could try the script below where I worked around the new stuff introduced in later dzVents versions. But much better to go a later domoticz with all the new great dzVents stuff in.
Code: Select all
return {
on = { timer = { "at 00:01" },
devices = { "ESPTestSwitch" } }, -- Your switch device triggered by ESP bucket full
logging = { level = domoticz.LOG_DEBUG, -- INFO, ERROR or DEBUG
marker = "ESP test" }, --
execute = function(dz, trigger, triggerInfo)
function round(num, numDecimalPlaces)
return tonumber(string.format("%." .. (numDecimalPlaces or 0) .. "f", num))
end
local rainDevice = dz.devices("ESPTestRain") -- Your (virtual) rain device
local rainSwitch = dz.devices("ESPTestSwitch") -- Your (virtual) rain device
local rainmm = 2 -- find out what 1 bucket full means in terms of mm
local rainFactor = 100 -- To get a realistic mm/hr value (adjust to your needs)
local rainTotal = 0
local timeSlice = math.min( (rainDevice.lastUpdate.secondsAgo / 3600), 10) -- at least 1 bucket in 10 hours
local rainAmountHour =round((rainmm / timeSlice * rainFactor),1)
-- Calc raintotal and reset raintotal at midnight
if triggerInfo.type == dz.EVENT_TYPE_TIMER then
rainDevice.updateRain(round(rainDevice.rainRate,0),0) -- reset rainTotal
dz.log("Reset raintotal to 0",dz.log_DEBUG)
elseif rainSwitch.state ~= "Off" then
rainTotal = round((rainmm + rainDevice.rain),1)
rainDevice.updateRain(rainAmountHour,rainTotal)
dz.log("One bucket full ==>> updating raindevice. rainrate: " .. rainAmountHour .. " mm/hr, " .. rainTotal .." mm in total today " ,dz.log_DEBUG)
rainSwitch.switchOff()
end
end
}
Re: Rain counter
Posted: Wednesday 16 May 2018 9:38
by tage
Thx. The last one seems to work I have to do some test tonight.
My domoticz Version: 3.8153
Guess it is the latest stable.
I have not try the beta and don't know how stable they are that's why I'm on stable versions....
/ tage
Re: Rain counter
Posted: Wednesday 16 May 2018 10:08
by waaren
Just backup your current version, directories and most important database (and make sure you can restore

) before you install the latest Beta and enjoy all new features ..
Re: Rain counter
Posted: Wednesday 16 May 2018 16:52
by tage
Aah I'm maybe give it a try.
It act little strange when set the rain mm to 1 (one bucket is 1mm)
And rain factor I'm not sure what value to set. Gonna do some more test later
Re: Rain counter
Posted: Wednesday 23 May 2018 4:55
by tage
I just found that the script is counting both on and off.. the rain gauge is making the on switch and goes back to off again.
possble to make the script only count "on'" command ?
/ Tage
Re: Rain counter
Posted: Wednesday 23 May 2018 8:27
by waaren
tage wrote: ↑Wednesday 23 May 2018 4:55
I just found that the script is counting both on and off.. the rain gauge is making the on switch and goes back to off again.
possble to make the script only count "on'" command ?
/ Tage
@tage,
the line
Code: Select all
elseif rainSwitch.state ~= "Off" then
should ensure the script only counts if the switch is set to a state not equal to "Off".
In later versions of dzVents (> 2.2) you would change the line
Code: Select all
rainSwitch.switchOff()
to
rainSwitch.switchOff().silent()
to prevent the triggering of the script if the rainSwitch is set to "Off".
You could add
Code: Select all
dz.log("Script triggered by rainSwitch state: " .. rainSwitch.state,dz.log_DEBUG)
just above the switchOff() line to see in the log when the counting part of the script is executed.
Re: Rain counter
Posted: Monday 31 August 2020 12:44
by snellejellep
i wanted to try this out for my 3d printed rainmeter but i have one issue.
i have a raindevice in domoticz and a switch which switches when the bucket tips so all the same as above but now the following:
i get no errors and the rain device updates but only the rain rate and not the total fallen rain.
am i overlooking something?
this is my log:
Code: Select all
2020-08-31 12:41:25.068 Status: User: Admin initiated a switch command (1126/tippingbucketrain/On)
2020-08-31 12:41:25.345 Status: dzVents: Info: Handling events for: "tippingbucketrain", value: "On"
2020-08-31 12:41:25.346 Status: dzVents: Info: ESP test: ------ Start internal script: regenmeter: Device: "tippingbucketrain (Dummy/virtual)", Index: 1126
2020-08-31 12:41:25.347 Status: dzVents: Debug: ESP test: Processing device-adapter for Regenmeter: Rain device
2020-08-31 12:41:25.348 Status: dzVents: Info: ESP test: One bucket full ==>> updating raindevice. rainrate: 133.3 mm/hr, 2.0 mm in total today
2020-08-31 12:41:25.348 Status: dzVents: Debug: ESP test: Constructed timed-command: Off
2020-08-31 12:41:25.348 Status: dzVents: Debug: ESP test: Constructed timed-command: Off NOTRIGGER
2020-08-31 12:41:25.348 Status: dzVents: Info: ESP test: ------ Finished regenmeter
Re: Rain counter
Posted: Monday 31 August 2020 18:44
by waaren
snellejellep wrote: ↑Monday 31 August 2020 12:44
i have a raindevice in domoticz and a switch which switches when the bucket tips so all the same as above but now the following:
i get no errors and the rain device updates but only the rain rate and not the total fallen rain.
The value shown on the title row of a rain device shows the latest send value minus the first value of today. So if you have send the same value it will show 0
The value will be reset to 0 at midnight.
Re: Rain counter
Posted: Monday 31 August 2020 18:58
by snellejellep
waaren wrote: ↑Monday 31 August 2020 18:44
The value shown on the title row of a rain device shows the latest send value minus the first value of today. So if you have send the same value it will show 0
The value will be reset to 0 at midnight.
then i think i do not understand how it works. i thought the following:
- the script adds the rainmm defined amount to the rain device every time the bucket tips and thus the device is activated
- that way each time the bucket tips by default it should add 2mm of rain to the upper right corner of the rain device?
- but the only thing i am seeing is the rain rate being updated?
correct me if i am wrong but i thought that it would update the total rain in mm in the upper right corner
Re: Rain counter
Posted: Monday 31 August 2020 19:03
by waaren
snellejellep wrote: ↑Monday 31 August 2020 18:58
then i think i do not understand how it works. i thought the following:
- the script adds the rainmm defined amount to the rain device every time the bucket tips and thus the device is activated
- that way each time the bucket tips by default it should add 2mm of rain to the upper right corner of the rain device?
- but the only thing i am seeing is the rain rate being updated?
correct me if i am wrong but i thought that it would update the total rain in mm in the upper right corner
Can you please share the script as you use it now?
Re: Rain counter
Posted: Monday 31 August 2020 20:56
by snellejellep
waaren wrote: ↑Monday 31 August 2020 19:03
Can you please share the script as you use it now?
Code: Select all
return {
on = { timer = { "at 00:01" },
devices = { "tippingbucketrain" } }, -- Your switch device triggered by ESP bucket full
logging = { level = domoticz.LOG_DEBUG, -- INFO, ERROR or DEBUG
marker = "ESP test" }, --
execute = function(dz, trigger)
local rainDevice = dz.devices("Regenmeter") -- Your (virtual) rain device
local rainSwitch = dz.devices("tippingbucketrain") -- Your (triggered by bucket full) switch
local rainmm = 2 -- find out what 1 bucket full means in terms of mm
local rainTotal = 0
local timeSlice = math.min( (rainDevice.lastUpdate.secondsAgo / 3600), 10) -- at least 1 bucket in 10 hours
local rainAmountHour = dz.utils.round((rainmm / timeSlice),1)
-- Calc raintotal and reset raintotal at midnight
if trigger.isTimer then
rainDevice.updateRain(dz.utils.round(rainDevice.rainRate,0),0) -- reset rainTotal
rainSwitch.switchOff().silent()
dz.log("Reset raintotal to 0",dz.log_DEBUG)
else
rainTotal = dz.utils.round((rainmm + rainDevice.rain),1)
rainDevice.updateRain(rainAmountHour,rainTotal)
dz.log("One bucket full ==>> updating raindevice. rainrate: " .. rainAmountHour .. " mm/hr, " .. rainTotal .." mm in total today " ,dz.log_DEBUG)
rainSwitch.switchOff().silent()
end
end
}
that is the script, just copied from above and entered my own devices
and for raindevice i use the domoticz dummy rain device
Re: Rain counter
Posted: Monday 31 August 2020 22:35
by waaren
snellejellep wrote: ↑Monday 31 August 2020 18:58
correct me if i am wrong but i thought that it would update the total rain in mm in the upper right corner
I tested with below script (the reset at 0:00 is not needed as domoticz does that anyway without a script) and the upper right corner does display the total rain in mm
for today
BUT: if the lowest value of today in database table RAIN is > 0, the calculation in the script fails because the value of rain is
current number - lowest number of today in database table RAIN for this device
Code: Select all
return
{
on =
{
devices =
{
"tippingbucketrain",
},
},
logging =
{
level = domoticz.LOG_DEBUG, -- INFO, ERROR or DEBUG
marker = "ESP test",
},
execute = function(dz, item)
local rainDevice = dz.devices("Regenmeter") -- Your (virtual) rain device
local rainSwitch = dz.devices("tippingbucketrain") -- Your (triggered by bucket full) switch
local rainmm = 2 -- find out what 1 bucket full means in terms of mm
local rainTotal = 0
local timeSlice = math.min( (rainDevice.lastUpdate.secondsAgo / 3600), 10) -- at least 1 bucket in 10 hours
local rainAmountHour = dz.utils.round((rainmm / timeSlice),1)
if item.active then
rainTotal = dz.utils.round((rainmm + rainDevice.rain),1)
rainDevice.updateRain(rainAmountHour, rainTotal)
dz.log("One bucket full ==>> updating raindevice. rainrate: " .. rainAmountHour .. " mm/hr, " .. rainTotal .." mm in total today ", dz.LOG_DEBUG )
rainSwitch.switchOff().silent()
end
end
}
Re: Rain counter
Posted: Wednesday 02 September 2020 18:50
by snellejellep
waaren wrote: ↑Monday 31 August 2020 22:35
I tested with below script (the reset at 0:00 is not needed as domoticz does that anyway without a script) and the upper right corner does display the total rain in mm
for today
BUT: if the lowest value of today in database table RAIN is > 0, the calculation in the script fails because the value of rain is
current number - lowest number of today in database table RAIN for this device
Code: Select all
return
{
on =
{
devices =
{
"tippingbucketrain",
},
},
logging =
{
level = domoticz.LOG_DEBUG, -- INFO, ERROR or DEBUG
marker = "ESP test",
},
execute = function(dz, item)
local rainDevice = dz.devices("Regenmeter") -- Your (virtual) rain device
local rainSwitch = dz.devices("tippingbucketrain") -- Your (triggered by bucket full) switch
local rainmm = 2 -- find out what 1 bucket full means in terms of mm
local rainTotal = 0
local timeSlice = math.min( (rainDevice.lastUpdate.secondsAgo / 3600), 10) -- at least 1 bucket in 10 hours
local rainAmountHour = dz.utils.round((rainmm / timeSlice),1)
if item.active then
rainTotal = dz.utils.round((rainmm + rainDevice.rain),1)
rainDevice.updateRain(rainAmountHour, rainTotal)
dz.log("One bucket full ==>> updating raindevice. rainrate: " .. rainAmountHour .. " mm/hr, " .. rainTotal .." mm in total today ", dz.LOG_DEBUG )
rainSwitch.switchOff().silent()
end
end
}
thank you verry much! this works like a charm!