Advertisement
gio_aggiustatutto

Code to be added below the one provided by ESPHome

Jun 8th, 2025 (edited)
442
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
YAML 7.59 KB | None | 0 0
  1. switch:
  2.   - platform: gpio
  3.     pin: GPIO16
  4.     name: "Water pump"
  5.     id: water_pump
  6.     on_turn_on:
  7.       then:
  8.         - if:
  9.             condition:
  10.               - binary_sensor.is_on: water_level_ok
  11.             then:
  12.               - light.turn_on: blue_led
  13.             else:
  14.               - delay: 1s
  15.               - switch.turn_off: water_pump      
  16.     on_turn_off:
  17.       then:
  18.         - if:
  19.             condition:
  20.               - binary_sensor.is_on: water_level_ok
  21.             then:
  22.               - light.turn_off: blue_led
  23.  
  24. button:
  25.   - platform: template
  26.     name: Start Watering
  27.     id: button_start
  28.     icon: "mdi:power-on"
  29.     on_press:
  30.       then:
  31.         - if:
  32.             condition:
  33.               - binary_sensor.is_on: water_level_ok
  34.             then:
  35.               - switch.turn_on: water_pump
  36.               - delay: !lambda "return id(watering_time).state*60*1000;"
  37.               - switch.turn_off: water_pump
  38.             else:
  39.               - switch.turn_off: water_pump
  40.  
  41.   - platform: template
  42.     name: Stop Watering
  43.     id: button_stop
  44.     icon: "mdi:power-off"
  45.     on_press:
  46.       then:
  47.         - switch.turn_off: water_pump
  48.      
  49.  
  50. number:
  51.   - platform: template
  52.     name: Full Distance
  53.     icon: mdi:arrow-collapse-vertical
  54.     entity_category: config
  55.     id: full_distance_m
  56.     min_value: 0.01
  57.     max_value: 1.50
  58.     initial_value: 0.1
  59.     optimistic: true
  60.     step: 0.01
  61.     restore_value: true
  62.     unit_of_measurement: meters
  63.     mode: box
  64.  
  65.   - platform: template
  66.     name: Empty Distance
  67.     icon: mdi:arrow-expand-vertical
  68.     entity_category: config
  69.     id: empty_distance_m
  70.     min_value: 0.01
  71.     max_value: 1.50
  72.     initial_value: 0.1
  73.     optimistic: true
  74.     step: 0.01
  75.     restore_value: true
  76.     unit_of_measurement: meters
  77.     mode: box
  78.  
  79.   - platform: template
  80.     name: Safe Level Distance
  81.     icon: mdi:arrow-expand-vertical
  82.     entity_category: config
  83.     id: safe_distance_m
  84.     min_value: 0.01
  85.     max_value: 1.50
  86.     initial_value: 0.1
  87.     optimistic: true
  88.     step: 0.01
  89.     restore_value: true
  90.     unit_of_measurement: meters
  91.     mode: box
  92.  
  93.   - platform: template
  94.     name: Watering Time
  95.     icon: mdi:clock-edit
  96.     id: watering_time
  97.     min_value: 1
  98.     max_value: 60
  99.     initial_value: 10
  100.     optimistic: true
  101.     step: 1
  102.     restore_value: true
  103.     unit_of_measurement: minutes
  104.     mode: box
  105.  
  106. output:
  107.   - platform: gpio
  108.     pin: GPIO26
  109.     id: red_led_output
  110.   - platform: gpio
  111.     pin: GPIO23
  112.     id: yellow_led_1_output
  113.   - platform: gpio
  114.     pin: GPIO18
  115.     id: yellow_led_2_output
  116.   - platform: gpio
  117.     pin: GPIO19
  118.     id: green_led_output
  119.   - platform: gpio
  120.     pin: GPIO27
  121.     id: blue_led_output
  122.  
  123. light:
  124.   - platform: binary
  125.     name: "Red LED"
  126.     id: red_led
  127.     disabled_by_default: True
  128.     output: red_led_output
  129.     internal: False
  130.     restore_mode: RESTORE_DEFAULT_OFF
  131.     effects:
  132.       - strobe:
  133.           name: Blink
  134.           colors:
  135.             - state: true
  136.               brightness: 100%
  137.               duration: 500ms
  138.             - state: false
  139.               duration: 500ms
  140.   - platform: binary
  141.     name: "Yellow LED 1"
  142.     id: yellow_led_1
  143.     disabled_by_default: True
  144.     output: yellow_led_1_output
  145.     restore_mode: RESTORE_DEFAULT_OFF
  146.   - platform: binary
  147.     name: "Yellow LED 2"
  148.     id: yellow_led_2
  149.     disabled_by_default: True
  150.     output: yellow_led_2_output
  151.     restore_mode: RESTORE_DEFAULT_OFF
  152.   - platform: binary
  153.     name: "Green LED"
  154.     id: green_led
  155.     disabled_by_default: True
  156.     output: green_led_output
  157.     restore_mode: RESTORE_DEFAULT_OFF
  158.   - platform: binary
  159.     name: "Blue LED"
  160.     id: blue_led
  161.     disabled_by_default: True
  162.     output: blue_led_output
  163.     effects:
  164.       - strobe:
  165.           name: Double Blink
  166.           colors:
  167.             - state: true
  168.               brightness: 100%
  169.               duration: 150ms
  170.             - state: false
  171.               duration: 200ms
  172.             - state: true
  173.               brightness: 100%
  174.               duration: 150ms
  175.             - state: false
  176.               duration: 500ms
  177.  
  178. binary_sensor:
  179.   - platform: gpio
  180.     pin:
  181.       number: GPIO33
  182.       mode:
  183.         input: true
  184.         pullup: true
  185.       inverted: true
  186.     name: "Pushbutton"
  187.     id: start_button
  188.     filters:
  189.       - delayed_on: 100ms
  190.     on_press:
  191.       then:
  192.         - if:
  193.             condition:
  194.               - switch.is_off: water_pump
  195.             then:
  196.               - button.press: button_start
  197.               - delay: 1s
  198.             else:
  199.               - button.press: button_stop
  200.               - delay: 1s
  201.        
  202.  
  203.   - platform: template
  204.     name: "Water Level OK"
  205.     id: water_level_ok
  206.     lambda: |-
  207.       if (id(ultrasonic_sensor_distance).state < id(safe_distance_m).state) {
  208.         //there is enough water
  209.         return true;
  210.       } else {
  211.         //there is not enough water
  212.         return false;
  213.       }
  214.     on_state:
  215.       then:
  216.         - lambda: |-
  217.             if (x == false) {
  218.               id(blue_led).turn_on().set_effect("Double Blink").perform();
  219.               id(water_pump).turn_off();
  220.             } else {
  221.               //do nothing
  222.             }
  223.     on_press:
  224.       then:
  225.         - light.turn_off: blue_led
  226.        
  227.   - platform: template
  228.     name: "Pump Status"
  229.     id: pump_status
  230.     device_class: running
  231.     lambda: |-
  232.       return id(water_pump).state;
  233.  
  234. sensor:
  235.   - platform: ultrasonic
  236.     trigger_pin: GPIO22
  237.     echo_pin: GPIO21
  238.     name: "Level Sensor Distance"
  239.     id: ultrasonic_sensor_distance
  240.     device_class: distance
  241.     update_interval: 1s
  242.     pulse_time: 10us
  243.     timeout: 20m
  244.     filters:
  245.       - sliding_window_moving_average:
  246.           window_size: 15
  247.           send_every: 15
  248.  
  249.   - platform: template
  250.     name: "Water Level"
  251.     id: water_level
  252.     lambda: |-
  253.         return id(ultrasonic_sensor_distance).state;
  254.     update_interval: 1s
  255.     accuracy_decimals: 0
  256.     unit_of_measurement: "%"
  257.     filters:
  258.       - 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));
  259.       - clamp:
  260.           min_value: 0
  261.           max_value: 100
  262.           ignore_out_of_range: false
  263.     on_value_range:
  264.       - below: 20
  265.         then:
  266.           - light.turn_off: green_led
  267.           - light.turn_off: yellow_led_1
  268.           - light.turn_off: yellow_led_2
  269.           - light.turn_on:
  270.               id: red_led
  271.               effect: Blink          
  272.       - above: 20
  273.         then:
  274.           - light.turn_on:
  275.               id: red_led
  276.               effect: None
  277.       - below: 40
  278.         then:
  279.           - light.turn_off: green_led
  280.           - light.turn_off: yellow_led_1
  281.           - light.turn_off: yellow_led_2
  282.       - above: 40
  283.         then:
  284.           - light.turn_on:
  285.               id: red_led
  286.               effect: None
  287.           - light.turn_on: yellow_led_1
  288.       - below: 60
  289.         then:
  290.           - light.turn_off: yellow_led_2
  291.           - light.turn_off: green_led
  292.       - above: 60
  293.         then:
  294.           - light.turn_on:
  295.               id: red_led
  296.               effect: None
  297.           - light.turn_on: yellow_led_1
  298.           - light.turn_on: yellow_led_2
  299.       - below: 80
  300.         then:
  301.           - light.turn_off: green_led
  302.  
  303.       - above: 80
  304.         then:
  305.           - light.turn_on:
  306.               id: red_led
  307.               effect: None
  308.           - light.turn_on: yellow_led_1
  309.           - light.turn_on: yellow_led_2
  310.           - light.turn_on: green_led
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement