---
substitutions:
  devicename: "nodemcu-17"
  id_name: nodemcu_17
  Device_Location: "Caravan Temperature"
  update_interval: !secret d1-update_interval
  ipaddress: 10.0.0.27
  unique_id: "nodemcu-17"
  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

# Enable Home Assistant API
api:
  encryption:
    key: !secret mogwai_api_key

ota:
  platform: esphome
  password: !secret mogwai_ota_key

captive_portal:

esphome:
  name: $devicename
  friendly_name: "NodeMCU-17 - Caravan Sensor"
  on_boot:
    then:
      - switch.turn_off: led_onboard1
      - switch.turn_off: led_onboard2
      - script.execute: consider_deep_sleep

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

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

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

<<: !include common/time.yaml

<<: !include common/website.yaml

<<: !include common/wifi1.yaml

<<: !include common/uptime_formatted.yaml

sensor:

  - <<: !include common/wifi2.yaml

  - <<: !include common/uptime.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"
    retain: False
    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³'

  - platform: template
    name: "dew_point"
    retain: False
    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'

text_sensor:

  - <<: !include common/version.yaml

binary_sensor:
  - platform: homeassistant
    id: prevent_deep_sleep
    name: Prevent Deep Sleep
    entity_id: input_boolean.prevent_deep_sleep

deep_sleep:
  id: deep_sleep_1
  # run_duration: $deep_sleep_interval
  sleep_duration: 10min

script:
  - id: consider_deep_sleep
    mode: queued
    then:
      - delay: $deep_sleep_interval
      - if:
          condition:
            binary_sensor.is_on: prevent_deep_sleep
          then:
            - logger.log: 'Skipping sleep, per prevent_deep_sleep'
          else:
            - deep_sleep.enter: deep_sleep_1
      - script.execute: consider_deep_sleep