script: make_clever_announcement: alias: Make Clever Announcement description: Play an announcement cleverly icon: mdi:bullhorn mode: queued fields: entity_id: description: Media player to use for announcements (If left empty, uses the ytube music player speaker(s) currently in use) example: media_player.announcements selector: entity: multiple: false domain: media_player media_content_id: description: "Media Content ID to announce" example: media-source://media_source/local/Sounds/tts/welcome_to_daddys_house.mp3 selector: text: media_content_type: description: "The media content type of the specified media" example: audio/mpeg selector: text: volume: description: "The volume that the announcement should play at" example: "0.6" selector: number: min: 0.0 max: 1.0 step: 0.01 variables: # This beast of a line uses the media players in this order: # If an entity_id was specified, use that always # If an entity_id was not specified, check if YTube Music Player is in use: # If YTube Music Player is in use, play the announcement over those speakers # If YTube Music Player is not in use, play the announcement over the media_player.announcements speaker group media_player: '{{ iif(entity_id is defined, entity_id, iif(is_state("media_player.ytube_music_player","playing") or is_state("media_player.ytube_music_player","idle") or is_state("media_player.ytube_music_player","paused"),state_attr("media_player.ytube_music_player","remote_player_id"),"media_player.announcements")) }}' remote_player: '{{ state_attr("media_player.ytube_music_player","remote_player_id") }}' content_id: '{{ iif(media_content_id is defined,media_content_id,"media-source://media_source/local/Sounds/tts/welcome_to_daddys_house.mp3") }}' content_type: '{{ iif(media_content_type is defined,media_content_type,"audio/mpeg") }}' volume: "{{ iif(volume is defined,volume,0.6) }}" original_volume: '{{ iif(is_state("media_player.ytube_music_player","off"),0.0,state_attr(state_attr("media_player.ytube_music_player","remote_player_id"), "volume_level")) }}' ytube_in_use: '{{ is_state("media_player.ytube_music_player","playing") or is_state("media_player.ytube_music_player","idle") or is_state("media_player.ytube_music_player","paused") }}' sequence: - choose: - alias: "Ytube music player is playing something" conditions: "{{ ytube_in_use }}" sequence: - alias: "Interrupt ytube music player" service: ytube_music_player.call_method entity_id: media_player.ytube_music_player data: command: interrupt_start - alias: "Then wait a second" delay: hours: 0 minutes: 0 seconds: 2 # This looks to cause the first part of the message to be missed. # Instead of initally muting the speakers, I'll just set them to the appropriate volume off the bat # - alias: "Mute the announcement speakers momentarily" # service: media_player.volume_set # target: # entity_id: '{{ media_player }}' # data: # volume_level: 0 - alias: "Set the volume on the announcement speakers" service: media_player.volume_set target: entity_id: "{{media_player}}" data: volume_level: "{{volume}}" # - alias: "Wait another bit for the volume to be set" # delay: # hours: 0 # minutes: 0 # seconds: 2 - alias: "Play the announcement" service: media_player.play_media target: entity_id: "{{media_player}}" data: media_content_id: "{{content_id}}" media_content_type: "{{content_type}}" # This is now being done before the announcement is played # - alias: "Set the volume on the announcement speakers" # service: media_player.volume_set # target: # entity_id: '{{media_player}}' # data: # volume_level: '{{volume}}' # Wait until media player is idle # This doesn't seem to be working as written. Trying a wait_for_trigger instead #- wait_template: "{{ is_state(media_player,'idle')}}" # This shit doesn't work either - alias: Wait for the specified media_player(s) to become idle wait_for_trigger: - platform: template value_template: "{{ is_state(media_player, 'idle') or is_state(media_player, 'off') or is_state(media_player, 'unavailable')}}" timeout: "00:00:30" - alias: "Give a second of dead air after the announcement" delay: hours: 0 minutes: 0 seconds: 2 - choose: - alias: "Ytube music player was playing something earlier" conditions: "{{ ytube_in_use }}" sequence: - alias: "Mute the remote speakers momentarily" service: media_player.volume_set target: entity_id: "{{ remote_player }}" data: volume_level: 0 - alias: "Resume the ytube music player playlist" service: ytube_music_player.call_method entity_id: media_player.ytube_music_player data: command: interrupt_resume - alias: "Reset the volume of the remote speakers to the previous level" service: media_player.volume_set target: entity_id: "{{ remote_player }}" data: volume_level: "{{original_volume}}" default: - alias: "Since we're not resuming music, turn off the speakers used to make the announcement" service: media_player.turn_off data: {} target: entity_id: "{{ media_player }}" - alias: "Wait at least 1 seconds before handling the next announcement in queue" delay: "00:00:02"