automation:
  - id: update_home_location_from_gps
    alias: Update Home Location from GPS
    description: Updates the Home zone location based on GPS coordinates from Modbus
    trigger:
      - platform: state
        entity_id:
          - sensor.gps_latitude
          - sensor.gps_longitude
    condition:
      - condition: template
        value_template: >-
          {% set lat = states('sensor.gps_latitude')|float(0) %}
          {% set lon = states('sensor.gps_longitude')|float(0) %}
          {% set fix = states('sensor.gps_fix')|int(0) %}
          {{ lat != 0 and lon != 0 and fix > 0 }}
    action:
      - service: homeassistant.set_location
        data:
          latitude: "{{ states('sensor.gps_latitude') }}"
          longitude: "{{ states('sensor.gps_longitude') }}"

  - id: update_home_location_on_startup
    alias: Update Home Location on Startup
    description: Sets home location from last known GPS coordinates on HA restart
    trigger:
      - platform: homeassistant
        event: start
    condition:
      - condition: template
        value_template: >-
          {% set lat = states('sensor.gps_latitude')|float(0) %}
          {% set lon = states('sensor.gps_longitude')|float(0) %}
          {{ lat != 0 and lon != 0 }}
    action:
      - delay:
          seconds: 10
      - service: homeassistant.set_location
        data:
          latitude: "{{ states('sensor.gps_latitude') }}"
          longitude: "{{ states('sensor.gps_longitude') }}"

  - id: refresh_weather_hourly
    alias: Refresh Weather Hourly
    description: Forces weather to refresh hourly with current GPS location
    trigger:
      - platform: time_pattern
        hours: "/1"
    condition:
      - condition: state
        entity_id: binary_sensor.gps_fix_available
        state: "on"
    action:
      - service: homeassistant.update_entity
        target:
          entity_id: weather.home

template:
  - sensor:
      - name: Van Last Known Latitude
        unique_id: van_last_known_latitude
        unit_of_measurement: "°"
        availability: >-
          {{ this.state | float(none) is not none
             or (states('sensor.gps_latitude') | float(none) is not none
                 and states('sensor.gps_fix') | int(0) > 0) }}
        state: >-
          {% set lat = states('sensor.gps_latitude') | float(none) %}
          {% set fix = states('sensor.gps_fix') | int(0) %}
          {% if lat is not none and fix > 0 %}
            {{ lat }}
          {% else %}
            {{ this.state }}
          {% endif %}

      - name: Van Last Known Longitude
        unique_id: van_last_known_longitude
        unit_of_measurement: "°"
        availability: >-
          {{ this.state | float(none) is not none
             or (states('sensor.gps_longitude') | float(none) is not none
                 and states('sensor.gps_fix') | int(0) > 0) }}
        state: >-
          {% set lon = states('sensor.gps_longitude') | float(none) %}
          {% set fix = states('sensor.gps_fix') | int(0) %}
          {% if lon is not none and fix > 0 %}
            {{ lon }}
          {% else %}
            {{ this.state }}
          {% endif %}

      - name: GPS Location
        unique_id: gps_location
        state: >-
          {% set lat = states('sensor.gps_latitude')|float(0) %}
          {% set lon = states('sensor.gps_longitude')|float(0) %}
          {% if lat != 0 and lon != 0 %}
            {{ lat }}, {{ lon }}
          {% else %}
            unavailable
          {% endif %}
        attributes:
          latitude: "{{ states('sensor.gps_latitude') }}"
          longitude: "{{ states('sensor.gps_longitude') }}"
          speed: "{{ states('sensor.gps_speed') }}"
          course: "{{ states('sensor.gps_course') }}"
          altitude: "{{ states('sensor.gps_altitude') }}"
          satellites: "{{ states('sensor.gps_satellites') }}"
          fix: "{{ states('sensor.gps_fix') }}"
          last_updated: "{{ now().isoformat() }}"

      # Weather sensors based on current GPS location
      - name: Current Weather Temperature
        unique_id: current_weather_temperature
        unit_of_measurement: "°F"
        device_class: temperature
        state: "{{ state_attr('weather.home', 'temperature') }}"

      - name: Current Weather Humidity
        unique_id: current_weather_humidity
        unit_of_measurement: "%"
        device_class: humidity
        state: "{{ state_attr('weather.home', 'humidity') }}"

      - name: Current Weather Condition
        unique_id: current_weather_condition
        state: "{{ states('weather.home') }}"

      - name: Current Weather Wind Speed
        unique_id: current_weather_wind_speed
        unit_of_measurement: "mph"
        device_class: wind_speed
        state: "{{ state_attr('weather.home', 'wind_speed') }}"

  - binary_sensor:
      - name: GPS Fix Available
        unique_id: gps_fix_available
        device_class: connectivity
        state: "{{ states('sensor.gps_fix')|int(0) > 0 }}"
