Seamless Voice-Controlled Media Player Automation

Introduction:
In this project, I implemented a set of automations, scripts, and helpers to seamlessly manage my media player using vlc-telnet with the wyoming satellite setup on linux and raspberry pi. By integrating the voice assistant with the media player on the same playback device, I can ensure that my entertainment experience is smooth and interruption-free. Below, I share the details of my automations, which include handling media playback and saving the current media position for resuming playback later.

Automations:

  1. Handle Satellite and Media Player:
    • Description: This automation ensures that the media player stops playback when the satellite is turned on, and resumes playback when the satellite is idle.
    • Triggers:
      • The automation is triggered when the satellite starts listening or responding by announce.
    • Actions:
      • If the media player is currently playing, it stops the playback.
      • Waits for the satellite to go back to “idle.”
      • After a brief delay, resumes playback on the media player.
      • If the media player has a non-zero media duration, it seeks to the previously saved position.
      • If the media player is not playing, it simply stops the media player.
alias: Handle satellite and media player
description: ""
triggers:
  - trigger: state
    entity_id:
      # Use your actual satellite entity id
      - assist_satellite.my_satellite
    to: listening
  - trigger: state
    entity_id:
      - assist_satellite.my_satellite
    to: responding
conditions: []
actions:
  - if:
      - condition: device
        device_id: c87c30244ab002eaed5161fe1bc34ac5
        domain: media_player
        entity_id: d00fe3b645714f7e8d79c576fc4812bc
        type: is_playing
      # Use your actual media player device and entity id
    then:
      - action: automation.turn_off
        target:
          entity_id:
            - automation.save_media_player_position
        data:
          stop_actions: true
      - action: media_player.media_stop
        target:
          entity_id:
            - media_player.vlc_telnet
        data: {}
      - wait_for_trigger:
          - trigger: state
            entity_id:
              - assist_satellite.my_satellite
            to: idle
      - delay:
          hours: 0
          minutes: 0
          seconds: 10
          milliseconds: 0
      - action: media_player.media_play
        metadata: {}
        data: {}
        target:
          entity_id: media_player.vlc_telnet
      - if:
          - condition: state
            entity_id: media_player.vlc_telnet
            attribute: media_duration
            state: "0"
        then:
          - action: media_player.media_seek
            metadata: {}
            data:
              seek_position: "{{ states('input_number.bedroom_media_player_position') | int}}"
            target:
              entity_id: media_player.vlc_telnet
      - action: automation.turn_on
        target:
          entity_id:
            - automation.save_media_player_position
        data: {}
    else:
      - action: media_player.media_stop
        target:
          entity_id:
            - media_player.vlc_telnet
        data: {}
mode: single
  1. Save Media Player Position:
    • Description: This automation saves the current position of the media player when it is playing, ensuring that playback can be resumed from the same point later.
    • Triggers:
      • The automation is triggered when the media player starts playing or the media position changes.
    • Conditions:
      • The media player must have a media position above 0.
    • Actions:
      • After a brief delay, it saves the current media position to an input number entity.
alias: Save media player position
description: ""
triggers:
  # Use your actual media player device and entity id
  - device_id: c87c30244ab002eaed5161fe1bc34ac5
    domain: media_player
    entity_id: d00fe3b645714f7e8d79c576fc4812bc
    type: playing
    trigger: device
  - trigger: state
    entity_id:
      - media_player.vlc_telnet
    attribute: media_position
conditions:
  - condition: numeric_state
    entity_id: media_player.vlc_telnet
    attribute: media_position
    above: 0
actions:
  - delay:
      hours: 0
      minutes: 0
      seconds: 10
  - data_template:
     # Use your actual created helper entity id for media player position
      entity_id: input_number.bedroom_media_player_position
      value: "{{state_attr('media_player.vlc_telnet', 'media_position')}}"
    action: input_number.set_value
mode: single

Conclusion:
These automations enhance the user experience by providing a seamless transition between media playback and voice assistant interactions. By stopping and resuming playback intelligently, I ensure that my media content is always in sync with my activities.

Feel free to try out these automations and share your feedback or improvements!

Why would you stop the media player instead of just pausing it? You know that pause is equivalent to stop but remember the position, right?

FYI stop also will not work with media players from music assistant for example because they don’t accept the stop function. You can only pause them or turn them off.

If the media player platform like vlc-telnet is running on the same machine that runs wyoming satellite as my case, then it should be stopped at all during the satellite process so you can hear the response of the voice assistant.

No, it should be paused, so that you don’t lose the state of whatever media you are playing. You don’t need to stop VLC at all in order for any of this to work.

Or am I understanding correctly that you are actually using VLC to play back the TTS? You don’t need to stop in that circumstance either. You can just play whatever immediately you want to play. But of course, if you do that, you will lose the context of whatever VLC was doing before that.

When pausing only during the satelllite process, TTS response can’t be heard as the playback output is considered busy at that time cause it’s used by other source which is the other separate media player (vlc-telnet).

I’ve just realized that I experience this problem only on my linux server that runs HA core along with the satellite and vlc-telnet media player, but on the raspberry pi they play over each other at the same time normally. So, that’s something related to the OS with ALSA audio output that’s used by the 2 containers which works differently from the raspberry pi OS.