homeassistant:
  name: Maple House
  latitude: 43.6532
  longitude: -79.3832
  elevation: 76
  unit_system: metric
  currency: CAD
  country: CA
  time_zone: America/Toronto
  external_url: "https://home.example.net"
  internal_url: "http://192.168.1.10:8123"

default_config:

frontend:
  themes: !include_dir_merge_named themes

http:
  use_x_forwarded_for: true
  trusted_proxies:
    - 192.168.1.5
  ip_ban_enabled: true
  login_attempts_threshold: 5

logger:
  default: warning
  logs:
    homeassistant.components.mqtt: info
    homeassistant.components.automation: info

recorder:
  db_url: !secret database_url
  purge_keep_days: 14
  commit_interval: 5
  exclude:
    domains:
      - camera
      - updater
    entity_globs:
      - sensor.*_rssi
      - sensor.*_linkquality

history:
  include:
    domains:
      - climate
      - light
      - sensor
      - switch

mqtt:
  sensor:
    - name: "Basement Temperature"
      unique_id: basement_temperature
      state_topic: "home/basement/environment"
      value_template: "{{ value_json.temperature }}"
      unit_of_measurement: "°C"
      device_class: temperature
      state_class: measurement
    - name: "Basement Humidity"
      unique_id: basement_humidity
      state_topic: "home/basement/environment"
      value_template: "{{ value_json.humidity }}"
      unit_of_measurement: "%"
      device_class: humidity
      state_class: measurement

template:
  - sensor:
      - name: "Average Indoor Temperature"
        unique_id: average_indoor_temperature
        unit_of_measurement: "°C"
        device_class: temperature
        state_class: measurement
        state: >-
          {% set values = [
            states('sensor.living_room_temperature') | float(none),
            states('sensor.bedroom_temperature') | float(none),
            states('sensor.basement_temperature') | float(none)
          ] | reject('eq', none) | list %}
          {{ (values | sum / values | count) | round(1) if values else 'unknown' }}

automation:
  - alias: "Turn on porch light at sunset"
    id: porch_light_at_sunset
    mode: restart
    triggers:
      - trigger: sun
        event: sunset
        offset: "-00:15:00"
    conditions:
      - condition: state
        entity_id: input_boolean.vacation_mode
        state: "off"
    actions:
      - action: light.turn_on
        target:
          entity_id: light.front_porch
        data:
          brightness_pct: 65
          color_temp_kelvin: 2700

  - alias: "Turn off porch light overnight"
    id: porch_light_off_overnight
    mode: single
    triggers:
      - trigger: time
        at: "00:30:00"
    actions:
      - action: light.turn_off
        target:
          entity_id: light.front_porch

  - alias: "Notify when a leak is detected"
    id: leak_detection_alert
    mode: parallel
    max: 5
    triggers:
      - trigger: state
        entity_id:
          - binary_sensor.kitchen_sink_leak
          - binary_sensor.laundry_room_leak
          - binary_sensor.water_heater_leak
        from: "off"
        to: "on"
    actions:
      - action: notify.mobile_app_primary_phone
        data:
          title: "Water leak detected"
          message: >-
            {{ trigger.to_state.name }} reported a leak at
            {{ now().strftime('%H:%M') }}.
          data:
            tag: water-leak
            priority: high
      - action: switch.turn_off
        target:
          entity_id: switch.main_water_valve

  - alias: "Reduce heating when everyone leaves"
    id: heating_away_mode
    mode: restart
    triggers:
      - trigger: state
        entity_id: group.household
        to: "not_home"
        for: "00:10:00"
    conditions:
      - condition: state
        entity_id: input_boolean.guest_mode
        state: "off"
    actions:
      - action: climate.set_temperature
        target:
          entity_id: climate.main_floor
        data:
          temperature: 17

script:
  good_night:
    alias: "Good Night"
    icon: mdi:weather-night
    mode: single
    sequence:
      - action: light.turn_off
        target:
          area_id:
            - living_room
            - kitchen
            - basement
      - action: lock.lock
        target:
          entity_id:
            - lock.front_door
            - lock.back_door
      - action: climate.set_temperature
        target:
          entity_id: climate.main_floor
        data:
          temperature: 18
      - action: input_boolean.turn_on
        target:
          entity_id: input_boolean.night_mode

input_boolean:
  vacation_mode:
    name: Vacation Mode
    icon: mdi:airplane
  guest_mode:
    name: Guest Mode
    icon: mdi:account-group
  night_mode:
    name: Night Mode
    icon: mdi:weather-night

group:
  household:
    name: Household
    entities:
      - person.alex
      - person.sam

scene:
  - name: Movie Night
    id: movie_night
    entities:
      light.living_room_ceiling:
        state: "off"
      light.living_room_lamp:
        state: "on"
        brightness: 70
        color_temp_kelvin: 2400
      switch.television:
        state: "on"