Checking if raspotify exists as a media player source

Hi,

A few times I’ve experienced issues around Raspotify not publishing a source to media_player.spotify.
Everything goes back to how it should be when i restart raspotify service.
I would like to therefore “monitor” media_player.spotify[source_list] attribute and automate action restarting raspotify serice if raspotify isn’t present on the source list.
So far I got as far as below snippet but I am struggling to get this to work.
Can someone please shed some light as to what am I doing wrong?

Thanks.

sensor:
- platform: template
  sensors:
    spotify_raspotify:
      friendly_name: "Spotify Source"
      value_template: "raspotify"

automation:
- alias: 'Spotify Source (Sync)'
  id: 'spotify_raspotify_sync'
  trigger:
  - platform: homeassistant
    event: start
  - platform: state
    entity_id: sensor.spotify_raspotify
  condition:
    condition: template
    value_template: >
      {{ states.sensor.spotify_raspotify.state not in
           state_attr('media_player.spotify', 'source_list') }}
  action:
    service: shell_command.restart_raspotify_service

shell_command:
  restart_raspotify_service: ~/.homeassistant/restart_raspotify.sh

alright, I managed to get it to the working state, for anyone looking similar solution, please see the code below:

sensor:
  - platform: template
    sensors:
      spotify_raspotify:
        friendly_name: "Spotify Source"
        value_template: "raspotify"

automation:
  - alias: 'Spotify Source (Sync)'
    trigger:
    - platform: homeassistant
      event: start
    - platform: state
      entity_id: media_player.spotify
    - platform: time
      minutes: '/5'
      seconds: 00
    condition:
      condition: template
      value_template: "{{states.sensor.spotify_raspotify.state not in
             states.media_player.spotify.attributes.source_list }}"
    action:
      service: shell_command.restart_raspotify_service

shell_command:
  restart_raspotify_service: sudo systemctl restart raspotify.service

You shouldn’t have to trigger the automation every five minutes. I think your original problem was you didn’t list media_player.spotify anywhere in your trigger, so it never triggered if that entity (or, specifically, it’s source_list attribute) changed.

Also I don’t understand why you’re looking for the state of sensor.spotify_raspotify to be in media_player.spotify’s source_list. Shouldn’t you just be looking for the string ‘raspotify’ to be in that list?

In any case, assuming what you have is working, you might want to consider this instead:

automation:
  - alias: 'Spotify Source (Sync)'
    trigger:
    - platform: homeassistant
      event: start
    - platform: state
      entity_id:
        - media_player.spotify
        - sensor.spotify_raspotify
    condition:
      condition: template
      value_template: "{{states('sensor.spotify_raspotify') not in
             state_attr('media_player.spotify', 'source_list') }}"
    action:
      service: shell_command.restart_raspotify_service