#-------------------------------------------
#  Space Related Packages
# @CCOSTAN
# Original Repo : https://github.com/CCOSTAN/Home-AssistantConfig
#-------------------------------------------
#------ISS----------------------------------
#-------------------------------------------
# binary_sensor:
#   - platform: iss
#     show_on_map: true
#-------------------------------------------

sensor:

  - platform: rest
    scan_interval: 1800
    name: SpaceX Starman
    json_attributes:
      - earth_distance_mi
    value_template: '{{ value_json["speed_mph"] }}'
    unit_of_measurement: "mph"
    resource: 'https://api.spacexdata.com/v2/info/roadster'

  - platform: rest
    scan_interval: 1800
    name: SpaceX
    json_attributes:
      - mission_name
      - launch_site
      - rocket
    value_template: '{{ value_json["launch_date_unix"] }}'
    resource: 'https://api.spacexdata.com/v2/launches/next'

automation:
  - alias: Launch Window Approaching
    id: 1d42fc4f-a37d-4283-b64b-09242a145598
    trigger:
      - platform: template
        value_template: '{{ (now().strftime("%s") | int + 600) == (states.sensor.spacex.state | int) }}'

    action:
      - service: script.notify_engine
        data:
          value1: 'Go Outside!  There is a Rocket Launch very soon! {{states.sensor.next_launch.attributes.stream }}'
          group: 'information'