OpenSprinkler Forums Hardware Questions OpenSprinkler Pi (OSPi) Get and Display Rain Sensor

  • This topic is empty.
Viewing 10 posts - 1 through 10 (of 10 total)
  • Author
  • #22609


    I’m staring a new thread since we have wandered away from the posting at
    OSPi – Interval – Garage Door Sensing and Control

    Dan had posted:

    Check the Python GPIO info at: … n-library/

    Look under the Example Usage section at the bottom of the page to see how to set the pins for input/output.

    To get the Rain sensor state to show on the OpenSprinkler home page you just need to set the var = 1 for rain sensed, = 0 for no rain.

    I’m not sure how to handle the rain sensor input for general use. What if there is just enough precip to trigger the rain sensor but not enough to supply the proper amount of irrigation?

    EDIT: Scratch that. It needs more to make it work. I’ll let you know when I have the correct incantations.
    OK. I added +’,rs=’+str( to one of the script lines (~426) in the class home: definition. Now if you check “Use rain sensor:” on the Options page and have = 1 it will show “rain detected” on the home page and all stations will have a strike through. Bit it does not actually stop the stations from running – yet. I’ll work on adding that to the next update.


    I had already defined and set up an input pin as shown in the prior post. The pin is pulled up internally and the rain sensor is a switch closure to ground.

    pin_rain_sense = 25

    GPIO.setup(pin_rain_sense, GPIO.IN, pull_up_down=GPIO.PUD_UP)

    So today I added to the functions:

    def getRainState():
    if GPIO.input(pin_rain_sense):
    res2 = 0
    res2 = 1
    return res2

    To get it to read the rain sensor state, I added a line to the main_loop definition as follows. This is probably not the right place for it, but I wanted a way to check to see if it worked. This is at my line number 183 of

    if and not and (not or not and not

    lt = time.localtime(now)

    The +’,rs=’+str( in the class home: definition was already there.

    If the rain sensor switch is open the home page shows:


    If the rain sensor switch is closed the home page shows:


    So it looks like it is basically working! Probably need to put the call to getRainState() in a better spot, but it is reading and displaying as it should. As you say, getting the programs to honor the rain sensor state is next.

    As far as the amount of irrigation, the Toro wireless rain sensor that I got has an adjustment for the amount of rainfall to trigger the switch closure. According to the data sheet it goes from 1/4″ to an 1 inch. I haven’t tested this, but I suppose it could be used to regulate just how much rain you would need to shut the irrigation off.




    For the life of me, I could never get the Toro to work. The moisture level never went below 75 on either of the Toro wireless sensors I got. I’m glad yours is working. I was so frustrated with them. I’m thinking about using wired moisture sensors. Anyway, I’m looking forward to when the activation on/Off is all working.


    Dan in CA


    Cool, Glad to see you have it working.

    The only problem with putting the

    where you have it is that it will be missed if a program is already running. You could put it at the start of the loop before the preceding “if” statement or at the end just before the


    where the rain delay stuff is and it will get checked once per second.
    In the process of adding some rain sensor logic to the program I added:

    if program_running:
    if and # Stop stations if use rain sensor and rain detected.

    In the main loop at ~line 235 in my version, and also:

    if # Skip if rain delay
    if and # Skip if use rain sensor and rain detected.

    in the schedule_stations function.

    I will include this in the next update as it is similar to what the Arduino based OpenSprinkler does. I don’t know how many folks will add a rain sensor to their system so I won’t do much more than that for now but I will be glad to work with you to get the more advanced selective feature going.

    You describe it as a per program selection but I think a per zone/station selection would be doable .





    Hi Mike,
    The wireless Toro model I have is just a relay with normally open and normally closed contacts available. No readout or display. It has a sensitivity adjustment from 1/8″ to 1″ of rain. I was testing the web interface coding by manually opening and closing my input line. We haven’t actually had any rain yet here in the Minneapolis area, so I don’t know how it will work in real life. Maybe some rain forecast for later today. I’ll let you know how it works; I currently have the sensitivity set to 1/4″.

    The Hunter rain sensor on my old irrigation controller was a similar design. It has been reliable in sensing rain for quite a few years until last fall when the weather turned cooler. I think the battery in the transmitter is wearing out. The battery on the Hunter unit is not replaceable, they make you buy a whole new unit. The Toro unit uses a standard replaceable battery, that’s why I selected it. I hope I have better luck with sensing than you did 🙂


    Thanks for your note about the placement of the getRainState() call. I’ll move it to the start of the loop.

    Thanks for adding some logic to use the rain sensor. It looks like it would be applied globally at this point. I’ll wait for your next release to update my system with all those changes to make sure they are in the right places.

    I had referred to the idea of “rain sensor per program”, but you’re right, rain sensor input per station would would be a better way to describe it. In my case I have 4 stations for my lawn that I want to obey the rain sensor, and two drip irrigation stations that I do not. One is for window boxes, and the other is for potted plants on the deck. We have to use odd/even for lawn watering in our city, but the window boxes and deck plants get watered every day. They dry out quickly and some are sheltered from the rain. I’d be happy to work with you on getting per station rain sensing working.

    This project has been my first real exposure to Python, so it has been a lot of fun. I’m really impressed with the functionality of the GPIO library. Thanks for all your help!



    It’s been several weeks that the rain sensor and code have been in place, and we have had rain on and off, so I thought I would follow up with some data on the Toro wireless rain sensor. The unit seems to be quite reliable. I’m running a cron job every minute on the OSPi machine and logging to a remote system.

    zcat -f `ls -1tr /var/log/ospi*` | grep -i raining
    Sep 16 14:53:02 ospi Rain state is now NotRaining
    Sep 19 11:19:01 ospi Rain state is now Raining
    Sep 20 16:55:01 ospi Rain state is now NotRaining
    Sep 20 16:56:02 ospi Rain state is now Raining
    Sep 20 17:19:02 ospi Rain state is now NotRaining
    Sep 28 12:56:02 ospi Rain state is now Raining
    Sep 29 11:06:02 ospi Rain state is now NotRaining
    Oct 2 22:04:01 ospi Rain state is now Raining
    Oct 8 00:57:02 ospi Rain state is now NotRaining
    Oct 14 22:42:01 ospi Rain state is now Raining
    Oct 17 15:17:02 ospi Rain state is now NotRaining
    Oct 18 03:23:01 ospi Rain state is now Raining

    As you can see the sensor goes on and off, and it’s raining now. I have the unit set at 1/4″ so it’s pretty sensitive.

    For reference, this is the one I’m using.
    Toro 53770 Sprinkler System Timer Wireless Rain Sensor



    Hi There,

    I’ve just done a github update to my OSPi directory and now have a strange problem.

    All was working fine, when i set the “use rain sensor” option I got the following error:

    Unhandled exception in thread started by 
    Traceback (most recent call last):
    File "", line 338, in timing_loop
    File "", line 86, in check_rain
    if not GPIO.input(pin_rain_sense):
    NameError: global name 'pin_rain_sense' is not defined

    and it appears that OSPi has locked, however a refresh updates the CPU temp, but not the time and date. A quick look at the code and I don’t see anywhere that “pin_rain_sense” is defined.

    Interested to see if this is a specific fault to my setup or if it’s a bug.



    After updating, did you restart the program?


    Dan in CA

    It’s basically a bug.
    Since the RASPi version does not have a rain sensor connection by default you need to follow Denny’s instruction for hooking it up and modifying the code as he describes at the beginning of this thread.

    I will look at fixing the code so it won’t cause an error if “use rain sensor” is selected without the other modifications.



    Dan in CA

    I pushed an updated version of to GitHub with error checking added for this problem.

    The reason the rain sensor code was added to the program was to allow use of the built in rain sensor connector on Ray’s new Beagle Bone Black version of OpenSprinkler. If you want to hook up a rain sensor to the Pi version please follow Denny’s instruction at the start of this thread.




    Thanks for that.

    Makes sense – I’ll have a look at adding the code as described by Denny


Viewing 10 posts - 1 through 10 (of 10 total)
  • You must be logged in to reply to this topic.

OpenSprinkler Forums Hardware Questions OpenSprinkler Pi (OSPi) Get and Display Rain Sensor