NASA API and other Space integrations

Hi all,

I would like to share with you an initial state of my personal project for a NASA package in Home Assistant.

There are several resources through the internet on this, and I am trying to collect them in a unique package.

Let me share with you what I have untill now (very basic, but a beginning).

My nasa.yaml file looks like this:

###
# NASA API collection
#
# Get API Token at: https://api.nasa.gov/index.html
###

# Input text to manage NASA API Token in a resource_template 
input_text:
  nasa_api_key:
    name: NASA API Key
    initial: !secret nasa_api_key
    mode: password

# API APOD: NASA Astronomical Picture of the Day
sensor:
  - platform: rest
    resource_template: https://api.nasa.gov/planetary/apod?api_key={{ states("input_text.nasa_api_key") }} 
    name: nasa_apod
    value_template: "OK"
    json_attributes:
      - date
      - title
      - explanation
      - url

homeassistant:
  customize:
    sensor.nasa_apod:
      friendly_name: Astronomical Picture of the Day
      icon: mdi:image
  
###
# Clear recorder
###

recorder:
  exclude:
    entities:
      - input_text.nasa_api_key
      - sensor.nasa_apod

I also added a Lovelace Markdown card to show this first element:

type: markdown
content: |
  ## {{ states.sensor.nasa_apod.attributes.title }}
  _{{ states.sensor.nasa_apod.attributes.date }}_
  ![Image]({{ states.sensor.nasa_apod.attributes.url }})
  {{ states.sensor.nasa_apod.attributes.explanation }}

As an extra, in the same package I have added ISS integration to show in the Map the position of the station:

# International Space Station position
# Home Assistant Integration: https://www.home-assistant.io/integrations/iss/
binary_sensor:
  - platform: iss
    name: International Space Station
    show_on_map: true

I’ve seen that in NASA’s API Collection I can see Mars Rover’s images and Asteroids that will pass near the Earth, among other interesting integrations.

I have these ideas to evolve:

  • Notifications: if ISS is visible in my location, any Space event alert…
  • Next rocket launches
  • SpaceX integration (I’ve seen that there is an API)

Please tell me if you like this and any other suggestions you may have. I am starting with it, please don’t be bad with me :slight_smile:

Thanks for reading!

8 Likes

Hi again!

I’ve added a little bit more to my project. I have edited previous post just to correct some issues in my code. And now the next part of my project.

Taking a look at jones’s tutorial for SpaceX sensors: Starman and next rocket launch and also a little bit of research in r/SpaceX API Docs, I’ve been playing a little bit and this is the result, of course everything is improvable… lets go!

I created another package called spacex.yaml with the following content:

###
# SpaceX API Collection
#
# Credit: 
#   @jones (https://community.home-assistant.io/t/spacex-sensors-starman-and-next-rocket-launch/65467)
#
# API Documentation: https://docs.spacexdata.com/
###

sensor:
# Next Rocket Launch
  - platform: rest
    resource: 'https://api.spacexdata.com/v3/launches/next'
    value_template: '{{ value_json["rocket"]["rocket_name"] }}'
    name: spacex_next_rocket_launch
    json_attributes:
      - mission_name
      - launch_date_local
      - launch_date_utc
      - launch_site
      - rocket
      - links
      - details

homeassistant:
  customize:
    sensor.spacex_next_rocket_launch:
      friendly_name: SpaceX Next Rocket Launch
      icon: mdi:rocket-launch

###
# Clear recorder
###

recorder:
  exclude:
    entities:
      - sensor.spacex_next_rocket_launch

And created the following card:

type: markdown
content: |
  ## {{ states.sensor.spacex_next_rocket_launch.attributes.mission_name }}: {{ states.sensor.spacex_next_rocket_launch.attributes.rocket.rocket_name }}
  ![Image]({{ states.sensor.spacex_next_rocket_launch.attributes.links.mission_patch }})
  __Launch site__: {{ states.sensor.spacex_next_rocket_launch.attributes.launch_site.site_name_long }} 
  __Launch date__: {{ as_timestamp(states.sensor.spacex_next_rocket_launch.attributes.launch_date_utc) | timestamp_custom('%a %d %m %Y, %I:%M %p') }} UTC
  <p>{{ states.sensor.spacex_next_rocket_launch.attributes.details }}</p>

The result is this:

I hope you like it!

Thank you for reading!

Nice!

Do you know if there is a way to get the next ISS visible passes from your location?

Hi, investigating a little bit I found several ways:

Option 1: HTML Embedded

I found that in Spot The Station you can configure to obtain an embedded HTML.

Select your place from the map, clicking View sighting opportunities:

Select <…> Embed from the buttons next to your location:

Select a theme:

This will give you an HTML code like this:

<div style='display:inline-block; border: 1px solid #CCC; border-radius: 6px; -webkit-border-radius: 6px; -o-border-radius: 6px; position: relative; overflow: hidden; width: 310px; height: 450px;'> <iframe src='https://spotthestation.nasa.gov/widget/widget.cfm?country=United_States&region=National_Parks&city=Theodore_Roosevelt_Birthplace_National_Historic_Site' width='310' height='450' frameborder='0' ></iframe></div>

Then, Home Assistant as default I can’t find an HTML card other than Webpage card or iframe Panel, none of those does the work. So you will need to install Lovelace HTML Jinja2 Template card from HACS.

With this you can then create a card like this:

type: custom:html-template-card
ignore_line_breaks: true
content: >
  <iframe
  src='https://spotthestation.nasa.gov/widget/widget.cfm?country=United_States&region=National_Parks&city=Theodore_Roosevelt_Birthplace_National_Historic_Site'
  width='450' height='450' frameborder='0' ></iframe>

Check that I didn’t use <div> block and modified variables width and height.

The result is this:

Probably with a theme or other card it should be possible to have a better looking card.

Option 2: RSS Feed in Persisten Notifications

I haven’t checked this yet, but probably Feedreader is a good option.

First you’ll have to obtain the RSS feed from Spot the Station.

Right click in RSS from your location menu:

And copy the link.

In configuration.yaml / sensor.yaml or your package, you should create the following:

feedreader:
  urls:
    - https://spotthestation.nasa.gov/sightings/indexrss.cfm?country=United_States&region=National_Parks&city=Theodore_Roosevelt_Birthplace_National_Historic_Site

You can then create automations to receive notifications (copied from Feedreader integration page):

automation:
  - alias: "Trigger action when new element(s) in RSS feed"
    trigger:
      platform: event
      event_type: feedreader
    action:
      service: script.turn_on
      target:
        entity_id: script.my_action
automation:
  - alias: "Send notification of RSS feed title when updated"
    trigger:
      platform: event
      event_type: feedreader
    action:
      service: persistent_notification.create
      data:
        title: "New ISS sighting"
        message: "New ISS sighting - {{ as_timestamp(now()) | timestamp_custom('%I:%M:%S %p %d%b%Y', true) }}"
        notification_id: "{{ trigger.event.data.title }}"

I can’t help you much on this second option because I haven’t checked it yet. I hope someone has used it before and can help us with it.

For sure there are many options, please share yours!

Hi all,

Slight update on my nasa.yaml file. Today Astronomical Picture of the Day is a Video and the original Markdown Card didn’t show the image/video.

Also I was not happy on how was the sensor calling the API, beacuse the token was not as secret as I liked. Now I don’t need input_text and the token is kept secret in my secrets.yaml file.

The code now is as simple as this:

###
# NASA API collection
#
# Get API Token at: https://api.nasa.gov/index.html
###

# API APOD: NASA Astronomical Picture of the Day
sensor:
  - platform: rest
    resource: https://api.nasa.gov/planetary/apod
    name: nasa_apod
    value_template: "OK"
    params:
      api_key: !secret nasa_api_key
      thumbs: true
    json_attributes:
      - date
      - title
      - explanation
      - url
      - thumbnail_url
      - media_type

homeassistant:
  customize:
    sensor.nasa_apod:
      friendly_name: Astronomical Picture of the Day
      icon: mdi:image
  
###
# Clear recorder
###

recorder:
  exclude:
    entities:
      - sensor.nasa_apod

The card is now like this:

type: markdown
content: |
  ## {{ states.sensor.nasa_apod.attributes.title }} 
  _{{ states.sensor.nasa_apod.attributes.date }}_ 
  {%- if (states.sensor.nasa_apod.attributes.media_type == "video") %} 
    [![Image]({{ states.sensor.nasa_apod.attributes.thumbnail_url }})]({{ states.sensor.nasa_apod.attributes.url }}&autoplay=1&loop=1) 
  {%- else -%} 
    ![Image]({{ states.sensor.nasa_apod.attributes.url }})
  {% endif -%} 
  {{ states.sensor.nasa_apod.attributes.explanation }}

Now if media_type is “video” it then shows a thumbnail (included in the API). If it’s not a video and shows the original url.

If its a video it will open a new browser windows if you click the image (I really don’t like this, but I’m not yet an expert in card creation)

Hi alessbarb,

Don’t know if this is the right place to ask you that, but i hope it’s ok. I don’t know how to implement a file like your nasa.yaml in the configuration.yaml. I Know it has be something like this:

integration: !include nasa.yaml

but which integration is the right one?

  • “rest” does not work

Thanks in advance

You have to use packages.
For example, you create a packages folder in the config directory
and in configuration.yaml

homeassistant:
   packages: !include_dir_named packages

then put the nasa.yaml file in the packages directory and restart home assistant.

1 Like

@alessbarb many thanks for your contribution.
Do you know why the spacex data are not being updated?
I am getting the same results with the one you shared (CRS-21 Falcon 9). It seems that the next launch date is Sun 06 12 2020 which is not normal. Thanks!

My SpaceX integration is updating correctly so it must be on your side. I use this integration:

1 Like

Thank you for sharing. Using your integration resolved the data refresh issue. :slight_smile:

I was able to find out why data are not current using the spacex package. The API used (v3) has been depracated. We need to use v4 which comes with new ‘json_attributes’ that need to be declared in spacex.yaml file. I will try to update your solution and post it in this thread for anyone who is interested. I am not very good with apis so it might take some time for me until i make it work. If you see this and you have the time to update the solution, feel free to do so :slight_smile: