Rule to count rain tipping bucket

Hello folks,

Any ideas on how to implement a rule to count and chart participation? I have a generic tipping bucket from eBay. It has a simple reed switch that is tripped each time the bucket tips. Each tip of the tipping bucket collector is counted by a reed switch calibrated to record 0.01 inch (=0.25 mm) of rain.

This is an interesting problem. So every time the bucket tips you need to record 0.01 in. Then you need to sum the values for the past time period (e.g. part hour, past 24 hours, today, etc.).

The easy part is recording the tips themselves. Represent the bucket as a Contact or a Switch and create a rule that triggers when it receives a command.

The hard part is doing the summing. I think I would try the following.

Set up db4o, MySQL, or some other db that isn’t MapDB or rrd4j. Create a Number item and update it to 0.01 every time the bucket tips in the rule. Configure this item to persist every update to the db. Then create Number items for each time period you care about and in the same rule postUpdate the new value using the sumSince method on the bucket counting Item. Eg

BucketCount.sendCommand(0.01)
RainHour.sendCommand (BucketCount.sumSince (now.minusHours (1))
...

Now I’m not 100% sure how well charting works with db4o or MySQL . If it doesn’t work you will want to set up rrd4j on your time period items to save every minute for your charting.

How are you interfacing the reed switch with OH? I’ve been thinking about doing this with Mysensors as I’ve got some MS devices already in service (temp, humidity, alarm reed contacts). You may look at this if you are looking for a way to tie this in via Mysensors:

Also, can you like the tipping bucket you bought on eBay?

Thanks rlksoshak. I’ll give it a go… Hopefully, this weekend.

I am using ESPEasy to run an ESP Wemos Mini. The reed switch is a basically a pull-down on any IO. The signal is passed to Openhab via the MQTT protocol. I’ll post the actual build later.

Here is the Ebay link to the sensor. The reed sensor is in the little housing at the back of the housing.

When you get this working I and others would love to see the working code. This is is a good project for those with automated irrigation and could be a lot more accurate than the Total Rain value from Wunderground.

For example, where I am rain is usually highly isolated to such an extent that the weather station half a mile away could get a quarter inch of rain and I’d get none. And I don’t even have a station that close.

Another idea:
Maybe you could count within your ESP with gating time (e.g. all pulse within one minute) and send the counted value via MQTT to openHAB.
If this is less accurate then get two or three topics/items more for 10 minutes or 1 hour or 1 day.
The items can hold the counting value or the calculated rain (inch or mm).
This way you don’t have to calculate within openHAB…

If you’re counting every minute and push this value to openhab, with persitence (rrd4j) you can have all the calculations done…

Andreas

ESPEasy has a digital pulsecounter option that may work here. There is also a formula option. We’ll see.

Initial setup seems to work. I’ve decided to go with the switch configuration because the pulse counter seems to still be problematic. So… the switch will send 1.00 via MQTT every time the reed circuit is closed. I’ve simulated rain with a physical push button and will start to track how well the charting works.

ESPEasy config

—Items—
(Not all of the groups from my items are listed here.)

Number    Period_RainTotal
//Rain Counter to receive 1.00 from the esp
Number    RainCount    "Rain Counter[%.2f in]"    (All,Test,Esp)    {mqtt="<[mymosquitto:/RainTest/TippingBucket/Count:state:default]"}
//Rain Total to receive the increment rule adding .01 each
Number    RainTotal    "Rain Total[%.2f in]"    (All,Test,Esp,Chart_RainTotal)
DateTime    RainTotal_Time    "Last time is rained [%1$tm/%1$td, %1$tI:%1$tM %1$tp]"    <DateTimeIcon>    (All,Test,Esp,Updates)

—Rule—

var Number RainIncr = 0

rule "Increment Rain bucket"
    when
        Item RainCount received update 1.00
    then    
        RainIncr = RainIncr + 0.01
        RainTotal.postUpdate(RainIncr.toString)
        postUpdate(RainTotal_Time, new DateTimeType())
end

rule "Reset Rain Total at Midnight"
    when
        Time cron "0 0 0 1/1 * ? *"
    then    
        postUpdate(RainTotal, 0.0)
end

—Persistence—
(rrd4j.persist)

Strategies {
    // for rrd charts, we need a cron strategy
    everyMinute : "0 * * * * ?"
    everyHour : "0 0 * * * ?"
    
    default = everyChange
}

Items {
* : strategy = everyChange, restoreOnStartup
RainTotal
    : strategy = everyMinute
}

—Sitemap—

//page showing all items in the test group
Frame label="Most Used" {
        Group    item=Test label="Test" icon="TestIcon"
    }    
    Frame label="Data" {
        //Group item=charts label="Charts" icon="chart"     
        Text label="Charts" icon="ChartIcon" {
            Frame {
                Text label="Home Rainfall" icon="water" {
                    Frame {
                        Text item=RainTotal
                    }
                    Frame {
                        Switch item=Period_RainTotal label="HVAC Chart Period" mappings=[0="Hour", 1="Day", 2="Week", 3="Month"]
                        Chart item=Chart_RainTotal period=h refresh=6000 visibility=[Period_RainTotal==0, Period_RainTotal=="Uninitialized"]
                        Chart item=Chart_RainTotal period=D refresh=30000 visibility=[Period_RainTotal==1]
                        Chart item=Chart_RainTotal period=W refresh=30000 visibility=[Period_RainTotal==2]
                        Chart item=Chart_RainTotal period=M refresh=30000 visibility=[Period_RainTotal==3]
                    }
                }
            }
      }
2 Likes

Going to have to switch charting to db4o or MySQL… rrd4j spreads the rain throughout the day… Should have listened…:blush:

Typo?

Yes it was. I fixed the post… Thanks

Thanks for posting the code. I love to create and see working examples like this.

One suggestion, it will be much easier to read if you use code formatting. There are several ways you can do this. Perhaps the easiest is to put three backticks (i.e. `) immediately before and after your code. This will preserve your indentation and add some minor color highlighting.

I might suggest MySQL over db4o mainly because db4o, being an embedded DB, does not have as many tools available for you to externally manage and maintain them (e.g. delete old data when you DB grows too large).

My parts are in and I plan to have a working system by the end of the weekend. FYI… I started another thread on improving an irrigation controller setup. Please chime in and help make it better for all.

It works… Here’s the final build information
—ESPEasy config—

Device: Switch input
Name: Reed
IDX / Var: 100
1st GPIO: 12
Pull UP: X
Inversed:
Switch Type: Switch
Switch Button Type: Push Button Active High
Value Name 1: RainGuage

—Items—

Number RainCount "Rain Counter[%.2f]" (All,Test,Esp,Irrigation) {mqtt="<[mymosquitto:/espShop/Reed/RainGuage:state:default]"}
Number RainTotal "Rain Total[%.2f in]" (All,Test,Esp,Chart_RainTotal,Irrigation)
DateTime RainTotal_Time "Last time is rained [%1$tm/%1$td, %1$tI:%1$tM %1$tp]" <DateTimeIcon> (All,Test,Esp,Updates,Irrigation)

—Rules—

rule "Increment Rain bucket"
//Rain Counter to receive 1.00 or 0.00 from the esp
    when
        Item RainCount received update 1.00 or
        Item RainCount received update 0.00
    then    
        RainIncr = RainIncr + 0.01
        RainTotal.postUpdate(RainIncr.toString)
        postUpdate(RainTotal_Time, new DateTimeType())
end

rule "Reset Rain Total at Midnight"
    when
        Time cron "0 0 0 1/1 * ? *"
    then    
        postUpdate(RainTotal, 0.0)
end

Here the video of the rain gauge in action.

1 Like

Reviving a old thread here, I found this and wanted to build the same but to use with my Grafana server. No problem since I’m using influx with my mqtt server but I had a question.
the —items— and —rules— are they made in openhab or in ESPEasy?

Items and rules are in Openhab. ESPEasy just sends a 1.0 or 0.0 to openhab as the bucket tips.

Ah thanks @opnhab7373 then I get it, and the bucket tips at 1ml I guess. Should be able to use the rule function in ESPEasy to, I’m waiting for my rain gauge atm. but will do some testing.
Looked like the gauge had a rj11 or something at the end, is it just pos, neg and signal?

Has someone an example with tasmota?