Rain counter

Easy to use, 100% Lua-based event scripting framework.
Post Reply
tage
Posts: 10
Joined: Monday 14 May 2018 15:56
Target OS: Raspberry Pi
Domoticz version:
Contact:

Rain counter

Post by tage » Tuesday 15 May 2018 14:20

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: 174
Joined: Sunday 29 November 2015 21:46
Target OS: Raspberry Pi
Domoticz version: 2.3852
Location: Spain
Contact:

Re: Rain counter

Post by elmortero » Tuesday 15 May 2018 15:15


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

Re: Rain counter

Post by tage » 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..

User avatar
waaren
Posts: 464
Joined: Tuesday 03 January 2017 15:18
Target OS: Raspberry Pi
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Rain counter

Post by waaren » Tuesday 15 May 2018 19:44

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 ?
Raspberry (debian stretch via berryboot on Synology DS916+) , Domoticz (almost) latest Beta, RFLink, RFXtrx433e, P1, Youless, Harmony, Hue, Yeelight, Xiaomi, HomeWizard, Zwave , dzVents 2.4.n, Amazon echo

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

Re: Rain counter

Post by tage » 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.

User avatar
waaren
Posts: 464
Joined: Tuesday 03 January 2017 15:18
Target OS: Raspberry Pi
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Rain counter

Post by waaren » Tuesday 15 May 2018 22:33

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
 }
Raspberry (debian stretch via berryboot on Synology DS916+) , Domoticz (almost) latest Beta, RFLink, RFXtrx433e, P1, Youless, Harmony, Hue, Yeelight, Xiaomi, HomeWizard, Zwave , dzVents 2.4.n, Amazon echo

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

Re: Rain counter

Post by tage » Wednesday 16 May 2018 6:06

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: 464
Joined: Tuesday 03 January 2017 15:18
Target OS: Raspberry Pi
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Rain counter

Post by waaren » Wednesday 16 May 2018 7:27

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
 }
Raspberry (debian stretch via berryboot on Synology DS916+) , Domoticz (almost) latest Beta, RFLink, RFXtrx433e, P1, Youless, Harmony, Hue, Yeelight, Xiaomi, HomeWizard, Zwave , dzVents 2.4.n, Amazon echo

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

Re: Rain counter

Post by tage » Wednesday 16 May 2018 9:38

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: 464
Joined: Tuesday 03 January 2017 15:18
Target OS: Raspberry Pi
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Rain counter

Post by waaren » Wednesday 16 May 2018 10:08

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 ..
Raspberry (debian stretch via berryboot on Synology DS916+) , Domoticz (almost) latest Beta, RFLink, RFXtrx433e, P1, Youless, Harmony, Hue, Yeelight, Xiaomi, HomeWizard, Zwave , dzVents 2.4.n, Amazon echo

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

Re: Rain counter

Post by tage » Wednesday 16 May 2018 16:52

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

Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest