SpaceX sensors: Starman and next rocket launch

From my understanding you would need a sensor as trigger that triggers every second. Else you would never match the exact timestamp. For example I have a sensor that counts the hours I am alive and I update it with the entity sensor.date. But it is only updated once a day because thats when sensor.date changes state.

Ah yeah its running in seconds, my bad.

Can we do less than ? Greater than ? so it wouldn’t need an exact second to trigger, just went it gets past that 10 minute window, it would then class as true, and then trigger on the next minute ?

value_template: "{{ (as_timestamp(now()) + 300) | timestamp_custom('%H:%M') < states.sensor.spacex_next_launch_time.state }}"

I opened a feature request fo a UNIX time sensor. Maybe you want to leave your upvote there so we can get this automation to work. See Unix time sensor

1 Like

Vote left, fingers crossed we can get something sorted out :smiley:

It’s not pretty but I’m pretty sure this will work to trigger an automation ~10 minutes (+/- a few seconds) before launch:

{{ (as_timestamp(states.sensor.date.state + ' ' + states.sensor.time.state) |int ) - ((states.sensor.spacex.state)| int) - 660 < 0}}

You need to have created the ‘time’ & ‘date’ sensors already in your config:

- platform: time_date
  display_options:
    - time
    - date
    - date_time
    - time_date

Those sensors update every minute but once it updates then the comparison will be true around the 10 minute mark.

1 Like

That’s coming up a True when I paste it into the template editor at the moment,

Next launch isn’t until Feb 9th.

spacex2

oops! Sorry about that.

I pasted the wrong one from my template editor that I was playing around with trying to work it out.

This should work better:

{{ (((states.sensor.spacex.state)| int) - as_timestamp(states.sensor.date.state + ' ' + states.sensor.time.state) |int ) - 660 < 0 }}

1 Like

No problems, its all good. I have updated the automation to see what happens, shame we have to wait a few weeks to find out, but thank you very much for helping out.

Why 660? I’m reading this as the integer of the spacex sensor - the timestamp + the sensor.time value - 660 if less than 0.

The date time sensor updates every 10 minutes. To getbitvvto triggervaround 10 minutes before launch I subtract 11 minutes from now then do the comparison. That value less than zero is around 10 minutes before launch.

Automation worked! And on top of it I have an automation to TTS the mission details and then immediately play Elton John’s “Rocket Man” starting 5 min before launch. I cannot have wanted it to work out any more perfect. The mission details lasted about 20s approx and then the song kicked in. It ended exactly when the rocket launched!!! Unreal timing!

2 Likes

Could you please post your entire automation text? I’m still trying to get mine to work.

Sure.

#
  - id: space_x_launch_notify_5
    alias: Space X Launch Notify 5
    initial_state: 'on'
    trigger:
      - platform: template
        value_template: "{{ (((states.sensor.spacex.state)| int) - as_timestamp(states.sensor.date.state + ' ' + states.sensor.time.state) |int ) - 330 < 0 }}"
    condition:
      - condition: template
        value_template: "{{ now().strftime('%m.%d.%Y') == states.sensor.spacex_next_launch_day.state }}"
      - condition: time
        after: '08:00:00'
      - condition: time
        before: '22:30:00'
    action:
      - service_template: >
          {% if is_state ('device_tracker.my_phone' ,'home') %}
            script.rocket_man_home
          {% else %}
            script.rocket_man_not_home
          {% endif %}
      - service: notify.automation_script_triggers
        data:
          message:
            '{{now().strftime("%Y-%m-%d %H:%M:%S")}} -- space_x_launch_notify_5'
            
script:
  rocket_man_home:
    alias: Rocket Man Home
    sequence:
      - service: notify.hangouts
        data:
          title: "SPACE X LAUNCH!!"
          message: >
            SPACEX START IN 5 MIN
            ══════════════════════════


            Mission: {{ states('sensor.spacex_next_mission_name') }}
              │
              ├─ Start Time: .. {{ states('sensor.spacex_next_launch_time') }}
              ├─ Rocket: ..... {{ states('sensor.spacex_next_rocket') }}
              └─ Location: ........... {{ states('sensor.spacex_next_launch_site') }}
      - service: media_player.volume_set
        data:
          entity_id: media_player.device1
          volume_level: .5
      - service: tts.google_say
        entity_id: media_player.device1
        data_template:
          message:
            "{{ states.sensor.spacex.attributes.details }}"
          cache: false
      - delay: '00:00:30'
      - service: media_player.play_media
        data:
          entity_id: media_player.device1
          media_content_id: rocket_man.mp3
          media_content_type: audio/mp3
      - service: notify.automation_script_triggers
        data:
          message:
            '{{now().strftime("%Y-%m-%d %H:%M:%S")}} -- rocket_man_home_script'
            
  rocket_man_not_home:
    alias: Rocket Man Not Home
    sequence:
      - service: notify.hangouts
        data:
          title: "SPACE X LAUNCH!!"
          message: >
            SPACEX START IN 5 MIN
            ══════════════════════════


            Mission: {{ states('sensor.spacex_next_mission_name') }}
              │
              ├─ Start Time: .. {{ states('sensor.spacex_next_launch_time') }}
              ├─ Rocket: ..... {{ states('sensor.spacex_next_rocket') }}
              └─ Location: ........... {{ states('sensor.spacex_next_launch_site') }}
      - service: notify.automation_script_triggers
        data:
          message:
            '{{now().strftime("%Y-%m-%d %H:%M:%S")}} -- rocket_man_not_home_script'

Just some added detail… I have the mp3 hosted and redacted above.

The Elton John song is exactly 4:42 in length but ends at about 4:39. Now whats great is that the details of the mission “if” they last around 20s, that will time the launch PERFECTLY if its on time! So just like tonight (by sheer luck!) the launch happened right when the song ended. So awesome!

3 Likes

I am going to guess you are based in America then somewhere. I sat up till 1 hour before launch, and was waiting to check if it worked, and then it failed for me. So I looked into it, and realised that, as the launch was scheduled for late evening America time, it ended up rolling over to the next day here in the UK, so the condition failed, as I was technically 1 day ahead. It was 1:45am GMT it due to launch, so I guess as long as it isn’t the 5-8 hour window when the timezones are on different days, it should work fine for me. I should also probably be asleep in that time frame as well anyway.

Has anyone got this part working yet?

Awesome, thanks a lot for sharing.

1 Like

I’ve also written a custom component integration which includes all of this additional data and simplifies some of the notifications etc. I’ve included details at https://community.home-assistant.io/t/spacex-next-launch-starman-integration/223417/20

2 Likes

Great Job. It’s really nice to see the initial idea evolving in this thread. I’ll test your component asap.