---
substitutions:
  devicename: "nodemcu-06"
  id_name: nodemcu_06
  Device_Location: ESP Weather Station
  update_interval: !secret d1-update_interval
  ipaddress: 10.0.0.16
  unique_id: "nodemcu-06"
  deep_sleep_interval: !secret deep_sleep_interval

logger:
  level: WARN

esp8266:
  board: nodemcuv2

wifi:
  ssid: !secret mogwai_wifi_name
  password: !secret mogwai_wifi_password
  <<: !include common/network.yaml

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "$devicename Fallback Hotspot"
    password: !secret mogwai_ap_key

api:
  encryption:
    key: !secret mogwai_api_key

ota:
  platform: esphome
  password: !secret mogwai_ota_key

captive_portal:

esphome:
  name: $devicename
  friendly_name: "NodeMCU-06 - Weather Station"
  on_boot:
    then:
      - switch.turn_off: led_onboard1
      - switch.turn_off: led_onboard2

<<: !include common/nodemcu-led-onboard-both-mqtt.yaml

<<: !include common/time.yaml

<<: !include common/website.yaml

<<: !include common/wifi1.yaml

<<: !include common/uptime_formatted.yaml

switch:

  - <<: !include common/nodemcu-led-onboard1.yaml

  - <<: !include common/nodemcu-led-onboard2.yaml

  - <<: !include common/restart.yaml

interval:

  - <<: !include common/version-interval.yaml

  - <<: !include common/whatami.yaml

#  - interval: 15s
#    then:
#    - if:
#        condition:
#          - lambda: 'return id(homeassistant_time).now().hour < 22 && id(homeassistant_time).now().hour > 7;'
#        then:
#          - switch.toggle: led_onboard

#  - interval: 60s
#    then:
#      - mqtt.publish:
#          topic: /sensors/$devicename/rain
#          payload: "${friendly_name} Rain Bucket Connected to D2"
#      - mqtt.publish:
#          topic: /sensors/$devicename/windspeed
#          payload: "${friendly_name} Wind Speed Connected to D1"
#      - mqtt.publish:
#          topic: /sensors/$devicename/windspeed
#          payload: "${friendly_name} Wind Direction Connected to A0"

sensor:

  - <<: !include common/wifi2.yaml

  - <<: !include common/uptime.yaml

  - <<: !include common/free_space.yaml

  # Wind Speed

  - platform: pulse_counter
    pin: D1
    name: "Wind Speed"
    unit_of_measurement: 'm/s'
    filters:
      - multiply: 0.002

#  - platform: pulse_counter
#    pin:
#      number: D1
#      mode: INPUT_PULLUP
#    unit_of_measurement: 'm/s'
#    name: "${friendly_name} wind speed"
#    icon: 'mdi:weather-windy'
#    count_mode:
#      rising_edge: DISABLE
#      falling_edge: INCREMENT
#    internal_filter: 50us
#    update_interval: 60s
#    on_raw_value:
#      then:
#        - sensor.template.publish:
#            id: template_sens
#            state: !lambda "return x;"
#    # timeout: 5s
#    # rotations_per_sec = pulses / 2 / 60
#    # circ_m = 0.09 * 2 * 3.14 = 0.5652
#    # mps = 1.18 * circ_m * rotations_per_sec
#    # mps = 1.18 * 0.5652 / 2 / 60 = 0,0055578
#    filters:
#      - multiply: 0.005560619
#      - sliding_window_moving_average:
#          window_size: 2
#          send_every: 1

  - platform: template
    name: "Template Sensor"
    id: template_sens
    filters:
      - multiply: 16.764
      - sliding_window_moving_average:
          window_size: 60
          send_every: 1

  # Rain Cup

  - platform: pulse_counter
    pin:
      number: D2
      mode: INPUT_PULLUP
    unit_of_measurement: 'mm'
    name: "${friendly_name} rain gauge"
    icon: 'mdi:weather-rainy'
    id: rain_gauge
    # internal: True
    count_mode:
      rising_edge: DISABLE
      falling_edge: INCREMENT
    internal_filter: 13us
    update_interval: 60s
    filters:
      # Each 0.011" (0.2794mm) of rain causes one momentary contact closure
      - multiply: 0.2794
    accuracy_decimals: 4


  - platform: integration
    name: "${friendly_name} rainfall p/m"
    id: rain_per_min
    time_unit: min
    unit_of_measurement: 'mm'
    icon: 'mdi:weather-rainy'
    sensor: rain_gauge

  - platform: total_daily_energy
    name: "${friendly_name} total daily rain"
    power_id: rain_gauge
    unit_of_measurement: 'mm'
    icon: 'mdi:weather-rainy'
    # x60 To convert to aggregated rain amount
    filters:
      - multiply: 60

  - platform: adc
    id: source_sensor
    pin: A0
    name: ADC
    # attenuation: 11db
    internal: True
    update_interval: 60s
    accuracy_decimals: 1
    # filters:
    #  - multiply: 0.846153 # 3.9 -> 3.3V

  - platform: resistance
    sensor: source_sensor
    id: resistance_sensor
    configuration: DOWNSTREAM
    resistor: 10kOhm
    internal: True
    name: Resistance Sensor
    reference_voltage: 3.3V
    accuracy_decimals: 1
    filters:
      - median:
          window_size: 7
          send_every: 4
          send_first_at: 3
      - heartbeat: 30s
    on_value:
      - if:
          condition:
            sensor.in_range:
              id: resistance_sensor
              above: 15000
              below: 15500
          then:
            - text_sensor.template.publish:
                id: wind_dir_card
                state: "N"
            - sensor.template.publish:
                id: wind_heading
                state: 0.0
      - if:
          condition:
            sensor.in_range:
              id: resistance_sensor
              above: 5000
              below: 5500
          then:
            - text_sensor.template.publish:
                id: wind_dir_card
                state: "NE"
            - sensor.template.publish:
                id: wind_heading
                state: 45.0
      - if:
          condition:
            sensor.in_range:
              id: resistance_sensor
              above: 350
              below: 450
          then:
            - text_sensor.template.publish:
                id: wind_dir_card
                state: "E"
            - sensor.template.publish:
                id: wind_heading
                state: 90.0
      - if:
          condition:
            sensor.in_range:
              id: resistance_sensor
              above: 1200
              below: 1400
          then:
            - text_sensor.template.publish:
                id: wind_dir_card
                state: "SE"
            - sensor.template.publish:
                id: wind_heading
                state: 135.0
      - if:
          condition:
            sensor.in_range:
              id: resistance_sensor
              above: 2400
              below: 2500
          then:
            - text_sensor.template.publish:
                id: wind_dir_card
                state: "S"
            - sensor.template.publish:
                id: wind_heading
                state: 180.0
      - if:
          condition:
            sensor.in_range:
              id: resistance_sensor
              above: 8900
              below: 9200
          then:
            - text_sensor.template.publish:
                id: wind_dir_card
                state: "SW"
            - sensor.template.publish:
                id: wind_heading
                state: 225.0
      - if:
          condition:
            sensor.in_range:
              id: resistance_sensor
              above: 37500
              below: 38500
          then:
            - text_sensor.template.publish:
                id: wind_dir_card
                state: "W"
            - sensor.template.publish:
                id: wind_heading
                state: 270.0
      - if:
          condition:
            sensor.in_range:
              id: resistance_sensor
              above: 24400
              below: 25000
          then:
            - text_sensor.template.publish:
                id: wind_dir_card
                state: "NW"
            - sensor.template.publish:
                id: wind_heading
                state: 315.0

  - platform: template
    name: "${friendly_name} wind heading"
    id: wind_heading
    unit_of_measurement: "°"

text_sensor:

  - <<: !include common/version.yaml

  - platform: template
    name: "${friendly_name} wind cardinal direction"
    id: wind_dir_card

sun:
  latitude: !secret latitude
  longitude: !secret longitude

<<: !include common/external-heapmon.yaml