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:
Have fun and (again) sorry for the weird mix between german and english.