
# Avec ESP32 microusb :
#      0.223 en ACTIF (10s)
#      0.012 en SLEEP (10s)

# Avec ESP32 usb-c :
#      0.193 en ACTIF
#      0.0


esphome:
  name: espdht22
  #platform: ESP32
  platform: esp32
  #board: esp-wrover-kit
  #board: nodemcu-32s
  board: lolin_d32
  
  on_boot:
    then:
      - logger.log: "Coucou !!!"
    # then:
    # - delay: 5s
    # - deep_sleep.enter:
        # id: deep_sleep_1
        # sleep_duration: 5sec
   
deep_sleep:
  run_duration: 10sec
  sleep_duration: 10sec
  id: deep_sleep_1 
# Enable logging
logger:
  level: DEBUG
# Enable Home Assistant API
api:
  password: !secret api_secret

ota:
  password: !secret ota_key

wifi:
  ssid:     !secret wireless_ssid
  password: !secret wireless_key
  domain:   !secret wireless_domain
  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "TEST22"
    password: "test2222"

captive_portal:

web_server:
  port: 80

mqtt:
  broker: domotic.home.cabestan.be
  username: esptest
  password: esptest

sensor:
  - platform: dht
    pin: GPIO23
    #pin: GPIO12
    
    temperature:
      name: "Test Temperature"
    humidity:
      name: "Test Humidity"
    update_interval: 5s     
          
  - platform: adc
    id: raw_adc
    pin: GPIO34
    #pin: GPIO4
    name: "Battery Raw"
    update_interval: 1s
    filters:
      - sliding_window_moving_average:
          window_size: 18
          send_every: 10
    on_value:
      then:
        - sensor.template.publish:
            id: battery_voltage
            state: !lambda 'return x;'
        - sensor.template.publish:
            id: battery_percent
            state: !lambda 'return x;'

  - platform: template
    id: battery_voltage
    name: "test_battery_voltage"
    unit_of_measurement: 'V'
    device_class: "voltage"
    
    accuracy_decimals: 2
    filters:
      - multiply: 5.5
    
  - platform: template
    id: battery_percent
    name: "test_battery_percent"
    unit_of_measurement: '%'
    device_class: "battery"
    
    accuracy_decimals: 0
    filters:
      - calibrate_polynomial:
         degree: 4
         datapoints:
          # Map 0.0 (from sensor) to 0.0 (true value)
          - 0.50909 -> 0.0
          - 0.58181 -> 7.6
          - 0.61818 -> 15.3
          - 0.63636 -> 23.0
          - 0.64909 -> 30.7
          - 0.65818 -> 38.4
          - 0.66545 -> 46.1
          - 0.68181 -> 53.8
          - 0.69090 -> 61.5
          - 0.70909 -> 69.2
          - 0.72181 -> 76.9
          - 0.73272 -> 84.6
          - 0.74181 -> 92.3
          - 0.75454 -> 100


