switch:
- platform: gpio
pin: GPIO16
name: "Water pump"
id: water_pump
on_turn_on:
then:
- if:
condition:
- binary_sensor.is_on: water_level_ok
then:
- light.turn_on: blue_led
else:
- delay: 1s
- switch.turn_off: water_pump
on_turn_off:
then:
- if:
condition:
- binary_sensor.is_on: water_level_ok
then:
- light.turn_off: blue_led

button:
- platform: template
name: Start Watering
id: button_start
icon: "mdi:power-on"
on_press:
then:
- if:
condition:
- binary_sensor.is_on: water_level_ok
then:
- switch.turn_on: water_pump
- delay: !lambda "return id(watering_time).state*60*1000;"
- switch.turn_off: water_pump
else:
- switch.turn_off: water_pump

- platform: template
name: Stop Watering
id: button_stop
icon: "mdi:power-off"
on_press:
then:
- switch.turn_off: water_pump


number:
- platform: template
name: Full Distance
icon: mdi:arrow-collapse-vertical
entity_category: config
id: full_distance_m
min_value: 0.01
max_value: 1.50
initial_value: 0.1
optimistic: true
step: 0.01
restore_value: true
unit_of_measurement: meters
mode: box

- platform: template
name: Empty Distance
icon: mdi:arrow-expand-vertical
entity_category: config
id: empty_distance_m
min_value: 0.01
max_value: 1.50
initial_value: 0.1
optimistic: true
step: 0.01
restore_value: true
unit_of_measurement: meters
mode: box

- platform: template
name: Safe Level Distance
icon: mdi:arrow-expand-vertical
entity_category: config
id: safe_distance_m
min_value: 0.01
max_value: 1.50
initial_value: 0.1
optimistic: true
step: 0.01
restore_value: true
unit_of_measurement: meters
mode: box

- platform: template
name: Watering Time
icon: mdi:clock-edit
id: watering_time
min_value: 1
max_value: 60
initial_value: 10
optimistic: true
step: 1
restore_value: true
unit_of_measurement: minutes
mode: box

output:
- platform: gpio
pin: GPIO26
id: red_led_output
- platform: gpio
pin: GPIO23
id: yellow_led_1_output
- platform: gpio
pin: GPIO18
id: yellow_led_2_output
- platform: gpio
pin: GPIO19
id: green_led_output
- platform: gpio
pin: GPIO27
id: blue_led_output

light:
- platform: binary
name: "Red LED"
id: red_led
disabled_by_default: True
output: red_led_output
internal: False
restore_mode: RESTORE_DEFAULT_OFF
effects:
- strobe:
name: Blink
colors:
- state: true
brightness: 100%
duration: 500ms
- state: false
duration: 500ms
- platform: binary
name: "Yellow LED 1"
id: yellow_led_1
disabled_by_default: True
output: yellow_led_1_output
restore_mode: RESTORE_DEFAULT_OFF
- platform: binary
name: "Yellow LED 2"
id: yellow_led_2
disabled_by_default: True
output: yellow_led_2_output
restore_mode: RESTORE_DEFAULT_OFF
- platform: binary
name: "Green LED"
id: green_led
disabled_by_default: True
output: green_led_output
restore_mode: RESTORE_DEFAULT_OFF
- platform: binary
name: "Blue LED"
id: blue_led
disabled_by_default: True
output: blue_led_output
effects:
- strobe:
name: Double Blink
colors:
- state: true
brightness: 100%
duration: 150ms
- state: false
duration: 200ms
- state: true
brightness: 100%
duration: 150ms
- state: false
duration: 500ms

binary_sensor:
- platform: gpio
pin:
number: GPIO33
mode:
input: true
pullup: true
inverted: true
name: "Pushbutton"
id: start_button
filters:
- delayed_on: 100ms
on_press:
then:
- if:
condition:
- switch.is_off: water_pump
then:
- button.press: button_start
- delay: 1s
else:
- button.press: button_stop
- delay: 1s


- platform: template
name: "Water Level OK"
id: water_level_ok
lambda: |-
if (id(ultrasonic_sensor_distance).state < id(safe_distance_m).state) {
//there is enough water
return true;
} else {
//there is not enough water
return false;
}
on_state:
then:
- lambda: |-
if (x == false) {
id(blue_led).turn_on().set_effect("Double Blink").perform();
id(water_pump).turn_off();
} else {
//do nothing
}
on_press:
then:
- light.turn_off: blue_led

- platform: template
name: "Pump Status"
id: pump_status
device_class: running
lambda: |-
return id(water_pump).state;

sensor:
- platform: ultrasonic
trigger_pin: GPIO22
echo_pin: GPIO21
name: "Level Sensor Distance"
id: ultrasonic_sensor_distance
device_class: distance
update_interval: 1s
pulse_time: 10us
timeout: 20m
filters:
- sliding_window_moving_average:
window_size: 15
send_every: 15

- platform: template
name: "Water Level"
id: water_level
lambda: |-
return id(ultrasonic_sensor_distance).state;
update_interval: 1s
accuracy_decimals: 0
unit_of_measurement: "%"
filters:
- lambda: return ((100 - 0)/(id(full_distance_m).state - id(empty_distance_m).state)) * x + (100 - (((100 - 0)/(id(full_distance_m).state - id(empty_distance_m).state)) * id(full_distance_m).state));
- clamp:
min_value: 0
max_value: 100
ignore_out_of_range: false
on_value_range:
- below: 20
then:
- light.turn_off: green_led
- light.turn_off: yellow_led_1
- light.turn_off: yellow_led_2
- light.turn_on:
id: red_led
effect: Blink
- above: 20
then:
- light.turn_on:
id: red_led
effect: None
- below: 40
then:
- light.turn_off: green_led
- light.turn_off: yellow_led_1
- light.turn_off: yellow_led_2
- above: 40
then:
- light.turn_on:
id: red_led
effect: None
- light.turn_on: yellow_led_1
- below: 60
then:
- light.turn_off: yellow_led_2
- light.turn_off: green_led
- above: 60
then:
- light.turn_on:
id: red_led
effect: None
- light.turn_on: yellow_led_1
- light.turn_on: yellow_led_2
- below: 80
then:
- light.turn_off: green_led

- above: 80
then:
- light.turn_on:
id: red_led
effect: None
- light.turn_on: yellow_led_1
- light.turn_on: yellow_led_2
- light.turn_on: green_led