esphome:
  name: espfan
  platform: ESP8266
  board: d1_mini

# Enable logging
logger:

# Enable Home Assistant API
api:
  password: "feelhome123"

ota:
  password: "feelhome123"

wifi:
  ssid: "lauIOT"
  password: "superiot1"
  domain: .home.cabestan.be

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

captive_portal:

web_server:
  port: 80


time:
  - platform: sntp
    id: esptime
    timezone: "Europe/Brussels"
    on_time:
      # Every 5 minutes
      - seconds: /5
        then:
          - lambda: |-
              ESP_LOGD("custom", "Test sensor is : %.1f   [target:  %.1f] \n", id(tsensor).state, id(ttarget).state );
              
              if( id(tsensor).state < id(ttarget).state ) {
                auto call = id(my_fan).turn_off();
                call.perform();
                ESP_LOGD("custom", "Temp is low, so turn off");
                return;
                }
                
              auto call = id(my_fan).turn_on();
              call.perform();
              
              if( ( id(tsensor).state - id(ttarget).state ) * ( id(sensivity).state / 5 ) > 100 ) 
                { id(my_fan).speed = 100; }
              else 
                { id(my_fan).speed = ( id(tsensor).state - id(ttarget).state ) * ( id(sensivity).state / 5 ); }
              
              
              
              
              
              
fan:
  - platform: speed
    output: output_fan
    name: "Fan"
    restore_mode: ALWAYS_ON 
    id: my_fan


dallas:
  - pin: GPIO4
    update_interval: 2s


number:
  - platform: template
    name: "Sensibilité"
    optimistic: true
    min_value: 0
    max_value: 100
    initial_value: 30
    restore_value: true
    step: 1
    id: sensivity
    
  - platform: template
    name: "Target Temp"
    optimistic: true
    min_value: 10
    max_value: 50
    initial_value: 25
    restore_value: true
    step: 1
    id: ttarget

sensor:
  - platform: dallas
    address: 0x5f0000041a8f8628
    name: "Battery Charger Temp"
    id: tsensor
    filters:
      - median:
          window_size: 5
          send_every: 2
          send_first_at: 1


# sensor:
  # - platform: pulse_counter
    # pin:
      # number: D2
    # name: Fan Speed
    # id: fan_rpm
    # unit_of_measurement: 'RPM'
    # filters:
      # - multiply: 1
    # update_interval: 3s
    # count_mode:
      # rising_edge: INCREMENT
      # falling_edge: DISABLE

# Example output entry
output:
  - platform: esp8266_pwm
    id: output_fan
    pin: D1
    max_power: 100%
    frequency: 25000 Hz
