Advertisement
gio_aggiustatutto

Example code

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