SpaceX sensors: Starman and next rocket launch

I’ve recently discovered that SpaceX provides a nice API for upcoming rocket launches and starman flying around in his tesla roadster. So why not integrating it into homeassistant? I made two sensors with several attributes that are further seperated into template sensors providing info about upcoming launches and starman.

Starman

Contains current Speed (km/h) as default value and has additional attribute for earth’s distance. There is also data in mph available.

- platform: rest
  name: Starman
  json_attributes:
    - earth_distance_km
  value_template: '{{ value_json["speed_kph"] }}'
  unit_of_measurement: "km/h"
  resource: 'https://api.spacexdata.com/v2/info/roadster'

Rocket Launches

I’ve chosen unix time as default value for easy conversion in local time. Sensor has attributes for mission name, launchsite and rocket type:

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

Template Sensors + formatting

Starman’s speed is displayed as integer and the distance as million km (x10⁶). Rocket information are seperated in launch date, time,

- platform: template
  sensors:
    starman_speed:
      friendly_name: 'Starman Geschwindigkeit'
      value_template: '{{ states.sensor.starman.state | round(0) }}'
      unit_of_measurement: 'km/h'
    starman_earth_distance:
      friendly_name: 'Starman Erdentfernung'
      value_template: '{{ (states.sensor.starman.attributes["earth_distance_km"] | multiply(0.000001)) | round(2) }}'
      unit_of_measurement: 'km x10⁶'
    spacex_next_launch_day:
      friendly_name: 'Nächstes Startdatum'
      value_template: '{{ states.sensor.spacex.state | int | timestamp_custom("%d.%m.%Y")}}'
    spacex_next_launch_time:
      friendly_name: 'Startzeit'
      value_template: '{{ states.sensor.spacex.state | int | timestamp_custom("%H:%M")}}'
      unit_of_measurement: 'h'
    spacex_next_rocket:
      friendly_name: 'Nächste Rakete'
      value_template: '{{ states.sensor.spacex.attributes["rocket"]["rocket_name"] }}'
    spacex_next_launch_site:
      friendly_name: 'Abflugsort'
      value_template: '{{ states.sensor.spacex.attributes["launch_site"]["site_name"] }}'
    spacex_next_mission_name:
      friendly_name: 'Mission'
      value_template: '{{ states.sensor.spacex.attributes["mission_name"] }}'

Let’s set some matching icons in customize.yaml:

sensor.starman_speed:
  icon: mdi:account-star
sensor.starman_earth_distance:
  icon: mdi:map-marker-distance
sensor.spacex_next_rocket:
  icon: mdi:rocket
sensor.spacex_next_launch_day:
  icon: mdi:calendar
sensor.spacex_next_launch_time:
  icon: mdi:clock-outline
sensor.spacex_next_launch_site:
  icon: mdi:map-marker-radius
sensor.spacex_next_mission_name:
  icon: mdi:information-outline

Automation: Notification 10min before liftoff

I wanted a notification 10 minutes before liftoff. You can adjust the offset by changing the value of 600. Don’t worry about weird spacing. I’m using xmpp component for notification which is sadly a bit outdated without OMEMO or new TLS features:

- alias: Raketenstartmeldung
  trigger:
  - platform: template
    value_template: '{{ (now().strftime("%s") | int + 600) == (states.sensor.spacex.state | int) }}'
  action:
  - service: notify.xmpp
    data:
      message: >
        SPACEX START IN 10MIN
        ══════════════════════════ 


        Mission: {{ states('sensor.spacex_next_mission_name') }} 
          │
          ├─ Startzeit: .. {{ states('sensor.spacex_next_launch_time') }}
          ├─ Rakete: ..... {{ states('sensor.spacex_next_rocket') }}
          └─ Ort: ........... {{ states('sensor.spacex_next_launch_site') }}

It looks like this when triggered:

Group

After all these steps create a group with the sensor and automation:

spacex:
  name: SpaceX
  entities:
  - sensor.starman_speed
  - sensor.starman_earth_distance
  - sensor.spacex_next_rocket
  - sensor.spacex_next_mission_name
  - sensor.spacex_next_launch_day
  - sensor.spacex_next_launch_time
  - sensor.spacex_next_launch_site
  - automation.raketenstartmeldung

which looks like this:
spacex

Have fun and (again) sorry for the weird mix between german and english.

22 Likes

Thank you for this @jones Super easy to follow, and was pretty easy to work out the English equivalent of the German :wink: Just added in all the SpaceX side of things, wasn’t interested in the Starman side, so left them out, and works perfectly. I changed the location to the long name, as I am not familiar with the short codes.

spacex

2 Likes

Many thanks for this, I’ve done as above, and will set up a TTS message to let my wife know of upcoming launches as she likes this sort of thing!

1 Like

How do one get the long name of location?
Thanks.

I merged these with the ISS (space station) tab and my rocket-nut grandson thanks you profusely.

3 Likes

In your:

sensors:
  platform: template
  sensors:
     spacex_next_launch_site:
       friendly_name: 'Launch Site'
       value_template: '{{ states.sensor.spacex.attributes["launch_site"]["site_name_long"] }}'

instead of “site_name”

5 Likes

I added some sensor values as well as created a package for this and made a few adjustments to reflect English sensors as well as US speed measurements. I also added a lovelace config to the repo. It’s just basic with vertical-stacks.

More API info - https://github.com/r-spacex/SpaceX-API

https://github.com/edif30/home-assistant-config/blob/master/packages/spacex.yaml

1 Like

How on earth do I create a template sensor for the Payload Information??
I’ve been trying in the template section and trying different examples but nothing…

This gets me close but I don’t know how to narrow it down after that…

{{ states.sensor.spacex.attributes[“rocket”][“second_stage”] }}

mission_name: SAOCOM 1A
    launch_site: {
      "site_id": "vafb_slc_4e",
      "site_name": "VAFB SLC 4E",
      "site_name_long": "Vandenberg Air Force Base Space Launch Complex 4E"
    }
    rocket: {
      "rocket_id": "falcon9",
      "rocket_name": "Falcon 9",
      "rocket_type": "FT",
      "first_stage": {
        "cores": [
          {
            "core_serial": "B1048",
            "flight": 2,
            "block": 5,
            "reused": true,
            "land_success": null,
            "landing_intent": true,
            "landing_type": "RTLS",
            "landing_vehicle": "LZ-4"
          }
        ]
      },
      "second_stage": {
        "block": 5,
        "payloads": [
          {
            "payload_id": "SAOCOM 1A",
            "norad_id": [],
            "reused": false,
            "customers": [
              "CONAE"
            ],
            "nationality": "Argentina",
            "manufacturer": "INVAP",
            "payload_type": "Satellite",
            "payload_mass_kg": 2800,
            "payload_mass_lbs": 6172.94,
            "orbit": "SSO",
            "orbit_params": {
              "reference_system": "geocentric",
              "regime": "sun-synchronous",
              "longitude": null,
              "semi_major_axis_km": null,
              "eccentricity": null,
              "periapsis_km": null,
              "apoapsis_km": null,
              "inclination_deg": null,
              "period_min": null,
              "lifespan_years": 5,
              "epoch": null,
              "mean_motion": null,
              "raan": null,
              "arg_of_pericenter": null,
              "mean_anomaly": null
            }
          }
        ]
      },
      "fairings": {
        "reused": false,
        "recovery_attempt": true,
        "recovered": null,
        "ship": "MR STEVEN"
      }
    }

Pick any one of these and give it a shot:

spacex_detail_first_stage_core_serial:
  value_template: '{{ states.sensor.spacex.attributes["rocket"]["first_stage"]["cores"]["core_serial"] }}'
spacex_detail_first_stage_core_flight:
  value_template: '{{ states.sensor.spacex.attributes["rocket"]["first_stage"]["cores"]["flight"] }}'
spacex_detail_first_stage_core_block:
  value_template: '{{ states.sensor.spacex.attributes["rocket"]["first_stage"]["cores"]["block"] }}'
spacex_detail_first_stage_core_reused:
  value_template: '{{ states.sensor.spacex.attributes["rocket"]["first_stage"]["cores"]["reused"] }}'
spacex_detail_first_stage_core_land_success:
  value_template: '{{ states.sensor.spacex.attributes["rocket"]["first_stage"]["cores"]["land_success"] }}'
spacex_detail_first_stage_core_landing_type:
  value_template: '{{ states.sensor.spacex.attributes["rocket"]["first_stage"]["cores"]["landing_type"] }}'
spacex_detail_first_stage_core_landing_vehicle:
  value_template: '{{ states.sensor.spacex.attributes["rocket"]["first_stage"]["cores"]["landing_vehicle"] }}'
spacex_detail_second_stage_payloads_payload_id:
  value_template: '{{ states.sensor.spacex.attributes["rocket"]["second_stage"]["payloads"]["payload_id"] }}'
spacex_detail_second_stage_payloads_norad_id:
  value_template: '{{ states.sensor.spacex.attributes["rocket"]["second_stage"]["payloads"]["norad_id"] }}'
spacex_detail_second_stage_payloads_reused:
  value_template: '{{ states.sensor.spacex.attributes["rocket"]["second_stage"]["payloads"]["reused"] }}'
spacex_detail_second_stage_payloads_customers:
  value_template: '{{ states.sensor.spacex.attributes["rocket"]["second_stage"]["payloads"]["customers"] }}'
spacex_detail_second_stage_payloads_nationality:
  value_template: '{{ states.sensor.spacex.attributes["rocket"]["second_stage"]["payloads"]["nationality"] }}'
spacex_detail_second_stage_payloads_manufacturer:
  value_template: '{{ states.sensor.spacex.attributes["rocket"]["second_stage"]["payloads"]["manufacturer"] }}'
spacex_detail_second_stage_payloads_payload_type:
  value_template: '{{ states.sensor.spacex.attributes["rocket"]["second_stage"]["payloads"]["payload_type"] }}'
spacex_detail_second_stage_payloads_payload_mass_lbs:
  value_template: '{{ states.sensor.spacex.attributes["rocket"]["second_stage"]["payloads"]["payload_mass_lbs"] }}'
spacex_detail_second_stage_payloads_orbit:
  value_template: '{{ states.sensor.spacex.attributes["rocket"]["second_stage"]["payloads"]["orbit"] }}'
spacex_detail_second_stage_payloads_payload_orbit_params_reference_system:
  value_template: '{{ states.sensor.spacex.attributes["rocket"]["second_stage"]["payloads"]["orbit"]["orbit_params"]["reference_system"] }}'
spacex_detail_second_stage_payloads_payload_orbit_params_regime:
  value_template: '{{ states.sensor.spacex.attributes["rocket"]["second_stage"]["payloads"]["orbit"]["orbit_params"]["regime"] }}'
spacex_detail_second_stage_payloads_payload_orbit_params_logitude:
  value_template: '{{ states.sensor.spacex.attributes["rocket"]["second_stage"]["payloads"]["orbit"]["orbit_params"]["longitude"] }}'
2 Likes

Maybe someone skilled can turn this into a component? Greetings from Vietnam.

or, you could put “- sensor.spacex” in your attributes of the group you are displaying. It’s not pretty but it’s complete.

First time really using a json result and making templates.
Always learning eh!

I actually get a blank result with your ones as well…

I only get blanks.

This is as far as I get

{{ states.sensor.spacex.attributes[“rocket”][“second_stage”][“payloads”] }}

which will give me

[{‘payload_id’: ‘SAOCOM 1A’, ‘norad_id’: [], ‘reused’: False, ‘customers’: [‘CONAE’], ‘nationality’: ‘Argentina’, ‘manufacturer’: ‘INVAP’, ‘payload_type’: ‘Satellite’, ‘payload_mass_kg’: 2800, ‘payload_mass_lbs’: 6172.94, ‘orbit’: ‘SSO’, ‘orbit_params’: {‘reference_system’: ‘geocentric’, ‘regime’: ‘sun-synchronous’, ‘longitude’: None, ‘semi_major_axis_km’: None, ‘eccentricity’: None, ‘periapsis_km’: None, ‘apoapsis_km’: None, ‘inclination_deg’: None, ‘period_min’: None, ‘lifespan_years’: 5, ‘epoch’: None, ‘mean_motion’: None, ‘raan’: None, ‘arg_of_pericenter’: None, ‘mean_anomaly’: None}}]

adding after that gives me blank.

1 Like

I am having issues making those sensors work. In the mean time, try adding ?pretty=true in your http request (i.e. ‘https://api.spacexdata.com/v2/launches/next?pretty=true’) to at least give you a more readable details screen when you click on the sensor.spacex.

1 Like

What is TSS?
How do I hide the sensor.spacex from UI. It is just ugly with background info in it.
Also how do I adjust the time of launch to show a different time zone (like + 7 hours for eEurope)?

ty

yea, how did you do the long name?
edit:
never mind, I’ve seen it :slight_smile:

I don’t seem to get any notifications from the automation. It works if I trigger it manually, but it never actually sends it.

Have I missed something. I’ve tried it in the template editor, and it comes up as true if I adjust the time to match.

Tried setting up a 30mins/1hr one just now to test it again as there is a launch due later on, but still doesn’t seem to send me anything.

Any help is much appreciated, as I am sure I have just overlooked something stupid.

- alias: Space X Launch Notificaiton 10 Min
  trigger:
  - platform: template
    value_template: '{{ (now().strftime("%s") | int + 600) == (states.sensor.spacex.state | int) }}'
  action:
  - service: notify.pushbullet
    data:
      title: SPACEX START IN 10MIN
      message: >
        Mission: {{ states('sensor.spacex_next_mission_name') }} 
          │
          ├─ Time: .. {{ states('sensor.spacex_next_launch_time') }}
          ├─ Rocket: ..... {{ states('sensor.spacex_next_rocket') }}
          └─ Location: ........... {{ states('sensor.spacex_next_launch_site') }}

- alias: Space X Launch Notificaiton 1 Hour
  trigger:
  - platform: template
    value_template: '{{ (now().strftime("%s") | int + 1800) == (states.sensor.spacex.state | int) }}'
  action:
  - service: notify.pushbullet
    data:
      title: SPACEX START IN 1HOUR
      message: >
        Mission: {{ states('sensor.spacex_next_mission_name') }} 
          │
          ├─ Time: .. {{ states('sensor.spacex_next_launch_time') }}
          ├─ Rocket: ..... {{ states('sensor.spacex_next_rocket') }}
          └─ Location: ........... {{ states('sensor.spacex_next_launch_site') }}

Sad to hear. I’ve actually never enabled the notification myself. I did the same as you and tried the trigger in the template editor and it seemed to work but as I said - I’ve never tested it. Templating with time and offsets is still something homeassistant needs to improve.

Maybe someone else can help you?

No help here either. I’ve got the same automation loaded and well… nothing happening even when I manually trigger it.

But this seems to be related to your config. Mine works perfectly if I trigger it manually.

1 Like