esphome:
  name: balkonflower
  platform: ESP32
  board: esp-wrover-kit

wifi:
  ssid: "yours"
  password: "yours"

  # Optional manual IP
  manual_ip:
    static_ip: yours
    gateway: yours
    subnet: yours

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "Balkonflower Fallback Hotspot"
    password: "yours"

captive_portal:

# Enable logging
logger:

# Enable Home Assistant API
api:

ota:

web_server:
  port: 80
  
  
    
    
script:
- id: fertilize_2ml
  mode: single     
  then:
    - stepper.report_position:
        id: my_stepper
        position: 0
    - stepper.set_target:
        id: my_stepper
        target: -2800 
        
    - delay: 6 sec  
    - switch.turn_off: bcy_fertilize_now  
    - switch.turn_off: bcy_fertilize_onwater  
        

- id: balkon_giesssequenz
  mode: single
  then:
    - switch.turn_on: bcy_irrigation_relay  
    - switch.turn_on: bcy_state_led  
    - if:
        condition:
          lambda: 'return id(bcy_fertilize_onwater).state;'
        then:
          - script.execute: fertilize_2ml
          - switch.turn_off: bcy_fertilize_onwater  
    - delay: 10 sec  
    - switch.turn_off: bcy_irrigation_relay  
    - delay: 20 sec  
    
    
    
    - switch.turn_on: bcy_irrigation_relay  
    - switch.turn_on: bcy_state_led  
    - delay: 10 sec  
    - switch.turn_off: bcy_irrigation_relay  
    - delay: 20 sec  
    
    - switch.turn_on: bcy_irrigation_relay  
    - switch.turn_on: bcy_state_led  
    - delay: 10 sec  
    - switch.turn_off: bcy_irrigation_relay  
    - switch.turn_off: bcy_irrigate_3x 
    - switch.turn_off: bcy_state_led   
    


    

dallas:
  - pin: 14


binary_sensor:
  - platform: gpio
    pin:
      number: 13
      mode: INPUT_PULLUP
    name: "Balkon Taster"
    id: "bcy_pushbutton"
    filters:
      - delayed_off: 40ms
      - delayed_on: 40ms
    on_release:
        then:
          - script.stop: balkon_giesssequenz
          - switch.toggle: bcy_irrigation_relay
          - if: # nur abschalten wenns läuft, sonst geht relay nicht an
              condition:
                lambda: 'return id(bcy_irrigate_3x).state;'
              then:
                - switch.turn_off: bcy_irrigate_3x



# Individual outputs
switch:
  - platform: template
    name: "Fertilize NOW"
    id: "bcy_fertilize_now"
    icon: "mdi:sprout"
    turn_on_action:
      - script.execute: fertilize_2ml

    turn_off_action:
      - script.stop: fertilize_2ml


  - platform: template
    name: "Fertilize while watering"
    id: "bcy_fertilize_onwater"
    icon: "mdi:sprout"
    turn_on_action:
      - lambda: 'id(bcy_fertilize_onwater).publish_state(true);'
    turn_off_action:
      - lambda: 'id(bcy_fertilize_onwater).publish_state(false);'
    

  - platform: template
    name: "Iriigate 3x"
    id: "bcy_irrigate_3x"
    icon: "mdi:watering-can"  
    turn_on_action:
      - script.execute: balkon_giesssequenz  
      - lambda: 'id(bcy_irrigate_3x).publish_state(true);'
    turn_off_action:
      - script.stop: balkon_giesssequenz
      - lambda: 'id(bcy_irrigate_3x).publish_state(false);'
      - switch.turn_off: bcy_irrigation_relay  
      - switch.turn_off: bcy_state_led   
    
  - platform: gpio
    id: "bcy_irrigation_relay"
    name: "Balkon Bewesserung Pumpe"
    restore_mode: ALWAYS_OFF 
    pin:
      number: 12
      mode: OUTPUT
      #inverted: True
      
      
    
  - platform: gpio
    id: "bcy_state_led"
    name: "Balkon LED Status"
    restore_mode: ALWAYS_OFF 
    pin:
      number: 27
      mode: OUTPUT
    #  inverted: True



  - platform: gpio
    name: "Stepper ENA"
    id: "stepper_ena"
    pin:
      number: 23
      mode: OUTPUT
    
#  - platform: gpio
#    id: "stepper_sleep"
#    name: "Stepper SLEEP"
#    pin:
#      number: 17
#      mode: OUTPUT
    
  - platform: gpio
    id: "stepper_reset"
    name: "Stepper RESET"
    pin:
      number: 04
      mode: OUTPUT
      inverted: True





stepper:
  - platform: a4988
    id: my_stepper
    step_pin: 0
    dir_pin: 15
    max_speed: 1000 steps/s

    # Optional:
    sleep_pin: 
      number: 17
    acceleration: 500
    deceleration: inf



esp32_ble_tracker:
  scan_parameters:
    active: false

  # Individual sensors
sensor:
  - platform: dallas
    address: 0x110000065CEF0828
    name: "Balkon Wassertonne Wassertemp"#
    id: "bcy_water_temp"
    

  - platform: xiaomi_hhccjcy01
    mac_address: '80:ea:ca:89:0d:da'
    temperature:
      name: "Xiaomi Flower 4 Temperature"
    moisture:
      name: "Xiaomi Flower 4  Moisture"
    illuminance:
      name: "Xiaomi Flower 4  Illuminance"
    conductivity:
      name: "Xiaomi Flower 4  Soil Conductivity"
    battery_level:
      name: "Xiaomi Flower 4  Battery Level"
      
  - platform: xiaomi_hhccjcy01
    mac_address: '80:ea:ca:89:0e:22'
    temperature:
      name: "Xiaomi Flower 3 Temperature"
    moisture:
      name: "Xiaomi Flower 3  Moisture"
    illuminance:
      name: "Xiaomi Flower 3  Illuminance"
    conductivity:
      name: "Xiaomi Flower 3  Soil Conductivity"
    battery_level:
      name: "Xiaomi Flower 3  Battery Level"
      
      
      
      
time:
  - platform: homeassistant
    id: homeassistant_time
    # ...
    on_time:
      # Every 5 minutes
      - seconds: 0
        minutes: /5
        then:
        - logger.log: "jk timed Transmit"
        - lambda: |-
            if (id(bcy_irrigate_3x).state) {
              id(bcy_irrigate_3x).publish_state(true);
            } else {
              id(bcy_irrigate_3x).publish_state(false);
            }    
    
        - delay: 100ms
        - lambda: |-
            if (id(bcy_fertilize_onwater).state) {
              id(bcy_fertilize_onwater).publish_state(true);
            } else {
              id(bcy_fertilize_onwater).publish_state(false);
            }    
    
          