---
substitutions:
  devicename: "nodemcu-08"
  id_name: nodemcu_08
  Device_Location: Garage Temp
  update_interval: !secret d1-update_interval
  ipaddress: 10.0.0.18
  unique_id: "nodemcu-08"
  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:

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

<<: !include common/time-sntp.yaml

<<: !include common/website.yaml

<<: !include common/wifi1.yaml

<<: !include common/uptime_formatted.yaml

esphome:
  name: $devicename
  friendly_name: "NodeMCU-08 - Garage Temp"
  on_boot:
    then:
      - switch.turn_off: led_onboard1
      - switch.turn_off: led_onboard2

switch:

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

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

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

  - <<: !include common/restart.yaml

interval:

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

  - <<: !include common/whatami.yaml

sensor:

  - <<: !include common/wifi2.yaml

  - <<: !include common/uptime.yaml

  - <<: !include common/free_space.yaml

  - platform: dht
    pin: GPIO14
    model: DHT11
    # update_interval: $update_interval

    temperature:
      name: "temperature"
      unit_of_measurement: "°C"
      id: temperature
      retain: True

    humidity:
      name: "humidity"
      id: humidity
      retain: False

  - platform: template
    name: "absolute_humidity"
    lambda: |-
      const float mw = 18.01534;    // molar mass of water g/mol
      const float r = 8.31447215;   // Universal gas constant J/mol/K
      return (6.112 * powf(2.718281828, (17.67 * id(temperature).state) /
        (id(temperature).state + 243.5)) * id(temperature).state * mw) /
        ((273.15 + id(temperature).state) * r); // in grams/m^3
    accuracy_decimals: 2
    # update_interval: $update_interval
    icon: 'mdi:water'
    unit_of_measurement: 'g/m³'
    retain: False

  - platform: template
    name: "dew_point"
    lambda: |-
      return (243.5*(log(id(humidity).state/100)+((17.67*id(temperature).state)/
      (243.5+id(temperature).state)))/(17.67-log(id(humidity).state/100)-
      ((17.67*id(temperature).state)/(243.5+id(temperature).state))));
    unit_of_measurement: °C
    icon: 'mdi:thermometer-alert'
    retain: False

  - platform: template
    name: "Random Numbers"
    id: random_sensor
    lambda: |-
      return int(rand() % 187);
    update_interval: !secret d1-update_interval
    retain: False

text_sensor:

  - <<: !include common/version.yaml

#  - interval: 30s
#    then:
#        - http_request.post:
#            url: "http://10.0.0.48/win&T=1&A=128&FX=2&SX=255&R=255&G=255&B=255"
#            verify_ssl: false
#            on_response:
#              then:
#                - logger.log:
#                    level: INFO
#                    tag: http_post
#                    format: 'HTTP POST Response status: %d'
#                    args: [status_code]
#
#                - lambda: |-
#                    if (status_code == 200) {
#                      ESP_LOGI ("http_post", "HTTP POST sucessfull");
#                    } else {
#                      ESP_LOGI ("http_post", "HTTP POST failed");
#                    }
#
#
<<: !include common/external-heapmon.yaml