Files
esphome-config/irrigation.yaml
T

454 lines
11 KiB
YAML

# Forked from: https://github.com/bruxy70/Irrigation-with-display
# MIT License: https://github.com/brianhanifin/Irrigation-with-display/blob/master/LICENSE
#
# Credit: @bruxy70 thank you for the significant head start!
# Personal project goals: https://github.com/brianhanifin/Home-Assistant-Config/issues/37
#
substitutions:
project: Irrigation Controller
id: irrigation
# Duration in seconds
duration1: "3600" #Zone #1 (pool filler): 60 minutes
duration2: "300" #Zone #2 (drip system): 5 minutes
duration3: "0" #Zone #3 (disabled)
duration4: "0" #Zone #4 (disabled)
duration_default: "300" #5 minutes
<<: !include common/substitutions/gpio/sonoff4chpror2.yaml
esphome:
name: $id
platform: ESP8266
board: esp01_1m
<<: !include common/system/status_led_api.yaml
<<: !include common/wifi.yaml
<<: !include common/ota.yaml
<<: !include common/logger_debug.yaml
# Enable Home Assistant API
api:
services:
- service: set_time_1
variables:
time: int
then:
globals.set:
id: time1
value: !lambda "return time;"
- service: set_time_2
variables:
time: int
then:
globals.set:
id: time2
value: !lambda "return time;"
- service: set_time_3
variables:
time: int
then:
globals.set:
id: time3
value: !lambda "return time;"
- service: set_time_4
variables:
time: int
then:
globals.set:
id: time4
value: !lambda "return time;"
globals:
# Irrigation time elapsed
- id: time1
type: int
restore_value: no
initial_value: $duration_default
- id: time2
type: int
restore_value: no
initial_value: $duration_default
- id: time3
type: int
restore_value: no
initial_value: $duration_default
- id: time4
type: int
restore_value: no
initial_value: $duration_default
# Irrigation time remaining
- id: remaining_time1
type: int
restore_value: yes
#initial_value: "0"
- id: remaining_time2
type: int
restore_value: yes
#initial_value: "0"
- id: remaining_time3
type: int
restore_value: yes
#initial_value: "0"
- id: remaining_time4
type: int
restore_value: no
# Store previous values to verify change.
- id: remaining_time1_previous
type: int
restore_value: no
initial_value: "0"
- id: remaining_time2_previous
type: int
restore_value: no
initial_value: "0"
- id: remaining_time3_previous
type: int
restore_value: no
initial_value: "0"
- id: remaining_time4_previous
type: int
restore_value: no
initial_value: "0"
# Common housekeeping components.
output:
<<: !include common/output/status_led.yaml
light:
<<: !include common/light/status_led.yaml
binary_sensor:
- !include common/binary_sensors/status.yaml
# Buttons along the left side of the unit (R1, R2, R3, R4).
- platform: gpio
id: key1
pin:
number: $button1_gpio
mode: INPUT_PULLUP
inverted: True
filters:
- delayed_on: 100ms
- delayed_off: 100ms
on_click:
min_length: 50ms
max_length: 350ms
then:
- switch.toggle: zone1
- platform: gpio
id: key2
pin:
number: $button2_gpio
mode: INPUT_PULLUP
inverted: True
filters:
- delayed_on: 100ms
- delayed_off: 100ms
on_click:
min_length: 50ms
max_length: 350ms
then:
- switch.toggle: zone2
- platform: gpio
id: key3
pin:
number: $button3_gpio
mode: INPUT_PULLUP
inverted: True
filters:
- delayed_on: 100ms
- delayed_off: 100ms
on_click:
min_length: 100ms
max_length: 500ms
then:
- switch.toggle: zone3
- platform: gpio
id: key4
pin:
number: $button4_gpio
mode: INPUT_PULLUP
inverted: True
filters:
- delayed_on: 100ms
- delayed_off: 100ms
on_click:
min_length: 100ms
max_length: 500ms
then:
- switch.toggle: zone4
switch:
- !include common/switches/restart.yaml
# Virtual Zone Switches which
- platform: template
name: $project Zone 1
optimistic: true
id: zone1
turn_on_action:
# Turn on if not disabled.
if:
condition:
lambda: return $duration1 > 0;
then:
- switch.turn_on: relay1
turn_off_action:
- switch.turn_off: relay1
lambda: return id(relay1).state;
- platform: template
name: $project Zone 2
optimistic: true
id: zone2
turn_on_action:
# Turn on if not disabled.
if:
condition:
lambda: return $duration2 > 0;
then:
- switch.turn_on: relay2
turn_off_action:
- switch.turn_off: relay2
lambda: return id(relay2).state;
- platform: template
name: $project Zone 3
optimistic: true
id: zone3
turn_on_action:
# Turn on if not disabled.
if:
condition:
lambda: return $duration3 > 0;
then:
- switch.turn_on: relay3
turn_off_action:
- switch.turn_off: relay3
lambda: return id(relay3).state;
- platform: template
name: $project Zone 4
optimistic: true
id: zone4
turn_on_action:
# Turn on if not disabled.
if:
condition:
lambda: return $duration4 > 0;
then:
- switch.turn_on: relay4
turn_off_action:
- switch.turn_off: relay4
lambda: return id(relay4).state;
# Relays which trigger solenoids
- platform: gpio
id: relay1
pin: $relay1_gpio
on_turn_on:
then:
- sensor.template.publish:
id: ${id}_zone1_remaining
state: !lambda 'return id(time1)/60;'
- globals.set:
id: time1
value: $duration1
- globals.set:
id: remaining_time1
value: !lambda "return id(time1);"
on_turn_off:
- sensor.template.publish:
id: ${id}_zone1_remaining
state: "0"
- platform: gpio
id: relay2
pin: $relay2_gpio
on_turn_on:
then:
- sensor.template.publish:
id: ${id}_zone2_remaining
state: !lambda 'return id(time2)/60;'
- globals.set:
id: time2
value: $duration2
- globals.set:
id: remaining_time2
value: !lambda "return id(time2);"
on_turn_off:
- sensor.template.publish:
id: ${id}_zone2_remaining
state: "0"
- platform: gpio
id: relay3
pin: $relay3_gpio
on_turn_on:
- sensor.template.publish:
id: ${id}_zone3_remaining
state: !lambda 'return id(time3)/60;'
- globals.set:
id: time3
value: $duration3
- globals.set:
id: remaining_time3
value: !lambda "return id(time3);"
on_turn_off:
- sensor.template.publish:
id: ${id}_zone3_remaining
state: "0"
- platform: gpio
id: relay4
pin: $relay4_gpio
on_turn_on:
- sensor.template.publish:
id: ${id}_zone4_remaining
state: !lambda 'return id(time4)/60;'
- globals.set:
id: time4
value: $duration4
- globals.set:
id: remaining_time4
value: !lambda "return id(time4);"
on_turn_off:
- sensor.template.publish:
id: ${id}_zone4_remaining
state: "0"
sensor:
- !include common/sensors/uptime.yaml
- !include common/sensors/wifi_signal.yaml
# Countdown sensors.
- platform: template
name: $project Zone 1 Time Remaining
id: ${id}_zone1_remaining
lambda: "return 0;"
accuracy_decimals: 0
unit_of_measurement: minutes
icon: mdi:timer
filters:
- throttle: 15s
on_value:
then:
- if:
condition:
lambda: 'return id(remaining_time1) == 0;'
then:
- switch.turn_off: relay1
- platform: template
name: $project Zone 2 Time Remaining
id: ${id}_zone2_remaining
lambda: "return 0;"
accuracy_decimals: 0
unit_of_measurement: minutes
icon: mdi:timer
filters:
- throttle: 15s
on_value:
then:
- if:
condition:
lambda: 'return id(remaining_time2) == 0;'
then:
- switch.turn_off: relay2
- platform: template
name: $project Zone 3 Time Remaining
id: ${id}_zone3_remaining
lambda: "return 0;"
accuracy_decimals: 0
unit_of_measurement: minutes
icon: mdi:timer
filters:
- throttle: 15s
on_value:
then:
- if:
condition:
lambda: 'return id(remaining_time3) == 0;'
then:
- switch.turn_off: relay3
- platform: template
name: $project Zone 4 Time Remaining
id: ${id}_zone4_remaining
lambda: "return 0;"
accuracy_decimals: 0
unit_of_measurement: minutes
icon: mdi:timer
filters:
- throttle: 15s
on_value:
then:
- if:
condition:
lambda: 'return id(remaining_time4) == 0;'
then:
- switch.turn_off: relay4
interval:
- interval: 5s
then:
- lambda: |-
id(remaining_time1_previous) = id(remaining_time1);
if(id(relay1).state) {
id(remaining_time1)=id(remaining_time1)-5;
if(id(remaining_time1)<=0){
id(relay1).turn_off();
id(remaining_time1) = 0;
}
}
if (id(remaining_time1_previous) != id(remaining_time1)) {
id(${id}_zone1_remaining).publish_state(id(remaining_time1)/60);
}
id(remaining_time2_previous) = id(remaining_time2);
if(id(relay2).state) {
id(remaining_time2)=id(remaining_time2)-5;
if(id(remaining_time2)<=0){
id(relay2).turn_off();
id(remaining_time2) = 0;
}
}
if (id(remaining_time2_previous) != id(remaining_time2)) {
id(${id}_zone2_remaining).publish_state(id(remaining_time2)/60);
}
id(remaining_time3_previous) = id(remaining_time3);
if(id(relay3).state) {
id(remaining_time3)=id(remaining_time3)-5;
if(id(remaining_time3)<=0){
id(relay3).turn_off();
id(remaining_time3) = 0;
}
}
if (id(remaining_time3_previous) != id(remaining_time3)) {
id(${id}_zone3_remaining).publish_state(id(remaining_time3)/60);
}
id(remaining_time4_previous) = id(remaining_time4);
if(id(relay4).state) {
id(remaining_time4)=id(remaining_time4)-5;
if(id(remaining_time4)<=0){
id(relay4).turn_off();
id(remaining_time4) = 0;
}
}
if (id(remaining_time4_previous) != id(remaining_time4)) {
id(${id}_zone4_remaining).publish_state(id(remaining_time4)/60);
}