---
title: "Smart Rules: Hysteresis-based filling control of a retention tank according to water level"
description: "Fill a retention tank by water level with Smart Rules: two equations, LastState, or the HYSTERESIS formula on an analog level sensor to switch the valve."
url: "https://taphome.com/en/docs/configuration/hysteresis-equation/"
language: en
lastmod: 2026-10-13
generator: hugo
ai:
  topics: ["hysteresis","smart-rules","retention-tank","water-level-control","valve-automation","level-sensors"]
  questions: ["How do I configure hysteresis control for a retention tank in TapHome?","What is the HYSTERESIS function and how do I use it?","How do I prevent frequent valve switching with water level control?","What does the equation (LastState AND !MaxLevel) OR (!LastState AND !MinLevel) do?","What are the parameters of the HYSTERESIS function for level control?","When should I use Formula vs Equation Smart Rule for level control?","How do I configure valve control with digital level sensors?"]
---

# Smart Rules: Hysteresis-based filling control of a retention tank according to water level

Smart Rules configuration of filling a retention tank according to water level with a hysteresis function for efficient valve control.

Last updated: 2026-10-13

The following example shows how to configure the filling of a retention tank based on measurements of the minimum and maximum water levels.

Water tank with two reed contacts: MAX (variable MaxLevel) and MIN (variable MinLevel); the valve (variable Valve) fills the tank between them.


## Configuration using two equations

An alternative solution uses two separate Smart Rules of type **Equation** – one for turning the valve on and the other for turning it off.

### 1. Smart Rule: "Turn on the valve"

**Variables**
- MinLevel (Status contact for the minimum level)
- MaxLevel (Status contact for the maximum level)
- Valve (Current state of the valve)

**Equation**
```taphome
!MinLevel
```

**Actions**

**If result is TRUE, perform following actions:**
- Valve ... Turn on

Explanation: When the level falls below the minimum level (MinLevel = FALSE), the equation `!MinLevel` returns TRUE and the valve turns on.

### 2. Smart Rule: "Turn off the valve"

**Variables**
- MaxLevel (Status contact for the maximum level)

**Equation**
```taphome
MaxLevel
```

**Actions**

**If result is TRUE, perform following actions:**
- Valve ... Turn off

Explanation: When the level reaches the maximum level (MaxLevel = TRUE), the valve turns off.

## Configuration using LastState

#### **Variables**
- Status contact for the minimum level (Variable name: MinLevel)
- Status contact for the maximum level (Variable name: MaxLevel)

#### Controlled device
- Digital output to control the valve or pump (Name: Valve)

The **Equation** Smart Rule has an internal variable named LastState. This variable returns TRUE or FALSE based on the last evaluation result of this Smart Rule.

#### **Equation**
```taphome
(LastState AND !MaxLevel) OR (!LastState AND !MinLevel)
```

#### **Actions**

**If result is TRUE, perform following actions:**
- Valve ... Turn on

#### **If result is FALSE, perform following actions:**
- Valve ... Turn off

#### **Duration**
Minimum duration ... 0:05:00 minutes

## Configuration with analog level measurement

If you have an analog level measurement available (e.g., an ultrasonic sensor measuring the distance from the bottom of the tank), you can use a more elegant solution with the HYSTERESIS function and a Smart Rule of type **Formula**.

#### **Variables**
- Analog measurement of the level (Variable name: Level) – distance from the bottom of the tank in cm

#### **Output devices**
- Digital output to control the valve or pump (Name: Valve)

#### Configuration using Smart Rule **Formula**

The **Formula** Smart Rule automatically assigns the calculated value to the output device.

**Formula**
```taphome
HYSTERESIS(Level, 90, 20, 0, 1, Valve)
```

The arguments are the input value `Level`, the upper bound `90`, the lower bound `20`, the output at the upper bound `0`, the output at the lower bound `1` and the previous state `Valve` — the parameters are described at [HYSTERESIS](https://taphome.com/en/docs/script/interpolation-and-thresholds/#hysteresis).

Explanation: The HYSTERESIS function ensures that the valve turns on when the level falls below 20 cm and turns off only when the level reaches 90 cm. Between these values, the valve remains in its last state, thus preventing frequent switching.
