Waterlevel Sensor Rule Help!

Hi Guys!
I Got a Problem creating a Rule for a Dog Bowl Water System. I really ned you help here!!

There are 3 items in this Configuration:

  • 1 Relay as a Switch, running the Pump
  • 1 Ultrasonic Sensor as a Number, controlls the Waterlevel
  • 1 Pir Sensor as a Number, controlls if there is a Motion/the Dog

i want to use it like this:
The max distance to the bowl is 45cm (the Bowl is empty) and 36cm (the Bowl is full with water)
So the Ultrasonic Sensor should check if the Waterlevel goes over for example 44cm then he should trigger the Switch On to fill up the bowl again till it reaches its maximum level of 36cm then trigger the Switch Off.
But if the Dog is around (the Pir Sensor checks that) everything should be turned Off, so the Ultrasonic Sensor and the Switch should be Off.

I think its something like:
does something needs to be up in this rule file?
rule "Waterlevel"
when Item Ultrasonic Sensor changed
then
if (Ultrasonic Sensor.state > 45) {
Switch.sendCommand(ON)
}

but how does it continues? i don´t have any clue and would be more than happy if someone could show me the right rule.

thanks a lot
Mike

basically you’re on the right way…

just think of it as if the PIR sensor isn’t there as this is the main task of the rule: make sure, there’s enough
water in the bowl, so basically

  • Trigger: distance from the sensor
  • rule: if > 45 pump until 36
  • exception: dog is around
  • make sure, your item names never have Special characters like blanks and stuff (Ultrasonic Sensor)
  • make sure, your item names never have names like internal commands or names (Switch)

step 1: make sure, the bowl is not empty:

rule "Waterlevel"
when 
    Item UltrasonicSensor changed
then
    if (UltrasonicSensor.state > 45) { // turn pump on, if state is > 45cm
        SwitchPump.sendCommand(ON)
    }
    if (UltrasonicSensor.state < 36) {
        SwitchPump.sendCommand(Off)
    }
end

(you noticed, the UltrasonicSensor has no blanks and SwitchPump doesn’t resemble the item Switch anymore)

so, problem is, the pump will stop immediately after reaching 45cm again as the trigger is the ultrasonic sensor’s state - it won’t reach 36cm at all in that rule. so, there are many ways to overcome this, the simplest one would be to count how long the pump needs to fill the bowl with 10cm of water. The rule would then trigger the pump and you’ll need a way to stop the pump after xx seconds. Let’s just say, the pump needs 30secs.

so, the improved rule is:

rule "Waterlevel"
when 
    Item UltrasonicSensor changed
then
    var Timer timer = null // define a timer variable
    if (UltrasonicSensor.state > 45) { // turn pump on, if state is > 45cm
        timer = Timer.createTimer(now.plusSeconds(30) [|  // start the timer with 30secs
            SwitchPump.sendCommand(ON)
            timer = null // reset the timer
        ])
    }
end

To make it more elegant and to save some codelines, you could

  • use expire-binding
  • automatically set the switch-item for the pump to OFF after 30secs.

So, for that you have to install the expire-binding.

Now, you define the time until the SwitchPump is set to OFF again in the items-definition

Switch  SwitchPump  { <<YOURSWITCHBINDING>>, expire="30s,command=OFF" }

(just add , expire="30s,command=OFF" at your existing item.

So, the rule can be simple as that:

rule "Waterlevel"
when 
    Item UltrasonicSensor changed
then
    if (UltrasonicSensor.state > 45) { // turn pump on, if state is > 45cm
        SwitchPump.sendCommand(ON)
   }
end

the SwitchPump will turn ON if the bowl is empty and the expire-binding will turn it OFF after 30secs.

Step 2: add the dog

so, now you only have to overrule the dog.
could be simple:

rule "Waterlevel"
when 
    Item UltrasonicSensor changed
then
    if (UltrasonicSensor.state > 45 && PIRdogSensor.state == OFF) { //turn pump on, if state is > 45cm and if dog isn`t around
        SwitchPump.sendCommand(ON)
   }
end

so, problem could be, that the water isn’t condensing fast enough and is only at 45cm if dog IS around. :wink:
So, in that case, I would just add another trigger, if dog isn’t around anymore (and has the bowl left empty):

rule "Waterlevel"
when 
    Item UltrasonicSensor changed or
    Item PIRdogSensor changed
then
    if (UltrasonicSensor.state > 45 && PIRdogSensor.state == OFF) { //turn pump on, if state is > 45cm and if dog isn`t around
        SwitchPump.sendCommand(ON)
   }
end

So, I basically made your homework, but I hope, you learned something from it and you can continue learning OH2! :wink:

cheers,
Thomas.

1 Like

omg thanks alot @bindereth you are great!!
i´ll try this out right now…hope i get it to work but with your perfect description it should be no problem!
thanks again man!!!

greets

mike

I just edited the rules above, there was an potential ERROR:

rule "NAME"
when
    trigger.....

the rule syntax was wrong as you have to divide “when” and the trigger in two lines…

1 Like

thanks!!!
it works perfect!!! my dog and my wife are happy haha
you made my day!!
have a great sunday!!
thanks again

greets
mike

2 Likes