---
substitutions:
  devicename: "nodemcu-03"
  id_name: nodemcu_03
  Device_Location: Study Temp
  update_interval: !secret d1-update_interval
  ipaddress: 10.0.0.13
  unique_id: "nodemcu-03"
  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-03 - Study Temp"
  on_boot:
    then:
      - switch.turn_off: led_onboard1
      - switch.turn_off: led_onboard2

<<: !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

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

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

sensor:

  - <<: !include common/wifi2.yaml

  - <<: !include common/uptime.yaml

  - <<: !include common/free_space.yaml

  - platform: bme280_i2c
    address: 0x76
    # update_interval: $update_interval

    temperature:
      name: "temperature"
      id: bme280_temperature
      unit_of_measurement: "°C"
      retain: False

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

    pressure:
      name: "pressure"
      id: bme280_pressure
      retain: False

  - platform: template
    name: "altitude"
    lambda: |-
      const float STANDARD_SEA_LEVEL_PRESSURE = 1020.4; //in hPa, see note
      return ((id(bme280_temperature).state + 273.15) / 0.0065) *
        (powf((STANDARD_SEA_LEVEL_PRESSURE / id(bme280_pressure).state), 0.190234) - 1); // in meter
    # update_interval: $update_interval
    icon: 'mdi:signal'
    unit_of_measurement: 'm'

  - 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(bme280_temperature).state) /
        (id(bme280_temperature).state + 243.5)) * id(bme280_humidity).state * mw) /
        ((273.15 + id(bme280_temperature).state) * r); // in grams/m^3
    accuracy_decimals: 2
    # update_interval: $update_interval
    icon: 'mdi:water'
    unit_of_measurement: 'g/m³'

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

text_sensor:

  - <<: !include common/version.yaml

i2c:
  sda: D1
  scl: D2
  scan: True
  id: bus_a
  frequency: 400kHz