# Air Story for Home Assistant: the air you breathe, as sensors.
#
# Install: save this file as config/packages/airstory.yaml, make sure configuration.yaml has
#   homeassistant:
#     packages: !include_dir_named packages
# then restart Home Assistant.
#
# - No account, no key.
# - Your home (zone.home) is rounded to 0.1 degree (about 7 miles) before it leaves the house.
# - Polls once every 15 minutes. Please do not lower scan_interval: the answer only changes
#   every 15 minutes, and faster polling costs the free service money for nothing.
# - Show the credit "NOAA / NWS · theairstory.com" wherever you show this data.
# - Units: the API answers in °F, mph and miles. Home Assistant converts temperature and distance
#   to your unit system on its own, so metric homes see °C and km.

rest:
  - resource_template: >-
      https://www.theairstory.com/api/v1/now?lat={{ '%.1f' | format(state_attr('zone.home', 'latitude') | float(0)) }}&lon={{ '%.1f' | format(state_attr('zone.home', 'longitude') | float(0)) }}&client=ha
    scan_interval: 900 # 15 minutes. Must not be lower.
    timeout: 20
    sensor:
      # Raw answer. The friendly sensor.air_story below is built from it.
      - name: "Air Story feed"
        unique_id: airstory_feed
        icon: mdi:weather-windy
        availability: "{{ value_json is defined and value_json.key is defined }}"
        value_template: "{{ value_json.key }}"
        json_attributes:
          - chip
          - headline
          - color
          - say
          - link
          - place
          - alerts
          - trail
          - credit
          - at

      - name: "Air Story temperature"
        unique_id: airstory_temperature
        availability: "{{ value_json is defined and value_json.now is defined }}"
        value_template: "{{ value_json.now.temp }}"
        device_class: temperature
        state_class: measurement
        unit_of_measurement: "°F" # matches labels.temp of the default (°F) answer

      - name: "Air Story dew point"
        unique_id: airstory_dew_point
        icon: mdi:water-thermometer
        availability: "{{ value_json is defined and value_json.now is defined }}"
        value_template: "{{ value_json.now.dew }}"
        device_class: temperature
        state_class: measurement
        unit_of_measurement: "°F"

      - name: "Air Story humidity"
        unique_id: airstory_humidity
        availability: "{{ value_json is defined and value_json.now is defined }}"
        value_template: "{{ value_json.now.rh }}"
        device_class: humidity
        state_class: measurement
        unit_of_measurement: "%"

      - name: "Air Story trail from"
        unique_id: airstory_trail_from
        icon: mdi:compass-outline
        availability: "{{ value_json is defined and value_json.key is defined }}"
        value_template: "{{ value_json.trail.from if value_json.trail else none }}"

      - name: "Air Story trail distance"
        unique_id: airstory_trail_distance
        icon: mdi:map-marker-path
        availability: "{{ value_json is defined and value_json.key is defined }}"
        value_template: "{{ value_json.trail.miles if value_json.trail else none }}"
        device_class: distance
        unit_of_measurement: "mi"

template:
  - sensor:
      # sensor.air_story: the chip ("Marine layer", "Santa Ana winds"…), or "Right now" when the air is calm.
      - name: "Air Story"
        unique_id: airstory_now
        icon: mdi:weather-windy
        availability: "{{ has_value('sensor.air_story_feed') }}"
        state: "{{ state_attr('sensor.air_story_feed', 'chip') or 'Right now' }}"
        attributes:
          headline: "{{ state_attr('sensor.air_story_feed', 'headline') }}"
          key: "{{ states('sensor.air_story_feed') }}"
          color: "{{ state_attr('sensor.air_story_feed', 'color') }}"
          say: "{{ state_attr('sensor.air_story_feed', 'say') }}"
          link: "{{ state_attr('sensor.air_story_feed', 'link') }}"
          place: "{{ state_attr('sensor.air_story_feed', 'place') }}"
          alerts: "{{ state_attr('sensor.air_story_feed', 'alerts') or [] }}"
          trail_from: "{{ (state_attr('sensor.air_story_feed', 'trail') or {}).get('from') }}"
          trail_miles: "{{ (state_attr('sensor.air_story_feed', 'trail') or {}).get('miles') }}"
          credit: "{{ state_attr('sensor.air_story_feed', 'credit') or 'NOAA / NWS · theairstory.com' }}"

# Dashboard card. Add a Markdown card and paste the content below (remove the leading "# ").
#
# type: markdown
# content: |
#   {% set a = states.sensor.air_story.attributes %}
#   ## {{ a.headline }}
#   **{{ states('sensor.air_story') }}** · {{ states('sensor.air_story_temperature') }}{{ state_attr('sensor.air_story_temperature', 'unit_of_measurement') }}, dew point {{ states('sensor.air_story_dew_point') }}{{ state_attr('sensor.air_story_dew_point', 'unit_of_measurement') }}
#   {% if a.trail_from %}
#   3 days ago this air was {{ states('sensor.air_story_trail_distance') | round(0) | int }} {{ state_attr('sensor.air_story_trail_distance', 'unit_of_measurement') }} {{ a.trail_from }}.
#   {% endif %}
#   {% for alert in a.alerts %}**Alert:** {{ alert }}<br>
#   {% endfor %}
#   [Open Air Story]({{ a.link }})
#
#   <sub>{{ a.credit }}</sub>
