---
substitutions:
  devicename: "nodemcu-01"
  id_name: nodemcu_01
  Device_Location: Family Room
  update_interval: !secret d1-update_interval
  ipaddress: 10.0.0.11
  unique_id: "nodemcu-01"
  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

api:
  encryption:
    key: !secret mogwai_api_key

ota:
  platform: esphome
  password: !secret mogwai_ota_key

captive_portal:

esphome:
  name: nodemcu-01
  includes:
    - veml6075_custom_sensor.h
  libraries:
    - "Wire"
    - SparkFun_VEML6075=https://github.com/sparkfun/SparkFun_VEML6075_Arduino_Library.git
  friendly_name: "NodeMCU-01 - Family Room"
  on_boot:
    then:
      - switch.turn_off: led_onboard1
      - switch.turn_off: led_onboard2
  build_path: .esphome/build/shared_build

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

<<: !include common/time-sntp.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:

  - interval: 5s
    then:
      - display.page.show_next: my_display
      - component.update: my_display

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

  - <<: !include common/whatami.yaml

sensor:

  - <<: !include common/wifi2.yaml

  - <<: !include common/uptime.yaml

  - <<: !include common/free_space.yaml

  # BME 280 Sensor
  - platform: bme280_i2c
    address: 0x76
    update_interval: $update_interval

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

    humidity:
      name: "humidity"
      id: bme280_humidity

    pressure:
      name: "pressure"
      id: bme280_pressure

  - 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);
    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'

  # 1750 Illuminance Sensor
  - platform: bh1750
    name: "illuminance"
    address: 0x23
    update_interval: $update_interval
    id: family_illuminance

  # UV 6075 Sensor
  - platform: custom
    lambda: |-
      auto veml6075 = new VEML6075CustomSensor(50000);
      App.register_component(veml6075);
      return {veml6075->uva_sensor, veml6075->uvb_sensor};

    sensors:
      - name: "uva"
        id: zelva_uva
        unit_of_measurement: "µW/cm²"
        accuracy_decimals: 0

      - name: "uvb"
        id: zelva_uvb
        unit_of_measurement: "µW/cm²"
        accuracy_decimals: 0

  - platform: mqtt_subscribe
    id: weatherst_temp
    topic: /sensors/weatherst/bme280temp/state

text_sensor:

  - platform: mqtt_subscribe
    name: "Lounge Room Temperature from MQTT"
    id: lounge_temp
    topic: /sensors/nodemcu-02/sensor/temperature/state

  - platform: mqtt_subscribe
    name: "Forecast from MQTT"
    id: today_condition
    topic: /sensors/ha/forecast

  - <<: !include common/version.yaml

i2c:
  sda: D5
  scl: D6
  scan: True
  id: bus_a
  frequency: 400kHz

external_components:
  - source:
      type: git
      url: https://github.com/ssieb/custom_components
    components: [heapmon, crash]
  - source:
      type: git
      url: https://github.com/robertklep/esphome-custom-component
    components: [custom, custom_component]

font:

  - file: "slkscr.ttf"
    id: slkscr8
    size: 8

  - file: 'BebasNeue-Regular.ttf'
    id: font2
    size: 48

  - file: 'arial.ttf'
    id: font5
    size: 20

display:

  - platform: ssd1306_i2c
    model: "SSD1306 128x64"
    address: 0x3C
    id: my_display
    rotation: 180

    pages:

      - id: page1
        lambda: |-
          it.printf(0, 0, id(slkscr8), "Time :");
          // Print time in HH:MM format
          it.strftime(0, 60, id(font2), TextAlign::BASELINE_LEFT, "%l:%M", id(sntp_time).now());
          // Print AM/PM
          it.strftime(127, 60, id(font5), TextAlign::BASELINE_RIGHT, "%P", id(sntp_time).now());

      - id: page2
        lambda: |-
          it.printf(0, 0, id(slkscr8), "Temp :");
          it.printf(0, 60, id(font2), TextAlign::BASELINE_LEFT, "%.2f", id(bme280_temperature).state);

      - id: page3
        lambda: |-
          if (id(weatherst_temp).has_state()) {
          it.printf(0, 0, id(slkscr8), "OutSide :");
          it.printf(0, 60, id(font2), TextAlign::BASELINE_LEFT, "%.2f", id(weatherst_temp).state);
          }

      - id: page4
        lambda: |-
          it.printf(0, 0, id(slkscr8), "Forecast :");
          it.printf(0, 60, id(font2), TextAlign::BASELINE_LEFT, "%s", id(today_condition).state.c_str());

      - id: page5
        lambda: |-
          it.printf(0, 0, id(slkscr8), "Light :");
          it.printf(0, 60, id(font2), TextAlign::BASELINE_LEFT, "%.2f", id(family_illuminance).state);