Lovelace card to turn avr on and play spotify music with one button

Hello community
short introduction:
I started with a raspberry to make my home a little smarter. and meanwhile I’m happy that I made this decision. due to the many forum posts i was able to solve my first problems and meanwhile have installed some components in the system.
As far as I have helpful configurations I will share it to the community.

problem:
I have integrated my denon avr and my spotify premium acc and added them to a lovelace card.
when I press the play button on the spotify card, HA is showing the following error. For sure there is no media payer selected .
image

With help of the three dots in edge I can select the avr as source for my spotify. avr gets turned on with correct spotify connect source. I am not sure which command the spotify card is using there. Afterwards I can start playing music by pressing the play button.
My wish is, to press the button once: turn avr on and start playing on selected spotify source. Is this possible? and if yes, how? script? I tried out with a script but my success was very little. I can turn on the AVR and switch the source, but no music is playing :frowning:

avr_spotify:
  alias: AVR Spotify
  sequence:
  - service: media_player.turn_on
    target:
      device_id: 95daaxxxxx
      entity_id: media_player.spotify_xxxx
  - delay:
      hours: 0
      minutes: 0
      seconds: 10
      milliseconds: 0
  - service: media_player.volume_set
    data:
      volume_level: 0.35
    target:
      device_id: 95daaxxxxx
  - service: media_player.select_source
    target:
      entity_id: media_player.spotify_xxxx
    data:
      source: Denon AVR-X1300W
  - service: media_player.play_media
    target:
      entity_id: media_player.spotify_xxxx
      device_id: 95daaxxxxx
  mode: single

Currently I could not find any answers to this issue within the community.

Thanks for your help!

Does your AVR switch on when you do the media_player.select_source?

If so, you might just need to add a delay between it and the media_player.play_media, time for the AVR to “warm up”.
And/or add a “choose” to only do the delay if the AVR is not available, yet.

yes the AVR turns on when selecting the source, but the AVR is showing the spotify source a half second and selects the initial source instead of spotify. I don’t know why

general question: play_media command expects content ID and type (spotify playlist for example). I would be happy if the last online title is played. Is that possible?

Which is not the case if you select the spotify source “manually”?

It is selecting spotify source when I turn of the AVR and select spotify source.

It is selecting spotify source for a half second and afterwards the initial source when I select spotify source (and it turns on automatically).

Then just turn on the AVR and select its spotify source in the sequence. Not possible?

Hi! This is the script that I use to play Spotify on LG WebOS TV-

alias: Play Spotify Lounge TV
sequence:
  - service: homeassistant.turn_on
    target:
      entity_id: media_player.lounge_tv
  - delay:
      seconds: 10
  - repeat:
      until:
        - condition: state
          entity_id: media_player.lounge_tv
          state: Spotify - Music and Podcasts
          attribute: source
      sequence:
        - service: media_player.select_source
          target:
            entity_id: media_player.lounge_tv
          data:
            source: Spotify - Music and Podcasts
  - delay:
      hours: 0
      minutes: 0
      seconds: 30
      milliseconds: 0
  - service: media_player.select_source
    target:
      entity_id: media_player.spotify_xxxx
    data:
      source: Lounge TV
  - delay:
      hours: 0
      minutes: 0
      seconds: 3
      milliseconds: 0
  - service: media_player.play_media
    target:
      entity_id: media_player.spotify_xxxx
    data:
      media_content_id: spotify:user:spotify:playlist:37i9dQZF1DWT1y71ZcMPe5
      media_content_type: playlist
  - service: webostv.button
    data:
      entity_id: media_player.lounge_tv
      button: PLAY
mode: single

Yes, it’s turning on and selecting the source. But it’s not playing music.

The log is showing the following issue:


Why no active device?

Thanks ardysusilo, I will try out