# ──────────────────────────────────────────────────────────────────────────
#  BeerWeer in Home Assistant  —  via de publieke API (8-model-gemiddelde)
#  Bron: https://weer.mickbeer.com/api/v1/forecast?lat=<..>&lon=<..>
#  Geen tracking, geen sleutel nodig. Rate-limit: 60 verzoeken/min per IP.
#
#  Zet je eigen coördinaten in de `resource`-URL hieronder (voorbeeld = De Bilt).
#  Bv. Rotterdam: lat=51.92&lon=4.48
#  Plak dit in configuration.yaml (of split via !include) en herstart HA.
# ──────────────────────────────────────────────────────────────────────────

# ── 1) Simpele REST-sensors (één API-call per 15 min → 10 sensoren) ─────────
rest:
  - resource: "https://weer.mickbeer.com/api/v1/forecast?lat=52.10&lon=5.18"
    scan_interval: 900            # 15 min (900 s); de buurt-cache deelt opvragen, de modellen draaien ~uurlijks
    timeout: 30
    sensor:
      - name: "BeerWeer temperatuur"
        unique_id: beerweer_temperature
        value_template: "{{ value_json.current.temperature }}"
        unit_of_measurement: "°C"
        device_class: temperature
        state_class: measurement
      - name: "BeerWeer gevoelstemperatuur"
        unique_id: beerweer_apparent_temperature
        value_template: "{{ value_json.current.apparent_temperature }}"
        unit_of_measurement: "°C"
        device_class: temperature
        state_class: measurement
      - name: "BeerWeer luchtvochtigheid"
        unique_id: beerweer_humidity
        value_template: "{{ value_json.current.humidity }}"
        unit_of_measurement: "%"
        device_class: humidity
        state_class: measurement
      - name: "BeerWeer neerslag"
        unique_id: beerweer_precipitation
        value_template: "{{ value_json.current.precipitation }}"
        unit_of_measurement: "mm"
        device_class: precipitation
        state_class: measurement
      - name: "BeerWeer luchtdruk"
        unique_id: beerweer_pressure
        value_template: "{{ value_json.current.pressure }}"
        unit_of_measurement: "hPa"
        device_class: atmospheric_pressure
        state_class: measurement
      - name: "BeerWeer windsnelheid"
        unique_id: beerweer_wind_speed
        value_template: "{{ value_json.current.wind_speed }}"
        unit_of_measurement: "km/h"
        device_class: wind_speed
        state_class: measurement
      - name: "BeerWeer windstoten"
        unique_id: beerweer_wind_gust
        value_template: "{{ value_json.current.wind_gust }}"
        unit_of_measurement: "km/h"
        device_class: wind_speed
        state_class: measurement
      - name: "BeerWeer windrichting"
        unique_id: beerweer_wind_bearing
        value_template: "{{ value_json.current.wind_bearing }}"
        unit_of_measurement: "°"
        icon: mdi:compass-outline
      - name: "BeerWeer UV-index"
        unique_id: beerweer_uv_index
        value_template: "{{ value_json.current.uv_index }}"
        icon: mdi:weather-sunny-alert
        state_class: measurement
      - name: "BeerWeer conditie"
        unique_id: beerweer_condition
        value_template: "{{ value_json.current.condition }}"
        icon: mdi:weather-partly-cloudy
      # "data"-sensor in DEZELFDE resource → alles uit één API-call. Bewaart de
      # volledige verwachting (48u + 7d, ~13KB) in attributen voor de weather-entiteit.
      - name: "BeerWeer data"
        unique_id: beerweer_data
        value_template: "{{ value_json.current.condition }}"
        json_attributes_path: "$"
        json_attributes:
          - current
          - hourly
          - daily

# Die "data"-sensor draagt een grote verwachting (~13KB) als attribuut. Sluit 'm
# uit van de recorder, anders schrijft HA dat elke 30 min naar de geschiedenis-DB.
# Voeg dit toe aan je BESTAANDE recorder:-blok (of plak het hele blok als je er
# nog geen hebt — maar NIET twee keer "recorder:" in je config):
#
# recorder:
#   exclude:
#     entities:
#       - sensor.beerweer_data

# ── 2) (Optioneel) echte weather-entiteit voor de weerkaart + forecast ──────
#  Gebruikt de "BeerWeer data"-sensor hierboven. Geeft je een nette
#  weather.beerweer met uur- en dagverwachting (8-model-gemiddelde).
template:
  - weather:
      - name: "BeerWeer"
        unique_id: beerweer_weather
        condition_template: "{{ state_attr('sensor.beerweer_data','current').condition }}"
        temperature_template: "{{ state_attr('sensor.beerweer_data','current').temperature }}"
        temperature_unit: "°C"
        humidity_template: "{{ state_attr('sensor.beerweer_data','current').humidity }}"
        pressure_template: "{{ state_attr('sensor.beerweer_data','current').pressure }}"
        pressure_unit: "hPa"
        wind_speed_template: "{{ state_attr('sensor.beerweer_data','current').wind_speed }}"
        wind_speed_unit: "km/h"
        wind_bearing_template: "{{ state_attr('sensor.beerweer_data','current').wind_bearing }}"
        # Dagverwachting (7 dagen)
        forecast_daily_template: >
          {% set ns = namespace(l=[]) %}
          {% for d in state_attr('sensor.beerweer_data','daily') or [] %}
            {% set ns.l = ns.l + [{
              'datetime': d.datetime,
              'condition': d.condition,
              'temperature': d.temperature,
              'templow': d.templow,
              'precipitation': d.precipitation,
              'precipitation_probability': d.precipitation_probability,
              'wind_speed': d.wind_speed,
              'wind_bearing': d.wind_bearing }] %}
          {% endfor %}
          {{ ns.l }}
        # Uurverwachting (48 uur)
        forecast_hourly_template: >
          {% set ns = namespace(l=[]) %}
          {% for h in state_attr('sensor.beerweer_data','hourly') or [] %}
            {% set ns.l = ns.l + [{
              'datetime': h.datetime,
              'condition': h.condition,
              'temperature': h.temperature,
              'apparent_temperature': h.apparent_temperature,
              'precipitation': h.precipitation,
              'precipitation_probability': h.precipitation_probability,
              'wind_speed': h.wind_speed,
              'wind_bearing': h.wind_bearing,
              'uv_index': h.uv_index }] %}
          {% endfor %}
          {{ ns.l }}
