Rain counter

Easy to use, 100% Lua-based event scripting framework.

Moderator: leecollings

tage
Posts: 14
Joined: Monday 14 May 2018 15:56
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Rain counter

Post 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
elmortero
Posts: 247
Joined: Sunday 29 November 2015 20:46
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.9639
Location: Spain
Contact:

Re: Rain counter

Post by elmortero »

tage
Posts: 14
Joined: Monday 14 May 2018 15:56
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Rain counter

Post by tage »

Thx. But I don't think I'm able to make a script from scratch. I was hoping I could modify one..
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Rain counter

Post 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 ?
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
tage
Posts: 14
Joined: Monday 14 May 2018 15:56
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Rain counter

Post 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.
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Rain counter

Post 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
 }
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
tage
Posts: 14
Joined: Monday 14 May 2018 15:56
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Rain counter

Post 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
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Rain counter

Post 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
 }
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
tage
Posts: 14
Joined: Monday 14 May 2018 15:56
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Rain counter

Post 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
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Rain counter

Post by waaren »

Just backup your current version, directories and most important database (and make sure you can restore :D ) before you install the latest Beta and enjoy all new features ..
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
tage
Posts: 14
Joined: Monday 14 May 2018 15:56
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Rain counter

Post 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
tage
Posts: 14
Joined: Monday 14 May 2018 15:56
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Rain counter

Post 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
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Rain counter

Post 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.
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
snellejellep
Posts: 241
Joined: Tuesday 16 May 2017 13:05
Target OS: Raspberry Pi / ODroid
Domoticz version: 2020.2
Location: The Neterlands
Contact:

Re: Rain counter

Post 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
raspberry pi | xiaomi vacuum | yeelight | philips hue | zwave | ubiquiti unifi | harmony | sonoff | zigbee2mqtt | https://www.youtube.com/channel/UC2Zidl ... m1OLuNldfQ
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Rain counter

Post 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.
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
snellejellep
Posts: 241
Joined: Tuesday 16 May 2017 13:05
Target OS: Raspberry Pi / ODroid
Domoticz version: 2020.2
Location: The Neterlands
Contact:

Re: Rain counter

Post 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
raspberry pi | xiaomi vacuum | yeelight | philips hue | zwave | ubiquiti unifi | harmony | sonoff | zigbee2mqtt | https://www.youtube.com/channel/UC2Zidl ... m1OLuNldfQ
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Rain counter

Post 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?
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
snellejellep
Posts: 241
Joined: Tuesday 16 May 2017 13:05
Target OS: Raspberry Pi / ODroid
Domoticz version: 2020.2
Location: The Neterlands
Contact:

Re: Rain counter

Post 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
raspberry pi | xiaomi vacuum | yeelight | philips hue | zwave | ubiquiti unifi | harmony | sonoff | zigbee2mqtt | https://www.youtube.com/channel/UC2Zidl ... m1OLuNldfQ
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Rain counter

Post 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
 }
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
snellejellep
Posts: 241
Joined: Tuesday 16 May 2017 13:05
Target OS: Raspberry Pi / ODroid
Domoticz version: 2020.2
Location: The Neterlands
Contact:

Re: Rain counter

Post 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!
raspberry pi | xiaomi vacuum | yeelight | philips hue | zwave | ubiquiti unifi | harmony | sonoff | zigbee2mqtt | https://www.youtube.com/channel/UC2Zidl ... m1OLuNldfQ
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest