esphome:
  name: espwater
  platform: ESP32
  board: nodemcu-32s

logger:

api:
  #  password: "feelhome123"
  encryption:
    key: "pIj3rhf93C6iBLqk+rshFHkUed0bdJhtD1iE6pWkeUE="

ota:
  password: "5h4554h5x4th5x4d"

wifi:
  ssid: "lauIOT"
  password: "superiot1"
  domain: .home.cabestan.be
  ap:
    ssid: "Espwater Fallback Hotspot"
    password: "jTlMOFiFOzog"

captive_portal:

web_server:
  port: 80

mqtt:
  #  broker: hass20.home.cabestan.be
  broker: domotic.home.cabestan.be
    # username: espsalon
  username: espwater
    #  password: sala45224
  password: r5h4r65h4
  on_message:
    topic: espwater/set_total_drop
    qos: 0
    then: 
      lambda: |-
        // sprintf(id(lcd_line1),"|%s|%u|",x.c_str(),atol(x.c_str()));
        id(drops_absolute) = atol(x.c_str())*4;
        sprintf(id(lcd_line1),"Set Total:%lu",id(drops_absolute)/4);

globals:
  # - id: software_version
    # type: float
    # initial_value: '0.11'
  - id: drops_absolute
    type: unsigned long int
    restore_value: yes
    initial_value: '0'
  - id: drops_today
    type: int
    restore_value: no
    initial_value: '0'
  # - id: drops_1min
    # type: int
    # restore_value: no
    # initial_value: '0'
  - id: lcd_line1
    type: char[17]
    initial_value: "{' ',' ',' ','I','N','I','T','I','A','L','I','Z','E','\0'}"
    # it.strftime(0, 60, id(font2), TextAlign::BASELINE_LEFT, "%H:%M", id(esptime).now());
    restore_value: no
  - id: lcd_line2
    type: char[17]
    restore_value: no
    initial_value: "{'v','e','r',':',' ','0','.','4','1','\0'}"

time:
  - platform: sntp
    id: sntp_time
    timezone: "Europe/Brussels"
    on_time:
      - seconds: 0
        minutes: 0
        hours: 0
        then: 
          lambda: |-
            id(drops_today) = 0;    

switch:
  - platform: restart
    name: "ESP Water Restart"

  - platform: gpio
    id: relay_2
    name: "Relay 2"
    pin: 25
    inverted: true
    
  - platform: gpio
    id: co2_drop
    name: "Relay CO2"
    pin: 26
    inverted: true
    on_turn_on:
      - delay: 60ms
      - switch.turn_off: co2_drop
    
  - platform: template
    name: "Drop CO2"
    id: drop_co2
    turn_on_action:
      - switch.turn_on: co2_drop
      - switch.turn_off: drop_co2
      - sensor.template.publish:
          id: template_water_absolute
          state: !lambda 'return id(drops_absolute)/4;'
      - sensor.template.publish:
          id: template_water_today
          state: !lambda 'return id(drops_today)/4;'
    

display:
  - platform: lcd_gpio
    dimensions: 16x2
    data_pins:
      - 17
      - 16
      - 4
      - 0
    enable_pin: 5
    rs_pin: 18
    id: my_display
    lambda: |-
      it.printf(0,0,"%s",id(lcd_line1));
      it.printf(0,1,"%s",id(lcd_line2));     
    update_interval: 1s
 
# sensor:
  # - platform: rotary_encoder
    # name: "Rotary Encoder"
    # pin_a:
      # number: 5
      # pullup: true
    # pin_b: 
      # number: 7
      # pullup: true

sensor:
  - platform: template
    id: template_water_absolute
    name: "Water Total Liters"
    lambda: |-
      return id(drops_absolute)/4;
    update_interval: 60s
  - platform: template
    id: template_water_today
    name: "Water Today Liters"
    lambda: |-
      return id(drops_today)/4;
    update_interval: 60s  
    state_class: measurement
    #last_reset: 1970-01-01T00:00:00+00:00

binary_sensor:
  - platform: gpio
    name: "rotary Left"
    id: rot_left
    pin: 
      number: 12
      mode:
        input: true
        pullup: true
    filters:
      - delayed_on:  5ms
      - delayed_off: 5ms
  - platform: gpio
    name: "rotary Right"
    id: rot_right
    pin: 
      number: 14
      mode:
        input: true
        pullup: true
    filters:
      - delayed_on:  5ms
      - delayed_off: 5ms
  - platform: gpio
    name: "rotary Push Button"
    id: rot_push
    pin: 
      number: 27
      mode:
        input: true
        pullup: true
    filters:
      - delayed_on:  20ms
      - delayed_off: 100ms
  - platform: gpio
    name: "push Button"
    id: push_button
    pin: 
      number: 13
      mode:
        input: true
        pullup: true
    filters:
      - delayed_on:  20ms
      - delayed_off: 100ms
    on_press:
      - switch.turn_on: drop_co2
  - platform: gpio
    name: "Water Hall Sensor"
    id: water_relay
    pin: 34
    filters:
      - delayed_on:  2ms
      - delayed_off: 50ms
    on_press:
      - switch.turn_on: drop_co2
      - lambda: |-
          id(drops_absolute) += 1;
          id(drops_today)    += 1;
          sprintf(id(lcd_line1),"%s%10.1f","Total:",float(id(drops_absolute)*0.25));
          sprintf(id(lcd_line2),"%s%10.1f","Today:",float(id(drops_today)*0.25));


      
