# Loads default set of integrations. Do not remove.
default_config:

# Core Utility Meter Engine (Required for Hilo to generate compatible energy dashboard metrics)
utility_meter:

# Shell Commands
shell_command:
  download_camera_image: "curl -s -o {{ media_path }} -H 'Authorization: Bearer $SUPERVISOR_TOKEN' 'http://supervisor/core/api/camera_proxy/camera.hallway_hallway_camera' && cp {{ media_path }} {{ www_path }}"
  cleanup_blink_snapshots: "find /media /config/www -name 'blink_*.jpg' -mmin +360 -delete"

# Load ffmpeg
ffmpeg:

# Enable Streams
stream:

# Load frontend themes from the themes folder
frontend:
  themes: !include_dir_merge_named themes

automation: !include automations.yaml
script: !include scripts.yaml
scene: !include scenes.yaml

panel_custom:
  - name: developer-tools
    sidebar_title: Developer Tools
    sidebar_icon: mdi:hammer-wrench
    url_path: developer-tools/yaml
    embed_iframe: true
    require_admin: true

# Combined Template Section
template:
  # Nest Camera Event Trigger
  - trigger:
      - platform: event
        event_type: nest_event
        event_data:
          type: camera_motion
    binary_sensor:
      - name: "Nest Camera Motion"
        state: "true"
        auto_off: 10
        device_class: motion

  # Time-Pattern Trigger (Forces calculation updates every minute)
  - trigger:
      - platform: time_pattern
        minutes: "/1"
    sensor:
      - name: "Hydro Quebec Active Rate"
        unique_id: hydro_quebec_active_rate
        unit_of_measurement: "CAD/kWh"
        state: >
          {% set daily_consumption = states('sensor.meter00_daily_grid_energy') | float(999) %}
          {% set low_rate = states('sensor.hilo_rate_low') | float(0.07065) %}
          {% set medium_rate = states('sensor.hilo_rate_medium') | float(0.11142) %}

          {% if daily_consumption < 40.0 %}
            {{ low_rate }}
          {% else %}
            {{ medium_rate }}
          {% endif %}

      # AUTOMATIC LOOKUP SENSOR: Dynamically extracts the active living room climate entity ID
      - name: "HVAC Real-Time Power Draw"
        unique_id: hvac_real_time_power_draw
        unit_of_measurement: "kW"
        device_class: power
        state_class: measurement
        state: >
          {% set hvac = 'climate.living_room_living_room' %}
          {% if states(hvac) not in ['unknown', 'unavailable'] %}
            {% set action = state_attr(hvac, 'hvac_action') | string | lower %}
            {% if action == 'cooling' %}
              2.12
            {% elif action == 'heating' %}
              {% if is_state_attr(hvac, 'preset_mode', 'eco') or is_state_attr(hvac, 'aux_heat', 'on') %}
                20.5
              {% else %}
                2.5
              {% endif %}
            {% else %}
              0
            {% endif %}
          {% else %}
            0
          {% endif %}

      # ADJUSTED SENSOR: Scaled down from 1.50 to 1.35 to fix Energy Dashboard overlap
      - name: "Pool Pump Estimated Power Draw"
        unique_id: pool_pump_estimated_power_draw
        unit_of_measurement: "kW"
        device_class: power
        state_class: measurement
        state: >
          {% if is_state('input_boolean.pool_pump_in_use', 'on') %}
            1.25
          {% else %}
            0
          {% endif %}

      # FIXED SENSOR: Pulls true variable watts from sensor.basement_power and converts to kW
      - name: "Basement Heater Power Draw"
        unique_id: basement_heater_power_draw
        unit_of_measurement: "kW"
        device_class: power
        state_class: measurement
        state: >
          {% set raw_watts = states('sensor.basement_power') | float(0) %}
          {{ (raw_watts / 1000) | round(2) }}

      # WATER HEATER SENSOR: Pulls hardware power from Hilo water heater switch and converts W to kW
      - name: "Water Heater Power Draw"
        unique_id: water_heater_power_draw
        unit_of_measurement: "kW"
        device_class: power
        state_class: measurement
        state: >
          {% set raw_watts = states('sensor.water_heater_power') | float(0) %}
          {{ (raw_watts / 1000) | round(2) }}

  # State-based Template Sensors (Blink Temperature Dropout Filtering for all 4 cameras)
  - sensor:
      - name: "Blink Front Temperature Clean"
        unique_id: blink_front_temperature_clean
        unit_of_measurement: "°C"
        device_class: temperature
        state_class: measurement
        state: >
          {% set raw = states('sensor.blink_front_temperature') | float(default=-999) %}
          {% if raw > -40 and raw < 60 %}
            {{ raw }}
          {% else %}
            {{ states('sensor.blink_front_temperature_clean') }}
          {% endif %}

      - name: "Blink Backyard Shed Temperature Clean"
        unique_id: blink_backyard_shed_temperature_clean
        unit_of_measurement: "°C"
        device_class: temperature
        state_class: measurement
        state: >
          {% set raw = states('sensor.blink_backyard_shed_temperature') | float(default=-999) %}
          {% if raw > -40 and raw < 60 %}
            {{ raw }}
          {% else %}
            {{ states('sensor.blink_backyard_shed_temperature_clean') }}
          {% endif %}

      - name: "Blink Backyard Tree Temperature Clean"
        unique_id: blink_backyard_tree_temperature_clean
        unit_of_measurement: "°C"
        device_class: temperature
        state_class: measurement
        state: >
          {% set raw = states('sensor.blink_backyard_tree_temperature') | float(default=-999) %}
          {% if raw > -40 and raw < 60 %}
            {{ raw }}
          {% else %}
            {{ states('sensor.blink_backyard_tree_temperature_clean') }}
          {% endif %}

      - name: "Blink Backyard Temperature Clean"
        unique_id: blink_backyard_temperature_clean
        unit_of_measurement: "°C"
        device_class: temperature
        state_class: measurement
        state: >
          {% set raw = states('sensor.blink_backyard_temperature') | float(default=-999) %}
          {% if raw > -40 and raw < 60 %}
            {{ raw }}
          {% else %}
            {{ states('sensor.blink_backyard_temperature_clean') }}
          {% endif %}

      # POOL PUMP SENSOR: Converts kW sensor to Watts for Watt-based gauge cards
      - name: "Pool Pump Estimated Power Draw Watts"
        unique_id: pool_pump_estimated_power_draw_watts
        unit_of_measurement: "W"
        device_class: power
        state_class: measurement
        state: >
          {% set kw = states('sensor.pool_pump_estimated_power_draw') | float(0) %}
          {{ (kw * 1000) | round(0) }}

      # HVAC SENSOR: Converts kW sensor to Watts for Watt-based gauge cards
      - name: "HVAC Real-Time Power Draw Watts"
        unique_id: hvac_real_time_power_draw_watts
        unit_of_measurement: "W"
        device_class: power
        state_class: measurement
        state: >
          {% set kw = states('sensor.hvac_real_time_power_draw') | float(0) %}
          {{ (kw * 1000) | round(0) }}

# HTTP Proxy Configuration
http:
  use_x_forwarded_for: true
  trusted_proxies:
    - 192.168.122.1
    - 192.168.1.0/24

# Legacy and Integration Sensor Section
sensor:
  - platform: torque
    email: torque-car@asuscomm.com
    name: MyCar

  # RE-ADDED INTEGRATION ENGINE: Converts your pool pump kW into valid long-term energy stats
  - platform: integration
    source: sensor.pool_pump_estimated_power_draw
    name: Pool Pump Total Energy
    unique_id: pool_pump_total_energy_riemann_sum
    method: left
    round: 2

  # NEW INTEGRATION SENSOR: Converts HVAC kW into valid long-term energy stats (kWh)
  - platform: integration
    source: sensor.hvac_real_time_power_draw
    name: HVAC Total Energy
    unique_id: hvac_total_energy_riemann_sum
    method: left
    round: 2

emulated_hue:
  host_ip: 192.168.1.140
  advertise_ip: 192.168.1.140
  listen_port: 80
  expose_by_default: true
  entities:
    input_boolean.cync_motion_bridge:
      name: "Cync Motion Bridge"
      hidden: false
