Advertisement
joaopedros2

ESP Chicken Coop Doors

Sep 3rd, 2024 (edited)
289
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
YAML 4.46 KB | Help | 0 0
  1. substitutions:
  2.   name: esp-chicken-coop-doors
  3.   friendly_name: ESP Chicken Coop Doors
  4.   comment: Controls coop doors using sun position
  5.   door_cycle_time: 120s
  6.  
  7. packages:
  8.   base: !include common/base.yaml
  9.   wifi: !include common/wifi.yaml
  10.   esp32: !include common/devices/esp32_az-delivery-devkit-v4.yaml
  11.  
  12. external_components:
  13.   - source:
  14.       type: git
  15.       url: https://github.com/trombik/esphome-component-ds1302
  16.     components:
  17.      - ds1302
  18.  
  19. ota:
  20. api:
  21. esphome:
  22.   on_boot:
  23.     - priority: 600
  24.       then:
  25.         - delay: 10s
  26.         - if:
  27.             condition:
  28.               lambda: |-
  29.                 auto now = id(ds1302_time).now();
  30.                 auto timeopen = id(time_open_test).state_as_esptime();
  31.                 auto timeclose = id(time_close_test).state_as_esptime();
  32.  
  33.                 return (now.hour > timeopen.hour || (now.hour == timeopen.hour && now.minute > timeopen.minute)) && (now.hour < timeclose.hour || (now.hour == timeclose.hour && now.minute < timeclose.minute));
  34.  
  35.             then:
  36.               - switch.turn_on: door_test_debug
  37.             else:
  38.               - switch.turn_off: door_test_debug
  39.  
  40. datetime:
  41.   - platform: template
  42.     id: time_open_test
  43.     type: time
  44.     name: "Time Open - TEST"
  45.     icon: mdi:sort-clock-descending
  46.     optimistic: yes
  47.     initial_value: "08:30:00"
  48.     restore_value: true
  49.     on_time:
  50.       then:
  51.         - switch.turn_on: door_test_debug
  52.  
  53.   - platform: template
  54.     id: time_close_test
  55.     type: time
  56.     name: "Time Close - TEST"
  57.     icon: mdi:sort-clock-ascending
  58.     optimistic: yes
  59.     initial_value: "21:30:00"
  60.     restore_value: true
  61.     on_time:
  62.       then:
  63.         - switch.turn_off: door_test_debug
  64.  
  65. sun:
  66.   latitude: xx.xxx
  67.   longitude: xx.xxx
  68.  
  69.   on_sunrise:
  70.     - elevation: 20°
  71.       then:
  72.         - delay: 5min
  73.         - lambda: |-
  74.             if (id(sunrise_schedule).state) {
  75.               // Get the current time as ESPTime
  76.               auto now = id(ds1302_time).now();
  77.  
  78.               // Calculate the time 5 minutes earlier
  79.               int hour = now.hour;
  80.               int minute = now.minute;
  81.               int second = now.second;
  82.  
  83.               // Subtract 5 minutes
  84.               minute -= 5;
  85.               if (minute < 0) {
  86.                 minute += 60;
  87.                 hour--;
  88.                 if (hour < 0) {
  89.                   hour += 24;
  90.                 }
  91.               }
  92.  
  93.               // Update the time_open_test to the adjusted time
  94.               auto call = id(time_open_test).make_call();
  95.               call.set_time(hour, minute, second);
  96.               call.perform();
  97.             }
  98.  
  99.   on_sunset:
  100.     - elevation: -10°
  101.       then:
  102.         - delay: 5min
  103.         - lambda: |-
  104.             if (id(sunset_schedule).state) {
  105.               // Get the current time as ESPTime
  106.               auto now = id(ds1302_time).now();
  107.  
  108.               // Calculate the time 5 minutes earlier
  109.               int hour = now.hour;
  110.               int minute = now.minute;
  111.               int second = now.second;
  112.  
  113.               // Subtract 5 minutes
  114.               minute -= 5;
  115.               if (minute < 0) {
  116.                 minute += 60;
  117.                 hour--;
  118.                 if (hour < 0) {
  119.                   hour += 24;
  120.                 }
  121.               }
  122.  
  123.               // Update the time_close_test to the adjusted time
  124.               auto call = id(time_close_test).make_call();
  125.               call.set_time(hour, minute, second);
  126.               call.perform();
  127.             }
  128.  
  129. sensor:
  130.   - platform: sun
  131.     name: Sun Elevation
  132.     type: elevation
  133.  
  134. text_sensor:
  135.   - platform: sun
  136.     name: "Next Sunrise +20°"
  137.     id: next_sunrise_plus_20
  138.     type: sunrise
  139.     elevation: +20.0°
  140.  
  141.   - platform: sun
  142.     name: "Next Sunrise"
  143.     id: next_sunrise_normal
  144.     type: sunrise
  145.  
  146.   - platform: sun
  147.     name: Next Sunset
  148.     id: next_sunset_normal
  149.     type: sunset
  150.  
  151.   - platform: sun
  152.     name: "Next Sunset -10°"
  153.     id: next_sunset_minus_10
  154.     type: sunset
  155.     elevation: -10.0°
  156.  
  157. time:
  158.   - platform: ds1302
  159.     id: ds1302_time
  160.     cs_pin: GPIO25
  161.     dio_pin: GPIO26
  162.     clk_pin: GPIO27
  163.     update_interval: never
  164.  
  165.   - platform: sntp
  166.     id: sntp_time
  167.     on_time_sync:
  168.       then:
  169.         ds1302.write_time:
  170. switch:
  171.   - platform: template
  172.     name: "Door TEST (ON/OPEN OFF/CLOSE) Debug"
  173.     id: door_test_debug
  174.     icon: mdi:window-shutter-cog
  175.     optimistic: true
  176.     restore_mode: RESTORE_DEFAULT_OFF
  177.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement